mirror of
https://github.com/mjallen18/snowfall-lib.git
synced 2026-04-18 09:05:58 -05:00
feat: add path.get-relative-module-path helper to extract module paths consistently
This commit is contained in:
@@ -9,7 +9,6 @@ let
|
||||
inherit (core-inputs.nixpkgs.lib)
|
||||
foldl
|
||||
mapAttrs
|
||||
hasPrefix
|
||||
hasSuffix
|
||||
isFunction
|
||||
splitString
|
||||
@@ -39,18 +38,7 @@ in
|
||||
let
|
||||
user-modules = snowfall-lib.fs.get-default-nix-files-recursive src;
|
||||
create-module-metadata = module: {
|
||||
name =
|
||||
let
|
||||
# We are building flake outputs based on file paths. Nix doesn't allow this
|
||||
# so we have to explicitly discard the string's path context for string manipulation.
|
||||
path-name = builtins.replaceStrings [ (builtins.toString src) "/default.nix" ] [ "" "" ] (
|
||||
builtins.unsafeDiscardStringContext module
|
||||
);
|
||||
in
|
||||
if hasPrefix "/" path-name then
|
||||
builtins.substring 1 ((builtins.stringLength path-name) - 1) path-name
|
||||
else
|
||||
path-name;
|
||||
name = snowfall-lib.path.get-relative-module-path src module;
|
||||
path = module;
|
||||
};
|
||||
modules-metadata = builtins.map create-module-metadata user-modules;
|
||||
|
||||
@@ -11,7 +11,7 @@ let
|
||||
dirOf
|
||||
concatStringsSep
|
||||
;
|
||||
inherit (core-inputs.nixpkgs.lib) assertMsg last init;
|
||||
inherit (core-inputs.nixpkgs.lib) assertMsg last init hasPrefix;
|
||||
|
||||
file-name-regex = "(.*)\\.(.*)$";
|
||||
in
|
||||
@@ -146,5 +146,27 @@ in
|
||||
#@ Path -> String
|
||||
get-directory-name = path:
|
||||
builtins.unsafeDiscardStringContext (baseNameOf path);
|
||||
|
||||
## Get relative module path from source directory.
|
||||
## Example Usage:
|
||||
## ```nix
|
||||
## get-relative-module-path "/modules/nixos" "/modules/nixos/foo/bar/default.nix"
|
||||
## ```
|
||||
## Result:
|
||||
## ```nix
|
||||
## "foo/bar"
|
||||
## ```
|
||||
#@ String -> Path -> String
|
||||
get-relative-module-path =
|
||||
src: module:
|
||||
let
|
||||
path-name = builtins.replaceStrings [ (builtins.toString src) "/default.nix" ] [ "" "" ] (
|
||||
builtins.unsafeDiscardStringContext module
|
||||
);
|
||||
in
|
||||
if hasPrefix "/" path-name then
|
||||
builtins.substring 1 ((builtins.stringLength path-name) - 1) path-name
|
||||
else
|
||||
path-name;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user