add translator.package.json

- update default.nix template
  - add npm to dream lock schema
  - handle empty depedency graph in readDreamLock
This commit is contained in:
DavHau 2021-11-01 14:42:02 +07:00
parent 5a00d557b9
commit 4ef9af0f73
5 changed files with 119 additions and 11 deletions

View File

@ -47,15 +47,13 @@ in
''') {},
}:
(dream2nix.riseAndShine {
dream2nix.riseAndShine {
dreamLock = ./dream.lock;
${lib.optionalString (dreamLock.sources."${mainPackageName}"."${mainPackageVersion}".type == "unknown") ''
sourceOverrides = oldSources: {
"${mainPackageName}#${mainPackageVersion}" = ./${sourcePathRelative};
};
''}
}).defaultPackage.overrideAttrs (old: {
})
}
'';
}

View File

@ -20,6 +20,7 @@
"git",
"github",
"gitlab",
"npm",
"path",
"pypi-sdist",
"unknown"
@ -71,6 +72,21 @@
"additionalProperties": false
}
},
{
"if": {
"properties": { "type": { "const": "npm" } }
},
"then": {
"properties": {
"pname": { "type": "string" },
"version": { "type": "string" },
"hash": { "type": "string" },
"type": { "type": "string" }
},
"required": ["type", "pname"],
"additionalProperties": false
}
},
{
"if": {
"properties": { "type": { "const": "path" } }

View File

@ -0,0 +1,90 @@
{
# dream2nix utils
translators,
utils,
# nixpkgs dependenies
bash,
coreutils,
jq,
lib,
nodePackages,
writeScriptBin,
...
}:
{
# the input format is specified in /specifications/translator-call-example.json
# this script receives a json file including the input paths and specialArgs
translateBin = utils.writePureShellScript
[ bash coreutils jq nodePackages.npm ]
''
# accroding to the spec, the translator reads the input from a json file
jsonInput=$1
# read the json input
outputFile=$(jq '.outputFile' -c -r $jsonInput)
inputDirectory=$(jq '.inputDirectories | .[0]' -c -r $jsonInput)
# inputFiles=$(jq '.inputFiles | .[]' -c -r $jsonInput)
cp -r $inputDirectory/* ./
chmod -R +w ./
cat ./package.json
npm install --package-lock-only
cat package-lock.json
jq ".inputDirectories[0] = \"$(pwd)\"" -c -r $jsonInput > ./newJsonInput
${translators.translators.nodejs.pure.package-lock.translateBin}/bin/run $(realpath ./newJsonInput)
'';
# From a given list of paths, this function returns all paths which can be processed by this translator.
# This allows the framework to detect if the translator is compatible with the given inputs
# 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 [ ''.*package.json'' ])
args.inputDirectories;
inputFiles = [];
};
# If the translator requires additional arguments, specify them here.
# There are only two types of arguments:
# - string argument (type = "argument")
# - boolean flag (type = "flag")
# String arguments contain a default value and examples. Flags do not.
extraArgs = {
# # Example: boolean option
# # Flags always default to 'false' if not specified by the user
# dev-dependenices = {
# description = "Include dev dependencies";
# type = "flag";
# };
# # Example: string option
# the-answer = {
# default = "42";
# description = "The Answer to the Ultimate Question of Life";
# examples = [
# "0"
# "1234"
# ];
# type = "argument";
# };
};
}

View File

@ -32,6 +32,8 @@
parsed = b.fromJSON (b.readFile packageLock);
parsedDependencies = parsed.dependencies or {};
identifyGitSource = dependencyObject:
# TODO: when integrity is there, and git url is github then use tarball instead
# ! (dependencyObject ? integrity) &&
@ -64,7 +66,7 @@
)
dependencies;
packageLockWithPinnedVersions = pinVersions parsed.dependencies parsed.dependencies;
packageLockWithPinnedVersions = pinVersions parsedDependencies parsedDependencies;
in
@ -79,7 +81,7 @@
{ name = pname; version = getVersion pdata; })
(lib.filterAttrs
(pname: pdata: ! (pdata.dev or false) || dev)
parsed.dependencies);
parsedDependencies);
buildSystemName = "nodejs";
buildSystemAttrs = { nodejsVersion = args.nodejs; };

View File

@ -23,6 +23,9 @@ let
else
dreamLock;
mainPackageName = lock.generic.mainPackageName;
mainPackageVersion = lock.generic.mainPackageVersion;
buildSystemAttrs = lock.buildSystem;
sources = lock.sources;
@ -35,7 +38,9 @@ let
lib.attrNames
(lib.genAttrs
(lib.flatten
((lib.attrValues dependencyGraph) ++ (lib.attrNames dependencyGraph)))
((lib.attrValues dependencyGraph)
++ (lib.attrNames dependencyGraph)
++ [ "${mainPackageName}#${mainPackageVersion}" ]))
(x: null));
in
lib.foldl'
@ -90,12 +95,9 @@ let
inherit lock;
interface = rec {
inherit (lock.generic)
inherit
mainPackageName
mainPackageVersion
;
inherit
buildSystemAttrs
cyclicDependencies
getCyclicDependencies