Skip to content

Commit

Permalink
v0.8.1
Browse files Browse the repository at this point in the history
- fix issue #3: example fails in last `cabal run test` step
  • Loading branch information
yvan-sraka committed Feb 13, 2023
1 parent 2ed1fc5 commit 7692f26
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 74 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/build_nix.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v19
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Building package
run: nix build
- name: Running test scenario
run: cd ./tests && ./simple.sh
22 changes: 0 additions & 22 deletions .github/workflows/rust.yml

This file was deleted.

59 changes: 46 additions & 13 deletions flake.lock

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

21 changes: 15 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
outputs = { self, flake-utils, naersk, nixpkgs, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
in with pkgs; {
devShells.default = mkShell {
buildInputs = [ (rust-bin.fromRustupToolchainFile ./rust-toolchain) ];
toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain;
naersk' = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in rec {

# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage { src = ./.; };

# For `nix develop`:
devShell = pkgs.mkShell { nativeBuildInputs = [ toolchain ]; };
});
}
2 changes: 1 addition & 1 deletion src/cabal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ custom-setup
-- documentation, see: http://haskell.org/cabal/users-guide/
--
-- The name of the package.
name: {module}
name: {name}
-- The package version.
-- See the Haskell package versioning policy (PVP) for standards
Expand Down
40 changes: 21 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,27 +327,29 @@ fn main() {
}

fn routine() -> Result<(), Error> {
// Parse Cargo.toml file content ...
let cargo = fs::read_to_string("Cargo.toml").or(Err(Error::NoCargoToml))?;
let root: cargo::Root = toml::from_str(&cargo).or(Err(Error::WrongCargoToml))?;
let package = root.clone().package.ok_or(Error::NotCargoPackage)?;
let version = package.version.unwrap_or_else(|| "0.1.0.0".to_owned());
let name = package.name.ok_or(Error::NoCargoNameField)?;
let module = name
.split(&['-', '_'])
.map(|s| format!("{}{}", &s[..1].to_uppercase(), &s[1..]))
.collect::<Vec<String>>()
.join("");

// Parse `cargo-cabal` CLI arguments
match Args::parse().cabal {
Wrapper::Cabal(command) => match command {
Commands::Init {
enable_nix,
overwrite,
} => init(root, &version, &name, &module, enable_nix, overwrite),
Commands::Clean => clean(&name),
},
Wrapper::Cabal(command) => {
// Parse Cargo.toml file content ...
let cargo = fs::read_to_string("Cargo.toml").or(Err(Error::NoCargoToml))?;
let root: cargo::Root = toml::from_str(&cargo).or(Err(Error::WrongCargoToml))?;
let package = root.clone().package.ok_or(Error::NotCargoPackage)?;
let version = package.version.unwrap_or_else(|| "0.1.0.0".to_owned());
let name = package.name.ok_or(Error::NoCargoNameField)?;
let module = name
.split(&['-', '_'])
.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),
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist-newstyle
1 change: 1 addition & 0 deletions tests/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: ./greetings ./test
6 changes: 6 additions & 0 deletions tests/greetings/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
/Cargo.lock
/greetings.cabal
/hsbindgen.toml
/Setup.lhs
/src/Greetings.hs
12 changes: 12 additions & 0 deletions tests/greetings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "greetings"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hs-bindgen = { version = "0.8.0", features = ["full"] }

[lib]
crate-type = ["staticlib"]
6 changes: 6 additions & 0 deletions tests/greetings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use hs_bindgen::*;

#[hs_bindgen]
fn hello(name: &str) {
println!("Hello, {name}!");
}
9 changes: 9 additions & 0 deletions tests/simple.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p cargo rustc cabal-install ghc
cd greetings
cargo add hs-bindgen --features full
../../result/bin/cargo-cabal cabal init
cargo build
cabal build
cd ..
cabal run test
7 changes: 7 additions & 0 deletions tests/test/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Main where

import Foreign.C.String
import Greetings

main :: IO ()
main = withCString "Rust 🦀" hello
73 changes: 73 additions & 0 deletions tests/test/test.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cabal-version: 3.8
-- The cabal-version field refers to the version of the .cabal specification,
-- and can be different from the cabal-install (the tool) version and the
-- Cabal (the library) version you are using. As such, the Cabal (the library)
-- version used must be equal or greater than the version stated in this field.
-- Starting from the specification version 2.2, the cabal-version field must be
-- the first thing in the cabal file.

-- Initial package description 'test' generated by
-- 'cabal init'. For further documentation, see:
-- http://haskell.org/cabal/users-guide/
--
-- The name of the package.
name: test

-- The package version.
-- See the Haskell package versioning policy (PVP) for standards
-- guiding when and how versions should be incremented.
-- https://pvp.haskell.org
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- The license under which the package is released.
license: NONE

-- The package author(s).
author: yvan@sraka.xyz

-- An email address to which users can send suggestions, bug reports, and patches.
maintainer: Yvan Sraka

-- A copyright notice.
-- copyright:
build-type: Simple

-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README.
-- extra-doc-files: CHANGELOG.md

-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
-- extra-source-files:

common warnings
ghc-options: -Wall

executable test
-- Import common warning flags.
import: warnings

-- .hs or .lhs file containing the Main module.
main-is: Main.hs

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:

-- Other library packages from which modules are imported.
build-depends: base, greetings

-- Directories containing source files.
hs-source-dirs: app

-- Base language which the package is written in.
default-language: Haskell2010

0 comments on commit 7692f26

Please sign in to comment.