~kaction/cookiecutter-haskell

92478119289f828645d56721df7f1450bdd44861 — Barry Moore 2 years ago 7613ac3
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 <me@utdemir.com>

* Use forward slash to split cookiecutter command up a bit

Co-authored-by: Utku Demir <me@utdemir.com>

* Remove chomping character from multi-line yaml

* Switch to yes/no use_hpack prompt, no multiline github action

Co-authored-by: Utku Demir <me@utdemir.com>
M .github/workflows/build.yaml => .github/workflows/build.yaml +5 -1
@@ 4,6 4,10 @@ on:
  push:
jobs:
  build:
    strategy:
      matrix:
        use_hpack: ["no", "yes"]
        add_executable_section: ["no", "yes"]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2


@@ 12,4 16,4 @@ jobs:
      with:
        name: hs-nix-template
        signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
    - run: nix-build ci.nix
    - run: nix-build ci.nix --argstr use_hpack "${{ matrix.use_hpack }}" --argstr add_executable_section "${{ matrix.add_executable_section }}"

M ci.nix => ci.nix +9 -14
@@ 1,30 1,25 @@
{
  use_hpack ? "no",
  add_executable_section ? "no"
}:

let
  sources = import (./. + "/{{cookiecutter.project_name}}/nix/sources.nix");
  pkgs = import sources.nixpkgs {};
in
rec {

  generated = pkgs.runCommand "hs-nix-template" {
    buildInputs = [ pkgs.cookiecutter ];
    preferLocalBuild = true;
  } ''
    HOME="$(mktemp -d)"
    mkdir "$out"
    cookiecutter --no-input --output-dir "$out" ${./.}
    cookiecutter \
      --no-input --output-dir "$out" ${./.} \
      use_hpack="${use_hpack}" \
      add_executable_section="${add_executable_section}"
  '';

  build = pkgs.recurseIntoAttrs
    (import "${generated}/your-project-name" {});

  generatedWithHpack = pkgs.runCommand "hs-nix-template" {
    buildInputs = [ pkgs.cookiecutter ];
    preferLocalBuild = true;
  } ''
    HOME="$(mktemp -d)"
    mkdir "$out"
    cookiecutter --no-input --output-dir "$out" ${./.} project_configuration_tool="package.yaml (hpack)"
  '';

  buildWithHpack = pkgs.recurseIntoAttrs
    (import "${generatedWithHpack}/your-project-name" {});
}

M cookiecutter.json => cookiecutter.json +2 -1
@@ 5,5 5,6 @@
  "module": "{{cookiecutter.project_name|title|replace('-', '')}}",
  "author_name": "Your Name",
  "gh_user": "your_github_username",
  "project_configuration_tool": ["{{cookiecutter.project_name|replace('-', '')}}.cabal (cabal's default)", "package.yaml (hpack)"]
  "use_hpack": ["no", "yes"],
  "add_executable_section": ["no", "yes"]
}

M hooks/post_gen_project.py => hooks/post_gen_project.py +4 -2
@@ 2,8 2,10 @@ import os
import sys

REMOVE_PATHS = [
    '{% if cookiecutter.project_configuration_tool.startswith("package.yaml") %} {{ cookiecutter.project_name }}.cabal {% endif %}',
    '{% if not cookiecutter.project_configuration_tool.startswith("package.yaml") %} package.yaml {% endif %}',
    '{% if cookiecutter.use_hpack == "yes" %} {{ cookiecutter.project_name }}.cabal {% endif %}',
    '{% if cookiecutter.use_hpack == "no" %} package.yaml {% endif %}',
    '{% if cookiecutter.add_executable_section == "no" %} app/Main.hs {% endif %}',
    '{% if cookiecutter.add_executable_section == "no" %} app {% endif %}',
]

for path in REMOVE_PATHS:

M {{cookiecutter.project_name}}/default.nix => {{cookiecutter.project_name}}/default.nix +5 -3
@@ 26,25 26,27 @@ let
      pkgs.haskellPackages.ghcid
      pkgs.haskellPackages.ormolu
      pkgs.haskellPackages.hlint
      {% if cookiecutter.project_configuration_tool.startswith("package.yaml") %}pkgs.haskellPackages.hpack
      {% if cookiecutter.use_hpack == "yes" %}pkgs.haskellPackages.hpack
      {% endif -%}
      pkgs.niv
      pkgs.nixpkgs-fmt
    ];
    withHoogle = true;
  };

{% if cookiecutter.add_executable_section == "yes" %}
  exe = pkgs.haskell.lib.justStaticExecutables (myHaskellPackages."{{cookiecutter.project_name}}");

  docker = pkgs.dockerTools.buildImage {
    name = "{{cookiecutter.project_name}}";
    config.Cmd = [ "${exe}/bin/{{cookiecutter.project_name}}" ];
  };
{% endif -%}
in
{
  inherit shell;
  inherit exe;
  {% if cookiecutter.add_executable_section == "yes" %}inherit exe;
  inherit docker;
  {% endif -%}
  inherit myHaskellPackages;
  "{{cookiecutter.project_name}}" = myHaskellPackages."{{cookiecutter.project_name}}";
}

M {{cookiecutter.project_name}}/package.yaml => {{cookiecutter.project_name}}/package.yaml +2 -1
@@ 13,13 13,14 @@ dependencies:
library:
  source-dirs: src

executables:
{% if cookiecutter.add_executable_section == "yes" %}executables:
  {{cookiecutter.project_name}}-exe:
    source-dirs: app
    main: Main.hs
    dependencies:
      - {{cookiecutter.project_name}}

{% endif -%}
tests:
  {{cookiecutter.project_name}}-test:
    source-dirs: test

M {{cookiecutter.project_name}}/{{cookiecutter.project_name}}.cabal => {{cookiecutter.project_name}}/{{cookiecutter.project_name}}.cabal +2 -1
@@ 18,7 18,7 @@ library
  ghc-options:         -Wall -fno-warn-name-shadowing
  build-depends:       base >= 4.11 && < 5

executable {{cookiecutter.project_name}}
{% if cookiecutter.add_executable_section == "yes" %}executable {{cookiecutter.project_name}}
  main-is:            Main.hs
  hs-source-dirs:     app
  default-language:   Haskell2010


@@ 26,6 26,7 @@ executable {{cookiecutter.project_name}}
  build-depends:      base
                    , {{cookiecutter.project_name}}

{% endif -%}
test-suite {{cookiecutter.project_name}}-tests
  type:             exitcode-stdio-1.0
  hs-source-dirs:   test