forked from nix-community/dream2nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
348 lines (317 loc) · 9.56 KB
/
default.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
{
name,
dlib,
lib,
...
}: let
l = lib // builtins;
in {
type = "pure";
/*
Automatically generate unit tests for this translator using project sources
from the specified list.
!!! Your first action should be adding a project here. This will simplify
your work because you will be able to use `nix run .#tests-unit` to
test your implementation for correctness.
*/
generateUnitTestsForProjects = [
(builtins.fetchTarball {
url = "https://github.com/tinybeachthor/dream2nix-php-composer-lock/archive/refs/tags/complex.tar.gz";
sha256 = "sha256:1xa5paafhwv4bcn2jsmbp1v2afh729r2h153g871zxdmsxsgwrn1";
})
];
# translate from a given source and a project specification to a dream-lock.
translate = {
/*
A list of projects returned by `discoverProjects`
Example:
[
{
"dreamLockPath": "packages/optimism/dream-lock.json",
"name": "optimism",
"relPath": "",
"subsystem": "nodejs",
"subsystemInfo": {
"workspaces": [
"packages/common-ts",
"packages/contracts",
"packages/core-utils",
]
},
"translator": "yarn-lock",
"translators": [
"yarn-lock",
"package-json"
]
}
]
*/
project,
/*
Entire source tree represented as deep attribute set.
(produced by `prepareSourceTree`)
This has the advantage that files will only be read once, even when
accessed multiple times or by multiple translators.
Example:
{
files = {
"package.json" = {
relPath = "package.json"
fullPath = "${source}/package.json"
content = ;
jsonContent = ;
tomlContent = ;
}
};
directories = {
"packages" = {
relPath = "packages";
fullPath = "${source}/packages";
files = {
};
directories = {
};
};
};
# returns the tree object of the given sub-path
getNodeFromPath = path: ...
}
*/
tree,
# arguments defined in `extraArgs` (see below) specified by user
noDev,
...
} @ args: let
inherit
(import ../../semver.nix {inherit lib;})
satisfies
multiSatisfies
;
# get the root source and project source
rootSource = tree.fullPath;
projectSource = "${tree.fullPath}/${project.relPath}";
projectTree = tree.getNodeFromPath project.relPath;
composerJson = (projectTree.getNodeFromPath "composer.json").jsonContent;
composerLock = (projectTree.getNodeFromPath "composer.lock").jsonContent;
# toplevel php semver
phpSemver = composerJson.require."php" or "*";
# all the php extensions
phpExtensions = let
allDepNames = l.flatten (map (x: l.attrNames (getRequire x)) packages);
extensions = l.unique (l.filter (l.hasPrefix "ext-") allDepNames);
in
map (l.removePrefix "ext-") extensions;
composerPluginApiSemver = l.listToAttrs (l.flatten (map
(
pkg: let
requires = getRequire pkg;
in
l.optional (requires ? "composer-plugin-api")
{
name = "${pkg.name}@${pkg.version}";
value = requires."composer-plugin-api";
}
)
packages));
# get cleaned pkg attributes
getRequire = pkg:
l.mapAttrs
(_: version: resolvePkgVersion pkg version)
(pkg.require or {});
getProvide = pkg:
l.mapAttrs
(_: version: resolvePkgVersion pkg version)
(pkg.provide or {});
getReplace = pkg:
l.mapAttrs
(_: version: resolvePkgVersion pkg version)
(pkg.replace or {});
resolvePkgVersion = pkg: version:
if version == "self.version"
then pkg.version
else version;
# project package
toplevelPackage = {
name = project.name;
version = composerJson.version or "unknown";
source = {
type = "path";
path = rootSource;
};
require =
(l.optionalAttrs (!noDev) (composerJson.require-dev or {}))
// (composerJson.require or {});
};
# all the packages
packages =
# Add the top-level package, this is not written in composer.lock
[toplevelPackage]
++ composerLock.packages
++ (l.optionals (!noDev) (composerLock.packages-dev or []));
# packages with replace/provide applied
resolvedPackages = let
apply = pkg: dep: candidates: let
original = getRequire pkg;
applied =
l.filterAttrs (
name: semver:
!((candidates ? "${name}") && (multiSatisfies candidates."${name}" semver))
)
original;
in
pkg
// {
require =
applied
// (
l.optionalAttrs
(applied != original)
{"${dep.name}" = "${dep.version}";}
);
};
dropMissing = pkgs: let
doDropMissing = pkg:
pkg
// {
require =
l.filterAttrs
(name: semver: l.any (pkg: (pkg.name == name) && (satisfies pkg.version semver)) pkgs)
(getRequire pkg);
};
in
map doDropMissing pkgs;
doReplace = pkg:
l.foldl
(pkg: dep: apply pkg dep (getProvide dep))
pkg
packages;
doProvide = pkg:
l.foldl
(pkg: dep: apply pkg dep (getReplace dep))
pkg
packages;
in
dropMissing (map (pkg: (doProvide (doReplace pkg))) packages);
# resolve semvers into exact versions
pinPackages = pkgs: let
clean = requires:
l.filterAttrs
(name: _:
!(l.elem name ["php" "composer-plugin-api" "composer-runtime-api"])
&& !(l.strings.hasPrefix "ext-" name))
requires;
doPin = name: semver:
(l.head
(l.filter (dep: satisfies dep.version semver)
(l.filter (dep: dep.name == name)
resolvedPackages)))
.version;
doPins = pkg:
pkg
// {
require = l.mapAttrs doPin (clean pkg.require);
};
in
map doPins pkgs;
in
dlib.simpleTranslate2.translate
({objectsByKey, ...}: rec {
translatorName = name;
# relative path of the project within the source tree.
location = project.relPath;
# the name of the subsystem
subsystemName = "php";
# Extract subsystem specific attributes.
# The structure of this should be defined in:
# ./src/specifications/{subsystem}
subsystemAttrs = {
inherit noDev;
inherit phpSemver phpExtensions;
inherit composerPluginApiSemver;
};
# name of the default package
defaultPackage = toplevelPackage.name;
/*
List the package candidates which should be exposed to the user.
Only top-level packages should be listed here.
Users will not be interested in all individual dependencies.
*/
exportedPackages = {
"${defaultPackage}" = toplevelPackage.version;
};
/*
a list of raw package objects
If the upstream format is a deep attrset, this list should contain
a flattened representation of all entries.
*/
serializedRawObjects = pinPackages resolvedPackages;
/*
Define extractor functions which each extract one property from
a given raw object.
(Each rawObj comes from serializedRawObjects).
Extractors can access the fields extracted by other extractors
by accessing finalObj.
*/
extractors = {
name = rawObj: finalObj:
rawObj.name;
version = rawObj: finalObj:
rawObj.version;
dependencies = rawObj: finalObj:
l.attrsets.mapAttrsToList
(name: version: {inherit name version;})
(getRequire rawObj);
sourceSpec = rawObj: finalObj:
if rawObj ? "source" && rawObj.source.type == "path"
then {
inherit (rawObj.source) type path;
rootName = finalObj.name;
rootVersion = finalObj.version;
}
else if rawObj ? "source" && rawObj.source.type == "git"
then {
inherit (rawObj.source) type url;
rev = rawObj.source.reference;
submodules = false;
}
else if rawObj ? "dist" && rawObj.dist.type == "path"
then {
inherit (rawObj.dist) type;
path = rawObj.dist.url;
rootName = null;
rootVersion = null;
}
else
l.abort ''
Cannot find source for ${finalObj.name}@${finalObj.version},
rawObj: ${l.toJSON rawObj}
'';
};
/*
Optionally define extra extractors which will be used to key all
final objects, so objects can be accessed via:
`objectsByKey.${keyName}.${value}`
*/
keys = {
};
/*
Optionally add extra objects (list of `finalObj`) to be added to
the dream-lock.
*/
extraObjects = [
];
});
# If the translator requires additional arguments, specify them here.
# Users will be able to set these arguments via `settings`.
# There are only two types of arguments:
# - string argument (type = "argument")
# - boolean flag (type = "flag")
# String arguments contain a default value and examples. Flags do not.
# Flags are false by default.
extraArgs = {
noDev = {
description = "Exclude development dependencies";
type = "flag";
};
};
}