summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/build-openscad-package/default.nix
blob: 11bebe1bfb09629a74d557785bd40cd614470417 (plain)
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
{
  lib,
  stdenvNoCC,
  buildPackages,
}:

lib.extendMkDerivation {
  constructDrv = stdenvNoCC.mkDerivation;
  extendDrvArgs =
    finalAttrs:
    {
      pname,
      version,
      libName ? pname,
      installTargets ? [ "." ],

      ...
    }@args:

    {
      inherit
        pname
        version
        libName
        installTargets
        ;

      __structuredAttrs = true;
      name = "openscad-package-${finalAttrs.pname}-${finalAttrs.version}";

      strictDeps = true;

      # Use the interactive Bash shell (with readline support) for the compgen shell built-in.
      realBuilder = lib.getExe buildPackages.bash;

      installPhase =
        args.installPhase or ''
          runHook preInstall

          installDir="$out/share/openscad/libraries/$libName"
          mkdir -p "$installDir"

          for _target in "''${installTargets[@]}"; do
            _targetDirName="$(dirname "$_target")"
            _targetBaseName="$(basename "$_target")"
            # Support globs using Bash's compgen built-in.
            (
              if [[ "$_targetDirName" != "." ]]; then
                if ! cd "$_targetDirName"; then
                  echo "ERROR: $name: Fails to switch to the dirname of $_target -- $_targetDirName" >&2
                  exit 1
                fi
              fi
              if [[ "$_targetBaseName" == "." ]]; then
                echo "$_targetBaseName"
              else
                if ! compgen -G "$_targetBaseName"; then
                  echo "ERROR: $name: Glob $_target does not match any files or directories." >&2
                  exit 1
                fi
              fi
            # Keep the number of input files per batch under Bash's command length limitation.
            ) | while read -r _line; do
              printf "%s/%s\0" "$_targetDirName" "$_line"
            done | xargs -0 cp -r -t "$installDir"
          done

          runHook postInstall
        '';
    };
}