-
Notifications
You must be signed in to change notification settings - Fork 824
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
Port "wapm install" to Wasmer #3317
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af73775
Update the GraphQL schema to be in sync with wapm.io
fe2e2b4
Fixed a typo in Cargo.toml which caused warnings
60f4ffa
List bindings associated with a package
e630c50
Added an install command
02e6130
Add install to the list of supported commands
8d46d34
Renamed "wasmer install" to "wasmer add"
0830f10
Update the changelog
3f09c82
Delete PackageSpecifier in favour of SplitVersion
ac67997
Copy across "impl Registries" from the wapm CLI
c3ebf06
Fixed a broken doc-link
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
use std::process::{Command, Stdio}; | ||
|
||
use anyhow::{Context, Error}; | ||
use clap::Parser; | ||
use wasmer_registry::{Bindings, PartialWapmConfig, ProgrammingLanguage}; | ||
|
||
use crate::cli::SplitVersion; | ||
|
||
/// Add a WAPM package's bindings to your application. | ||
#[derive(Debug, Parser)] | ||
pub struct Add { | ||
/// The registry to fetch bindings from. | ||
#[clap(long, env = "WAPM_REGISTRY")] | ||
registry: Option<String>, | ||
/// Add the JavaScript bindings using "npm install". | ||
#[clap(long, groups = &["bindings", "js"])] | ||
npm: bool, | ||
/// Add the JavaScript bindings using "yarn add". | ||
#[clap(long, groups = &["bindings", "js"])] | ||
yarn: bool, | ||
/// Add the package as a dev-dependency. | ||
#[clap(long, requires = "js")] | ||
dev: bool, | ||
/// Add the Python bindings using "pip install". | ||
#[clap(long, groups = &["bindings", "py"])] | ||
pip: bool, | ||
/// The packages to add (e.g. "wasmer/[email protected]" or "python/python") | ||
#[clap(parse(try_from_str))] | ||
packages: Vec<SplitVersion>, | ||
} | ||
|
||
impl Add { | ||
/// Execute [`Add`]. | ||
pub fn execute(&self) -> Result<(), Error> { | ||
anyhow::ensure!(!self.packages.is_empty(), "No packages specified"); | ||
|
||
let registry = self | ||
.registry() | ||
.context("Unable to determine which registry to use")?; | ||
|
||
let bindings = self.lookup_bindings(®istry)?; | ||
|
||
let mut cmd = self.target().command(&bindings); | ||
|
||
#[cfg(feature = "debug")] | ||
log::debug!("Running {cmd:?}"); | ||
|
||
let status = cmd | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
.status() | ||
.with_context(|| { | ||
format!( | ||
"Unable to start \"{:?}\". Is it installed?", | ||
cmd.get_program() | ||
) | ||
})?; | ||
|
||
anyhow::ensure!(status.success(), "Command failed: {:?}", cmd); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn lookup_bindings(&self, registry: &str) -> Result<Vec<Bindings>, Error> { | ||
#[cfg(feature = "debug")] | ||
log::debug!("Querying WAPM for the bindings packages"); | ||
|
||
let mut bindings_to_add = Vec::new(); | ||
let language = self.target().language(); | ||
|
||
for pkg in &self.packages { | ||
let bindings = lookup_bindings_for_package(registry, pkg, &language) | ||
.with_context(|| format!("Unable to find bindings for {pkg}"))?; | ||
bindings_to_add.push(bindings); | ||
} | ||
|
||
Ok(bindings_to_add) | ||
} | ||
|
||
fn registry(&self) -> Result<String, Error> { | ||
match &self.registry { | ||
Some(r) => Ok(r.clone()), | ||
None => { | ||
let cfg = PartialWapmConfig::from_file() | ||
.map_err(Error::msg) | ||
.context("Unable to load WAPM's config file")?; | ||
Ok(cfg.registry.get_current_registry()) | ||
} | ||
} | ||
} | ||
|
||
fn target(&self) -> Target { | ||
match (self.pip, self.npm, self.yarn) { | ||
(true, false, false) => Target::Pip, | ||
(false, true, false) => Target::Npm { dev: self.dev }, | ||
(false, false, true) => Target::Yarn { dev: self.dev }, | ||
_ => unreachable!( | ||
"Clap should ensure at least one item in the \"bindings\" group is specified" | ||
), | ||
} | ||
} | ||
} | ||
|
||
fn lookup_bindings_for_package( | ||
registry: &str, | ||
pkg: &SplitVersion, | ||
language: &ProgrammingLanguage, | ||
) -> Result<Bindings, Error> { | ||
let all_bindings = | ||
wasmer_registry::list_bindings(registry, &pkg.package, pkg.version.as_deref())?; | ||
|
||
match all_bindings.iter().find(|b| b.language == *language) { | ||
Some(b) => { | ||
#[cfg(feature = "debug")] | ||
{ | ||
let Bindings { url, generator, .. } = b; | ||
log::debug!("Found {pkg} bindings generated by {generator} at {url}"); | ||
} | ||
|
||
Ok(b.clone()) | ||
} | ||
None => { | ||
if all_bindings.is_empty() { | ||
anyhow::bail!("The package doesn't contain any bindings"); | ||
} else { | ||
todo!(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
enum Target { | ||
Pip, | ||
Yarn { dev: bool }, | ||
Npm { dev: bool }, | ||
} | ||
|
||
impl Target { | ||
fn language(self) -> ProgrammingLanguage { | ||
match self { | ||
Target::Pip => ProgrammingLanguage::PYTHON, | ||
Target::Yarn { .. } | Target::Npm { .. } => ProgrammingLanguage::JAVASCRIPT, | ||
} | ||
} | ||
|
||
/// Construct a command which we can run to add packages. | ||
/// | ||
/// This deliberately runs the command using the OS shell instead of | ||
/// invoking the tool directly. That way we can handle when a version | ||
/// manager (e.g. `nvm` or `asdf`) replaces the tool with a script (e.g. | ||
/// `npm.cmd` or `yarn.ps1`). | ||
/// | ||
/// See <https://github.com/wasmerio/wapm-cli/issues/291> for more. | ||
fn command(self, packages: &[Bindings]) -> Command { | ||
let command_line = match self { | ||
Target::Pip => "pip install", | ||
Target::Yarn { dev: true } => "yarn add --dev", | ||
Target::Yarn { dev: false } => "yarn add", | ||
Target::Npm { dev: true } => "npm install --dev", | ||
Target::Npm { dev: false } => "npm install", | ||
}; | ||
let mut command_line = command_line.to_string(); | ||
|
||
for pkg in packages { | ||
command_line.push(' '); | ||
command_line.push_str(&pkg.url); | ||
} | ||
|
||
if cfg!(windows) { | ||
let mut cmd = Command::new("cmd"); | ||
cmd.arg("/C").arg(command_line); | ||
cmd | ||
} else { | ||
let mut cmd = Command::new("sh"); | ||
cmd.arg("-c").arg(command_line); | ||
cmd | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
query GetBindingsQuery ($name: String!, $version: String = "latest") { | ||
packageVersion: getPackageVersion(name:$name, version:$version) { | ||
bindings { | ||
id | ||
language | ||
url | ||
|
||
generator { | ||
packageVersion { | ||
id | ||
version | ||
package { | ||
name | ||
} | ||
} | ||
commandName | ||
} | ||
|
||
__typename | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, you don't need to update the changelog at all in PRs. When doing a release, I just do
gh search --prs --merged --repo wasmerio/wasmer
and then copy the list since the last release. Otherwise I'd have to look if every PR has included this message. So this commit is nice, but pointless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay. We should probably update the pull request template so we don't tell people to update the changelog.