Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up data writers #263096

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions pkgs/build-support/writers/data.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, runCommand, dasel }:
{ lib, pkgs, formats, runCommand, dasel }:
let
daselBin = lib.getExe dasel;

Expand All @@ -23,7 +23,7 @@ rec {
# writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; };
# myConfig = writeJSON "config.json" { hello = "world"; }
#
makeDataWriter = { input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data:
makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data:
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
let
name = last (builtins.split "/" nameOrPath);
Expand All @@ -40,41 +40,25 @@ rec {
mkdir -p $out/$(dirname "${nameOrPath}")
mv tmp $out/${nameOrPath}
''}
'';
'');

# Writes the content to text.
#
# Example:
# writeText "filename.txt" "file content"
writeText = makeDataWriter {
input = toString;
output = "cp $inputPath $out";
};
inherit (pkgs) writeText;

# Writes the content to a JSON file.
#
# Example:
# writeJSON "data.json" { hello = "world"; }
writeJSON = makeDataWriter {
input = builtins.toJSON;
output = "${daselBin} -f $inputPath -r json -w json > $out";
};
writeJSON = (pkgs.formats.json {}).generate;

# Writes the content to a TOML file.
#
# Example:
# writeTOML "data.toml" { hello = "world"; }
writeTOML = makeDataWriter {
input = builtins.toJSON;
output = "${daselBin} -f $inputPath -r json -w toml > $out";
};
writeTOML = (pkgs.formats.toml {}).generate;

# Writes the content to a YAML file.
#
# Example:
# writeYAML "data.yaml" { hello = "world"; }
writeYAML = makeDataWriter {
input = builtins.toJSON;
output = "${daselBin} -f $inputPath -r json -w yaml > $out";
};
writeYAML = (pkgs.formats.yaml {}).generate;
}
14 changes: 5 additions & 9 deletions pkgs/build-support/writers/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, python3Packages
, pypy3Packages
, runCommand
, testers
, writers
, writeText
}:
Expand Down Expand Up @@ -36,14 +37,7 @@ let
let
expectedFile = writeText "${file.name}-expected" expected;
in
runCommand "run-${file.name}" {} ''
if ! diff -u ${file} ${expectedFile}; then
echo 'test ${file.name} failed'
exit 1
fi

touch $out
'';
testers.testEqualContents { expected = expectedFile; actual = file; assertion = "${file.name} matches"; };
in
lib.recurseIntoAttrs {
bin = lib.recurseIntoAttrs {
Expand Down Expand Up @@ -261,7 +255,9 @@ lib.recurseIntoAttrs {

toml = expectDataEqual {
file = writeTOML "data.toml" { hello = "world"; };
expected = "hello = 'world'\n";
expected = ''
hello = "world"
'';
};

yaml = expectDataEqual {
Expand Down