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
349
350
351
352
353
354
355
356
357
358
359
360
|
--!The Make-like Build Utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
--
-- @author ruki
-- @file package.lua
--
-- imports
import("core.base.semver")
import("core.base.option")
import("core.base.global")
import("core.project.cache")
import("core.project.project")
import("core.package.package", {alias = "core_package"})
import("devel.git")
import("net.fasturl")
import("repository")
--
-- parse require string
--
-- add_requires("tboox.tbox >=1.5.1", "zlib >=1.2.11")
-- add_requires("zlib master")
-- add_requires("[email protected] >=1.5.1")
-- add_requires("https://github.com/tboox/[email protected] >=1.5.1")
-- add_requires("tboox.tbox >=1.5.1 <1.6.0 optional")
--
function _parse_require(require_str)
-- get it from cache first
local requires = _g._REQUIRES or {}
local required = requires[require_str]
if required then
return required.packagename, required.requireinfo
end
-- split package and version info
local splitinfo = require_str:split('%s+')
assert(splitinfo and #splitinfo > 0, "require(\"%s\"): invalid!", require_str)
-- get package info
local packageinfo = splitinfo[1]
-- get mode at last position
--
-- .e.g
--
-- must
-- optional
--
local mode = "must"
if #splitinfo > 1 then
-- get mode
local modes = {must = true, optional = true}
local value = splitinfo[#splitinfo]:lower()
if modes[value] then
mode = value
table.remove(splitinfo)
end
end
-- get version
--
-- .e.g
--
-- >=1.5.1 <1.6.0
-- master || >1.4
-- ~1.2.3
-- ^1.1
--
local version = "master"
if #splitinfo > 1 then
version = table.concat(table.slice(splitinfo, 2), " ")
end
assert(version, "require(\"%s\"): unknown version!", require_str)
-- get repository name, package name and package url
local reponame = nil
local packageurl = nil
local packagename = nil
local pos = packageinfo:find_last('@', true)
if pos then
-- get package name
packagename = packageinfo:sub(pos + 1)
-- get reponame or packageurl
local repo_or_pkgurl = packageinfo:sub(1, pos - 1)
-- is package url?
if repo_or_pkgurl:find('[/\\]') then
packageurl = repo_or_pkgurl
else
reponame = repo_or_pkgurl
end
else
packagename = packageinfo
end
-- check package name
assert(packagename, "require(\"%s\"): the package name not found!", require_str)
-- init required item
local required = {}
required.packagename = packagename
required.requireinfo = {reponame = reponame, packageurl = packageurl, version = version, mode = mode}
-- save this required item to cache
requires[require_str] = required
_g._REQUIRES = requires
-- ok
return required.packagename, required.requireinfo
end
-- load package instance from the given package url
function _load_package_from_url(packagename, packageurl)
-- load it
return core_package.load_from_url(packagename, packageurl)
end
-- load package instance from project
function _load_package_from_project(packagename)
-- load it
return core_package.load_from_project(packagename)
end
-- load package instance from repositories
function _load_package_from_repository(packagename, reponame)
-- get package directory from the given package name
local packagedir, is_global = repository.packagedir(packagename, reponame)
if packagedir then
-- load it
return core_package.load_from_repository(packagename, is_global, packagedir)
end
end
-- load required packages
function _load_package(packagename, requireinfo)
-- attempt to get it from cache first
local packages = _g._PACKAGES or {}
local instance = packages[packagename]
if instance then
-- satisfy required version?
if not semver.satisfies(instance:version(), requireinfo.version) then
raise("package(%s): version conflict, '%s' does not satisfy '%s'!", packagename, instance:version(), requireinfo.version)
end
-- ok
return instance
end
-- load package instance
instance = nil
if requireinfo.packageurl then
-- load package from the given package url
instance = _load_package_from_url(packagename, requireinfo.packageurl)
else
-- load package from project first
instance = _load_package_from_project(packagename)
if not instance then
-- load package from repositories
instance = _load_package_from_repository(packagename, requireinfo.reponame)
end
end
-- check
assert(instance, "package(%s) not found!", packagename)
-- save require info to package
instance:requireinfo_set(requireinfo)
-- save this package instance to cache
packages[packagename] = instance
_g._PACKAGES = packages
-- ok
return instance
end
-- load all required packages
function _load_packages(requires)
-- no requires?
if not requires or #requires == 0 then
return {}
end
-- load packages
local packages = {}
for packagename, requireinfo in pairs(load_requires(requires)) do
-- load package instance
local package = _load_package(packagename, requireinfo)
-- load required packages and save them first of this package
requires = package:get("requires")
if requires then
table.join2(packages, _load_packages(requires))
end
-- save this package instance
table.insert(packages, package)
end
-- ok?
return packages
end
-- load all git refs from packages
function _load_packages_gitrefs(packages)
-- enter cache scope
cache.enter("local.require")
-- load cache
local gitrefs = nil
if option.get("force") then
gitrefs = {}
else
gitrefs = cache.get("gitrefs") or {}
end
-- run tasks
local results = {}
process.runjobs(function (index)
local package = packages[index]
if package then
-- attempt to get refs from cache first
local refs = gitrefs[package:name()]
if refs then
results[package:name()] = {tags = refs.tags, branches = refs.branches}
else
-- attempt to get refs from the git url
local tags = {}
local branches = {}
for _, url in ipairs(package:urls()) do
if git.checkurl(url) then
-- fetch refs
tags, branches = git.refs(url)
-- save result
results[package:name()] = {tags = tags, branches = branches}
-- cache result
gitrefs[package:name()] = {tags = tags, branches = branches}
break
end
end
end
end
end, #packages)
-- save cache
cache.set("gitrefs", gitrefs)
cache.flush()
-- ok?
return results
end
-- select package version
function _select_package_version(package, required_ver, gitrefs)
-- get versions
local versions = package:get("versions")
-- attempt to get tags and branches from the git url
local refs = {}
if gitrefs then
refs = gitrefs[package:name()] or {}
end
-- select required version
return semver.select(required_ver, versions, refs.tags, refs.branches)
end
-- the cache directory
function cachedir()
return path.join(global.directory(), "cache", "packages")
end
-- load requires
function load_requires(requires)
-- parse requires
local requireinfos = {}
for _, require_str in ipairs(requires) do
-- parse require info
local packagename, requireinfo = _parse_require(require_str)
-- save this required package
requireinfos[packagename] = requireinfo
end
-- ok
return requireinfos
end
-- load all required packages
function load_packages(requires)
-- laod all required packages recursively
local packages = _load_packages(requires)
-- add all urls to fasturl and prepare to sort them together
for _, package in ipairs(packages) do
fasturl.add(package:urls())
end
-- load git refs from packages
local gitrefs = _load_packages_gitrefs(packages)
-- sort and update urls
for _, package in ipairs(packages) do
-- sort package urls
package:urls_set(fasturl.sort(package:urls()))
-- exists urls? otherwise be phony package (only as package group)
if #package:urls() > 0 then
-- select package version
local version, source = _select_package_version(package, package:requireinfo().version, gitrefs)
-- save version to package
package:version_set(version, source)
end
end
-- ok
return packages
end
|