mirror of
https://github.com/denoland/deno.git
synced 2024-11-30 16:40:57 -05:00
refactor(cli): update checker - use a single option instead of two (#16372)
This commit is contained in:
parent
869acee8fb
commit
e825022c82
1 changed files with 99 additions and 82 deletions
|
@ -40,19 +40,12 @@ pub fn check_for_upgrades(cache_dir: PathBuf) -> Option<String> {
|
||||||
Err(_) => "".to_string(),
|
Err(_) => "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (last_checked, latest_version) = parse_upgrade_check_file(content);
|
let maybe_file = CheckVersionFile::parse(content);
|
||||||
|
|
||||||
if latest_version.is_none() || latest_version.as_ref().unwrap().is_empty() {
|
let should_check = match &maybe_file {
|
||||||
let last_checked_dt = last_checked.and_then(|last_checked| {
|
Some(file) => {
|
||||||
chrono::DateTime::parse_from_rfc3339(&last_checked)
|
|
||||||
.map(|dt| dt.with_timezone(&chrono::Utc))
|
|
||||||
.ok()
|
|
||||||
});
|
|
||||||
|
|
||||||
let should_check = match last_checked_dt {
|
|
||||||
Some(last_checked_dt) => {
|
|
||||||
let last_check_age =
|
let last_check_age =
|
||||||
chrono::Utc::now().signed_duration_since(last_checked_dt);
|
chrono::Utc::now().signed_duration_since(file.last_checked);
|
||||||
last_check_age > chrono::Duration::hours(UPGRADE_CHECK_INTERVAL)
|
last_check_age > chrono::Duration::hours(UPGRADE_CHECK_INTERVAL)
|
||||||
}
|
}
|
||||||
None => true,
|
None => true,
|
||||||
|
@ -78,17 +71,21 @@ pub fn check_for_upgrades(cache_dir: PathBuf) -> Option<String> {
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let contents =
|
let contents = CheckVersionFile {
|
||||||
serialize_upgrade_check_file(chrono::Utc::now(), latest_version);
|
last_checked: chrono::Utc::now(),
|
||||||
let _ =
|
latest_version,
|
||||||
std::fs::write(cache_dir.join(UPGRADE_CHECK_FILE_NAME), contents);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
.serialize();
|
||||||
|
let _ = std::fs::write(cache_dir.join(UPGRADE_CHECK_FILE_NAME), contents);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return `Some(version)` if a new version is available, `None` otherwise.
|
// Return `Some(version)` if a new version is available, `None` otherwise.
|
||||||
latest_version
|
maybe_file
|
||||||
.filter(|v| v != version::release_version_or_canary_commit_hash())
|
.map(|f| f.latest_version)
|
||||||
|
.filter(|latest_version| {
|
||||||
|
latest_version != version::release_version_or_canary_commit_hash()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> {
|
pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> {
|
||||||
|
@ -387,55 +384,75 @@ fn check_exe(exe_path: &Path) -> Result<(), AnyError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_upgrade_check_file(
|
struct CheckVersionFile {
|
||||||
content: String,
|
pub last_checked: chrono::DateTime<chrono::Utc>,
|
||||||
) -> (Option<String>, Option<String>) {
|
pub latest_version: String,
|
||||||
let (mut last_checked, mut latest_version) = (None, None);
|
}
|
||||||
|
|
||||||
|
impl CheckVersionFile {
|
||||||
|
pub fn parse(content: String) -> Option<Self> {
|
||||||
let split_content = content.split('!').collect::<Vec<_>>();
|
let split_content = content.split('!').collect::<Vec<_>>();
|
||||||
|
|
||||||
if split_content.len() == 2 {
|
if split_content.len() != 2 {
|
||||||
last_checked = Some(split_content[0].to_owned());
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
if !split_content[1].is_empty() {
|
let latest_version = split_content[1].trim().to_owned();
|
||||||
latest_version = Some(split_content[1].to_owned());
|
if latest_version.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let last_checked = chrono::DateTime::parse_from_rfc3339(split_content[0])
|
||||||
|
.map(|dt| dt.with_timezone(&chrono::Utc))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
Some(CheckVersionFile {
|
||||||
|
last_checked,
|
||||||
|
latest_version,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serialize(&self) -> String {
|
||||||
|
format!("{}!{}", self.last_checked.to_rfc3339(), self.latest_version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(last_checked, latest_version)
|
#[cfg(test)]
|
||||||
}
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_upgrade_check_file() {
|
fn test_parse_upgrade_check_file() {
|
||||||
let (last_checked, latest_version) =
|
let file =
|
||||||
parse_upgrade_check_file("2020-01-01T00:00:00+00:00!1.2.3".to_string());
|
CheckVersionFile::parse("2020-01-01T00:00:00+00:00!1.2.3".to_string())
|
||||||
assert_eq!(last_checked, Some("2020-01-01T00:00:00+00:00".to_string()));
|
.unwrap();
|
||||||
assert_eq!(latest_version, Some("1.2.3".to_string()));
|
assert_eq!(
|
||||||
|
file.last_checked.to_rfc3339(),
|
||||||
|
"2020-01-01T00:00:00+00:00".to_string()
|
||||||
|
);
|
||||||
|
assert_eq!(file.latest_version, "1.2.3".to_string());
|
||||||
|
|
||||||
let (last_checked, latest_version) =
|
let result =
|
||||||
parse_upgrade_check_file("2020-01-01T00:00:00+00:00!".to_string());
|
CheckVersionFile::parse("2020-01-01T00:00:00+00:00!".to_string());
|
||||||
assert_eq!(last_checked, Some("2020-01-01T00:00:00+00:00".to_string()));
|
assert!(result.is_none());
|
||||||
assert_eq!(latest_version, None);
|
|
||||||
|
|
||||||
let (last_checked, latest_version) =
|
let result = CheckVersionFile::parse("garbage!test".to_string());
|
||||||
parse_upgrade_check_file("2020-01-01T00:00:00+00:00".to_string());
|
assert!(result.is_none());
|
||||||
assert_eq!(last_checked, None);
|
|
||||||
assert_eq!(latest_version, None);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_upgrade_check_file(
|
let result = CheckVersionFile::parse("test".to_string());
|
||||||
dt: chrono::DateTime<chrono::Utc>,
|
assert!(result.is_none());
|
||||||
version: String,
|
|
||||||
) -> String {
|
|
||||||
format!("{}!{}", dt.to_rfc3339(), version)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_upgrade_check_file() {
|
fn test_serialize_upgrade_check_file() {
|
||||||
let s = serialize_upgrade_check_file(
|
let file = CheckVersionFile {
|
||||||
chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
|
last_checked: chrono::DateTime::parse_from_rfc3339(
|
||||||
|
"2020-01-01T00:00:00Z",
|
||||||
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.with_timezone(&chrono::Utc),
|
.with_timezone(&chrono::Utc),
|
||||||
"1.2.3".to_string(),
|
latest_version: "1.2.3".to_string(),
|
||||||
);
|
};
|
||||||
assert_eq!(s, "2020-01-01T00:00:00+00:00!1.2.3");
|
assert_eq!(file.serialize(), "2020-01-01T00:00:00+00:00!1.2.3");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue