1# set of utilities that assure the cwd of a build
2# is completely clean after the build, meaning all
3# files were either discarded or moved to outputs.
4# This ensures nothing is forgotten and new files
5# are correctly handled on update.
6{
7 lib,
8 stdenv,
9 file,
10 writeScript,
11}:
12
13let
14 globWith = lib.concatMapStringsSep "\n";
15 rmNoise = noiseGlobs: globWith (f: "rm -rf ${f}") noiseGlobs;
16 mvDoc = docGlobs: globWith (f: ''mv ${f} "$DOCDIR" 2>/dev/null || true'') docGlobs;
17
18 # Shell script that implements common move & remove actions
19 # $1 is the doc directory (will be created).
20 # Best used in conjunction with checkForRemainingFiles
21 commonFileActions =
22 {
23 # list of fileglobs that are removed from the source dir
24 noiseFiles,
25 # files that are moved to the doc directory ($1)
26 # TODO(Profpatsch): allow to set target dir with
27 # { glob = …; to = "html" } (relative to docdir)
28 docFiles,
29 }:
30 writeScript "common-file-actions.sh" ''
31 #!${stdenv.shell}
32 set -e
33 DOCDIR="''${1?commonFileActions: DOCDIR as argv[1] required}"
34 shopt -s globstar extglob nullglob
35 mkdir -p "$DOCDIR"
36 ${mvDoc docFiles}
37 ${rmNoise noiseFiles}
38 '';
39
40 # Shell script to check whether the build directory is empty.
41 # If there are still files remaining, exit 1 with a helpful
42 # listing of all remaining files and their types.
43 checkForRemainingFiles = writeScript "check-for-remaining-files.sh" ''
44 #!${stdenv.shell}
45 echo "Checking for remaining source files"
46 rem=$(find -mindepth 1 -xtype f -print0 \
47 | tee $TMP/remaining-files)
48 if [[ "$rem" != "" ]]; then
49 echo "ERROR: These files should be either moved or deleted:"
50 cat $TMP/remaining-files | xargs -0 ${file}/bin/file
51 exit 1
52 fi
53 '';
54
55in
56{
57 inherit commonFileActions checkForRemainingFiles;
58}