5
5
"os"
6
6
"os/exec"
7
7
"path/filepath"
8
- "strings"
9
8
10
9
"github.com/cmepw/221b/logger"
11
10
"github.com/cmepw/221b/templates"
@@ -19,69 +18,80 @@ type Loader interface {
19
18
type baseLoader struct {}
20
19
21
20
const (
22
- windowsExt = ".exe "
23
- tmpFile = "tmp.go "
21
+ tmpFile = "tmp.go "
22
+ tmpDir = "/ tmp/221b-compile "
24
23
)
25
24
26
- func (b baseLoader ) Compile (path string , content []byte ) error {
27
- outputPath := strings .TrimSuffix (filepath .Base (path ), filepath .Ext (path )) + windowsExt
28
-
29
- dir := "/tmp/test"
30
- if err := os .MkdirAll (dir , 0750 ); err != nil {
31
- logger .Error (fmt .Errorf ("could not create temporary directory" ))
25
+ func (b baseLoader ) Compile (outputPath string , content []byte ) error {
26
+ if err := b .setupTmpDir (content ); err != nil {
32
27
return err
33
28
}
34
-
35
29
defer func () {
36
- _ = os .RemoveAll (dir )
30
+ logger .Debug (fmt .Sprintf ("cleanup temporary dir %s" , tmpDir ))
31
+ _ = os .RemoveAll (tmpDir )
37
32
}()
38
33
39
- // Set environment
40
- logger .Debug ("write content to temporary file" )
41
- if err := os .WriteFile (filepath .Join (dir , tmpFile ), content , 0666 ); err != nil {
42
- logger .Error (fmt .Errorf ("could not write tmp file" ))
43
- return err
44
- }
45
-
46
- if err := os .WriteFile (filepath .Join (dir , "go.mod" ), []byte (templates .GoMod ), 0666 ); err != nil {
47
- logger .Error (fmt .Errorf ("could not write tmp go.mod file" ))
48
- return err
49
- }
50
-
51
- initCmd := exec .Command ("go" , "get" , "-u" , "golang.org/x/sys/windows" )
52
- initCmd .Dir = dir
53
- initCmd .Stderr = os .Stderr
54
- initCmd .Env = append (os .Environ (), "GOOS=windows" , "GOARCH=amd64" )
55
- if err := initCmd .Run (); err != nil {
34
+ err := b .execCmd ("go" , "get" , "-u" , "golang.org/x/sys/windows" )
35
+ if err != nil {
56
36
logger .Error (fmt .Errorf ("could not install dependency" ))
57
37
return err
58
38
}
59
39
60
40
logger .Debug ("dependency installed" )
41
+ logger .Debug ("start compiling binary" )
61
42
62
- pwd , err := os . Getwd ( )
43
+ relOutputPath , err := filepath . Abs ( outputPath )
63
44
if err != nil {
64
45
return err
65
46
}
66
47
67
- buildCmd := exec . Command (
48
+ err = b . execCmd (
68
49
"go" ,
69
50
"build" ,
70
51
"-ldflags" ,
71
52
"-s -w -H=windowsgui" ,
72
53
"-o" ,
73
- filepath . Join ( pwd , outputPath ) ,
74
- filepath .Join (dir , tmpFile ),
54
+ relOutputPath ,
55
+ filepath .Join (tmpDir , tmpFile ),
75
56
)
76
- buildCmd .Env = append (os .Environ (), "GOOS=windows" , "GOARCH=amd64" )
77
- buildCmd .Stderr = os .Stderr
78
- buildCmd .Dir = dir
79
-
80
- if err := buildCmd .Run (); err != nil {
57
+ if err != nil {
81
58
logger .Error (fmt .Errorf ("failed to compile" ))
82
59
return err
83
60
}
84
61
85
- logger .Info (fmt .Sprintf ("file compiled to %s" , filepath .Join (pwd , outputPath )))
62
+ logger .Info (fmt .Sprintf ("file compiled to %s" , relOutputPath ))
63
+
64
+ return nil
65
+ }
66
+
67
+ func (b baseLoader ) execCmd (name string , args ... string ) error {
68
+ logger .Debug (fmt .Sprintf ("execute command %s" , name ))
69
+
70
+ cmd := exec .Command (name , args ... )
71
+ cmd .Env = append (os .Environ (), "GOOS=windows" , "GOARCH=amd64" )
72
+ cmd .Stderr = os .Stderr
73
+ cmd .Dir = tmpDir
74
+
75
+ return cmd .Run ()
76
+ }
77
+
78
+ func (b baseLoader ) setupTmpDir (goFile []byte ) error {
79
+ logger .Debug (fmt .Sprintf ("setup temporary directory %s" , tmpDir ))
80
+
81
+ if err := os .MkdirAll (tmpDir , 0750 ); err != nil {
82
+ logger .Error (fmt .Errorf ("could not create temporary directory" ))
83
+ return err
84
+ }
85
+
86
+ if err := os .WriteFile (filepath .Join (tmpDir , tmpFile ), goFile , 0666 ); err != nil {
87
+ logger .Error (fmt .Errorf ("could not write tmp file" ))
88
+ return err
89
+ }
90
+
91
+ if err := os .WriteFile (filepath .Join (tmpDir , "go.mod" ), []byte (templates .GoMod ), 0666 ); err != nil {
92
+ logger .Error (fmt .Errorf ("could not write tmp go.mod file" ))
93
+ return err
94
+ }
95
+
86
96
return nil
87
97
}
0 commit comments