Added a little xtask to create a source package in one go

This is a wrapper around `npm pack` that temporarily edits the relative
paths out of `Cargo.toml` to make the package work outside of the git
repo.
This commit is contained in:
Simon Hausmann 2020-10-22 10:58:16 +02:00
parent b5b5a24941
commit f3d87f00a6
3 changed files with 67 additions and 1 deletions

60
xtask/src/nodepackage.rs Normal file
View file

@ -0,0 +1,60 @@
use anyhow::Context;
use xshell::{cmd, pushd, read_file, write_file};
pub fn generate() -> Result<(), Box<dyn std::error::Error>> {
let root = super::root_dir().context("error determining root directory")?;
let node_dir = root.join("api").join("sixtyfps-node");
let cargo_toml_path = node_dir.join("native").join("Cargo.toml");
println!("Removing relative paths from {}", cargo_toml_path.to_string_lossy());
let toml_source =
read_file(cargo_toml_path.clone()).context("Failed to read Node Cargo.toml")?;
let mut toml: toml_edit::Document = toml_source.parse().context("Error parsing Cargo.toml")?;
// Remove all `path = ` entries from dependencies
for dep_key in ["dependencies", "build-dependencies"].iter() {
let dep_table = match toml[dep_key].as_table_mut() {
Some(table) => table,
_ => continue,
};
let deps: Vec<_> = dep_table.iter().map(|(name, _)| name.to_string()).collect();
deps.iter().for_each(|name| {
dep_table[name].as_inline_table_mut().map(|dep_config| dep_config.remove("path"));
});
}
let edited_toml = toml.to_string();
write_file(cargo_toml_path.clone(), edited_toml).context("Error writing Cargo.toml")?;
println!("Running npm package to create the tarball");
{
let _p = pushd(node_dir.clone())
.context(format!("Error changing to node directory {}", node_dir.to_string_lossy()))?;
cmd!("npm pack").run()?;
}
println!("Reverting Cargo.toml");
write_file(cargo_toml_path, toml_source).context("Error writing Cargo.toml")?;
let package_json_source =
read_file(node_dir.join("package.json")).context("Error reading package.json")?;
let package_json: serde_json::Value = serde_json::from_str(&package_json_source)?;
let file_name = node_dir.join(format!(
"{}-{}.tar.gz",
package_json["name"].as_str().unwrap(),
package_json["version"].as_str().unwrap()
));
println!("Source package created and located in {}", file_name.to_string_lossy());
Ok(())
}