@@ -4,13 +4,14 @@ import (
44 "context"
55 "embed"
66 "fmt"
7- "log"
87 "os"
98 "path/filepath"
109 "runtime"
1110 "text/template"
1211
1312 "github.com/launchrctl/launchr"
13+ "github.com/launchrctl/launchr/pkg/cli"
14+ "github.com/launchrctl/launchr/pkg/log"
1415)
1516
1617const launchrPkg = "github.com/launchrctl/launchr"
@@ -85,7 +86,7 @@ func NewBuilder(opts *BuildOptions) (*Builder, error) {
8586
8687// Build prepares build environment, generates go files and build the binary.
8788func (b * Builder ) Build (ctx context.Context ) error {
88- log . Printf ( "[INFO] Start building" )
89+ cli . Println ( "Starting building %s" , b . PkgName )
8990 // Prepare build environment dir and go executable.
9091 var err error
9192 b .env , err = newBuildEnvironment ()
@@ -99,10 +100,10 @@ func (b *Builder) Build(ctx context.Context) error {
99100 _ = b .Close ()
100101 }
101102 }()
102- log .Printf ( "[DEBUG] Temporary folder: %s" , b .env .wd )
103+ log .Debug ( " Temporary folder: %s" , b .env .wd )
103104
104105 // Write files to dir and generate go mod.
105- log . Printf ( "[INFO] Creating project files and fetching dependencies" )
106+ cli . Println ( " Creating project files and fetching dependencies" )
106107 b .env .SetEnv ("CGO_ENABLE" , "0" )
107108 err = b .env .CreateModFile (ctx , b .BuildOptions )
108109 if err != nil {
@@ -138,9 +139,7 @@ func (b *Builder) Build(ctx context.Context) error {
138139 }
139140
140141 // Generate code for provided plugins.
141- genArgs := []string {"generate" , "./..." }
142- cmdGen := b .env .NewCommand (ctx , b .env .Go (), genArgs ... )
143- err = b .env .RunCmd (ctx , cmdGen )
142+ err = b .runGen (ctx )
144143 if err != nil {
145144 return err
146145 }
@@ -152,19 +151,20 @@ func (b *Builder) Build(ctx context.Context) error {
152151 }
153152
154153 // Build the main go package.
155- log . Printf ( "[INFO] Building Launchr" )
154+ cli . Println ( " Building %s" , b . PkgName )
156155 err = b .goBuild (ctx )
157156 if err != nil {
158157 return err
159158 }
160159
161- log . Printf ( "[INFO] Build complete: %s" , b .BuildOutput )
160+ cli . Println ( " Build complete: %s" , b .BuildOutput )
162161 return nil
163162}
164163
165164// Close does cleanup after build.
166165func (b * Builder ) Close () error {
167166 if b .env != nil && ! b .Debug {
167+ log .Debug ("Cleaning build environment: %s" , b .env .wd )
168168 return b .env .Close ()
169169 }
170170 return nil
@@ -186,10 +186,6 @@ func (b *Builder) goBuild(ctx context.Context) error {
186186 args = append (args , "-ldflags" , "-w -s" , "-trimpath" )
187187 }
188188 cmd := b .env .NewCommand (ctx , b .env .Go (), args ... )
189- cmd .Env = envFromOs ()
190-
191- log .Printf ("[DEBUG] Go build command: %s" , cmd )
192- log .Printf ("[DEBUG] Environment variables: %v" , cmd .Env )
193189 err = b .env .RunCmd (ctx , cmd )
194190 if err != nil {
195191 return err
@@ -216,3 +212,15 @@ func (b *Builder) getBuildVersion(version *launchr.AppVersion) *launchr.AppVersi
216212
217213 return & bv
218214}
215+
216+ func (b * Builder ) runGen (ctx context.Context ) error {
217+ genArgs := []string {"generate" , "./..." }
218+ cmd := b .env .NewCommand (ctx , b .env .Go (), genArgs ... )
219+ env := make (envVars , len (cmd .Env ))
220+ copy (env , cmd .Env )
221+ // Exclude target platform information as it may break "go run".
222+ env .Unset ("GOOS" )
223+ env .Unset ("GOARCH" )
224+ cmd .Env = env
225+ return b .env .RunCmd (ctx , cmd )
226+ }
0 commit comments