Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Support canary/nightly releases. #193

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/cli/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub struct InstallArgs {
#[arg(default_value = "latest", help = "Version or alias of tool")]
pub spec: Option<UnresolvedVersionSpec>,

#[arg(long, help = "Install a canary (next, nightly, etc) version")]
pub canary: bool,

#[arg(long, help = "Pin version as the global default")]
pub pin: bool,

Expand All @@ -26,10 +29,14 @@ pub struct InstallArgs {
}

pub async fn internal_install(args: InstallArgs) -> SystemResult {
let version = args.spec.clone().unwrap_or_default();
let mut tool = load_tool(&args.id).await?;
let version = if args.canary {
UnresolvedVersionSpec::Canary
} else {
args.spec.clone().unwrap_or_default()
};

if tool.is_setup(&version).await? {
if !args.canary && tool.is_setup(&version).await? {
info!(
"{} has already been installed at {}",
tool.get_name(),
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/install_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub async fn install_all() {

for (id, version) in config.tools {
futures.push(internal_install(InstallArgs {
canary: false,
id,
pin: false,
passthrough: vec![],
Expand Down
11 changes: 6 additions & 5 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@ pub async fn run(args: ArgsRef<RunArgs>) -> SystemResult {
// Install the tool
debug!("Auto-install setting is configured, attempting to install");

let resolved_version = tool.get_resolved_version();

internal_install(InstallArgs {
canary: resolved_version.is_canary(),
id: args.id.clone(),
spec: Some(tool.get_resolved_version().to_unresolved_spec()),
pin: false,
passthrough: vec![],
spec: Some(resolved_version.to_unresolved_spec()),
})
.await?;

// Find the new binaries
tool.locate_bins().await?;
}

let resolved_version = tool.get_resolved_version();

// Update the last used timestamp
if env::var("PROTO_SKIP_USED_AT").is_err() {
tool.manifest.track_used_at(&resolved_version);
tool.manifest.track_used_at(tool.get_resolved_version());

// Ignore errors in case of race conditions...
// this timestamp isn't *super* important
Expand Down Expand Up @@ -126,7 +127,7 @@ pub async fn run(args: ArgsRef<RunArgs>) -> SystemResult {
.args(&args.passthrough)
.env(
format!("{}_VERSION", tool.get_env_var_prefix()),
resolved_version.to_string(),
tool.get_resolved_version().to_string(),
)
.env(
format!("{}_BIN", tool.get_env_var_prefix()),
Expand Down
34 changes: 23 additions & 11 deletions crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ impl Tool {
if cache_path.exists() && (is_cache_enabled() || is_offline()) {
let metadata = fs::metadata(&cache_path)?;

// If offline, always use the cache, otherwise only within the last 24 hours
// If offline, always use the cache, otherwise only within the last 12 hours
let read_cache = if is_offline() {
true
} else if let Ok(modified_time) = metadata.modified().or_else(|_| metadata.created()) {
modified_time > SystemTime::now() - Duration::from_secs(60 * 60 * 24)
modified_time > SystemTime::now() - Duration::from_secs(60 * 60 * 12)
} else {
false
};
Expand Down Expand Up @@ -423,11 +423,27 @@ impl Tool {
return Ok(());
}

debug!(
tool = self.id.as_str(),
initial_version = initial_version.to_string(),
"Resolving a semantic version or alias",
);

// If offline but we have a fully qualified semantic version,
// exit early and assume the version is legitimate!
if is_offline() && matches!(initial_version, UnresolvedVersionSpec::Version(_)) {
// exit early and assume the version is legitimate! Additionally,
// canary is a special type that we can simply just use.
if is_offline() && matches!(initial_version, UnresolvedVersionSpec::Version(_))
|| matches!(initial_version, UnresolvedVersionSpec::Canary)
{
let version = initial_version.to_spec();

debug!(
tool = self.id.as_str(),
version = version.to_string(),
"Resolved to {}",
version
);

self.on_resolved_version
.emit(ResolvedVersionEvent {
candidate: initial_version.to_owned(),
Expand All @@ -440,12 +456,6 @@ impl Tool {
return Ok(());
}

debug!(
tool = self.id.as_str(),
initial_version = initial_version.to_string(),
"Resolving a semantic version",
);

let resolver = self.load_version_resolver(initial_version).await?;
let mut version = VersionSpec::default();
let mut resolved = false;
Expand Down Expand Up @@ -1118,7 +1128,9 @@ impl Tool {
fn is_installed(&self) -> bool {
let dir = self.get_tool_dir();

self.version.as_ref().is_some_and(|v| !v.is_latest())
self.version
.as_ref()
.is_some_and(|v| !v.is_latest() && !v.is_canary())
&& dir.exists()
&& !fs::is_dir_locked(dir)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/tool_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ impl ToolManifest {
Ok(())
}

pub fn track_used_at(&mut self, version: &VersionSpec) {
pub fn track_used_at(&mut self, version: VersionSpec) {
self.versions
.entry(version.to_owned())
.entry(version)
.and_modify(|v| {
v.last_used_at = Some(now());
})
Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::str::FromStr;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged, into = "String", try_from = "String")]
pub enum UnresolvedVersionSpec {
Canary,
Alias(String),
Req(VersionReq),
ReqAny(Vec<VersionReq>),
Expand All @@ -24,6 +25,7 @@ impl UnresolvedVersionSpec {

pub fn to_spec(&self) -> VersionSpec {
match self {
UnresolvedVersionSpec::Canary => VersionSpec::Alias("canary".to_owned()),
UnresolvedVersionSpec::Alias(alias) => VersionSpec::Alias(alias.to_owned()),
UnresolvedVersionSpec::Version(version) => VersionSpec::Version(version.to_owned()),
_ => unreachable!(),
Expand All @@ -43,6 +45,10 @@ impl FromStr for UnresolvedVersionSpec {
fn from_str(value: &str) -> Result<Self, Self::Err> {
let value = remove_space_after_gtlt(remove_v_prefix(value.trim().replace(".*", "")));

if value == "canary" {
return Ok(UnresolvedVersionSpec::Canary);
}

if is_alias_name(&value) {
return Ok(UnresolvedVersionSpec::Alias(value));
}
Expand Down Expand Up @@ -115,6 +121,7 @@ impl Into<String> for UnresolvedVersionSpec {
impl Display for UnresolvedVersionSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Canary => write!(f, "canary"),
Self::Alias(alias) => write!(f, "{}", alias),
Self::Req(req) => write!(f, "{}", req),
Self::ReqAny(reqs) => write!(
Expand All @@ -133,6 +140,7 @@ impl Display for UnresolvedVersionSpec {
impl PartialEq<VersionSpec> for UnresolvedVersionSpec {
fn eq(&self, other: &VersionSpec) -> bool {
match (self, other) {
(Self::Canary, VersionSpec::Alias(a)) => a == "canary",
(Self::Alias(a1), VersionSpec::Alias(a2)) => a1 == a2,
(Self::Version(v1), VersionSpec::Version(v2)) => v1 == v2,
_ => false,
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/version_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub fn resolve_version(
};

match &candidate {
UnresolvedVersionSpec::Canary => {
return Ok(VersionSpec::Alias("canary".into()));
}
UnresolvedVersionSpec::Alias(alias) => {
let mut alias_value = None;

Expand Down
3 changes: 3 additions & 0 deletions crates/pdk-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub enum PluginError {
#[error("Unable to install {tool}, unsupported architecture {arch}.")]
UnsupportedArch { tool: String, arch: String },

#[error("{tool} does not support canary/nightly versions.")]
UnsupportedCanary { tool: String },

#[error("Unable to install {tool}, unsupported OS {os}.")]
UnsupportedOS { tool: String, os: String },

Expand Down
12 changes: 12 additions & 0 deletions crates/pdk/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ where
fetch(HttpRequest::new(url.as_ref()), None, false)
}

/// Fetch the provided URL and return the text response.
pub fn fetch_url_text<U>(url: U) -> anyhow::Result<String>
where
U: AsRef<str>,
{
let req = HttpRequest::new(url.as_ref());
let res = request::<String>(&req, None)
.map_err(|e| anyhow::anyhow!("Failed to make request to {}: {e}", req.url))?;

String::from_bytes(res.body())
}

/// Fetch the provided URL, deserialize the response as JSON,
/// and cache the response in memory for the duration of the WASM instance.
pub fn fetch_url_with_cache<R, U>(url: U) -> anyhow::Result<R>
Expand Down
20 changes: 16 additions & 4 deletions crates/pdk/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,25 @@ macro_rules! host_log {

#[macro_export]
macro_rules! err {
($msg:expr) => {
Err(WithReturnCode::new(PluginError::Message($msg).into(), 1))
($msg:literal) => {
Err(WithReturnCode::new(
PluginError::Message($msg.into()).into(),
1,
))
};
($msg:expr, $code:expr) => {
($msg:literal, $($arg:tt)*) => {
Err(WithReturnCode::new(
PluginError::Message($msg).into(),
PluginError::Message(format!($msg, $($arg)*)).into(),
1,
))
};
($msg:literal, $code:expr) => {
Err(WithReturnCode::new(
PluginError::Message($msg.into()).into(),
$code,
))
};
($msg:expr) => {
Err(WithReturnCode::new($msg, 1))
};
}
2 changes: 2 additions & 0 deletions crates/schema-plugin/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub struct DetectSchema {
pub struct InstallSchema {
pub arch: HashMap<String, String>,
pub checksum_url: Option<String>,
pub checksum_url_canary: Option<String>,
pub download_url: String,
pub download_url_canary: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
Expand Down