-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetModule.nix
More file actions
28 lines (27 loc) · 1.03 KB
/
getModule.nix
File metadata and controls
28 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{ lib }:
let
# Replaces all characters besides alphanumeric, dash, and underscore with dash.
# This should match toNixName in process-build/generator.go
sanitize =
name:
builtins.concatStringsSep "" (
map (p: if builtins.isList p then "-" else p) (builtins.split "[^a-zA-Z0-9_-]+" name)
);
# Takes a path to a go.mod file and subpackage and returns sanitized module name
parseGoMod =
path: subPackage:
let
content = builtins.readFile path;
lines = builtins.filter (s: builtins.isString s && s != "") (builtins.split "\n" content);
isModuleLine = line: builtins.match "^module[[:space:]]+.*" line != null;
moduleLine =
lib.findFirst isModuleLine
(builtins.throw "go.mod at ${toString path} does not contain a 'module' directive")
lines;
match = builtins.match "^module[[:space:]]+([^[:space:]]+).*" moduleLine;
mod = builtins.head match;
package = if subPackage == "" then mod else mod + "/" + subPackage;
in
sanitize package;
in
parseGoMod