about summary refs log tree commit diff
path: root/nixpkgs/pkgs/tools/text/shab/default.nix
blob: b5bb98e5f4d2a6b3ea8cb5631e53b513728d5ca5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{ bash, stdenv, lib, runCommand, writeText, fetchFromGitHub }:
let
  version = "1.0.0";

  shab = stdenv.mkDerivation {
    pname = "shab";
    inherit version;

    src = fetchFromGitHub {
      owner = "zimbatm";
      repo = "shab";
      rev = "v${version}";
      sha256 = "02lf1s6plhhcfyj9xha44wij9jbphb1x5q55xj3b5bx2ji2jsvji";
    };

    postPatch = ''
      for f in test.sh test/*.sh; do
        patchShebangs "$f"
      done
    '';

    doCheck = true;
    doInstallCheck = true;

    checkPhase = ''
      ./test.sh
    '';

    installPhase = ''
      mkdir -p $out/bin
      cp ./shab $out/bin/shab
    '';

    installCheckPhase = ''
      [[ "$(echo 'Hello $entity' | entity=world $out/bin/shab)" == 'Hello world' ]]
    '';

    passthru = {
      inherit render renderText;
    };

    meta = with lib; {
      description = "The bash templating language";
      homepage = "https://github.com/zimbatm/shab";
      license = licenses.unlicense;
      maintainers = with maintainers; [ zimbatm ];
      platforms = bash.meta.platforms;
    };
  };

  /*
     shabScript:       a path or filename to use as a template
     parameters.name:  the name to use as part of the store path
     parameters:       variables to expose to the template
   */
  render = shabScript: parameters:
    let extraParams = {
          inherit shabScript;
        };
    in runCommand "out" (parameters // extraParams) ''
      ${shab}/bin/shab "$shabScript" >$out
    '';

  /*
     shabScriptText:   a string to use as a template
     parameters.name:  the name to use as part of the store path
     parameters:       variables to expose to the template
   */
  renderText = shabScriptText: parameters:
    render (writeText "template" shabScriptText) parameters;

in
  shab