Skip to content

Commit 9247811

Browse files
chiropticalutdemir
andauthored
Allow user to select library or executable (#22)
* Allow user to select library or executable * Need to dedent the endif in default.nix * Use choice variable for add_executable_section * Use matrix strategy in github actions to test all combinations * Use argstr, not attr * Use double quotes * Use multiline string operator Co-authored-by: Utku Demir <[email protected]> * Use forward slash to split cookiecutter command up a bit Co-authored-by: Utku Demir <[email protected]> * Remove chomping character from multi-line yaml * Switch to yes/no use_hpack prompt, no multiline github action Co-authored-by: Utku Demir <[email protected]>
1 parent 7613ac3 commit 9247811

File tree

7 files changed

+29
-23
lines changed

7 files changed

+29
-23
lines changed

.github/workflows/build.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
push:
55
jobs:
66
build:
7+
strategy:
8+
matrix:
9+
use_hpack: ["no", "yes"]
10+
add_executable_section: ["no", "yes"]
711
runs-on: ubuntu-latest
812
steps:
913
- uses: actions/checkout@v2
@@ -12,4 +16,4 @@ jobs:
1216
with:
1317
name: hs-nix-template
1418
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
15-
- run: nix-build ci.nix
19+
- run: nix-build ci.nix --argstr use_hpack "${{ matrix.use_hpack }}" --argstr add_executable_section "${{ matrix.add_executable_section }}"

ci.nix

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1+
{
2+
use_hpack ? "no",
3+
add_executable_section ? "no"
4+
}:
5+
16
let
27
sources = import (./. + "/{{cookiecutter.project_name}}/nix/sources.nix");
38
pkgs = import sources.nixpkgs {};
49
in
510
rec {
6-
711
generated = pkgs.runCommand "hs-nix-template" {
812
buildInputs = [ pkgs.cookiecutter ];
913
preferLocalBuild = true;
1014
} ''
1115
HOME="$(mktemp -d)"
1216
mkdir "$out"
13-
cookiecutter --no-input --output-dir "$out" ${./.}
17+
cookiecutter \
18+
--no-input --output-dir "$out" ${./.} \
19+
use_hpack="${use_hpack}" \
20+
add_executable_section="${add_executable_section}"
1421
'';
1522

1623
build = pkgs.recurseIntoAttrs
1724
(import "${generated}/your-project-name" {});
18-
19-
generatedWithHpack = pkgs.runCommand "hs-nix-template" {
20-
buildInputs = [ pkgs.cookiecutter ];
21-
preferLocalBuild = true;
22-
} ''
23-
HOME="$(mktemp -d)"
24-
mkdir "$out"
25-
cookiecutter --no-input --output-dir "$out" ${./.} project_configuration_tool="package.yaml (hpack)"
26-
'';
27-
28-
buildWithHpack = pkgs.recurseIntoAttrs
29-
(import "${generatedWithHpack}/your-project-name" {});
3025
}

cookiecutter.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"module": "{{cookiecutter.project_name|title|replace('-', '')}}",
66
"author_name": "Your Name",
77
"gh_user": "your_github_username",
8-
"project_configuration_tool": ["{{cookiecutter.project_name|replace('-', '')}}.cabal (cabal's default)", "package.yaml (hpack)"]
8+
"use_hpack": ["no", "yes"],
9+
"add_executable_section": ["no", "yes"]
910
}

hooks/post_gen_project.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import sys
33

44
REMOVE_PATHS = [
5-
'{% if cookiecutter.project_configuration_tool.startswith("package.yaml") %} {{ cookiecutter.project_name }}.cabal {% endif %}',
6-
'{% if not cookiecutter.project_configuration_tool.startswith("package.yaml") %} package.yaml {% endif %}',
5+
'{% if cookiecutter.use_hpack == "yes" %} {{ cookiecutter.project_name }}.cabal {% endif %}',
6+
'{% if cookiecutter.use_hpack == "no" %} package.yaml {% endif %}',
7+
'{% if cookiecutter.add_executable_section == "no" %} app/Main.hs {% endif %}',
8+
'{% if cookiecutter.add_executable_section == "no" %} app {% endif %}',
79
]
810

911
for path in REMOVE_PATHS:

{{cookiecutter.project_name}}/default.nix

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,27 @@ let
2626
pkgs.haskellPackages.ghcid
2727
pkgs.haskellPackages.ormolu
2828
pkgs.haskellPackages.hlint
29-
{% if cookiecutter.project_configuration_tool.startswith("package.yaml") %}pkgs.haskellPackages.hpack
29+
{% if cookiecutter.use_hpack == "yes" %}pkgs.haskellPackages.hpack
3030
{% endif -%}
3131
pkgs.niv
3232
pkgs.nixpkgs-fmt
3333
];
3434
withHoogle = true;
3535
};
36-
36+
{% if cookiecutter.add_executable_section == "yes" %}
3737
exe = pkgs.haskell.lib.justStaticExecutables (myHaskellPackages."{{cookiecutter.project_name}}");
3838

3939
docker = pkgs.dockerTools.buildImage {
4040
name = "{{cookiecutter.project_name}}";
4141
config.Cmd = [ "${exe}/bin/{{cookiecutter.project_name}}" ];
4242
};
43+
{% endif -%}
4344
in
4445
{
4546
inherit shell;
46-
inherit exe;
47+
{% if cookiecutter.add_executable_section == "yes" %}inherit exe;
4748
inherit docker;
49+
{% endif -%}
4850
inherit myHaskellPackages;
4951
"{{cookiecutter.project_name}}" = myHaskellPackages."{{cookiecutter.project_name}}";
5052
}

{{cookiecutter.project_name}}/package.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ dependencies:
1313
library:
1414
source-dirs: src
1515

16-
executables:
16+
{% if cookiecutter.add_executable_section == "yes" %}executables:
1717
{{cookiecutter.project_name}}-exe:
1818
source-dirs: app
1919
main: Main.hs
2020
dependencies:
2121
- {{cookiecutter.project_name}}
2222

23+
{% endif -%}
2324
tests:
2425
{{cookiecutter.project_name}}-test:
2526
source-dirs: test

{{cookiecutter.project_name}}/{{cookiecutter.project_name}}.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ library
1818
ghc-options: -Wall -fno-warn-name-shadowing
1919
build-depends: base >= 4.11 && < 5
2020

21-
executable {{cookiecutter.project_name}}
21+
{% if cookiecutter.add_executable_section == "yes" %}executable {{cookiecutter.project_name}}
2222
main-is: Main.hs
2323
hs-source-dirs: app
2424
default-language: Haskell2010
2525
ghc-options: -threaded -O2
2626
build-depends: base
2727
, {{cookiecutter.project_name}}
2828

29+
{% endif -%}
2930
test-suite {{cookiecutter.project_name}}-tests
3031
type: exitcode-stdio-1.0
3132
hs-source-dirs: test

0 commit comments

Comments
 (0)