refactor: source instead of inputDirectories/Files

This commit is contained in:
DavHau 2022-02-23 15:26:34 +07:00
parent b4fef5baf0
commit 02eb4a1cf4
13 changed files with 115 additions and 164 deletions

View File

@ -445,8 +445,7 @@ class AddCommand(Command):
with tempfile.NamedTemporaryFile("r") as output_temp_file: with tempfile.NamedTemporaryFile("r") as output_temp_file:
# arguments for calling the translator nix module # arguments for calling the translator nix module
translator_input = dict( translator_input = dict(
inputFiles=[], source=sourcePath,
inputDirectories=[sourcePath],
outputFile=output_temp_file.name, outputFile=output_temp_file.name,
) )
translator_input.update(specified_extra_args) translator_input.update(specified_extra_args)

View File

@ -51,7 +51,7 @@ def checkLockJSON(lock):
def list_translators_for_source(sourcePath): def list_translators_for_source(sourcePath):
translators_dict = callNixFunction( translators_dict = callNixFunction(
"translators.translatorsForInputRecursive", "translators.translatorsForInputRecursive",
inputDirectories=[sourcePath], source=sourcePath,
) )
for path, translators_list in translators_dict.copy().items(): for path, translators_list in translators_dict.copy().items():
translators_dict[path] = \ translators_dict[path] = \

View File

@ -226,8 +226,7 @@ let
dreamLock' = translators.translators."${t.subsystem}"."${t.type}"."${t.name}".translate dreamLock' = translators.translators."${t.subsystem}"."${t.type}"."${t.name}".translate
(translatorArgs // { (translatorArgs // {
inputFiles = []; inherit source;
inputDirectories = [ source ];
}); });
dreamLock = dreamLock =

View File

