-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugins.mk
More file actions
335 lines (319 loc) · 9.28 KB
/
plugins.mk
File metadata and controls
335 lines (319 loc) · 9.28 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
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
# HEX_URL ?= https://s3.amazonaws.com/s3.hex.pm
# HEX_URL ?= http://s3.hex.pm.global.prod.fastly.net
HEX_URL ?= https://hexpmrepo.global.ssl.fastly.net
HEX_REGISTRY ?= registry.ets.gz
HEX_PATH ?= $(HOME)/.hex
HEX_REGISTRY_FILE = $(HEX_PATH)/$(HEX_REGISTRY)
HEX_REGISTRY_URL = $(HEX_URL)/$(HEX_REGISTRY)
ifeq ($(shell which gunzip 2>/dev/null | wc -l), 1)
define hex_gunzip
gunzip -c $(2) > $(1)
endef
else
define hex_gunzip.erl
{ok, Data} = file:read_file("$(1)"),
Unzipped = zlib:gunzip(Data),
ok = file:write_file("$(2)", Unzipped),
halt().
endef
define hex_gunzip
$(call erlang,$(call hex_gunzip.erl,$(call core_native_path,$1),$(call core_native_path,$2)))
endef
endif
hex-update: hex-clean $(basename $(HEX_REGISTRY_FILE))
hex-clean:
$(verbose) rm -f $(HEX_REGISTRY_FILE)
$(verbose) rm -f $(basename $(HEX_REGISTRY_FILE))
define hex_search.erl
{ok, R} = ets:file2tab("$(basename $(HEX_REGISTRY_FILE))"),
case ets:lookup(R, <<"$(1)">>) of
[{_,[Releases]}] ->
Vsns = lists:map(fun erlang:binary_to_list/1, Releases),
io:format("~s: ~p~n", ["$(1)", Vsns]);
_ ->
io:format("~n!! Package $(1) not found. Try to update the package index.~n")
end,
halt().
endef
define hex_search
$(verbose) $(call erlang,$(call hex_search.erl,$(1)))
endef
ifdef p
hex-search: $(basename $(HEX_REGISTRY_FILE))
$(call hex_search,$(p))
else
hex-search:
$(verbose) printf "%s\n" "" \
"ERR Missing package name"
endif
$(basename $(HEX_REGISTRY_FILE)):
$(verbose) mkdir -p $(HEX_PATH)
$(verbose) mkdir -p $(HEX_PATH)/packages
$(verbose) $(call core_http_get,$(HEX_REGISTRY_FILE),$(HEX_REGISTRY_URL))
$(gen_verbose) $(call hex_gunzip,$(basename $(HEX_REGISTRY_FILE)),$(HEX_REGISTRY_FILE))
define dep_fetch_hexpm.erl
FunToInteger = fun
([]) -> nil;
(X) -> list_to_integer(X)
end,
FunPrefixToAtom = fun
("") -> pre;
(P) -> list_to_atom(P)
end,
FunBuildPre = fun(Prefix, Value, Sep) ->
Pre = FunPrefixToAtom(Prefix),
case lists:member(Pre, [alpha, a, beta, b, pre, rc]) of
true -> {Pre, Value};
false -> {pre, Prefix ++ Sep ++ Value}
end
end,
FunParsePre = fun
([]) ->
nil;
(Pre) ->
Re = "(?<pre>-[a-z]+)?(?<sep>[\\.|-])?(?<num>[0-9A-Za-z-\\.]+)?",
case re:run(Pre, Re, [{capture, [pre, sep, num], list}]) of
{match, [[\$$$$-|Prefix], Sep, Value]} -> FunBuildPre(Prefix, Value, Sep);
{match, [[], _, Value]} -> {pre, Value};
nomatch -> {pre, Pre}
end
end,
FunParseBuild = fun
([]) -> nil;
([\$$$$+|Rest]) -> Rest
end,
FunParse = fun(Version) ->
Version1 = string:strip(Version, both, 32),
Re = "^v?(?<version>(?<major>\\\\d+)\\.?(?<minor>\\\\d+)?\\.?(?<patchlevel>\\\\d+)?)(?<pre>-[0-9A-Za-z-\\.]+)?(?<build>\\\\+[0-9A-Za-z-\\.]+)?\\\$$$$",
case re:run(Version1, Re, [{capture, [major, minor, patchlevel, version, pre, build], list}]) of
{match, [X0, Y0, Z0, V, Pre, Build]} ->
{ok, #{major => FunToInteger(X0),
minor => FunToInteger(Y0),
patch => FunToInteger(Z0),
d => length([E||E<-[X0, Y0, Z0], E =/= ""]),
v => V,
pre => FunParsePre(Pre),
build => FunParseBuild(Build)}};
nomatch ->
{error, invalid_version}
end
end,
FunTryToInteger = fun(V, E) ->
V1 = try list_to_integer(V)
catch
_:_ -> V
end,
E1 = try list_to_integer(E)
catch
_:_ -> E
end,
if
is_integer(V1) andalso is_integer(E1) -> {V1, E1};
true -> {V, E}
end
end,
FunCompareV = fun
(nil, nil) -> 0;
(nil, _) -> -1;
(_, nil) -> 1;
(V, E) ->
{V1, E1} = FunTryToInteger(V, E),
if
V1 > E1 -> 1;
V1 < E1 -> -1;
true -> 0
end
end,
FunCompareP = fun
(nil, nil) -> 0;
(nil, _) -> 1;
({P, V}, {P, E}) ->
FunCompareV(V, E);
({a, V}, {alpha, E}) ->
FunCompareV(V, E);
({alpha, V}, {a, E}) ->
FunCompareV(V, E);
({b, V}, {beta, E}) ->
FunCompareV(V, E);
({beta, V}, {b, E}) ->
FunCompareV(V, E);
({b, _}, {a, _}) -> 1;
({b, _}, {alpha, _}) -> 1;
({beta, _}, {a, _}) -> 1;
({beta, _}, {alpha, _}) -> 1;
({pre, _}, {a, _}) -> 1;
({pre, _}, {alpha, _}) -> 1;
({pre, _}, {b, _}) -> 1;
({pre, _}, {beta, _}) -> 1;
({rc, _}, {a, _}) -> 1;
({rc, _}, {alpha, _}) -> 1;
({rc, _}, {b, _}) -> 1;
({rc, _}, {beta, _}) -> 1;
({rc, _}, {pre, _}) -> 1;
(_, _) -> -1
end,
FunCompare = fun(Version, Expected) ->
case FunParse(Version) of
{ok, #{v := VVersion,
pre := VPre,
build := VBuild}} ->
case FunParse(Expected) of
{ok, #{v := EVersion,
pre := EPre,
build := EBuild}} ->
case {FunCompareV(VVersion, EVersion),
FunCompareP(VPre, EPre),
FunCompareV(VBuild, EBuild)} of
{0, 0, 0} -> 0;
{1, _, _} -> 1;
{0, 1, _} -> 1;
{0, 0, 1} -> 1;
_ -> -1
end;
_ -> {error, invalid_version_2}
end;
_ -> {error, invalid_version_1}
end
end,
FunMaxVersion = fun(V1, V2) ->
case FunCompare(V1, V2) of
-1 -> V2;
0 -> V1;
1 -> V1;
E -> E
end
end,
FunSup = fun(Version, Expected) ->
FunCompare(Version, Expected) =:= 1
end,
FunInf = fun(Version, Expected) ->
FunCompare(Version, Expected) =:= -1
end,
FunEqual = fun(Version, Expected) ->
FunCompare(Version, Expected) =:= 0
end,
FunBump = fun(Type, Version) ->
case FunParse(Version) of
{ok, #{major := Major,
minor := Minor,
patch := Patch,
v := V,
pre := Pre,
build := _,
d := _}} ->
case Type of
major ->
{ok, lists:flatten(io_lib:format("~w.0.0", [Major + 1]))};
minor ->
{ok, lists:flatten(io_lib:format("~w.~w.0", [Major, Minor + 1]))};
_ ->
case Pre of
nil ->
{ok, lists:flatten(io_lib:format("~w.~w.~w", [Major, Minor, Patch + 1]))};
_ ->
{ok, V}
end
end;
E -> E
end
end,
FunTild = fun(Version, Expected) ->
case FunCompare(Version, Expected) of
0 -> true;
-1 -> false;
1 ->
{ok, #{d := D}} = FunParse(Expected),
{ok, MaxExpected} = if
D =:= 3 ->
FunBump(minor, Expected);
true ->
FunBump(major, Expected)
end,
case FunCompare(Version, MaxExpected) of
-1 -> true;
_ -> false
end
end
end,
FunMatch = fun(V, E) ->
Fun = fun
(_, Version, [\$$$$>, \$$$$=|Expected]) ->
FunSup(Version, Expected) orelse FunEqual(Version, Expected);
(_, Version, [\$$$$<, \$$$$=|Expected]) ->
FunInf(Version, Expected) orelse FunEqual(Version, Expected);
(_, Version, [\$$$$=, \$$$$=|Expected]) ->
FunEqual(Version, Expected);
(_, Version, [\$$$$>|Expected]) ->
FunSup(Version, Expected);
(_, Version, [\$$$$<|Expected]) ->
FunInf(Version, Expected);
(_, Version, [\$$$$~, \$$$$>|Expected]) ->
FunTild(Version, Expected);
(F, Version, [32|Expected]) ->
F(F, Version, Expected);
(_, Version, Expected) ->
FunEqual(Version, Expected)
end,
Fun(Fun, V, E)
end,
FunExpected = fun(Versions, Expected, Fun) ->
lists:foldl(fun(V, M) ->
case {M, FunMatch(V, Expected)} of
{nil, true} -> V;
{_, true} -> Fun(V, M);
_ -> M
end
end, nil, Versions)
end,
ssl:start(),
inets:start(),
case filelib:is_file("$(HEX_REGISTRY_FILE)") of
true -> ok;
false ->
ok = filelib:ensure_dir("$(HEX_REGISTRY_FILE)"),
{ok, {{_, 200, _}, _, IdxBody}} = httpc:request(get, {"$(HEX_REGISTRY_URL)", []},
[], [{body_format, binary}]),
ok = file:write_file("$(HEX_REGISTRY_FILE)", IdxBody),
{ok, Data} = file:read_file("$(HEX_REGISTRY_FILE)"),
Unzipped = zlib:gunzip(Data),
ok = file:write_file("$(basename $(HEX_REGISTRY_FILE))", Unzipped)
end,
{ok, R} = ets:file2tab("$(basename $(HEX_REGISTRY_FILE))"),
[{_,[Releases]}] = ets:lookup(R, <<"$(1)">>),
Vsns = lists:map(fun erlang:binary_to_list/1, Releases),
Vsn = FunExpected(Vsns, "$(2)", FunMaxVersion),
PackageFile = "$(HEX_PATH)/packages/$(1)-" ++ Vsn ++ ".tar",
ok = filelib:ensure_dir(PackageFile),
case filelib:is_file(PackageFile) of
true -> ok;
false ->
PackageURL = "$(HEX_URL)/tarballs/$(1)-" ++ Vsn ++ ".tar",
{ok, {{_, 200, _}, _, PkgBody}} = httpc:request(get, {PackageURL, []}, [], [{body_format, binary}]),
ok = file:write_file(PackageFile, PkgBody)
end,
{ok, Files} = erl_tar:extract(PackageFile, [memory]),
{_, Source} = lists:keyfind("contents.tar.gz", 1, Files),
ok = erl_tar:extract({binary, Source}, [{cwd, "$(call core_native_path,$(DEPS_DIR)/$1)"}, compressed]),
halt().
endef
define dep_fetch_hexpm
$(call erlang,$(call dep_fetch_hexpm.erl,$(1),$(strip $(wordlist 2,$(words $(dep_$(1))),$(dep_$(1))))));
endef
define dep_fetch_hex
$(call erlang,$(call dep_fetch_hexpm.erl,$(1),$(strip $(wordlist 2,$(words $(dep_$(1))),$(dep_$(1))))));
endef
define dep_target_for_hex
ifeq ($(word 1,$(dep_$(1))),hex)
$(call dep_target,$(1))
endif
ifeq ($(word 1,$(dep_$(1))),hexpm)
$(call dep_target,$(1))
endif
endef
$(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target_for_hex,$(dep))))
## Help
help::
$(verbose) printf "%s\n" "" \
"Hex.pm targets:" \
" hex-update Updates the package index." \
" hex-search p=... Search a package in the package index."