fix: fix logic issues with package.json in python builder

This commit is contained in:
hsjobeki 2023-01-10 18:07:20 +01:00
parent 28f7534d5d
commit 540821481e
7 changed files with 54 additions and 35 deletions

View File

@ -116,13 +116,14 @@
devShellNodeModules = mkNodeModules {
isMain = true;
installMethod = "copy";
reason = "devShell";
packageJSON = "${src}/package.json";
inherit pname version depsTree nodeModulesTree;
};
# type: nodeModules :: Derivation
nodeModules = mkNodeModules {
inherit installMethod isMain depsTree nodeModulesTree;
inherit pname version;
packageJSON = "${src}/package.json";
};
installMethod =
@ -134,7 +135,7 @@
pkg = produceDerivation name (
pkgs.stdenv.mkDerivation
{
rec {
inherit pname version src;
inherit nodeSources installMethod isMain;
@ -183,6 +184,9 @@
cp -r ${nodeModules} ./node_modules
chmod -R +w node_modules
export NODE_PATH="$NODE_PATH:./node_modules"
export PATH="$PATH:node_modules/.bin"
runHook postConfigure
'';
@ -226,10 +230,13 @@
npm --production --offline --nodedir=$nodeSources run postinstall
fi
fi
export NODE_MODULES_PATH=${nodeModules}
${nodejsBuilder}/bin/d2nMakeOutputs
echo "DONE!"
runHook postInstall
'';

View File

@ -111,7 +111,6 @@
installMethod :: "copy" | "symlink",
depsTree :: DependencyTree,
nodeModulesTree :: NodeModulesTree,
installPath :: ? String,
}
*/
mkNodeModules = {
@ -119,10 +118,9 @@
installMethod,
pname,
version,
installPath ? "",
depsTree,
nodeModulesTree,
reason ? "default",
packageJSON,
}:
# dependency tree as JSON needed to build node_modules
let
@ -141,7 +139,7 @@
export isMain=${b.toString isMain}
export installMethod=${installMethod}
export REASON=${reason}
export packageJSON=${packageJSON}
${nodeModulesBuilder}
@ -149,6 +147,10 @@
if [ ! -d "$out" ]; then
mkdir -p $out
fi
# for debuging comment this out
# cp $depsTreeJSONPath $out/.d2n-resolved-dependency-tree.json
# cp $nmTreeJSONPath $out/.d2n-resolved-folder-structure.json
'';
in {
inherit nodeModulesTree mkNodeModules;

View File

@ -1,6 +1,5 @@
from pathlib import Path
from .derivation import get_env
from .logger import logger
# root is used to create the node_modules structure
# might default to $out,

View File

@ -54,6 +54,10 @@ def get_env(key: str) -> str:
return value
def get_package_json_path() -> Path:
return Path(get_env("packageJSON"))
@dataclass
class Info:
name: str

View File

@ -33,9 +33,9 @@ def _create_package_from_derivation(
logger.debug(f"{str(dep)} is not a package. Skipping installation")
return
# check if there is already the right package installed
if not get_package_json(target):
# folder might be created, but package.json only when installed
if not (target / Path("package.json")).exists():
if install_method == InstallMethod.copy:
# shutil.copytree(dep.derivation, target)
target.mkdir(parents=True, exist_ok=True)
for entry in os.listdir(dep.derivation):
if (dep.derivation / Path(entry)).is_dir():

View File

@ -1,23 +1,37 @@
import json
import os
from pathlib import Path
from typing import Any, Optional, TypedDict, Union
from typing import Any, Optional, TypedDict
from .dependencies import Dependency, DepsTree
from .derivation import get_env
from .derivation import get_env, env
package_json_cache = {}
package_json_cache: dict[Path, dict[str, Any]] = {}
def get_package_json(path: Path = Path("")) -> Union[dict[str, Any], None]:
if path not in package_json_cache:
if not os.path.isfile(path / Path("package.json")):
def get_package_json(
path: Path = Path(env.get("packageJSON", ""))
) -> Optional[dict[str, Any]]:
finalPath = path
result: Optional[dict[str, Any]]
if "package.json" not in path.name:
finalPath = path / Path("package.json")
if finalPath not in package_json_cache:
if not finalPath.exists():
# there is no package.json in the folder
return None
with open(f"{path}/package.json", encoding="utf-8-sig") as f:
package_json_cache[path] = json.load(f)
result = None
else:
with open(finalPath, encoding="utf-8-sig") as f:
parsed: dict[str, Any] = json.load(f)
return package_json_cache[path]
result = parsed
package_json_cache[path] = parsed
else:
result = package_json_cache[path]
return result
def has_scripts(
@ -27,10 +41,11 @@ def has_scripts(
"install",
"postinstall",
),
):
return package_json and (
package_json.get("scripts", {}).keys() & set(lifecycle_scripts)
)
) -> bool:
result = False
if package_json:
result = package_json.get("scripts", {}).keys() & set(lifecycle_scripts)
return result
def get_bins(dep: Dependency) -> dict[str, Path]:
@ -50,7 +65,7 @@ def get_bins(dep: Dependency) -> dict[str, Path]:
def create_binary(target: Path, source: Path):
target.parent.mkdir(parents=True, exist_ok=True)
if not os.path.lexists(target):
if not target.exists():
target.symlink_to(Path("..") / source)

View File

@ -2,7 +2,6 @@ from .lib.checks import check_platform
from .lib.derivation import (
is_main_package,
get_outputs,
get_inputs,
get_self,
InstallMethod,
get_install_method,
@ -66,7 +65,6 @@ def makeOutputs():
"""
outputs = get_outputs()
inputs = get_inputs()
pkg = get_self()
# create $lib output
@ -78,20 +76,14 @@ def makeOutputs():
bin_out.mkdir(parents=True, exist_ok=True)
install_method = get_install_method()
# create $out/lib/node_modules
# if not (lib_out / Path("node_modules")).exists():
# create $out/lib/
if install_method == InstallMethod.copy:
shutil.copytree(outputs.lib, lib_out, symlinks=True)
# shutil.copytree(
# inputs.node_modules, lib_out / Path("node_modules"), symlinks=True
# )
elif install_method == InstallMethod.symlink:
lib_out.mkdir(parents=True, exist_ok=True)
for entry in os.listdir(outputs.lib):
(lib_out / Path(entry)).symlink_to(outputs.lib / Path(entry))
# (lib_out / Path("node_modules")).symlink_to(inputs.node_modules)
# create $out/bin
# collect all binaries declared from package
# and create symlinks to their sources e.g. $out/bin/cli -> $out/lib/cli.json