/
builder.go
205 lines (178 loc) · 5.99 KB
/
builder.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
package builder
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/siscia/effe-tool/commons"
"github.com/siscia/effe-tool/sources"
"os"
"os/exec"
"path/filepath"
)
func createFilenameExecutable(name, version string) string {
return name + "_v" + version
}
// compileSingleFile compile an effe to a single binary.
// It start by creating a temporany directory where it moves
// the logic of the effe, and the core.
// Then it adds the temporany dir just created to the GOPATH
// Finally it invoke the go tool to actually compile the file,
// it redirects the Stdout and the Stderr so that the user can
// actually see compilation errors.
// It returns the path where the executable is been created
func compileSingleFile(sourcePath string, cgoEnabled bool) (string, error) {
// Creating temporany directory and structure
dir := os.TempDir() + "/effebuild-" + commons.RandomSuffix()
if err := os.Mkdir(dir, 0777); err != nil {
fmt.Println(err)
return "", err
}
dirEffe := dir + "/src/effe"
if err := os.MkdirAll(dirEffe, 0777); err != nil {
fmt.Println(err)
return "", err
}
if err := os.Mkdir(dirEffe+"/logic", 0777); err != nil {
fmt.Println(err)
return "", err
}
if err := os.Link(sourcePath, dirEffe+"/logic/logic.go"); err != nil {
fmt.Println(err)
return "", err
}
if err := commons.NewFile(dirEffe+"/effe.go", sources.Core); err != nil {
fmt.Println("Impossible to create file, exit.")
fmt.Println(err)
return "", err
}
// adding temporany dir to the GOPATH
gopath := os.Getenv("GOPATH")
os.Setenv("GOPATH", dir+":"+gopath)
defer os.Setenv("GOPATH", gopath)
if cgoEnabled == false {
cgoEnabled := os.Getenv("CGO_ENABLED")
os.Setenv("CGO_ENABLED", "0")
defer os.Setenv("CGO_ENABLED", cgoEnabled)
}
// actually compile
cmd := exec.Command("go", "build", "-a", "-ldflags", "'-s'", "-o", dir+"/out", "-buildmode=exe", dirEffe+"/effe.go")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
fmt.Println(err)
return "", err
}
if err := cmd.Wait(); err != nil {
fmt.Println(err)
return "", err
}
return dir + "/out", nil
}
// compileFile is the entry point to compile an effe source.
// The actual compilation is done by `compileSingleFile` but
// `compileFile` takes care of move the binary where the user
// is expecting.
// It first compile the file (passed as path).
// It execute the just compiled file to gather information
// about name and version.
// Finally it moves the executable.
//
// `path` is where the effe source is located.
// `dirName` rappresent in which directory save the executable,
// it has a default value set on the flag to `out`.
// `execName` is the name of the executable, if not given
// `compileFile` try to use the effe convetion to provide a name.
func compileFile(path, dirName, execName string, cgoEnabled bool) error {
// Actually compiling
tmpExecPath, err := compileSingleFile(path, cgoEnabled)
if err != nil {
fmt.Println("File: " + path + " | Impossible to compile.")
return err
}
// Gathering information
execVersion := ""
if execName == "" {
// the user want didn't provide a name for the executable
// we need to come out with a name
execName, execVersion, err = commons.GetNameVersion(tmpExecPath)
if err == nil {
// everything went well and the names comes from the info variable
execName = createFilenameExecutable(execName, execVersion)
} else {
// the info variable doesn't provide the right information
// we will use the hash of the executable as name
fmt.Println("File: " + path + " | Error in the executable info.")
fmt.Println("File: " + path + " | Falling back to use the hash as name.")
hashName, err := commons.ExecutableHash(tmpExecPath)
if err != nil {
// problem generating the hash, we don't move the executable
fmt.Println("File: " + path + " | Error in generating the hash name.")
fmt.Println("File: " + path + " | Actual path is: " + tmpExecPath)
return err
}
execName = hashName
}
}
// Moving the file
totalPath, err := filepath.Abs(dirName + `/` + execName)
if err != nil {
fmt.Println("File: " + path + " | Error in getting the absolute path.\nActual path is: " + tmpExecPath)
return err
}
if err := os.MkdirAll(filepath.Dir(totalPath), 0777); err != nil {
fmt.Println("File: " + path + " | Impossible to create the directory: " + filepath.Dir(totalPath))
fmt.Println("Actual path is: " + tmpExecPath)
return err
}
if err := os.Rename(tmpExecPath, totalPath); err != nil {
fmt.Println(err)
fmt.Println("File: " + path + " | Impossible to move the executable.\nActual path is: " + tmpExecPath)
}
fmt.Println("File: " + path + " | Everything went good, the file is been compiled.\nExecutable path: " + totalPath)
return nil
}
// compileDirectory simply walks the filesystem and
// try to compile every file it find.
// The real job is done by `walkAndCompile`
// walkAndCompile simply does nothing to the directory.
// walkAndCompile preserve the shape of the source dir
// into the executable directory.
func compileDirectory(originalPath string, c *cli.Context) {
walkAndCompile := func(path string, f os.FileInfo, _ error) error {
if f.IsDir() {
return nil
}
if f.Mode().IsRegular() {
fmt.Println()
relativePath, err := filepath.Rel(originalPath, path)
if err != nil {
fmt.Println("File: " + path + " | Error with the relative path.")
return nil
}
execLocation := filepath.Dir(relativePath)
err = compileFile(path, c.String("dirout")+"/"+execLocation, "", c.Bool("cgo"))
if err != nil {
fmt.Println(err)
}
}
return nil
}
filepath.Walk(originalPath, walkAndCompile)
}
// Compile is the main entry point
func Compile(c *cli.Context) {
path := c.Args().First()
f, err := os.Lstat(path)
if err != nil {
fmt.Println("Impossible to open the file, are you sure it exist ?")
return
}
if f.IsDir() {
compileDirectory(path, c)
}
if f.Mode().IsRegular() {
err := compileFile(path, c.String("dirout"), c.String("out"), c.Bool("cgo"))
if err != nil {
fmt.Println(err)
}
}
}