From d23c6110657257282e1b08c69a1fbd1a8cc213ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Tue, 18 Oct 2022 19:44:12 +0200 Subject: [PATCH] Replace yarn alias with a shell script This makes the yarn command available in Nix shell and direnv. --- src/textUtils.ts | 13 +++++++----- src/tmpl/yarn-project.nix.in | 38 ++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/textUtils.ts b/src/textUtils.ts index 4476219..5b6f935 100644 --- a/src/textUtils.ts +++ b/src/textUtils.ts @@ -46,13 +46,16 @@ export const renderTmpl = ( let result = tmpl; for (const [name, value] of Object.entries(vars)) { if (typeof value === "string") { - result = result.replace(`@@${name}@@`, value); + result = result.replace(new RegExp(`@@${name}@@`, "g"), value); } if (typeof value === "boolean") { - const lines = result.split("\n"); - const startIdx = lines.indexOf(`#@@ IF ${name}`); - const endIdx = lines.indexOf(`#@@ ENDIF ${name}`); - if (startIdx !== -1 && endIdx > startIdx) { + while (true) { + const lines = result.split("\n"); + const startIdx = lines.indexOf(`#@@ IF ${name}`); + const endIdx = lines.indexOf(`#@@ ENDIF ${name}`); + if (startIdx === -1 || endIdx < startIdx) { + break; + } if (value) { lines.splice(endIdx, 1); lines.splice(startIdx, 1); diff --git a/src/tmpl/yarn-project.nix.in b/src/tmpl/yarn-project.nix.in index 790fc47..732861a 100644 --- a/src/tmpl/yarn-project.nix.in +++ b/src/tmpl/yarn-project.nix.in @@ -1,49 +1,49 @@ # This file is generated by running "yarn install" inside your project. # Manual changes might be lost - proceed with caution! -{ lib, nodejs, stdenv, fetchurl, writeText, git, cacert, writeShellApplication }: +{ lib, stdenv, nodejs, git, cacert, fetchurl, writeShellScript, writeShellScriptBin }: { src, overrideAttrs ? null, ... } @ args: let - yarnPath = ./@@YARN_PATH@@; - lockfile = ./@@LOCKFILE@@; cacheFolder = @@CACHE_FOLDER@@; +#@@ IF NEED_ISOLATED_BUILD_SUPPRORT + lockfile = ./@@LOCKFILE@@; +#@@ ENDIF NEED_ISOLATED_BUILD_SUPPRORT # Call overrideAttrs on a derivation if a function is provided. optionalOverride = fn: drv: if fn == null then drv else drv.overrideAttrs fn; + # Simple stub that provides the global yarn command. + yarn = writeShellScriptBin "yarn" '' + exec '${nodejs}/bin/node' '${./@@YARN_PATH@@}' "$@" + ''; + # Common attributes between Yarn derivations. drvCommon = { # Make sure the build uses the right Node.js version everywhere. - buildInputs = [ nodejs ]; + buildInputs = [ nodejs yarn ]; # Tell node-gyp to use the provided Node.js headers for native code builds. npm_config_nodedir = nodejs; # Tell node-pre-gyp to never fetch binaries / always build from source. npm_config_build_from_source = "true"; - # Defines the shell alias to run Yarn. - postHook = '' - yarn() { - CI=1 node "${yarnPath}" "$@" - } - ''; }; # Create derivations for fetching dependencies. cacheDrvs = let - builder = builtins.toFile "builder.sh" '' + builder = writeShellScript "yarn-cache-builder" '' source $stdenv/setup cd "$src" HOME="$TMP" yarn_cache_folder="$TMP" CI=1 \ - node '${yarnPath}' nixify fetch-one $locator + yarn nixify fetch-one $locator # Because we change the cache dir, Yarn may generate a different name. mv "$TMP/$(sed 's/-[^-]*\.[^-]*$//' <<< "$outputFilename")"-* $out ''; in lib.mapAttrs (locator: { filename, sha512 }: stdenv.mkDerivation { inherit src builder locator; name = lib.strings.sanitizeDerivationName locator; - buildInputs = [ nodejs git cacert ]; + buildInputs = [ yarn git cacert ]; outputFilename = filename; outputHashMode = "flat"; outputHashAlgo = "sha512"; @@ -52,7 +52,7 @@ let # Create a shell snippet to copy dependencies from a list of derivations. mkCacheBuilderForDrvs = drvs: - writeText "collect-cache.sh" (lib.concatMapStrings (drv: '' + writeShellScript "collect-yarn-cache" (lib.concatMapStrings (drv: '' cp ${drv} '${drv.outputFilename}' '') drvs); @@ -172,13 +172,9 @@ let passthru = { inherit nodejs; - yarn = writeShellApplication { - name = "yarn"; - runtimeInputs = [ nodejs ]; - text = '' - ${yarnPath} --cwd ${project}/libexec/${project.name} "$@" - ''; - }; + yarn = writeShellScriptBin "yarn" '' + exec '${yarn}/bin/yarn' --cwd '${project}/libexec/${project.name}' "$@" + ''; }; });