···11+# set of utilities that assure the cwd of a build
22+# is completely clean after the build, meaning all
33+# files were either discarded or moved to outputs.
44+# This ensures nothing is forgotten and new files
55+# are correctly handled on update.
66+{ stdenv, file, writeScript }:
77+88+let
99+ globWith = stdenv.lib.concatMapStringsSep "\n";
1010+ rmNoise = noiseGlobs: globWith (f:
1111+ ''rm -rf ${f}'') noiseGlobs;
1212+ mvDoc = docGlobs: globWith
1313+ (f: ''mv ${f} "$DOCDIR" 2>/dev/null || true'')
1414+ docGlobs;
1515+1616+ # Shell script that implements common move & remove actions
1717+ # $1 is the doc directory (will be created).
1818+ # Best used in conjunction with checkForRemainingFiles
1919+ commonFileActions =
2020+ { # list of fileglobs that are removed from the source dir
2121+ noiseFiles
2222+ # files that are moved to the doc directory ($1)
2323+ # TODO(Profpatsch): allow to set target dir with
2424+ # { glob = …; to = "html" } (relative to docdir)
2525+ , docFiles }:
2626+ writeScript "common-file-actions.sh" ''
2727+ #!${stdenv.shell}
2828+ set -e
2929+ DOCDIR="$1"
3030+ shopt -s globstar extglob nullglob
3131+ ${rmNoise noiseFiles}
3232+ mkdir -p "$DOCDIR"
3333+ ${mvDoc docFiles}
3434+ '';
3535+3636+ # Shell script to check whether the build directory is empty.
3737+ # If there are still files remaining, exit 1 with a helpful
3838+ # listing of all remaining files and their types.
3939+ checkForRemainingFiles = writeScript "check-for-remaining-files.sh" ''
4040+ #!${stdenv.shell}
4141+ echo "Checking for remaining source files"
4242+ rem=$(find -mindepth 1 -xtype f -print0 \
4343+ | tee $TMP/remaining-files)
4444+ if [[ "$rem" != "" ]]; then
4545+ echo "ERROR: These files should be either moved or deleted:"
4646+ cat $TMP/remaining-files | xargs -0 ${file}/bin/file
4747+ exit 1
4848+ fi
4949+ '';
5050+5151+in {
5252+ inherit commonFileActions checkForRemainingFiles;
5353+}