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

View file

@ -13,4 +13,6 @@ test_driver_lib = { path = "../tests/driver_lib" }
anyhow = "1.0" anyhow = "1.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
regex = "1.4" regex = "1.4"
toml_edit = "0.2.0" toml_edit = "0.2.0"
xshell = "0.1.6"
serde_json = "1.0"

View file

@ -15,6 +15,7 @@ use structopt::StructOpt;
mod cbindgen; mod cbindgen;
mod cppdocs; mod cppdocs;
mod license_headers_check; mod license_headers_check;
mod nodepackage;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
pub enum TaskCommand { pub enum TaskCommand {
@ -24,6 +25,8 @@ pub enum TaskCommand {
CppDocs, CppDocs,
#[structopt(name = "cbindgen")] #[structopt(name = "cbindgen")]
Cbindgen(cbindgen::CbindgenCommand), Cbindgen(cbindgen::CbindgenCommand),
#[structopt(name = "node_package")]
NodePackage,
} }
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
@ -72,6 +75,7 @@ fn main() -> Result<(), Box<dyn Error>> {
TaskCommand::CheckLicenseHeaders(cmd) => cmd.check_license_headers()?, TaskCommand::CheckLicenseHeaders(cmd) => cmd.check_license_headers()?,
TaskCommand::CppDocs => cppdocs::generate()?, TaskCommand::CppDocs => cppdocs::generate()?,
TaskCommand::Cbindgen(cmd) => cmd.run()?, TaskCommand::Cbindgen(cmd) => cmd.run()?,
TaskCommand::NodePackage => nodepackage::generate()?,
}; };
Ok(()) Ok(())

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(())
}