From 02eb4a1cf4266444cac48f85f6df0e0c99db427b Mon Sep 17 00:00:00 2001 From: DavHau Date: Wed, 23 Feb 2022 15:26:34 +0700 Subject: [PATCH] refactor: source instead of inputDirectories/Files --- src/apps/cli/commands/add.py | 3 +- src/apps/cli/utils.py | 2 +- src/default.nix | 3 +- .../translator-call-example.json | 9 +- src/templates/translators/impure.nix | 82 ++++++++++--------- src/templates/translators/pure.nix | 36 ++++---- src/translators/default.nix | 20 ++--- .../go/impure/gomod2nix/default.nix | 17 ++-- .../nodejs/impure/package-json/default.nix | 19 ++--- .../nodejs/pure/package-lock/default.nix | 29 +++---- .../nodejs/pure/yarn-lock/default.nix | 18 ++-- src/translators/python/impure/pip/default.nix | 19 ++--- .../rust/pure/cargo-lock/default.nix | 22 ++--- 13 files changed, 115 insertions(+), 164 deletions(-) diff --git a/src/apps/cli/commands/add.py b/src/apps/cli/commands/add.py index 181feec5..9b2c3ead 100644 --- a/src/apps/cli/commands/add.py +++ b/src/apps/cli/commands/add.py @@ -445,8 +445,7 @@ class AddCommand(Command): with tempfile.NamedTemporaryFile("r") as output_temp_file: # arguments for calling the translator nix module translator_input = dict( - inputFiles=[], - inputDirectories=[sourcePath], + source=sourcePath, outputFile=output_temp_file.name, ) translator_input.update(specified_extra_args) diff --git a/src/apps/cli/utils.py b/src/apps/cli/utils.py index 3c687d20..b23a66ec 100644 --- a/src/apps/cli/utils.py +++ b/src/apps/cli/utils.py @@ -51,7 +51,7 @@ def checkLockJSON(lock): def list_translators_for_source(sourcePath): translators_dict = callNixFunction( "translators.translatorsForInputRecursive", - inputDirectories=[sourcePath], + source=sourcePath, ) for path, translators_list in translators_dict.copy().items(): translators_dict[path] = \ diff --git a/src/default.nix b/src/default.nix index ef4692da..4a76f50c 100644 --- a/src/default.nix +++ b/src/default.nix @@ -226,8 +226,7 @@ let dreamLock' = translators.translators."${t.subsystem}"."${t.type}"."${t.name}".translate (translatorArgs // { - inputFiles = []; - inputDirectories = [ source ]; + inherit source; }); dreamLock = diff --git a/src/specifications/translator-call-example.json b/src/specifications/translator-call-example.json index c041fc80..75a4d2cf 100644 --- a/src/specifications/translator-call-example.json +++ b/src/specifications/translator-call-example.json @@ -1,12 +1,5 @@ { - "inputDirectories": [ - "./some_project_src" - ], - - "inputFiles": [ - "./a/b/c/requirements.txt", - "./a/b/c/requirements-dev.txt" - ], + "source": "./some_project_src", "outputFile": [ "./a/b/c/dream-lock.json" diff --git a/src/templates/translators/impure.nix b/src/templates/translators/impure.nix index b2468cf0..3cd75f6f 100644 --- a/src/templates/translators/impure.nix +++ b/src/templates/translators/impure.nix @@ -1,13 +1,6 @@ { - # dream2nix utils - utils, - - # nixpkgs dependenies - bash, - jq, + dlib, lib, - writeScriptBin, - ... }: { @@ -22,26 +15,37 @@ # The program is expected to create a file at the location specified # by the input parameter `outFile`. # The output file must contain the dream lock data encoded as json. - translateBin = utils.writePureShellScript - [ - bash - coreutils - jq - nix - ] - '' - # accroding to the spec, the translator reads the input from a json file - jsonInput=$1 + translateBin = + { + # dream2nix utils + utils, - # read the json input - outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) - inputDirectories=$(${jq}/bin/jq '.inputDirectories | .[]' -c -r $jsonInput) - inputFiles=$(${jq}/bin/jq '.inputFiles | .[]' -c -r $jsonInput) + # nixpkgs dependenies + bash, + jq, + writeScriptBin, + ... + }: + utils.writePureShellScript + [ + bash + coreutils + jq + nix + ] + '' + # accroding to the spec, the translator reads the input from a json file + jsonInput=$1 - # TODO: - # read input files/dirs and produce a json file at $outputFile - # containing the dream lock similar to /specifications/dream-lock-example.json - ''; + # read the json input + outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) + source=$(${jq}/bin/jq '.source' -c -r $jsonInput) + inputFiles=$(${jq}/bin/jq '.inputFiles | .[]' -c -r $jsonInput) + + # TODO: + # read input files/dirs and produce a json file at $outputFile + # containing the dream lock similar to /specifications/dream-lock-example.json + ''; # From a given list of paths, this function returns all paths which can be processed by this translator. @@ -49,20 +53,18 @@ # to automatically select the right translator. compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - # TODO: insert regex here that matches valid input file names - # examples: - # - ''.*requirements.*\.txt'' - # - ''.*package-lock\.json'' - inputDirectories = lib.filter - (utils.containsMatchingFile [ ''TODO: regex1'' ''TODO: regex2'' ]) - args.inputDirectories; - - inputFiles = []; - }; + source, + }: + # TODO: insert regex here that matches valid input file names + # examples: + # - ''.*requirements.*\.txt'' + # - ''.*package-lock\.json'' + dlib.containsMatchingFile + [ + ''TODO: regex1'' + ''TODO: regex2'' + ] + source; # If the translator requires additional arguments, specify them here. diff --git a/src/templates/translators/pure.nix b/src/templates/translators/pure.nix index 4db43d15..1c121e02 100644 --- a/src/templates/translators/pure.nix +++ b/src/templates/translators/pure.nix @@ -1,18 +1,20 @@ { + dlib, lib, - nodejs, - - externals, - translatorName, - utils, - ... }: { translate = { - inputDirectories, - inputFiles, + nodejs, + + externals, + translatorName, + utils, + ... + }: + { + source, # arguments specified by user noDev, @@ -132,20 +134,18 @@ # to automatically select the right translator. compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { + source, + }: # TODO: insert regex here that matches valid input file names # examples: # - ''.*requirements.*\.txt'' # - ''.*package-lock\.json'' - inputDirectories = lib.filter - (utils.containsMatchingFile [ ''TODO: regex1'' ''TODO: regex2'' ]) - args.inputDirectories; - - inputFiles = []; - }; + dlib.containsMatchingFile + [ + ''TODO: regex1'' + ''TODO: regex2'' + ] + source; # If the translator requires additional arguments, specify them here. diff --git a/src/translators/default.nix b/src/translators/default.nix index d4ea78f5..20eccef2 100644 --- a/src/translators/default.nix +++ b/src/translators/default.nix @@ -134,9 +134,8 @@ let # if the translator is compatible to all given paths translatorsForInput = { - inputDirectories, - inputFiles, - }@args: + source, + }: lib.forEach translatorsList (t: rec { inherit (t) @@ -145,15 +144,14 @@ let subsystem type ; - compatiblePaths = t.compatiblePaths args; - compatible = compatiblePaths == args; + compatible = t.compatiblePaths { inherit source; }; }); # also includes subdirectories of the given paths up to a certain depth # to check for translator compatibility translatorsForInputRecursive = { - inputDirectories, + source, depth ? 2, }: let @@ -175,20 +173,19 @@ let subDirs)); dirsToCheck = - inputDirectories + [ source ] ++ (lib.flatten (map (inputDir: listDirsRec inputDir depth) - inputDirectories)); + [ source ])); in lib.genAttrs dirsToCheck (dir: translatorsForInput { - inputDirectories = [ dir ]; - inputFiles = []; + source = dir; } ); @@ -213,8 +210,7 @@ let }@args: let translatorsForSource = translatorsForInput { - inputFiles = []; - inputDirectories = [ source ]; + inherit source; }; nameFilter = diff --git a/src/translators/go/impure/gomod2nix/default.nix b/src/translators/go/impure/gomod2nix/default.nix index e034ca53..27a00d02 100644 --- a/src/translators/go/impure/gomod2nix/default.nix +++ b/src/translators/go/impure/gomod2nix/default.nix @@ -33,11 +33,11 @@ # read the json input outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) - inputDirectory=$(${jq}/bin/jq '.inputDirectories | .[0]' -c -r $jsonInput) + source=$(${jq}/bin/jq '.source' -c -r $jsonInput) tmpBuild=$(mktemp -d) cd $tmpBuild - cp -r $inputDirectory/* . + cp -r $source/* . chmod -R +w . # This should be in sync with gomod2nix version in flake.lock nix run github:tweag/gomod2nix/67f22dd738d092c6ba88e420350ada0ed4992ae8 @@ -51,16 +51,9 @@ # to automatically select the right translator. compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - inputDirectories = lib.filter - (dlib.containsMatchingFile [ ''go\.sum'' ''go\.mod'' ]) - args.inputDirectories; - - inputFiles = []; - }; + source, + }: + dlib.containsMatchingFile [ ''go\.sum'' ''go\.mod'' ] source; # If the translator requires additional arguments, specify them here. diff --git a/src/translators/nodejs/impure/package-json/default.nix b/src/translators/nodejs/impure/package-json/default.nix index 99023698..43bb551d 100644 --- a/src/translators/nodejs/impure/package-json/default.nix +++ b/src/translators/nodejs/impure/package-json/default.nix @@ -35,11 +35,11 @@ # read the json input outputFile=$(jq '.outputFile' -c -r $jsonInput) - inputDirectory=$(jq '.inputDirectories | .[0]' -c -r $jsonInput) + source=$(jq '.source' -c -r $jsonInput) npmArgs=$(jq '.npmArgs' -c -r $jsonInput) # inputFiles=$(jq '.inputFiles | .[]' -c -r $jsonInput) - cp -r $inputDirectory/* ./ + cp -r $source/* ./ chmod -R +w ./ rm -rf package-lock.json cat ./package.json @@ -55,7 +55,7 @@ cat package-lock.json - jq ".inputDirectories[0] = \"$(pwd)\"" -c -r $jsonInput > ./newJsonInput + jq ".source = \"$(pwd)\"" -c -r $jsonInput > ./newJsonInput ${translators.translators.nodejs.pure.package-lock.translateBin} $(realpath ./newJsonInput) ''; @@ -66,16 +66,9 @@ # to automatically select the right translator. compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - inputDirectories = lib.filter - (dlib.containsMatchingFile [ ''.*package.json'' ]) - args.inputDirectories; - - inputFiles = []; - }; + source, + }: + dlib.containsMatchingFile [ ''.*package.json'' ] source; # inherit options from package-lock translator extraArgs = diff --git a/src/translators/nodejs/pure/package-lock/default.nix b/src/translators/nodejs/pure/package-lock/default.nix index 17f34a0c..6c618825 100644 --- a/src/translators/nodejs/pure/package-lock/default.nix +++ b/src/translators/nodejs/pure/package-lock/default.nix @@ -15,8 +15,7 @@ in ... }: { - inputDirectories, - inputFiles, + source, name, noDev, @@ -29,13 +28,9 @@ in dev = ! noDev; - inputDir = lib.elemAt inputDirectories 0; + inputDir = source; - packageLock = - if inputDirectories != [] then - "${inputDir}/package-lock.json" - else - lib.elemAt inputFiles 0; + packageLock = "${inputDir}/package-lock.json"; parsed = b.fromJSON (b.readFile packageLock); @@ -217,16 +212,14 @@ in compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - inputDirectories = lib.filter - (dlib.containsMatchingFile [ ''.*package-lock\.json'' ''.*package.json'' ]) - args.inputDirectories; - - inputFiles = []; - }; + source, + }: + dlib.containsMatchingFile + [ + ''.*package-lock\.json'' + ''.*package.json'' + ] + source; extraArgs = { diff --git a/src/translators/nodejs/pure/yarn-lock/default.nix b/src/translators/nodejs/pure/yarn-lock/default.nix index b3ebba8f..837dc6f2 100644 --- a/src/translators/nodejs/pure/yarn-lock/default.nix +++ b/src/translators/nodejs/pure/yarn-lock/default.nix @@ -13,8 +13,7 @@ ... }: { - inputDirectories, - inputFiles, + source, # extraArgs name, @@ -28,7 +27,7 @@ b = builtins; dev = ! noDev; - sourceDir = lib.elemAt inputDirectories 0; + sourceDir = source; yarnLock = utils.readTextFile "${sourceDir}/yarn.lock"; packageJSON = b.fromJSON (b.readFile "${sourceDir}/package.json"); parser = import ../yarn-lock/parser.nix @@ -281,16 +280,9 @@ # to automatically select the right translator. compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - inputDirectories = lib.filter - (dlib.containsMatchingFile [ ''.*yarn\.lock'' ''.*package.json'' ]) - args.inputDirectories; - - inputFiles = []; - }; + source, + }: + dlib.containsMatchingFile [ ''.*yarn\.lock'' ''.*package.json'' ] source; # If the translator requires additional arguments, specify them here. diff --git a/src/translators/python/impure/pip/default.nix b/src/translators/python/impure/pip/default.nix index 1690e2bd..95637b3d 100644 --- a/src/translators/python/impure/pip/default.nix +++ b/src/translators/python/impure/pip/default.nix @@ -50,7 +50,7 @@ in # read the json input outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) - inputDirectory=$(${jq}/bin/jq '.inputDirectories | .[0]' -c -r $jsonInput) + source=$(${jq}/bin/jq '.source' -c -r $jsonInput) pythonAttr=$(${jq}/bin/jq '.pythonAttr' -c -r $jsonInput) application=$(${jq}/bin/jq '.application' -c -r $jsonInput) @@ -70,7 +70,7 @@ in tmp=$(mktemp -d) # extract python requirements from setup.py - cp -r $inputDirectory $tmpBuild/src + cp -r $source $tmpBuild/src chmod -R +w $tmpBuild/src cd $tmpBuild/src chmod +x setup.py || true @@ -110,14 +110,13 @@ in # from a given list of paths, this function returns all paths which can be processed by this translator compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - inputDirectories = []; - - inputFiles = lib.filter (f: builtins.match ''.*requirements.*\.txt'' f != null) args.inputFiles; - }; + source, + }: + dlib.containsMatchingFile + [ + ''.*requirements.*\.txt'' + ] + source; # define special args and provide defaults extraArgs = { diff --git a/src/translators/rust/pure/cargo-lock/default.nix b/src/translators/rust/pure/cargo-lock/default.nix index 42adc1d8..9ba7c53d 100644 --- a/src/translators/rust/pure/cargo-lock/default.nix +++ b/src/translators/rust/pure/cargo-lock/default.nix @@ -12,15 +12,14 @@ ... }: { - inputDirectories, - inputFiles, - + source, + packageName, ... }@args: let l = lib // builtins; - inputDir = l.elemAt inputDirectories 0; + inputDir = source; recurseFiles = path: l.flatten ( @@ -34,7 +33,7 @@ ); # Find all Cargo.toml files and parse them - allFiles = l.flatten (l.map recurseFiles inputDirectories); + allFiles = l.flatten (l.map recurseFiles [ inputDir ]); cargoTomlPaths = l.filter (path: l.baseNameOf path == "Cargo.toml") allFiles; cargoTomls = l.map @@ -248,16 +247,9 @@ # to automatically select the right translator. compatiblePaths = { - inputDirectories, - inputFiles, - }@args: - { - inputDirectories = lib.filter - (dlib.containsMatchingFile [ ''.*Cargo\.lock'' ]) - args.inputDirectories; - - inputFiles = [ ]; - }; + source, + }: + dlib.containsMatchingFile [ ''.*Cargo\.lock'' ] source; # If the translator requires additional arguments, specify them here.