This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
forked from purs-nix/purs-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.nix
155 lines (144 loc) · 3.94 KB
/
utils.nix
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
with builtins;
p:
let l = p.lib; in
rec
{ bundle =
{ entry-point
, esbuild ? {}
, main ? true
}:
let
esbuild' =
{ log-level = "warning";
outfile = "main.js";
}
// (if esbuild?platform then {} else { format = "esm"; })
// esbuild
// { bundle = true; };
flags =
toString
(l.mapAttrsToList
(n: v:
let
process = val:
let str = toString val; in
if any (a: l.hasPrefix a str) [ "\"" "$" "'" ]
then str
else l.escapeShellArg str;
in
if isBool v then
if v then "--${n}" else ""
else if isList v then
map (a: "--${n}:${process a}") v
else
"--${n}=${process v}"
)
esbuild'
);
build = "${p.esbuild}/bin/esbuild ${flags}";
in
if main
then ''echo 'import { main } from "${entry-point}"; main()' | ${build}''
else ''${build} ${entry-point}'';
compile =
purescript:
{ globs
, output ? null
, verbose-errors ? false
, comments ? false
, codegen ? null
, no-prefix ? false
, json-errors ? false
}:
let
flags =
toString
[ (make-flag "--output " output)
(make-flag "--verbose-errors" verbose-errors)
(make-flag "--comments" comments)
(make-flag "--codegen " codegen)
(make-flag "--no-prefix" no-prefix)
(make-flag "--json-errors" json-errors)
];
in
''${purescript}/bin/purs compile ${flags} ${globs}'';
repl =
purescript:
{ globs
, node-path ? null
, node-opts ? null
}:
let
flags =
toString
[ (make-flag "--node-path " node-path)
(make-flag "--node-opts " node-opts)
];
in
''${purescript}/bin/purs repl ${flags} ${globs}'';
make-flag = flag: arg:
if arg == null || arg == false then
""
else if arg == true then
flag
else
flag + arg;
make-name = unsanitized: version:
let name = l.strings.sanitizeDerivationName unsanitized; in
if version == null then
{ inherit name; }
else
{ pname = name; inherit version; };
node-command =
{ argv-1
, import
, nodejs
}:
''
${nodejs}/bin/node \
--preserve-symlinks \
--input-type=module \
-e 'import { main } from "${import}"; main()' \
-- "${argv-1}" "''${@:2}"
'';
has-version = pkg:
let info = pkg.purs-nix-info; in
if info?version then
if info.version == null then
l.warn
"the package '${info.name}' is built with an old version of purs-nix, please update it if possible"
false
else
true
else
false;
package-info = pkg:
let info = pkg.purs-nix-info; in
''
echo "name: ${info.name}"
echo "version: ${if has-version pkg then info.version else "none"}"
${if info?flake then
''
echo "flake: ${info.flake.url}"
echo "package: ${info.flake.package or "default"}"''
else if info?repo then
''
echo "repo: ${info.repo}"
${if info?rev
then ''echo "commit: ${info.rev}"''
else ''echo "path: ${pkg.src}"''
}''
else
''echo "path: ${pkg.src}"''
}
echo "source: ${pkg}"
'';
# subtract-string "abcdef" "abc" => "def"
subtract-string = s1: s2:
assert l.hasPrefix s2 s1;
let
l1 = stringLength s1;
l2 = stringLength s2;
in
substring l2 (l1 - l2) s1;
}