From 40954a79db9feea021a54fd4e720ab22dbf8d51f Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Tue, 22 Aug 2023 21:38:20 -0700 Subject: [PATCH] new: Copy info files during build. (#9) --- Cargo.lock | 1 + crates/cli/Cargo.toml | 3 + crates/cli/src/app.rs | 11 +-- crates/cli/src/commands/build.rs | 2 + crates/cli/tests/build_test.rs | 64 ++++++++++++ crates/package/src/package.rs | 57 +++++++++++ .../tests/__fixtures__/common/espm.toml | 2 + crates/package/tests/package_test.rs | 98 ++++++++++++++++++- tests/__fixtures__/monorepo/espm.toml | 2 + .../monorepo/packages/bar/CHANGELOG.md | 0 .../monorepo/packages/bar/espm.toml | 2 + .../monorepo/packages/bar/src/index.mts | 1 + .../monorepo/packages/baz/README.md | 0 .../monorepo/packages/baz/espm.toml | 2 + .../monorepo/packages/baz/src/index.mts | 1 + .../monorepo/packages/foo/LICENSE | 0 .../monorepo/packages/foo/espm.toml | 2 + .../monorepo/packages/foo/src/index.mts | 1 + tests/__fixtures__/polyrepo/espm.toml | 2 + tests/__fixtures__/polyrepo/src/index.mts | 1 + 20 files changed, 242 insertions(+), 10 deletions(-) create mode 100644 crates/cli/tests/build_test.rs create mode 100644 crates/package/tests/__fixtures__/common/espm.toml create mode 100644 tests/__fixtures__/monorepo/espm.toml create mode 100644 tests/__fixtures__/monorepo/packages/bar/CHANGELOG.md create mode 100644 tests/__fixtures__/monorepo/packages/bar/espm.toml create mode 100644 tests/__fixtures__/monorepo/packages/bar/src/index.mts create mode 100644 tests/__fixtures__/monorepo/packages/baz/README.md create mode 100644 tests/__fixtures__/monorepo/packages/baz/espm.toml create mode 100644 tests/__fixtures__/monorepo/packages/baz/src/index.mts create mode 100644 tests/__fixtures__/monorepo/packages/foo/LICENSE create mode 100644 tests/__fixtures__/monorepo/packages/foo/espm.toml create mode 100644 tests/__fixtures__/monorepo/packages/foo/src/index.mts create mode 100644 tests/__fixtures__/polyrepo/espm.toml create mode 100644 tests/__fixtures__/polyrepo/src/index.mts diff --git a/Cargo.lock b/Cargo.lock index defa5c4..d4ba7aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -930,6 +930,7 @@ dependencies = [ "miette 5.10.0", "mimalloc", "starbase", + "starbase_sandbox", "starbase_styles", "tokio", "tracing", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index a610325..9791c05 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -21,3 +21,6 @@ starbase = { workspace = true } starbase_styles = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } + +[dev-dependencies] +starbase_sandbox = { workspace = true } diff --git a/crates/cli/src/app.rs b/crates/cli/src/app.rs index 13439e1..2b53c3a 100644 --- a/crates/cli/src/app.rs +++ b/crates/cli/src/app.rs @@ -29,17 +29,11 @@ pub enum Commands { #[command( name = "build", about = "Build a package.", - long_about = "Build a package by transforming source files (from the package's `src` directory) to the `.espm/` output directory.", - rename_all = "camelCase" + long_about = "Build a package by transforming source files (from the package's `src` directory) to the `.espm/` output directory." )] Build(BuildArgs), - #[command( - name = "debug", - about = "Debug Espresso instance.", - rename_all = "camelCase", - hide = true - )] + #[command(name = "debug", about = "Debug Espresso instance.", hide = true)] Debug, } @@ -53,7 +47,6 @@ pub enum Commands { disable_help_subcommand = true, propagate_version = true, next_line_help = false, - rename_all = "camelCase" )] #[allow(clippy::upper_case_acronyms)] pub struct CLI { diff --git a/crates/cli/src/commands/build.rs b/crates/cli/src/commands/build.rs index ba4dcfc..38aeca4 100644 --- a/crates/cli/src/commands/build.rs +++ b/crates/cli/src/commands/build.rs @@ -39,6 +39,8 @@ pub async fn build( .compile(args.target) .await?; + package.copy_info_files(&out_dir)?; + println!("Built to {}", color::path(out_dir)); Ok(()) diff --git a/crates/cli/tests/build_test.rs b/crates/cli/tests/build_test.rs new file mode 100644 index 0000000..f327890 --- /dev/null +++ b/crates/cli/tests/build_test.rs @@ -0,0 +1,64 @@ +use starbase_sandbox::{create_command_with_name, create_sandbox}; + +mod build { + use super::*; + + #[test] + fn builds_polyrepo() { + let sandbox = create_sandbox("polyrepo"); + + create_command_with_name(sandbox.path(), "espm") + .args(["build", "--target", "es2015"]) + .assert(); + + assert!(sandbox.path().join(".espm/es2015").exists()); + } + + #[test] + fn builds_all_in_monorepo() { + let sandbox = create_sandbox("monorepo"); + + create_command_with_name(sandbox.path(), "espm") + .args(["build", "--target", "es2016", "--workspace"]) + .assert(); + + assert!(sandbox.path().join("packages/bar/.espm/es2016").exists()); + assert!(sandbox.path().join("packages/baz/.espm/es2016").exists()); + assert!(sandbox.path().join("packages/foo/.espm/es2016").exists()); + } + + #[test] + fn builds_selected_in_monorepo() { + let sandbox = create_sandbox("monorepo"); + + create_command_with_name(sandbox.path(), "espm") + .args(["build", "--target", "es2017", "--package", "mono/baz"]) + .assert(); + + assert!(!sandbox.path().join("packages/bar/.espm/es2017").exists()); + assert!(sandbox.path().join("packages/baz/.espm/es2017").exists()); + assert!(!sandbox.path().join("packages/foo/.espm/es2017").exists()); + } + + #[test] + fn copies_info_files_for_each_package() { + let sandbox = create_sandbox("monorepo"); + + create_command_with_name(sandbox.path(), "espm") + .args(["build", "--target", "es2018", "--workspace"]) + .assert(); + + assert!(sandbox + .path() + .join("packages/bar/.espm/es2018/CHANGELOG.md") + .exists()); + assert!(sandbox + .path() + .join("packages/baz/.espm/es2018/README.md") + .exists()); + assert!(sandbox + .path() + .join("packages/foo/.espm/es2018/LICENSE") + .exists()); + } +} diff --git a/crates/package/src/package.rs b/crates/package/src/package.rs index 39bb958..298047f 100644 --- a/crates/package/src/package.rs +++ b/crates/package/src/package.rs @@ -37,6 +37,28 @@ impl Package { self.manifest.package.name.as_str() } + pub fn copy_info_files(&self, out_dir: &Path) -> miette::Result<()> { + let mut files = vec![]; + + if let Some(file) = self.locate_changelog() { + files.push(file); + } + + if let Some(file) = self.locate_license() { + files.push(file); + } + + if let Some(file) = self.locate_readme() { + files.push(file); + } + + for file in files { + fs::copy_file(&file, out_dir.join(fs::file_name(&file)))?; + } + + Ok(()) + } + pub fn load_source_files(&self) -> miette::Result { debug!(package = self.name(), src_dir = ?self.src_dir, "Loading source files"); @@ -126,4 +148,39 @@ impl Package { Ok(sources) } + + pub fn locate_changelog(&self) -> Option { + self.locate_file_in_root(&["CHANGELOG", "HISTORY"]) + } + + pub fn locate_license(&self) -> Option { + self.locate_file_in_root(&["LICENSE"]) + } + + pub fn locate_readme(&self) -> Option { + self.locate_file_in_root(&["README", "ABOUT"]) + } + + fn locate_file_in_root(&self, lookups: &[&str]) -> Option { + let mut files = vec![]; + + for lookup in lookups { + files.push(format!("{lookup}.md")); + files.push(lookup.to_string()); + + let lookup = lookup.to_lowercase(); + files.push(format!("{lookup}.md")); + files.push(lookup.to_string()); + } + + for file in files { + let path = self.root.join(file); + + if path.exists() { + return Some(path); + } + } + + None + } } diff --git a/crates/package/tests/__fixtures__/common/espm.toml b/crates/package/tests/__fixtures__/common/espm.toml new file mode 100644 index 0000000..5c7dfa3 --- /dev/null +++ b/crates/package/tests/__fixtures__/common/espm.toml @@ -0,0 +1,2 @@ +[package] +name = "ns/common" diff --git a/crates/package/tests/package_test.rs b/crates/package/tests/package_test.rs index 5b2ff77..e3870d8 100644 --- a/crates/package/tests/package_test.rs +++ b/crates/package/tests/package_test.rs @@ -1,5 +1,5 @@ use espresso_package::*; -use starbase_sandbox::create_empty_sandbox; +use starbase_sandbox::{create_empty_sandbox, create_sandbox}; mod package { use super::*; @@ -11,4 +11,100 @@ mod package { Package::new(sandbox.path().join("missing")).unwrap(); } + + #[test] + fn copies_info_files_to_dir() { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + sandbox.create_file("CHANGELOG.md", "v1.0.0"); + sandbox.create_file("LICENSE", "MIT"); + sandbox.create_file("readme.md", "Intro"); + + let out_dir = sandbox.path().join("out"); + + package.copy_info_files(&out_dir).unwrap(); + + assert!(out_dir.join("CHANGELOG.md").exists()); + assert!(out_dir.join("LICENSE").exists()); + assert!(out_dir.join("readme.md").exists()); + } + + #[test] + fn locates_changelog() { + for file in [ + "CHANGELOG.md", + "CHANGELOG", + "changelog.md", + "changelog", + "HISTORY.md", + "HISTORY", + "history.md", + "history", + ] { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + sandbox.create_file(file, "v1.0.0"); + + assert!(package.locate_changelog().is_some()); + } + } + + #[test] + fn doesnt_locate_changelog() { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + assert!(package.locate_changelog().is_none()); + } + + #[test] + fn locates_license() { + for file in ["LICENSE.md", "LICENSE", "license.md", "license"] { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + sandbox.create_file(file, "MIT"); + + assert!(package.locate_license().is_some()); + } + } + + #[test] + fn doesnt_locate_license() { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + assert!(package.locate_license().is_none()); + } + + #[test] + fn locates_readme() { + for file in [ + "README.md", + "README", + "readme.md", + "readme", + "ABOUT.md", + "ABOUT", + "about.md", + "about", + ] { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + sandbox.create_file(file, "Intro"); + + assert!(package.locate_readme().is_some()); + } + } + + #[test] + fn doesnt_locate_readme() { + let sandbox = create_sandbox("common"); + let package = Package::new(sandbox.path()).unwrap(); + + assert!(package.locate_readme().is_none()); + } } diff --git a/tests/__fixtures__/monorepo/espm.toml b/tests/__fixtures__/monorepo/espm.toml new file mode 100644 index 0000000..2f613b1 --- /dev/null +++ b/tests/__fixtures__/monorepo/espm.toml @@ -0,0 +1,2 @@ +[workspace] +packages = ["packages/*"] diff --git a/tests/__fixtures__/monorepo/packages/bar/CHANGELOG.md b/tests/__fixtures__/monorepo/packages/bar/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/__fixtures__/monorepo/packages/bar/espm.toml b/tests/__fixtures__/monorepo/packages/bar/espm.toml new file mode 100644 index 0000000..99f4ae2 --- /dev/null +++ b/tests/__fixtures__/monorepo/packages/bar/espm.toml @@ -0,0 +1,2 @@ +[package] +name = "mono/bar" diff --git a/tests/__fixtures__/monorepo/packages/bar/src/index.mts b/tests/__fixtures__/monorepo/packages/bar/src/index.mts new file mode 100644 index 0000000..474ad0b --- /dev/null +++ b/tests/__fixtures__/monorepo/packages/bar/src/index.mts @@ -0,0 +1 @@ +export type Package = 'bar'; diff --git a/tests/__fixtures__/monorepo/packages/baz/README.md b/tests/__fixtures__/monorepo/packages/baz/README.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/__fixtures__/monorepo/packages/baz/espm.toml b/tests/__fixtures__/monorepo/packages/baz/espm.toml new file mode 100644 index 0000000..29654bf --- /dev/null +++ b/tests/__fixtures__/monorepo/packages/baz/espm.toml @@ -0,0 +1,2 @@ +[package] +name = "mono/baz" diff --git a/tests/__fixtures__/monorepo/packages/baz/src/index.mts b/tests/__fixtures__/monorepo/packages/baz/src/index.mts new file mode 100644 index 0000000..4959a8b --- /dev/null +++ b/tests/__fixtures__/monorepo/packages/baz/src/index.mts @@ -0,0 +1 @@ +export type Package = 'baz'; diff --git a/tests/__fixtures__/monorepo/packages/foo/LICENSE b/tests/__fixtures__/monorepo/packages/foo/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/tests/__fixtures__/monorepo/packages/foo/espm.toml b/tests/__fixtures__/monorepo/packages/foo/espm.toml new file mode 100644 index 0000000..011e060 --- /dev/null +++ b/tests/__fixtures__/monorepo/packages/foo/espm.toml @@ -0,0 +1,2 @@ +[package] +name = "mono/foo" diff --git a/tests/__fixtures__/monorepo/packages/foo/src/index.mts b/tests/__fixtures__/monorepo/packages/foo/src/index.mts new file mode 100644 index 0000000..5a2e111 --- /dev/null +++ b/tests/__fixtures__/monorepo/packages/foo/src/index.mts @@ -0,0 +1 @@ +export type Package = 'foo'; diff --git a/tests/__fixtures__/polyrepo/espm.toml b/tests/__fixtures__/polyrepo/espm.toml new file mode 100644 index 0000000..611549b --- /dev/null +++ b/tests/__fixtures__/polyrepo/espm.toml @@ -0,0 +1,2 @@ +[package] +name = "poly/root" diff --git a/tests/__fixtures__/polyrepo/src/index.mts b/tests/__fixtures__/polyrepo/src/index.mts new file mode 100644 index 0000000..2029fd1 --- /dev/null +++ b/tests/__fixtures__/polyrepo/src/index.mts @@ -0,0 +1 @@ +export type Poly = true;