Skip to content

Commit

Permalink
v0.8.2
Browse files Browse the repository at this point in the history
- refactor a bit main.rs routine, and tests/simple.sh
- add an haskell.nix based test scenario
  • Loading branch information
yvan-sraka committed Feb 15, 2023
1 parent 7692f26 commit 3afcc6d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
6 changes: 3 additions & 3 deletions flake.lock

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

53 changes: 34 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -341,29 +349,36 @@ fn routine() -> Result<(), Error> {
.map(|s| format!("{}{}", &s[..1].to_uppercase(), &s[1..]))
.collect::<Vec<String>>()
.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 ...
Expand Down Expand Up @@ -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 ...
Expand All @@ -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"))
Expand Down
8 changes: 8 additions & 0 deletions tests/haskell-nix.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions tests/simple.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3afcc6d

Please sign in to comment.