This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
install.go
144 lines (135 loc) · 4.26 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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
)
func (u *utility) install() {
if u.flags.allOS {
for _, os := range []string{"linux", "darwin", "windows"} {
u.copyFiles(os)
}
return
}
u.copyFiles(u.flags.goos)
}
func (u *utility) copyFiles(in string) {
if u.flags.dryRun {
if u.vendorDir != "" {
log.Printf("Copy %s SDK to path: %s\n", in, u.vendorDir)
}
log.Printf("Copy %s binaries to path: %s\n", in, u.userDir)
return
}
if _, err := os.Stat(filepath.Join(u.packageDir, "SDK")); os.IsNotExist(err) {
log.Fatalf("\nNo SDK detected. Please download the Ultralight SDK using this utility, or manually from here:\n\n\thttps://github.com/ultralight-ux/Ultralight/releases\n\nthen extract it into the following folder:\n\n\t%v\n\nExample structure:\n\ngo-ultralight\n |-SDK\n |-bin\n |-include\n\n", u.packageDir)
return
}
if _, err := os.Stat(filepath.Join(u.packageDir, "SDK", "bin")); os.IsNotExist(err) {
log.Fatalf("\nIncorrect SDK file structure detected! Ensure the binary files can be found in:\n\n\t%v\n\nExample structure:\n\ngo-ultralight\n |-SDK\n |-bin\n |-include\n\n", filepath.Join(u.packageDir, "bin"))
}
if u.vendorDir != "" {
u.copySDK(in)
}
u.copyBinaries(in)
}
func (u *utility) copySDK(in string) {
log.Printf("Copying %s SDK to vendor folder...\n", in)
filepath.Walk(filepath.Join(u.packageDir, "SDK", "bin"), func(path string, info os.FileInfo, err error) error {
relPath, err := filepath.Rel(filepath.Join(u.packageDir, "bin"), path)
if err != nil {
log.Fatalf("Failed to get relative path: %v", err)
return err
}
if !info.IsDir() {
switch in {
case "darwin":
if filepath.Ext(path) == ".dylib" {
copy(path, filepath.Join(u.vendorDir, "bin", filepath.Dir(relPath)))
}
case "linux":
if filepath.Ext(path) == ".so" {
copy(path, filepath.Join(u.vendorDir, "bin", filepath.Dir(relPath)))
}
case "windows":
if filepath.Ext(path) == ".dll" {
copy(path, filepath.Join(u.vendorDir, "bin", filepath.Dir(relPath)))
}
}
}
return nil
})
filepath.Walk(filepath.Join(u.packageDir, "SDK", "include"), func(path string, info os.FileInfo, err error) error {
relPath, err := filepath.Rel(filepath.Join(u.packageDir, "include"), path)
if err != nil {
log.Fatalf("Failed to get relative path: %v", err)
return err
}
if info.IsDir() {
os.MkdirAll(filepath.Join(u.vendorDir, "include", relPath), 0777)
} else {
copy(path, filepath.Join(u.vendorDir, "include", filepath.Dir(relPath)))
}
return nil
})
if in == "windows" {
filepath.Walk(filepath.Join(u.packageDir, "SDK", "lib"), func(path string, info os.FileInfo, err error) error {
relPath, err := filepath.Rel(filepath.Join(u.packageDir, "lib"), path)
if err != nil {
log.Fatalf("Failed to get relative path: %v", err)
return err
}
if info.IsDir() {
os.MkdirAll(filepath.Join(u.vendorDir, "lib", relPath), 0777)
} else {
copy(path, filepath.Join(u.vendorDir, "lib", filepath.Dir(relPath)))
}
return nil
})
}
log.Println("SDK files copied!")
}
func (u *utility) copyBinaries(in string) error {
log.Printf("Copying %s binaries to project directory...\n", in)
filepath.Walk(filepath.Join(u.packageDir, "SDK", "bin"), func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatalf("Failed to get relative path: %v", err)
return err
}
if !info.IsDir() {
switch in {
case "darwin":
if filepath.Ext(path) == ".dylib" {
copy(path, u.userDir)
}
case "linux":
if filepath.Ext(path) == ".so" {
copy(path, u.userDir)
}
case "windows":
if filepath.Ext(path) == ".dll" {
copy(path, u.userDir)
}
}
}
return nil
})
log.Println("Binary files copied!")
return nil
}
func copy(srcFile, destDir string) {
binData, err := ioutil.ReadFile(srcFile)
if err != nil {
log.Fatalf("Failed to read file %s: %v", filepath.Base(srcFile), err)
}
if _, err := os.Stat(destDir); err != nil {
if err := os.MkdirAll(destDir, 0777); err != nil {
log.Fatalf("Failed to make directory %s to copy files to: %v", destDir, err)
}
}
err = ioutil.WriteFile(filepath.Join(destDir, filepath.Base(srcFile)), binData, 0777)
if err != nil {
log.Fatalf("Failed to create file %s: %v", filepath.Base(srcFile), err)
}
}