Merge pull request #100 from Mic92/build-npm-package

Build npm package
This commit is contained in:
Jörg Thalheim 2022-11-10 17:59:03 +01:00 committed by GitHub
commit dcf5fcc402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 12 deletions

View File

@ -12,6 +12,7 @@ designed to work with nixpkgs but also other package sets.
- rubygems.org
- update buildRustPackage's cargoHash/cargoSha256
- update buildGoModule's vendorHash/vendorSha256
- update buildNpmPackage's npmDepsHash
- build and run the resulting package (see `--build`,
`--run` or `--shell`
- commit updated files (see `--commit` flag)

View File

@ -16,7 +16,6 @@ python3.pkgs.buildPythonApplication rec {
mypy
# technically not a test input, but we need it for development in PATH
pkgs.nixVersions.stable or nix_2_4
nix-prefetch
];
checkPhase = ''
echo -e "\x1b[32m## run black\x1b[0m"

View File

@ -29,6 +29,7 @@ class Package:
vendor_hash: Optional[str]
vendor_sha256: Optional[str]
cargo_deps: Optional[str]
npm_deps: Optional[str]
tests: List[str]
raw_version_position: InitVar[Optional[Dict[str, Any]]]
@ -70,6 +71,7 @@ def eval_expression(import_path: str, attr: str) -> str:
vendor_hash = pkg.vendorHash or null;
vendor_sha256 = pkg.vendorSha256 or null;
cargo_deps = pkg.cargoHash or pkg.cargoSha256 or null;
npm_deps = pkg.npmDepsHash or null;
tests = builtins.attrNames (pkg.passthru.tests or {{}});
}})"""

View File

@ -1,5 +1,5 @@
import fileinput
from typing import List, Optional, Dict
from typing import Optional, Dict
import subprocess
import tempfile
@ -67,18 +67,33 @@ def replace_hash(filename: str, current: str, target: str) -> None:
print(line, end="")
def nix_prefetch(cmd: List[str]) -> str:
def nix_prefetch(expr: str) -> str:
extra_env: Dict[str, str] = {}
tempdir: Optional[tempfile.TemporaryDirectory[str]] = None
if extra_env.get("XDG_RUNTIME_DIR") is None:
tempdir = tempfile.TemporaryDirectory()
extra_env["XDG_RUNTIME_DIR"] = tempdir.name
try:
res = run(["nix-prefetch"] + cmd, extra_env=extra_env)
res = run(
[
"nix-build",
"--expr",
f'({expr}).overrideAttrs (_: {{ outputHash = ""; outputHashAlgo = "sha256"; }})',
],
extra_env=extra_env,
check=False,
)
stderr = res.stderr.strip()
got = ""
for line in stderr.split("\n"):
line = line.strip()
if line.startswith("got:"):
got = line.split("got:")[1].strip()
break
finally:
if tempdir:
tempdir.cleanup()
return res.stdout.strip()
return got
def disable_check_meta(opts: Options) -> str:
@ -86,20 +101,28 @@ def disable_check_meta(opts: Options) -> str:
def update_src_hash(opts: Options, filename: str, current_hash: str) -> None:
expr = f"(import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}"
target_hash = nix_prefetch([expr])
expr = (
f"(import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}.src"
)
target_hash = nix_prefetch(expr)
replace_hash(filename, current_hash, target_hash)
def update_go_modules_hash(opts: Options, filename: str, current_hash: str) -> None:
expr = f"{{ sha256 }}: (import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}.go-modules.overrideAttrs (_: {{ inherit sha256; }})"
target_hash = nix_prefetch([expr])
expr = f"(import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}.go-modules"
target_hash = nix_prefetch(expr)
replace_hash(filename, current_hash, target_hash)
def update_cargo_deps_hash(opts: Options, filename: str, current_hash: str) -> None:
expr = f"{{ sha256 }}: (import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}.cargoDeps.overrideAttrs (_: {{ inherit sha256; }})"
target_hash = nix_prefetch([expr])
expr = f"(import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}.cargoDeps"
target_hash = nix_prefetch(expr)
replace_hash(filename, current_hash, target_hash)
def update_npm_deps_hash(opts: Options, filename: str, current_hash: str) -> None:
expr = f"(import {opts.import_path} {disable_check_meta(opts)}).{opts.attribute}.npmDeps"
target_hash = nix_prefetch(expr)
replace_hash(filename, current_hash, target_hash)
@ -153,4 +176,7 @@ def update(opts: Options) -> Package:
if package.cargo_deps:
update_cargo_deps_hash(opts, package.filename, package.cargo_deps)
if package.npm_deps:
update_npm_deps_hash(opts, package.filename, package.npm_deps)
return package

View File

@ -26,6 +26,7 @@ def run(
command: List[str],
cwd: Optional[Union[Path, str]] = None,
stdout: Union[None, int, IO[Any]] = subprocess.PIPE,
stderr: Union[None, int, IO[Any]] = subprocess.PIPE,
check: bool = True,
extra_env: Dict[str, str] = {},
) -> "subprocess.CompletedProcess[str]":
@ -33,5 +34,5 @@ def run(
env = os.environ.copy()
env.update(extra_env)
return subprocess.run(
command, cwd=cwd, check=check, text=True, stdout=stdout, env=env
command, cwd=cwd, check=check, text=True, stdout=stdout, stderr=stderr, env=env
)

27
tests/test_npm.py Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
from nix_update.options import Options
from nix_update.update import update
import subprocess
import conftest
def test_update(helpers: conftest.Helpers) -> None:
with helpers.testpkgs() as path:
opts = Options(attribute="npm", import_path=str(path))
update(opts)
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--extra-experimental-features",
"nix-command",
"-f",
path,
"npm.version",
],
text=True,
stdout=subprocess.PIPE,
)
assert version.stdout.strip() > "10.8.6"

View File

@ -3,4 +3,5 @@
pypi = pkgs.python3.pkgs.callPackage ./pypi.nix {};
sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix {};
savanna = pkgs.python3.pkgs.callPackage ./savanna.nix {};
npm = pkgs.callPackage ./npm.nix {};
}

18
tests/testpkgs/npm.nix Normal file
View File

@ -0,0 +1,18 @@
{ lib
, fetchFromGitHub
, buildNpmPackage
}:
buildNpmPackage rec {
pname = "jellyfin-web";
version = "10.8.6";
src = fetchFromGitHub {
owner = "jellyfin";
repo = "jellyfin-web";
rev = "v${version}";
sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
npmDepsHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}