add: descriptive comments, remove: some unused bindings

This commit is contained in:
hsjobeki 2022-12-30 22:21:37 +01:00
parent 96e8a0e297
commit ca80e29283
4 changed files with 216 additions and 180 deletions

View File

@ -112,12 +112,13 @@
dependencyGraph;
packageVersions =
l.zipAttrsWith
l.mapAttrs (name: versions: l.unique versions)
(l.zipAttrsWith
(name: versions: l.flatten versions)
[
allDependants
allDependencies
];
]);
cyclicDependencies = lock.cyclicDependencies;

View File

@ -34,11 +34,10 @@
}: let
l = lib // builtins;
b = builtins;
inherit (pkgs) stdenv python3 python310Packages makeWrapper jq;
nodejsVersion = subsystemAttrs.nodejsVersion;
defaultNodejsVersion = "14";
defaultNodejsVersion = l.versions.major pkgs.nodejs.version;
isMainPackage = name: version:
(packages."${name}" or null) == version;
@ -50,6 +49,11 @@
pkgs."nodejs-${nodejsVersion}_x"
or (throw "Could not find nodejs version '${nodejsVersion}' in pkgs");
nodeSources = pkgs.runCommandLocal "node-sources" {} ''
tar --no-same-owner --no-same-permissions -xf ${nodejs.src}
mv node-* $out
'';
# e.g.
# {
# "@babel/core": ["1.0.0","2.0.0"]
@ -73,25 +77,26 @@
)
packageVersions;
# our builder, written in python. We have huge complexity with how npm builds node_modules
nodejsBuilder = python310Packages.buildPythonApplication {
pname = "builder";
version = "0.1.0";
# our builder, written in python. Better handles the complexity with how npm builds node_modules
nodejsBuilder = pkgs.python310Packages.buildPythonApplication {
name = "builder";
src = ./nodejs_builder;
format = "pyproject";
nativeBuildInputs = with python310Packages; [poetry mypy flake8 black semantic-version];
propagatedBuildInputs = with python310Packages; [node-semver];
nativeBuildInputs = with pkgs.python310Packages; [poetry mypy flake8 black];
doCheck = false;
meta = {
description = "Custom builder";
};
};
mkNodeModule = name: version: let
pname = lib.replaceStrings ["@" "/"] ["__at__" "__slash__"] (name + "@" + version);
deps = getDependencies name version;
# type: resolveChildren :: { name :: String, version :: String, rootVersions :: { ${String} :: {String} }} -> { ${String} :: { version :: String, dependencies :: Self } }
# function that resolves local vs global dependencies.
# we copy dependencies into the global node_modules scope, if they dont have conflicts there.
# otherwise we need to declare the package as 'private'
# returns the recursive structure:
# {
# "pname": {
# "version": "1.0.0",
# "dependencies": {...},
# }
# }
resolveChildren = {
name, #a
version, #1.1.2
@ -125,14 +130,28 @@
in
localDeps;
# function that 'builds' a package.
# executes
# type: mkNodeModule :: String -> String -> Derivation
mkNodeModule = name: version: let
pname = lib.replaceStrings ["@" "/"] ["__at__" "__slash__"] (name + "@" + version);
# all direct dependencies of current package
deps = getDependencies name version;
# in case of a conflict pick the highest semantic version as root. All other version must then be private if used.
# TODO: pick the version that minimizes the tree
pickVersion = name: versions: directDepsAttrs.${name} or (l.head (l.sort (a: b: l.compareVersions a b == 1) versions));
rootPackages = l.mapAttrs (name: versions: pickVersion name versions) packageVersions;
packageVersions' = l.mapAttrs (n: v: l.unique v) packageVersions;
rootPackages = l.mapAttrs (name: versions: pickVersion name versions) packageVersions';
# direct dependencies are all direct dependencies parsed from the lockfile at root level.
directDeps = getDependencies name version;
# type: { ${String} :: String } # e.g { "prettier" = "1.2.3"; }
directDepsAttrs = l.listToAttrs (b.map (dep: l.nameValuePair dep.name dep.version) directDeps);
# build the node_modules tree from all known rootPackages
# type: { ${String} :: { version :: String, dependencies :: Self } }
nodeModulesTree =
l.mapAttrs (
name: version: let
@ -148,10 +167,19 @@
nmTreeJSON = b.toJSON nodeModulesTree;
depsTree = let
getDeps = deps: (b.foldl'
(
deps: dep:
# type: makeDepAttrs :: {
# deps :: { ${String} :: { ${String} :: { deps :: Self, derivation :: Derivation } } },
# dep :: { name :: String, version :: String }
# attributes :: {
# derivation :: Derivation,
# deps :: { ${String} :: { ${String} :: { deps :: Self, derivation :: Derivation } } } }
# }
# -> { ${String} :: { ${String} :: { deps :: Self, derivation :: Derivation } } }
makeDepAttrs = {
deps,
dep,
attributes,
}:
deps
// {
${dep.name} =
@ -159,11 +187,20 @@
// {
${dep.version} =
(deps.${dep.name}.${dep.version} or {})
// {
// attributes;
};
};
depsTree = let
getDeps = deps: (b.foldl'
(
deps: dep:
makeDepAttrs {
inherit deps dep;
attributes = {
deps = getDeps (getDependencies dep.name dep.version);
derivation = allPackages.${dep.name}.${dep.version}.lib;
};
};
}
)
{}
@ -172,22 +209,24 @@
depsTreeJSON = b.toJSON depsTree;
# Type: src :: Derivation
src = getSource name version;
# produceDerivation makes the mkDerivation overridable by the dream2nix users
pkg = produceDerivation name (
with pkgs;
stdenv.mkDerivation
{
inherit pname version src nodeSources;
inherit nmTreeJSON depsTreeJSON;
passAsFile = ["nmTreeJSON" "depsTreeJSON"];
inherit pname version src;
# needed for some current overrides
nativeBuildInputs = [makeWrapper];
buildInputs = [jq nodejs python3];
outputs = ["out" "lib" "deps"];
inherit (pkgs) system;
packageName = pname;
name = pname;
@ -274,8 +313,6 @@
installPhase = ''
runHook preInstall
if [ ! -n "$isMain" ];
then
if [ "$(jq '.scripts.preinstall' ./package.json)" != "null" ]; then

View File

@ -1,6 +1,6 @@
import os
from enum import Enum
from typing import Any, Optional, TypedDict
from typing import Any, Optional
from pathlib import Path
from .logger import logger
from dataclasses import dataclass

View File

@ -11,8 +11,6 @@ d2nMakeOutputs = 'nodejs_builder.main:makeOutputs'
[tool.poetry.dependencies]
python = "^3.10"
semantic-version = "^2.9.0"
node-semver = "^0.8.1"
[tool.poetry.dev-dependencies]
mypy = "^0.991"