diff --git a/Cargo.lock b/Cargo.lock index 32875c4..2713e1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cargo-cabal" -version = "0.8.0" +version = "0.8.2" dependencies = [ "ansi_term", "clap", diff --git a/Cargo.toml b/Cargo.toml index a911ba4..0ea65e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" name = "cargo-cabal" repository = "https://github.com/yvan-sraka/cargo-cabal" rust-version = "1.62.1" -version = "0.8.0" +version = "0.8.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/flake.lock b/flake.lock index c1ccfe0..059ff9b 100644 --- a/flake.lock +++ b/flake.lock @@ -108,11 +108,11 @@ "nixpkgs": "nixpkgs_3" }, "locked": { - "lastModified": 1676255417, - "narHash": "sha256-YYOQgNNQOq4oBbkbM+rG4mDOms3ztjIqxWOrMWsZ+xg=", + "lastModified": 1676341851, + "narHash": "sha256-T8cmSiriXdpZfqlserNyJ1solTCR0DbD8A75epSDOVY=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "1bd5d7bb2f31cbc43fb8f722e3a39a45ee4dcec8", + "rev": "956ddb5047f98ea08b792b22004b94a9971932c4", "type": "github" }, "original": { diff --git a/src/main.rs b/src/main.rs index ac2129d..99ab578 100644 --- a/src/main.rs +++ b/src/main.rs @@ -320,6 +320,14 @@ enum Commands { Clean, } +// TODO: rather use https://crates.io/crates/cargo_metadata?! +struct CargoMetadata { + root: cargo::Root, + version: String, + name: String, + module: String, +} + fn main() { if let Err(e) = routine() { println!("{}{}", Colour::Red.bold().paint("Error: "), e); @@ -341,29 +349,36 @@ fn routine() -> Result<(), Error> { .map(|s| format!("{}{}", &s[..1].to_uppercase(), &s[1..])) .collect::>() .join(""); - - match command { - Commands::Init { - enable_nix, - overwrite, - } => init(root, &version, &name, &module, enable_nix, overwrite), - Commands::Clean => clean(&name), + let metadata = CargoMetadata { + root, + version, + name, + module, + }; + if let Commands::Init { .. } = command { + init(command, metadata) + } else { + clean(&metadata.name) } } } } -fn init( - root: cargo::Root, - version: &str, - name: &str, - module: &str, - enable_nix: bool, - overwrite: bool, -) -> Result<(), Error> { +fn init(args: Commands, metadata: CargoMetadata) -> Result<(), Error> { + let Commands::Init { + enable_nix, + overwrite, + } = args else { unreachable!() }; + let CargoMetadata { + root, + version, + name, + module, + } = metadata; + // `cargo cabal init --overwrite` == `cargo cabal clean && cargo cabal init` if overwrite { - clean(name)?; + clean(&name)?; } // Check that project have a `crate-type` target ... @@ -393,12 +408,12 @@ fn init( // Generate wanted files from templates ... starting by a `.cabal` ... fs::write( cabal.clone(), - cabal::generate(name, module, version, enable_nix), + cabal::generate(&name, &module, &version, enable_nix), ) .or(Err(Error::FailedToWriteFile(cabal)))?; // `hsbindgen.toml` is a config file readed by `#[hs_bindgen]` proc macro ... - fs::write("hsbindgen.toml", hsbindgen::generate(module)) + fs::write("hsbindgen.toml", hsbindgen::generate(&module)) .map_err(|_| Error::FailedToWriteFile("hsbindgen.toml".to_owned()))?; // If `crate-type = [ "cdylib" ]` then a custom `build.rs` is needed ... @@ -409,7 +424,7 @@ fn init( // `--enable-nix` CLI option generate a `flake.nix` rather than a `Setup.lhs` if enable_nix { - fs::write("flake.nix", flake::generate(name)) + fs::write("flake.nix", flake::generate(&name)) .map_err(|_| Error::FailedToWriteFile("flake.nix".to_owned()))?; } else { fs::write("Setup.lhs", include_str!("Setup.lhs")) diff --git a/tests/haskell-nix.sh b/tests/haskell-nix.sh new file mode 100755 index 0000000..03724fd --- /dev/null +++ b/tests/haskell-nix.sh @@ -0,0 +1,8 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i bash -p cargo rustc +set -euxo pipefail +pushd greetings +cargo add hs-bindgen --features full +../../result/bin/cargo-cabal cabal init --overwrite --enable-nix +git add flake.nix +nix build diff --git a/tests/simple.sh b/tests/simple.sh index 6d69eec..9716033 100755 --- a/tests/simple.sh +++ b/tests/simple.sh @@ -1,9 +1,11 @@ #! /usr/bin/env nix-shell #! nix-shell -i bash -p cargo rustc cabal-install ghc -cd greetings +set -euxo pipefail +pushd greetings +cargo clean cargo add hs-bindgen --features full -../../result/bin/cargo-cabal cabal init +../../result/bin/cargo-cabal cabal init --overwrite cargo build -cabal build -cd .. +popd +cabal clean cabal run test