@ -1,12 +1,5 @@
{ {
"inputDirectories": [ "source": "./some_project_src",
"./some_project_src"
],
"inputFiles": [
"./a/b/c/requirements.txt",
"./a/b/c/requirements-dev.txt"
],
"outputFile": [ "outputFile": [
"./a/b/c/dream-lock.json" "./a/b/c/dream-lock.json"

View File

@ -1,13 +1,6 @@
{ {
# dream2nix utils dlib,
utils,
# nixpkgs dependenies
bash,
jq,
lib, lib,
writeScriptBin,
...
}: }:
{ {
@ -22,26 +15,37 @@
# The program is expected to create a file at the location specified # The program is expected to create a file at the location specified
# by the input parameter `outFile`. # by the input parameter `outFile`.
# The output file must contain the dream lock data encoded as json. # The output file must contain the dream lock data encoded as json.
translateBin = utils.writePureShellScript translateBin =
[ {
bash # dream2nix utils
coreutils utils,
jq
nix
]
''
# accroding to the spec, the translator reads the input from a json file
jsonInput=$1
# read the json input # nixpkgs dependenies
outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) bash,
inputDirectories=$(${jq}/bin/jq '.inputDirectories | .[]' -c -r $jsonInput) jq,
inputFiles=$(${jq}/bin/jq '.inputFiles | .[]' -c -r $jsonInput) writeScriptBin,
...
}:
utils.writePureShellScript
[
bash
coreutils
jq
nix
]
''
# accroding to the spec, the translator reads the input from a json file
jsonInput=$1
# TODO: # read the json input
# read input files/dirs and produce a json file at $outputFile outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput)
# containing the dream lock similar to /specifications/dream-lock-example.json 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. # 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. # to automatically select the right translator.
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: # TODO: insert regex here that matches valid input file names
{ # examples:
# TODO: insert regex here that matches valid input file names # - ''.*requirements.*\.txt''
# examples: # - ''.*package-lock\.json''
# - ''.*requirements.*\.txt'' dlib.containsMatchingFile
# - ''.*package-lock\.json'' [
inputDirectories = lib.filter ''TODO: regex1''
(utils.containsMatchingFile [ ''TODO: regex1'' ''TODO: regex2'' ]) ''TODO: regex2''
args.inputDirectories; ]
source;
inputFiles = [];
};
# If the translator requires additional arguments, specify them here. # If the translator requires additional arguments, specify them here.

View File

@ -1,18 +1,20 @@
{ {
dlib,
lib, lib,
nodejs,
externals,
translatorName,
utils,
...
}: }:
{ {
translate = translate =
{ {
inputDirectories, nodejs,
inputFiles,
externals,
translatorName,
utils,
...
}:
{
source,
# arguments specified by user # arguments specified by user
noDev, noDev,
@ -132,20 +134,18 @@
# to automatically select the right translator. # to automatically select the right translator.
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args:
{
# TODO: insert regex here that matches valid input file names # TODO: insert regex here that matches valid input file names
# examples: # examples:
# - ''.*requirements.*\.txt'' # - ''.*requirements.*\.txt''
# - ''.*package-lock\.json'' # - ''.*package-lock\.json''
inputDirectories = lib.filter dlib.containsMatchingFile
(utils.containsMatchingFile [ ''TODO: regex1'' ''TODO: regex2'' ]) [
args.inputDirectories; ''TODO: regex1''
''TODO: regex2''
inputFiles = []; ]
}; source;
# If the translator requires additional arguments, specify them here. # If the translator requires additional arguments, specify them here.

View File

@ -134,9 +134,8 @@ let
# if the translator is compatible to all given paths # if the translator is compatible to all given paths
translatorsForInput = translatorsForInput =
{ {
inputDirectories, source,
inputFiles, }:
}@args:
lib.forEach translatorsList lib.forEach translatorsList
(t: rec { (t: rec {
inherit (t) inherit (t)
@ -145,15 +144,14 @@ let
subsystem subsystem
type type
; ;
compatiblePaths = t.compatiblePaths args; compatible = t.compatiblePaths { inherit source; };
compatible = compatiblePaths == args;
}); });
# also includes subdirectories of the given paths up to a certain depth # also includes subdirectories of the given paths up to a certain depth
# to check for translator compatibility # to check for translator compatibility
translatorsForInputRecursive = translatorsForInputRecursive =
{ {
inputDirectories, source,
depth ? 2, depth ? 2,
}: }:
let let
@ -175,20 +173,19 @@ let
subDirs)); subDirs));
dirsToCheck = dirsToCheck =
inputDirectories [ source ]
++ ++
(lib.flatten (lib.flatten
(map (map
(inputDir: listDirsRec inputDir depth) (inputDir: listDirsRec inputDir depth)
inputDirectories)); [ source ]));
in in
lib.genAttrs lib.genAttrs
dirsToCheck dirsToCheck
(dir: (dir:
translatorsForInput { translatorsForInput {
inputDirectories = [ dir ]; source = dir;
inputFiles = [];
} }
); );
@ -213,8 +210,7 @@ let
}@args: }@args:
let let
translatorsForSource = translatorsForInput { translatorsForSource = translatorsForInput {
inputFiles = []; inherit source;
inputDirectories = [ source ];
}; };
nameFilter = nameFilter =

View File

@ -33,11 +33,11 @@
# read the json input # read the json input
outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) 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) tmpBuild=$(mktemp -d)
cd $tmpBuild cd $tmpBuild
cp -r $inputDirectory/* . cp -r $source/* .
chmod -R +w . chmod -R +w .
# This should be in sync with gomod2nix version in flake.lock # This should be in sync with gomod2nix version in flake.lock
nix run github:tweag/gomod2nix/67f22dd738d092c6ba88e420350ada0ed4992ae8 nix run github:tweag/gomod2nix/67f22dd738d092c6ba88e420350ada0ed4992ae8
@ -51,16 +51,9 @@
# to automatically select the right translator. # to automatically select the right translator.
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: dlib.containsMatchingFile [ ''go\.sum'' ''go\.mod'' ] source;
{
inputDirectories = lib.filter
(dlib.containsMatchingFile [ ''go\.sum'' ''go\.mod'' ])
args.inputDirectories;
inputFiles = [];
};
# If the translator requires additional arguments, specify them here. # If the translator requires additional arguments, specify them here.

View File

@ -35,11 +35,11 @@
# read the json input # read the json input
outputFile=$(jq '.outputFile' -c -r $jsonInput) 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) npmArgs=$(jq '.npmArgs' -c -r $jsonInput)
# inputFiles=$(jq '.inputFiles | .[]' -c -r $jsonInput) # inputFiles=$(jq '.inputFiles | .[]' -c -r $jsonInput)
cp -r $inputDirectory/* ./ cp -r $source/* ./
chmod -R +w ./ chmod -R +w ./
rm -rf package-lock.json rm -rf package-lock.json
cat ./package.json cat ./package.json
@ -55,7 +55,7 @@
cat package-lock.json 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) ${translators.translators.nodejs.pure.package-lock.translateBin} $(realpath ./newJsonInput)
''; '';
@ -66,16 +66,9 @@
# to automatically select the right translator. # to automatically select the right translator.
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: dlib.containsMatchingFile [ ''.*package.json'' ] source;
{
inputDirectories = lib.filter
(dlib.containsMatchingFile [ ''.*package.json'' ])
args.inputDirectories;
inputFiles = [];
};
# inherit options from package-lock translator # inherit options from package-lock translator
extraArgs = extraArgs =

View File

@ -15,8 +15,7 @@ in
... ...
}: }:
{ {
inputDirectories, source,
inputFiles,
name, name,
noDev, noDev,
@ -29,13 +28,9 @@ in
dev = ! noDev; dev = ! noDev;
inputDir = lib.elemAt inputDirectories 0; inputDir = source;
packageLock = packageLock = "${inputDir}/package-lock.json";
if inputDirectories != [] then
"${inputDir}/package-lock.json"
else
lib.elemAt inputFiles 0;
parsed = b.fromJSON (b.readFile packageLock); parsed = b.fromJSON (b.readFile packageLock);
@ -217,16 +212,14 @@ in
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: dlib.containsMatchingFile
{ [
inputDirectories = lib.filter ''.*package-lock\.json''
(dlib.containsMatchingFile [ ''.*package-lock\.json'' ''.*package.json'' ]) ''.*package.json''
args.inputDirectories; ]
source;
inputFiles = [];
};
extraArgs = { extraArgs = {

View File

@ -13,8 +13,7 @@
... ...
}: }:
{ {
inputDirectories, source,
inputFiles,
# extraArgs # extraArgs
name, name,
@ -28,7 +27,7 @@
b = builtins; b = builtins;
dev = ! noDev; dev = ! noDev;
sourceDir = lib.elemAt inputDirectories 0; sourceDir = source;
yarnLock = utils.readTextFile "${sourceDir}/yarn.lock"; yarnLock = utils.readTextFile "${sourceDir}/yarn.lock";
packageJSON = b.fromJSON (b.readFile "${sourceDir}/package.json"); packageJSON = b.fromJSON (b.readFile "${sourceDir}/package.json");
parser = import ../yarn-lock/parser.nix parser = import ../yarn-lock/parser.nix
@ -281,16 +280,9 @@
# to automatically select the right translator. # to automatically select the right translator.
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: dlib.containsMatchingFile [ ''.*yarn\.lock'' ''.*package.json'' ] source;
{
inputDirectories = lib.filter
(dlib.containsMatchingFile [ ''.*yarn\.lock'' ''.*package.json'' ])
args.inputDirectories;
inputFiles = [];
};
# If the translator requires additional arguments, specify them here. # If the translator requires additional arguments, specify them here.

View File

@ -50,7 +50,7 @@ in
# read the json input # read the json input
outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput) 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) pythonAttr=$(${jq}/bin/jq '.pythonAttr' -c -r $jsonInput)
application=$(${jq}/bin/jq '.application' -c -r $jsonInput) application=$(${jq}/bin/jq '.application' -c -r $jsonInput)
@ -70,7 +70,7 @@ in
tmp=$(mktemp -d) tmp=$(mktemp -d)
# extract python requirements from setup.py # extract python requirements from setup.py
cp -r $inputDirectory $tmpBuild/src cp -r $source $tmpBuild/src
chmod -R +w $tmpBuild/src chmod -R +w $tmpBuild/src
cd $tmpBuild/src cd $tmpBuild/src
chmod +x setup.py || true 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 # from a given list of paths, this function returns all paths which can be processed by this translator
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: dlib.containsMatchingFile
{ [
inputDirectories = []; ''.*requirements.*\.txt''
]
inputFiles = lib.filter (f: builtins.match ''.*requirements.*\.txt'' f != null) args.inputFiles; source;
};
# define special args and provide defaults # define special args and provide defaults
extraArgs = { extraArgs = {

View File

@ -12,15 +12,14 @@
... ...
}: }:
{ {
inputDirectories, source,
inputFiles, packageName,
... ...
}@args: }@args:
let let
l = lib // builtins; l = lib // builtins;
inputDir = l.elemAt inputDirectories 0; inputDir = source;
recurseFiles = path: recurseFiles = path:
l.flatten ( l.flatten (
@ -34,7 +33,7 @@
); );
# Find all Cargo.toml files and parse them # 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; cargoTomlPaths = l.filter (path: l.baseNameOf path == "Cargo.toml") allFiles;
cargoTomls = cargoTomls =
l.map l.map
@ -248,16 +247,9 @@
# to automatically select the right translator. # to automatically select the right translator.
compatiblePaths = compatiblePaths =
{ {
inputDirectories, source,
inputFiles, }:
}@args: dlib.containsMatchingFile [ ''.*Cargo\.lock'' ] source;
{
inputDirectories = lib.filter
(dlib.containsMatchingFile [ ''.*Cargo\.lock'' ])
args.inputDirectories;
inputFiles = [ ];
};
# If the translator requires additional arguments, specify them here. # If the translator requires additional arguments, specify them here.