summary refs log tree commit diff
path: root/pkgs/build-support/skaware/clean-packaging.nix
blob: 16bae04b21e2e6a22118b8700e3787826ab3cd14 (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
# set of utilities that assure the cwd of a build
# is completely clean after the build, meaning all
# files were either discarded or moved to outputs.
# This ensures nothing is forgotten and new files
# are correctly handled on update.
{ stdenv, file, writeScript }:

let
  globWith = stdenv.lib.concatMapStringsSep "\n";
  rmNoise = noiseGlobs: globWith (f:
    ''rm -rf ${f}'') noiseGlobs;
  mvDoc = docGlobs: globWith
    (f: ''mv ${f} "$DOCDIR" 2>/dev/null || true'')
    docGlobs;

  # Shell script that implements common move & remove actions
  # $1 is the doc directory (will be created).
  # Best used in conjunction with checkForRemainingFiles
  commonFileActions =
    { # list of fileglobs that are removed from the source dir
      noiseFiles
      # files that are moved to the doc directory ($1)
      # TODO(Profpatsch): allow to set target dir with
      # { glob = …; to = "html" } (relative to docdir)
    , docFiles }:
    writeScript "common-file-actions.sh" ''
      #!${stdenv.shell}
      set -e
      DOCDIR="$1"
      shopt -s globstar extglob nullglob
      ${rmNoise noiseFiles}
      mkdir -p "$DOCDIR"
      ${mvDoc docFiles}
    '';

  # Shell script to check whether the build directory is empty.
  # If there are still files remaining, exit 1 with a helpful
  # listing of all remaining files and their types.
  checkForRemainingFiles = writeScript "check-for-remaining-files.sh" ''
    #!${stdenv.shell}
    echo "Checking for remaining source files"
    rem=$(find -mindepth 1 -xtype f -print0 \
           | tee $TMP/remaining-files)
    if [[ "$rem" != "" ]]; then
      echo "ERROR: These files should be either moved or deleted:"
      cat $TMP/remaining-files | xargs -0 ${file}/bin/file
      exit 1
    fi
  '';

in {
  inherit commonFileActions checkForRemainingFiles;
}