Files
2026-10-18 12:41:34 -05:00

81 lines
4.6 KiB
Nix

{ pkgs, grafana }: let
swaps = [
[ ''gray05:"#111217"'' ''gray05:"#11111b"'' ]
[ ''gray10:"#181b1f"'' ''gray10:"#1e1e2e"'' ]
[ ''gray15:"#22252b"'' ''gray15:"#313244"'' ]
[ ''gray20:"#2c2f35"'' ''gray20:"#45475a"'' ]
[ ''gray25:"#383b42"'' ''gray25:"#585b70"'' ]
[ ''whiteBase="204, 204, 220"'' ''whiteBase="205, 214, 244"'' ]
[ ''blueDarkMain:"#3d71d9"'' ''blueDarkMain:"#cba6f7"'' ]
[ ''blueDarkText:"#6e9fff"'' ''blueDarkText:"#cba6f7"'' ]
[ ''redDarkMain:"#d10e5c"'' ''redDarkMain:"#f38ba8"'' ]
[ ''redDarkText:"#ff5286"'' ''redDarkText:"#f38ba8"'' ]
[ ''greenDarkMain:"#1a7f4b"'' ''greenDarkMain:"#a6e3a1"'' ]
[ ''greenDarkText:"#6ccf8e"'' ''greenDarkText:"#a6e3a1"'' ]
[ ''orangeDarkMain:"#ff9900"'' ''orangeDarkMain:"#fab387"'' ]
[ ''orangeDarkText:"#fbad37"'' ''orangeDarkText:"#fab387"'' ]
[ ''{color:"#73BF69",name:"green"'' ''{color:"#a6e3a1",name:"green"'' ]
[ ''{color:"#F2495C",name:"red"'' ''{color:"#f38ba8",name:"red"'' ]
[ ''{color:"#FADE2A",name:"yellow"'' ''{color:"#f9e2af",name:"yellow"'' ]
[ ''{color:"#5794F2",name:"blue"'' ''{color:"#89b4fa",name:"blue"'' ]
[ ''{color:"#FF9830",name:"orange"'' ''{color:"#fab387",name:"orange"'' ]
[ ''{color:"#B877D9",name:"purple"'' ''{color:"#cba6f7",name:"purple"'' ]
[ ''X=h==="kiosk";if(W||'' ''X=h==="kiosk";if(!0||'' ]
[ ''["#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0","#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C"''
''["#cba6f7","#89b4fa","#a6e3a1","#fab387","#f5c2e7","#94e2d5","#f9e2af","#89dceb","#f38ba8","#b4befe","#f2cdcd","#eba0ac","#74c7ec","#f5e0dc"'' ]
];
esc = s: builtins.replaceStrings [ "\\" "/" "&" "[" "]" "." "*" "$" "^" ] [ "\\\\" "\\/" "\\&" "\\[" "\\]" "\\." "\\*" "\\$" "\\^" ] s;
escR = s: builtins.replaceStrings [ "\\" "/" "&" ] [ "\\\\" "\\/" "\\&" ] s;
script = pkgs.writeText "mocha.sed" (pkgs.lib.concatStrings (map (x: "s/${esc (builtins.elemAt x 0)}/${escR (builtins.elemAt x 1)}/g\n") swaps));
patterns = pkgs.writeText "mocha.patterns" (pkgs.lib.concatStringsSep "\n" (map builtins.head swaps));
in pkgs.runCommand "grafana-public-mocha" { nativeBuildInputs = [ pkgs.jq pkgs.openssl ]; } ''
cp -rs ${grafana}/share/grafana/public $out
chmod -R u+w $out
rm -rf $out/app && cp -r ${grafana}/share/grafana/public/app $out/app
files=$(grep -RlF -f ${patterns} $out/build --include='*.js')
[ -n "$files" ] || { echo "mocha: no palette files found"; exit 1; }
for f in $files; do cp --remove-destination "$(readlink -f "$f")" "$f"; chmod u+w "$f"; sed -i -f ${script} "$f"; done
while IFS= read -r p; do
grep -RqF "$p" $out/build && { echo "mocha: unpatched: $p"; exit 1; }
grep -rqF "$p" ${grafana}/share/grafana/public/build || { echo "mocha: pattern gone upstream: $p"; exit 1; }
done < ${patterns}
m=$out/build/assets-manifest.json
cp --remove-destination "$(readlink -f $m)" $m; chmod u+w $m
rt=$out/$(jq -r '."runtime.js".src | ltrimstr("public/")' $m)
cp --remove-destination "$(readlink -f $rt)" $rt; chmod u+w $rt
sha384() { echo "sha384-$(openssl dgst -sha384 -binary "$1" | base64 -w0)"; }
rename() {
local f=$1 old new
old=$(basename "$f"); new="''${old%.js}.mocha$(sha256sum "$f" | head -c 8).js"
mv "$f" "$out/build/$new"; rm -f "$f.map"
jq --arg o "public/build/$old" --arg n "public/build/$new" \
--arg s "$(sha384 "$out/build/$new") sha512-$(openssl dgst -sha512 -binary "$out/build/$new" | base64 -w0)" \
'walk(if type == "object" and .src == $o then .src = $n | .integrity = $s
elif type == "string" and . == $o then $n else . end)
| with_entries(if .key == ($o | ltrimstr("public/build/")) then .key = ($n | ltrimstr("public/build/")) else . end)' \
$m > $m.new && mv $m.new $m
grep -qF "$old" $m && { echo "mocha: $old still in manifest"; exit 1; }
echo "$new"
}
for f in $files; do
old=$(basename "$f")
if grep -qF "public/build/$old" $m; then rename "$f" >/dev/null; continue; fi
id=''${old%%.*}; hash=''${old#*.}; hash=''${hash%.js}
grep -qF "$id:\"$hash\"" $rt || { echo "mocha: $old neither in manifest nor runtime"; exit 1; }
new=$(rename "$f"); nh=''${new#*.}; nh=''${nh%.js}
sed -i "s/\([{,]\)$id:\"$hash\"/\1$id:\"$nh\"/; s#\([{,]\)$id:\"sha384-[^\"]*\"#\1$id:\"$(sha384 "$out/build/$new")\"#" $rt
grep -qF "$id:\"$nh\"" $rt || { echo "mocha: runtime map not updated for $old"; exit 1; }
done
rename "$rt" >/dev/null
cp --remove-destination "$(readlink -f $out/views/index.html)" $out/views/index.html
chmod u+w $out/views/index.html
sed -i 's/#111217/#11111b/g; s/#181b1f/#1e1e2e/g' $out/views/index.html
''