feat(nix/utils): add storePathName, a more generic baseNameOf
This is a wrapper around baseNameOf which also can deal with derivations. Added to //nix/utils since I've found myself introducing an ad-hoc implementation of this for both //web/bubblegum and //nix/buildC. Change-Id: I2fcd97a150d6eda21ab323fa0d881ff7442a892e Reviewed-on: https://cl.tvl.fyi/c/depot/+/3049 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
parent
c21dee2b5b
commit
f88ac5c0b5
2 changed files with 48 additions and 8 deletions
|
@ -17,8 +17,50 @@ let
|
||||||
} // (attrs.meta or {});
|
} // (attrs.meta or {});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Get the basename of a store path without
|
||||||
|
the leading hash.
|
||||||
|
|
||||||
|
Type: (path | drv | string) -> string
|
||||||
|
|
||||||
|
Example:
|
||||||
|
storePathName ./foo.c
|
||||||
|
=> "foo.c"
|
||||||
|
|
||||||
|
storePathName (writeText "foo.c" "int main() { return 0; }")
|
||||||
|
=> "foo.c"
|
||||||
|
|
||||||
|
storePathName "${hello}/bin/hello"
|
||||||
|
=> "hello"
|
||||||
|
*/
|
||||||
|
storePathName = p:
|
||||||
|
if lib.isDerivation p
|
||||||
|
then p.name
|
||||||
|
else if builtins.isPath p
|
||||||
|
then builtins.baseNameOf p
|
||||||
|
else if builtins.isString p
|
||||||
|
then
|
||||||
|
let
|
||||||
|
# strip leading storeDir and trailing slashes
|
||||||
|
noStoreDir = lib.removeSuffix "/"
|
||||||
|
(lib.removePrefix "${builtins.storeDir}/" p);
|
||||||
|
# a basename of a child of a store path isn't really
|
||||||
|
# referring to a store path, so removing the string
|
||||||
|
# context is safe (e. g. "hello" for "${hello}/bin/hello").
|
||||||
|
basename = builtins.unsafeDiscardStringContext
|
||||||
|
(builtins.baseNameOf p);
|
||||||
|
in
|
||||||
|
# If p is a direct child of storeDir, we need to remove
|
||||||
|
# the leading hash as well to make sure that:
|
||||||
|
# `storePathName drv == storePathName (toString drv)`.
|
||||||
|
if noStoreDir == basename
|
||||||
|
then builtins.substring 33 (-1) basename
|
||||||
|
else basename
|
||||||
|
else builtins.throw "Don't know how to get (base)name of "
|
||||||
|
+ lib.generators.toPretty {} p;
|
||||||
|
|
||||||
in {
|
in {
|
||||||
inherit
|
inherit
|
||||||
drvTargets
|
drvTargets
|
||||||
|
storePathName
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ let
|
||||||
inherit (depot.nix)
|
inherit (depot.nix)
|
||||||
runExecline
|
runExecline
|
||||||
getBins
|
getBins
|
||||||
|
utils
|
||||||
;
|
;
|
||||||
|
|
||||||
statusCodes = {
|
statusCodes = {
|
||||||
|
@ -179,16 +180,13 @@ let
|
||||||
# the input path or name of the input derivation.
|
# the input path or name of the input derivation.
|
||||||
# Must be given if the input is a string.
|
# Must be given if the input is a string.
|
||||||
, name ? null
|
, name ? null
|
||||||
}:
|
, ...
|
||||||
|
}@args:
|
||||||
input: let
|
input: let
|
||||||
drvName =
|
drvName =
|
||||||
if name != null
|
if builtins.isString input || args ? name
|
||||||
then name
|
then args.name
|
||||||
else if builtins.isPath input
|
else utils.storePathName input;
|
||||||
then baseNameOf input
|
|
||||||
else if lib.isDerivation input
|
|
||||||
then input.name
|
|
||||||
else builtins.throw "Need name";
|
|
||||||
script =
|
script =
|
||||||
if builtins.isPath input || lib.isDerivation input
|
if builtins.isPath input || lib.isDerivation input
|
||||||
then input
|
then input
|
||||||
|
|
Loading…
Add table
Reference in a new issue