Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
new: Copy info files during build. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Aug 23, 2023
1 parent 6f2a6ee commit 40954a7
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ starbase = { workspace = true }
starbase_styles = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
starbase_sandbox = { workspace = true }
11 changes: 2 additions & 9 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/<target>` output directory.",
rename_all = "camelCase"
long_about = "Build a package by transforming source files (from the package's `src` directory) to the `.espm/<target>` 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,
}

Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
64 changes: 64 additions & 0 deletions crates/cli/tests/build_test.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
57 changes: 57 additions & 0 deletions crates/package/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceFiles> {
debug!(package = self.name(), src_dir = ?self.src_dir, "Loading source files");

Expand Down Expand Up @@ -126,4 +148,39 @@ impl Package {

Ok(sources)
}

pub fn locate_changelog(&self) -> Option<PathBuf> {
self.locate_file_in_root(&["CHANGELOG", "HISTORY"])
}

pub fn locate_license(&self) -> Option<PathBuf> {
self.locate_file_in_root(&["LICENSE"])
}

pub fn locate_readme(&self) -> Option<PathBuf> {
self.locate_file_in_root(&["README", "ABOUT"])
}

fn locate_file_in_root(&self, lookups: &[&str]) -> Option<PathBuf> {
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
}
}
2 changes: 2 additions & 0 deletions crates/package/tests/__fixtures__/common/espm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "ns/common"
98 changes: 97 additions & 1 deletion crates/package/tests/package_test.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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());
}
}
2 changes: 2 additions & 0 deletions tests/__fixtures__/monorepo/espm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
packages = ["packages/*"]
Empty file.
2 changes: 2 additions & 0 deletions tests/__fixtures__/monorepo/packages/bar/espm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "mono/bar"
1 change: 1 addition & 0 deletions tests/__fixtures__/monorepo/packages/bar/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Package = 'bar';
Empty file.
2 changes: 2 additions & 0 deletions tests/__fixtures__/monorepo/packages/baz/espm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "mono/baz"
1 change: 1 addition & 0 deletions tests/__fixtures__/monorepo/packages/baz/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Package = 'baz';
Empty file.
2 changes: 2 additions & 0 deletions tests/__fixtures__/monorepo/packages/foo/espm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "mono/foo"
1 change: 1 addition & 0 deletions tests/__fixtures__/monorepo/packages/foo/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Package = 'foo';
2 changes: 2 additions & 0 deletions tests/__fixtures__/polyrepo/espm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "poly/root"
1 change: 1 addition & 0 deletions tests/__fixtures__/polyrepo/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Poly = true;

0 comments on commit 40954a7

Please sign in to comment.