lol

Merge pull request #263096 from hercules-ci/clean-up-data-writers

Clean up data writers

authored by

Robert Hensing and committed by
GitHub
c1dc59dc 2b8b855f

+12 -32
+7 -23
pkgs/build-support/writers/data.nix
··· 1 - { lib, runCommand, dasel }: 2 let 3 daselBin = lib.getExe dasel; 4 ··· 23 # writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; }; 24 # myConfig = writeJSON "config.json" { hello = "world"; } 25 # 26 - makeDataWriter = { input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: 27 assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); 28 let 29 name = last (builtins.split "/" nameOrPath); ··· 40 mkdir -p $out/$(dirname "${nameOrPath}") 41 mv tmp $out/${nameOrPath} 42 ''} 43 - ''; 44 45 - # Writes the content to text. 46 - # 47 - # Example: 48 - # writeText "filename.txt" "file content" 49 - writeText = makeDataWriter { 50 - input = toString; 51 - output = "cp $inputPath $out"; 52 - }; 53 54 # Writes the content to a JSON file. 55 # 56 # Example: 57 # writeJSON "data.json" { hello = "world"; } 58 - writeJSON = makeDataWriter { 59 - input = builtins.toJSON; 60 - output = "${daselBin} -f $inputPath -r json -w json > $out"; 61 - }; 62 63 # Writes the content to a TOML file. 64 # 65 # Example: 66 # writeTOML "data.toml" { hello = "world"; } 67 - writeTOML = makeDataWriter { 68 - input = builtins.toJSON; 69 - output = "${daselBin} -f $inputPath -r json -w toml > $out"; 70 - }; 71 72 # Writes the content to a YAML file. 73 # 74 # Example: 75 # writeYAML "data.yaml" { hello = "world"; } 76 - writeYAML = makeDataWriter { 77 - input = builtins.toJSON; 78 - output = "${daselBin} -f $inputPath -r json -w yaml > $out"; 79 - }; 80 }
··· 1 + { lib, pkgs, formats, runCommand, dasel }: 2 let 3 daselBin = lib.getExe dasel; 4 ··· 23 # writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; }; 24 # myConfig = writeJSON "config.json" { hello = "world"; } 25 # 26 + makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: 27 assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); 28 let 29 name = last (builtins.split "/" nameOrPath); ··· 40 mkdir -p $out/$(dirname "${nameOrPath}") 41 mv tmp $out/${nameOrPath} 42 ''} 43 + ''); 44 45 + inherit (pkgs) writeText; 46 47 # Writes the content to a JSON file. 48 # 49 # Example: 50 # writeJSON "data.json" { hello = "world"; } 51 + writeJSON = (pkgs.formats.json {}).generate; 52 53 # Writes the content to a TOML file. 54 # 55 # Example: 56 # writeTOML "data.toml" { hello = "world"; } 57 + writeTOML = (pkgs.formats.toml {}).generate; 58 59 # Writes the content to a YAML file. 60 # 61 # Example: 62 # writeYAML "data.yaml" { hello = "world"; } 63 + writeYAML = (pkgs.formats.yaml {}).generate; 64 }
+5 -9
pkgs/build-support/writers/test.nix
··· 7 , python3Packages 8 , pypy3Packages 9 , runCommand 10 , writers 11 , writeText 12 }: ··· 36 let 37 expectedFile = writeText "${file.name}-expected" expected; 38 in 39 - runCommand "run-${file.name}" {} '' 40 - if ! diff -u ${file} ${expectedFile}; then 41 - echo 'test ${file.name} failed' 42 - exit 1 43 - fi 44 - 45 - touch $out 46 - ''; 47 in 48 lib.recurseIntoAttrs { 49 bin = lib.recurseIntoAttrs { ··· 261 262 toml = expectDataEqual { 263 file = writeTOML "data.toml" { hello = "world"; }; 264 - expected = "hello = 'world'\n"; 265 }; 266 267 yaml = expectDataEqual {
··· 7 , python3Packages 8 , pypy3Packages 9 , runCommand 10 + , testers 11 , writers 12 , writeText 13 }: ··· 37 let 38 expectedFile = writeText "${file.name}-expected" expected; 39 in 40 + testers.testEqualContents { expected = expectedFile; actual = file; assertion = "${file.name} matches"; }; 41 in 42 lib.recurseIntoAttrs { 43 bin = lib.recurseIntoAttrs { ··· 255 256 toml = expectDataEqual { 257 file = writeTOML "data.toml" { hello = "world"; }; 258 + expected = '' 259 + hello = "world" 260 + ''; 261 }; 262 263 yaml = expectDataEqual {