-
Notifications
You must be signed in to change notification settings - Fork 8
/
install.go
450 lines (400 loc) · 10.3 KB
/
install.go
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)
func postInstallLuaRocks(instdir string) error {
printf("chdir %s", instdir)
if err := os.Chdir(instdir); err != nil {
return err
}
printf("create lua_modules directory")
bin := fmt.Sprintf("%s/bin/luarocks", instdir)
var buf bytes.Buffer
if err := DoExecEx(bin, &buf, os.Stderr, "path"); err != nil {
return err
}
envs := map[string]MapStringSet{}
for _, line := range strings.Split(buf.String(), "\n") {
arr := strings.SplitN(line, "='", 2)
if len(arr) != 2 {
continue
}
switch arr[0] {
case "export LUA_PATH", "export LUA_CPATH":
paths := MapStringSet{}
name := strings.TrimPrefix(arr[0], "export ")
path := strings.TrimSuffix(arr[1], "'")
if name == "LUA_PATH" {
name = "lua_modules/lualib"
} else if name == "LUA_CPATH" {
name = "lua_modules/luaclib"
}
for _, v := range strings.Split(path, ";") {
// use the path under installation directory
if strings.HasPrefix(v, instdir) {
kv := strings.SplitN(v, "/?", 2)
if len(kv) != 2 || !filepath.IsAbs(kv[0]) {
continue
} else if err := mkdir(kv[0]); err != nil {
return err
}
kv[0] = strings.TrimPrefix(kv[0], instdir)
kv[1] = "/?" + kv[1]
for i, v := range kv {
kv[i] = filepath.Clean(v)
}
paths.Set(kv[0], kv[1])
}
}
if len(paths) > 0 {
envs[name] = paths
}
default:
continue
}
}
// create symlink:
// ./lua_modules/bin -> ../bin
// ./lua_modules/lualib/* -> ../../<lua_path>
// ./lua_modules/luaclib/* -> ../../<lua_cpath>
printf("ln -s ./bin ./lua_moduels/bin")
if err := createSymlink("../bin", "./lua_modules/bin"); err != nil {
return err
}
for name, paths := range envs {
var n int
for k, v := range paths {
printf("ln -s .%s ./%s/%d | %#v", k, name, n, v.Value())
if err := createSymlink(
"../.."+k, fmt.Sprintf("./%s/%d", name, n),
); err != nil {
return err
}
n++
}
}
return nil
}
func postInstallLuaJit(instdir string) error {
sep := string(filepath.Separator)
for _, dir := range []string{
"bin", "include", "lib",
} {
wd := filepath.Join(instdir, dir)
printf("chdir %s", wd)
if err := os.Chdir(wd); err != nil {
return err
}
switch dir {
case "bin":
infos, err := os.ReadDir(".")
if err != nil {
return err
}
for _, info := range infos {
name := info.Name()
if strings.HasPrefix(name, "luajit") {
printf("ln -s %s lua", name)
if err = createSymlink(name, "lua"); err != nil {
return err
}
break
}
}
case "include":
if err := filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if info.Mode().IsRegular() {
arr := strings.SplitN(path, sep, 3)
if len(arr) == 2 {
newname := strings.Join(arr[1:], sep)
printf("ln -s %s %s", path, newname)
if err = createSymlink(path, newname); err != nil {
return err
}
}
}
return nil
}); err != nil {
return err
}
case "lib":
infos, err := os.ReadDir(".")
if err != nil {
return err
}
for _, info := range infos {
if info.Type().IsRegular() {
oldname := info.Name()
if ext := filepath.Ext(oldname); ext != "" {
newname := "liblua" + ext
printf("ln -s %s %s", oldname, newname)
if err = createSymlink(oldname, newname); err != nil {
return err
}
}
}
}
}
}
return nil
}
func untarGz(dir string, data io.Reader) (string, error) {
gz, err := gzip.NewReader(data)
if err != nil {
return "", err
}
rootdir := dir
checked := false
r := tar.NewReader(gz)
for {
h, err := r.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return "", err
}
printf("%s", h.Name)
switch h.Typeflag {
case tar.TypeReg, tar.TypeRegA:
if !checked {
checked = true
}
err = writeFile(filepath.Join(dir, h.Name), h.FileInfo().Mode().Perm(), r)
case tar.TypeSymlink:
if !checked {
checked = true
}
err = createSymlink(h.Linkname, filepath.Join(dir, h.Name))
case tar.TypeDir:
if !checked {
checked = true
rootdir = filepath.Join(dir, h.Name)
}
err = mkdir(filepath.Join(dir, h.Name))
default:
err = fmt.Errorf("unknown type flag %v of %q", h.Typeflag, h.Name)
}
if err != nil {
return "", err
}
}
return rootdir, nil
}
func installRocks(instdir string, cfg *TargetConfig) error {
luadir := filepath.Dir(cfg.RootDir)
if err := DoExec("./configure", "--help"); err != nil {
return err
}
opts := []string{
fmt.Sprintf("--prefix=%s", instdir),
fmt.Sprintf("--with-lua=%s", luadir),
// fmt.Sprintf("--with-lua-include=%s/include", luadir),
// fmt.Sprintf("--with-lua-lib=%s/lib", luadir),
}
printf("./configure %s", strings.Join(opts, " "))
if err := DoExec("./configure", opts...); err != nil {
return err
} else if err = DoExec("make"); err != nil {
return err
} else if err = DoExec("make", "install"); err != nil {
return err
}
printf("postflight...")
return postInstallLuaRocks(instdir)
}
func installLuaJit(instdir string, opts []string) error {
if runtime.GOOS == "darwin" {
// append MACOSX_DEPLOYMENT_TARGET=10.8 by default on macOS platform
found := false
for _, opt := range opts {
found = strings.HasPrefix(opt, "MACOSX_DEPLOYMENT_TARGET")
if found {
break
}
}
if !found {
opts = append(opts, "MACOSX_DEPLOYMENT_TARGET=10.8")
}
}
// clean up working directory
if err := DoExec("make", append([]string{"clean"}, opts...)...); err != nil {
return err
}
printf("make %s", strings.Join(opts, " "))
if err := DoExec("make", opts...); err != nil {
return err
}
printf("make install PREFIX=" + instdir)
if err := DoExec("make", "install", "PREFIX="+instdir); err != nil {
return err
}
// clean up working directory
if err := DoExec("make", append([]string{"clean"}, opts...)...); err != nil {
return err
} else if err := DoExec("git", "checkout", "."); err != nil {
return err
}
printf("postflight...")
return postInstallLuaJit(instdir)
}
func installLua(instdir string, opts []string) error {
printf("make %s", strings.Join(opts, " "))
if err := DoExec("make", opts...); err != nil {
return err
}
printf("make install INSTALL_TOP=" + instdir)
return DoExec("make", "install", "INSTALL_TOP="+instdir)
}
func openCachedFile(url string) (io.Reader, error) {
file := filepath.Join(SrcDir, filepath.Base(url))
// open cached file if exists
if f, err := openFile(file); err != nil {
return nil, err
} else if f != nil {
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
return nil, nil
}
func extractCachedFile(tmpdir, url string) (string, error) {
data, err := openCachedFile(url)
if err != nil {
return "", fmt.Errorf("open cached file error: %w", err)
} else if data == nil {
return "", nil
} else if dir, err := untarGz(tmpdir, data); err != nil {
return "", fmt.Errorf("uncompress the cached file error: %w", err)
} else {
return dir, nil
}
}
func download(url string) (io.Reader, error) {
file := filepath.Join(SrcDir, filepath.Base(url))
// download from url
rsp, err := http.Get(url)
if err != nil {
return nil, err
} else if rsp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get %q: %s", url, rsp.Status)
}
defer rsp.Body.Close()
// create cache file
f, err := createFile(file, 0)
if err != nil {
return nil, err
}
defer f.Close()
b, err := io.ReadAll(rsp.Body)
if err != nil {
os.Remove(file)
return nil, err
} else if _, err = f.Write(b); err != nil {
os.Remove(file)
return nil, err
}
return bytes.NewReader(b), nil
}
func extractDownloadedFile(tmpdir, url string) (string, error) {
data, err := download(url)
if err != nil {
return "", fmt.Errorf("download error: %w", err)
} else if dir, err := untarGz(tmpdir, data); err != nil {
return "", fmt.Errorf("uncompress the downloadeded file error: %w", err)
} else {
return dir, nil
}
}
func switchBranch(repodir, remote, branch string) error {
if err := os.Chdir(repodir); err != nil {
fatalf("failed to chdir(): %v", err)
}
defer os.Chdir(CWD)
if err := DoExec("git", "fetch", "--depth", "1", remote, branch); err != nil {
return err
} else if err = DoExec("git", "checkout", branch); err != nil {
return err
}
return DoExec("git", "checkout", ".")
}
func doInstall(cfg *TargetConfig, item *VerItem, opts []string) {
printf("install %q", item.Ver)
var dir string
if cfg.RepoDir != "" {
dir = cfg.RepoDir
if err := switchBranch(dir, item.Remote, item.Name); err != nil {
fatalf("failed to git checkout %s/%s: %v", item.Remote, item.Name, err)
}
} else {
url := cfg.DownloadURL + filepath.Clean(item.Name)
tmpdir, err := os.MkdirTemp(os.TempDir(), "lenv_tmp_")
if err != nil {
fatalf("failed to create tempdir: %v", err)
}
defer os.RemoveAll(tmpdir)
dir, err = extractCachedFile(tmpdir, url)
if dir != "" {
printf("use cached file")
} else {
if err != nil {
printf("failed to extract cached file: %v", err)
}
if dir, err = extractDownloadedFile(tmpdir, url); err != nil {
fatalf("failed to extract downloaded file: %v", err)
}
}
}
instdir := filepath.Join(cfg.RootDir, item.Ver)
printf("remove old directory: %s", instdir)
if err := os.RemoveAll(instdir); err != nil {
fatalf("failed to os.RemoveAll(): %v", err)
}
printf("chdir %q", dir)
if err := os.Chdir(dir); err != nil {
fatalf("failed to chdir(): %v", err)
}
var err error
switch cfg.Name {
case "lua":
err = installLua(instdir, opts)
case "luajit":
err = installLuaJit(instdir, opts)
case "luarocks":
err = installRocks(instdir, cfg)
default:
fatalf("unsupported target name %q", cfg.Name)
}
if err != nil {
fatalf("failed to install %s version %s: %v", cfg.Name, item.Ver, err)
}
printf("\n%s version %s (%q) has been installed.", cfg.Name, item.Ver, instdir)
// automatically use the installed version
UseInstalledVersion(cfg, item.Ver)
}
func CmdInstall(opts []string) {
target := PickTargetVersion(opts[0], false)
if target.Lua != nil {
doInstall(target.Lua.Config, target.Lua.Version, opts[1:])
}
if target.LuaRocks != nil {
ResolveCurrentDir()
CheckLuaRocksRootDir()
doInstall(target.LuaRocks.Config, target.LuaRocks.Version, opts[1:])
}
}