@@ -22,6 +22,7 @@ type BuildOpts struct {
2222
2323type BuildEnv struct {
2424 GoVersion string
25+ TempDir string
2526}
2627
2728func verifyEnvironment (verbose bool ) (* BuildEnv , error ) {
@@ -154,16 +155,16 @@ func createGoMod(tempDir, appDirName, goVersion string, opts BuildOpts, verbose
154155 tidyCmd .Dir = tempDir
155156
156157 if verbose {
157- log .Printf ("Running go mod tidy in %s" , tempDir )
158+ log .Printf ("Running go mod tidy" )
159+ tidyCmd .Stdout = os .Stdout
160+ tidyCmd .Stderr = os .Stderr
158161 }
159162
160- output , err := tidyCmd .CombinedOutput ()
161- if err != nil {
162- return fmt .Errorf ("failed to run go mod tidy: %w\n Output: %s" , err , string (output ))
163+ if err := tidyCmd .Run (); err != nil {
164+ return fmt .Errorf ("failed to run go mod tidy: %w" , err )
163165 }
164166
165167 if verbose {
166- log .Printf ("go mod tidy output:\n %s" , string (output ))
167168 log .Printf ("Successfully ran go mod tidy" )
168169 }
169170
@@ -248,26 +249,28 @@ func verifyDistPath(distPath string) error {
248249 return nil
249250}
250251
251- func TsunamiBuild (opts BuildOpts ) error {
252+ func TsunamiBuild (opts BuildOpts ) ( * BuildEnv , error ) {
252253 buildEnv , err := verifyEnvironment (opts .Verbose )
253254 if err != nil {
254- return err
255+ return nil , err
255256 }
256257
257258 if err := verifyTsunamiDir (opts .Dir ); err != nil {
258- return err
259+ return nil , err
259260 }
260261
261262 if err := verifyDistPath (opts .DistPath ); err != nil {
262- return err
263+ return nil , err
263264 }
264265
265266 // Create temporary directory
266267 tempDir , err := os .MkdirTemp ("" , "tsunami-build-*" )
267268 if err != nil {
268- return fmt .Errorf ("failed to create temp directory: %w" , err )
269+ return nil , fmt .Errorf ("failed to create temp directory: %w" , err )
269270 }
270271
272+ buildEnv .TempDir = tempDir
273+
271274 log .Printf ("Building tsunami app from %s\n " , opts .Dir )
272275
273276 if opts .Verbose {
@@ -277,49 +280,55 @@ func TsunamiBuild(opts BuildOpts) error {
277280 // Copy all *.go files from the root directory
278281 goCount , err := copyGoFiles (opts .Dir , tempDir )
279282 if err != nil {
280- return fmt .Errorf ("failed to copy go files: %w" , err )
283+ return nil , fmt .Errorf ("failed to copy go files: %w" , err )
281284 }
282285
283286 // Copy static directory
284287 staticCount , err := copyStaticDir (opts .Dir , tempDir )
285288 if err != nil {
286- return fmt .Errorf ("failed to copy static directory: %w" , err )
289+ return nil , fmt .Errorf ("failed to copy static directory: %w" , err )
287290 }
288291
289292 // Create dist directory
290293 distDir := filepath .Join (tempDir , "dist" )
291294 if err := os .MkdirAll (distDir , 0755 ); err != nil {
292- return fmt .Errorf ("failed to create dist directory: %w" , err )
295+ return nil , fmt .Errorf ("failed to create dist directory: %w" , err )
296+ }
297+
298+ // Copy dist directory contents
299+ distCount , err := copyDirRecursive (opts .DistPath , distDir )
300+ if err != nil {
301+ return nil , fmt .Errorf ("failed to copy dist directory: %w" , err )
293302 }
294303
295304 if opts .Verbose {
296- log .Printf ("Copied %d go files, %d static files\n " , goCount , staticCount )
305+ log .Printf ("Copied %d go files, %d static files, %d dist files \n " , goCount , staticCount , distCount )
297306 }
298307
299308 // Copy main.go.tmpl from dist/templates to temp dir as main-app.go
300309 mainTmplSrc := filepath .Join (opts .DistPath , "templates" , "main.go.tmpl" )
301310 mainTmplDest := filepath .Join (tempDir , "main-app.go" )
302311 if err := copyFile (mainTmplSrc , mainTmplDest ); err != nil {
303- return fmt .Errorf ("failed to copy main.go.tmpl: %w" , err )
312+ return nil , fmt .Errorf ("failed to copy main.go.tmpl: %w" , err )
304313 }
305314
306315 // Create go.mod file
307316 appDirName := filepath .Base (opts .Dir )
308317 if err := createGoMod (tempDir , appDirName , buildEnv .GoVersion , opts , opts .Verbose ); err != nil {
309- return fmt .Errorf ("failed to create go.mod: %w" , err )
318+ return nil , fmt .Errorf ("failed to create go.mod: %w" , err )
310319 }
311320
312321 // Generate Tailwind CSS
313322 if err := generateAppTailwindCss (opts .DistPath , tempDir , opts .Verbose ); err != nil {
314- return fmt .Errorf ("failed to generate tailwind css: %w" , err )
323+ return nil , fmt .Errorf ("failed to generate tailwind css: %w" , err )
315324 }
316325
317326 // Build the Go application
318327 if err := runGoBuild (tempDir , opts .Verbose ); err != nil {
319- return fmt .Errorf ("failed to build application: %w" , err )
328+ return nil , fmt .Errorf ("failed to build application: %w" , err )
320329 }
321330
322- return nil
331+ return buildEnv , nil
323332}
324333
325334func runGoBuild (tempDir string , verbose bool ) error {
@@ -343,7 +352,7 @@ func runGoBuild(tempDir string, verbose bool) error {
343352 buildCmd .Dir = tempDir
344353
345354 if verbose {
346- log .Printf ("Running: %s in %s " , strings .Join (buildCmd .Args , " " ), tempDir )
355+ log .Printf ("Running: %s" , strings .Join (buildCmd .Args , " " ))
347356 buildCmd .Stdout = os .Stdout
348357 buildCmd .Stderr = os .Stderr
349358 }
@@ -360,14 +369,19 @@ func runGoBuild(tempDir string, verbose bool) error {
360369}
361370
362371func generateAppTailwindCss (distPath , tempDir string , verbose bool ) error {
363- tailwindInput := filepath .Join (distPath , "templates" , "tailwind.css" )
372+ // Copy tailwind.css from dist/templates to temp dir
373+ tailwindSrc := filepath .Join (distPath , "templates" , "tailwind.css" )
374+ tailwindDest := filepath .Join (tempDir , "tailwind.css" )
375+ if err := copyFile (tailwindSrc , tailwindDest ); err != nil {
376+ return fmt .Errorf ("failed to copy tailwind.css: %w" , err )
377+ }
378+
364379 tailwindOutput := filepath .Join (tempDir , "static" , "tw.css" )
365- contentGlob := filepath .Join (tempDir , "*.go" )
366380
367381 tailwindCmd := exec .Command ("npx" , "@tailwindcss/cli" ,
368- "-i" , tailwindInput ,
369- "-o" , tailwindOutput ,
370- "--content" , contentGlob )
382+ "-i" , "./tailwind.css" ,
383+ "-o" , tailwindOutput )
384+ tailwindCmd . Dir = tempDir
371385
372386 if verbose {
373387 log .Printf ("Running: %s" , strings .Join (tailwindCmd .Args , " " ))
@@ -429,9 +443,28 @@ func copyGoFiles(srcDir, destDir string) (int, error) {
429443}
430444
431445func TsunamiRun (opts BuildOpts ) error {
432- if err := TsunamiBuild (opts ); err != nil {
446+ buildEnv , err := TsunamiBuild (opts )
447+ if err != nil {
433448 return err
434449 }
435450
436- return fmt .Errorf ("TsunamiRun not implemented yet" )
451+ // Run the built application
452+ appPath := filepath .Join (buildEnv .TempDir , "bin" , "app" )
453+ runCmd := exec .Command (appPath )
454+ runCmd .Dir = buildEnv .TempDir
455+
456+ log .Printf ("Running tsunami app from %s" , opts .Dir )
457+
458+ runCmd .Stdin = os .Stdin
459+ if opts .Verbose {
460+ log .Printf ("Executing: %s" , appPath )
461+ runCmd .Stdout = os .Stdout
462+ runCmd .Stderr = os .Stderr
463+ }
464+
465+ if err := runCmd .Run (); err != nil {
466+ return fmt .Errorf ("failed to run application: %w" , err )
467+ }
468+
469+ return nil
437470}
0 commit comments