refactor npmlock2nix to use simpleTranslate

This commit is contained in:
DavHau 2021-10-18 00:15:54 +07:00
parent 0867db4b8a
commit 99cee262e5
4 changed files with 109 additions and 111 deletions

View File

@ -136,16 +136,20 @@ class PackageCommand(Command):
))[0]
else:
translator = translator.split('.')
if len(translator) == 3:
translator = list(filter(
lambda t: [t['subsystem'], t['type'], t['name']] == translator,
translatorsSorted,
))[0]
elif len(translator) == 1:
translator = list(filter(
lambda t: [t['name']] == translator,
translatorsSorted,
))[0]
try:
if len(translator) == 3:
translator = list(filter(
lambda t: [t['subsystem'], t['type'], t['name']] == translator,
translatorsSorted,
))[0]
elif len(translator) == 1:
translator = list(filter(
lambda t: [t['name']] == translator,
translatorsSorted,
))[0]
except IndexError:
print(f"Could not find translator '{'.'.join(translator)}'", file=sys.stderr)
exit(1)
# raise error if any specified extra arg is unknown
unknown_extra_args = set(specified_extra_args.keys()) - set(translator['specialArgs'].keys())

View File

@ -1,5 +1,6 @@
{
lib,
nodejs,
externals,
translatorName,
@ -14,8 +15,9 @@
inputFiles,
dev,
nodejs,
...
}:
}@args:
let
packageLock =
if inputDirectories != [] then
@ -28,12 +30,12 @@
parseGithubDependency = dependency:
externals.npmlock2nix.parseGitHubRef dependency.version;
getVersion = dependency:
if dependency ? from && dependency ? version then
builtins.substring 0 8 (parseGithubDependency dependency).rev
else
dependency.version;
getVersion = dependencyObject:
if dependencyObject ? from && dependencyObject ? version then
builtins.substring 0 8 (parseGithubDependency dependencyObject).rev
else
dependencyObject.version;
pinVersions = dependencies: parentScopeDeps:
lib.mapAttrs
(pname: pdata:
@ -45,9 +47,10 @@
if ! pdata ? requires then
[]
else
lib.forEach (lib.attrNames pdata.requires) (reqName:
"${reqName}#${getVersion selfScopeDeps."${reqName}"}"
);
lib.forEach (lib.attrNames pdata.requires) (reqName: {
name = reqName;
version = getVersion selfScopeDeps."${reqName}";
});
dependencies = pinVersions (pdata.dependencies or {}) selfScopeDeps;
}
)
@ -55,97 +58,74 @@
packageLockWithPinnedVersions = pinVersions parsed.dependencies parsed.dependencies;
# recursively collect dependencies
parseDependencies = dependencies:
lib.mapAttrsToList # returns list of lists
(pname: pdata:
if ! dev && pdata.dev or false then
[]
else
# handle github dependency
if pdata ? from && pdata ? version then
let
githubData = parseGithubDependency pdata;
in
[ rec {
name = "${pname}#${version}";
version = builtins.substring 0 8 githubData.rev;
owner = githubData.org;
repo = githubData.repo;
rev = githubData.rev;
type = "github";
depsExact = pdata.depsExact;
}]
# handle http(s) dependency
else
[rec {
name = "${pname}#${version}";
version = pdata.version;
url = pdata.resolved;
type = "fetchurl";
hash = pdata.integrity;
depsExact = pdata.depsExact;
}]
++
(lib.optionals (pdata ? dependencies)
(lib.flatten (parseDependencies pdata.dependencies))
)
)
dependencies;
in
# the dream lock
rec {
sources =
let
lockedSources = lib.listToAttrs (
map
(dep: lib.nameValuePair
dep.name
(
if dep.type == "github" then
{ inherit (dep) type version owner repo rev; }
else
{ inherit (dep) type version url hash; }
)
)
(lib.flatten (parseDependencies packageLockWithPinnedVersions))
);
in
# if only a package-lock.json is given, the main source is missing
lockedSources // {
"${parsed.name}" = {
type = "unknown";
version = parsed.version;
utils.simpleTranslate translatorName {
# values
inputData = packageLockWithPinnedVersions;
mainPackageName = parsed.name;
mainPackageVersion = parsed.version;
mainPackageDependencies =
lib.mapAttrsToList
(pname: pdata:
{ name = pname; version = getVersion pdata; })
(lib.filterAttrs
(pname: pdata: ! (pdata.dev or false) || dev)
parsed.dependencies);
buildSystemName = "nodejs";
buildSystemAttrs = { nodejsVersion = args.nodejs; };
# functions
serializePackages = inputData:
let
serialize = inputData:
lib.mapAttrsToList # returns list of lists
(pname: pdata:
[ (pdata // { inherit pname; }) ]
++
(lib.optionals (pdata ? dependencies)
(lib.flatten (serialize pdata.dependencies))))
inputData;
in
lib.filter
(pdata:
dev || ! (pdata.dev or false))
(lib.flatten (serialize inputData));
getName = dependencyObject: dependencyObject.pname;
inherit getVersion;
getSourceType = dependencyObject:
if dependencyObject ? from && dependencyObject ? version then
"github"
else
"fetchurl";
sourceConstructors = {
github = dependencyObject:
let
githubData = parseGithubDependency dependencyObject;
in
rec {
version = builtins.substring 0 8 githubData.rev;
owner = githubData.org;
repo = githubData.repo;
rev = githubData.rev;
};
fetchurl = dependencyObject:
rec {
version = dependencyObject.version;
url = dependencyObject.resolved;
hash = dependencyObject.integrity;
};
};
};
generic = {
buildSystem = "nodejs";
producedBy = translatorName;
mainPackage = parsed.name;
dependencyGraph =
{
"${parsed.name}" =
lib.mapAttrsToList
(pname: pdata: "${pname}#${getVersion pdata}")
(lib.filterAttrs
(pname: pdata: ! (pdata.dev or false) || dev)
parsed.dependencies);
}
//
lib.listToAttrs
(map
(dep: lib.nameValuePair dep.name dep.depsExact)
(lib.flatten (parseDependencies packageLockWithPinnedVersions))
);
sourcesCombinedHash = null;
getDependencies = dependencyObject: getDepByNameVer: getDepByOriginalID:
dependencyObject.depsExact;
};
buildSystem = {
nodejsVersion = 14;
};
};
compatiblePaths =
{
@ -167,5 +147,15 @@
type = "flag";
};
nodejs = {
description = "specify nodejs version";
default = lib.elemAt (lib.splitString "." nodejs.version) 0;
examples = [
"14"
"16"
];
type = "argument";
};
};
}

View File

@ -36,7 +36,7 @@
throw "parser failed at: \n${lib.substring failureOffset 50 tryParse.value.str}";
in
utils.simpleTranslate translatorName {
utils.simpleTranslate translatorName rec {
inputData = parsedLock;
mainPackageName = packageJSON.name;
@ -47,7 +47,7 @@
nodejsVersion = 14;
};
mainPackageDependencies = inputData:
mainPackageDependencies =
lib.mapAttrsToList
(depName: depSemVer:
let

View File

@ -9,12 +9,12 @@ let
simpleTranslate = translatorName:
{
# values
inputData,
mainPackageName,
mainPackageVersion,
mainPackageDependencies,
buildSystemName,
buildSystemAttrs,
inputData,
# functions
serializePackages,
@ -22,8 +22,8 @@ let
getVersion,
getSourceType,
sourceConstructors,
getOriginalID ? null,
getDependencies ? null,
getOriginalID ? null,
mainPackageSource ? { type = "unknown"; },
}:
let
@ -49,7 +49,11 @@ let
sources = b.foldl'
(result: pkgData: lib.recursiveUpdate result {
"${getName pkgData}#${getVersion pkgData}" =
sourceConstructors."${getSourceType pkgData}" pkgData;
let
type = getSourceType pkgData;
in
(sourceConstructors."${type}" pkgData)
// { inherit type; };
})
{}
serializedPackagesList;
@ -57,7 +61,7 @@ let
dependencyGraph =
{
"${mainPackageName}#${mainPackageVersion}" =
lib.forEach (mainPackageDependencies inputData)
lib.forEach mainPackageDependencies
(dep: "${dep.name}#${dep.version}");
}
//