|
| 1 | +package build |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type BuildOpts struct { |
| 12 | + Dir string |
| 13 | + Verbose bool |
| 14 | +} |
| 15 | + |
| 16 | +func verifyTsunamiDir(dir string) error { |
| 17 | + if dir == "" { |
| 18 | + return fmt.Errorf("directory path cannot be empty") |
| 19 | + } |
| 20 | + |
| 21 | + // Check if directory exists |
| 22 | + info, err := os.Stat(dir) |
| 23 | + if err != nil { |
| 24 | + if os.IsNotExist(err) { |
| 25 | + return fmt.Errorf("directory %q does not exist", dir) |
| 26 | + } |
| 27 | + return fmt.Errorf("error accessing directory %q: %w", dir, err) |
| 28 | + } |
| 29 | + |
| 30 | + if !info.IsDir() { |
| 31 | + return fmt.Errorf("%q is not a directory", dir) |
| 32 | + } |
| 33 | + |
| 34 | + // Check for app.go file |
| 35 | + appGoPath := filepath.Join(dir, "app.go") |
| 36 | + if err := CheckFileExists(appGoPath); err != nil { |
| 37 | + return fmt.Errorf("app.go check failed in directory %q: %w", dir, err) |
| 38 | + } |
| 39 | + |
| 40 | + // Check static directory if it exists |
| 41 | + staticPath := filepath.Join(dir, "static") |
| 42 | + if err := IsDirOrNotFound(staticPath); err != nil { |
| 43 | + return fmt.Errorf("static directory check failed in %q: %w", dir, err) |
| 44 | + } |
| 45 | + |
| 46 | + // Check that dist doesn't exist |
| 47 | + distPath := filepath.Join(dir, "dist") |
| 48 | + if err := FileMustNotExist(distPath); err != nil { |
| 49 | + return fmt.Errorf("dist check failed in %q: %w", dir, err) |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func TsunamiBuild(opts BuildOpts) error { |
| 56 | + if err := verifyTsunamiDir(opts.Dir); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Create temporary directory |
| 61 | + tempDir, err := os.MkdirTemp("", "tsunami-build-*") |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to create temp directory: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + log.Printf("Building tsunami app from %s\n", opts.Dir) |
| 67 | + |
| 68 | + if opts.Verbose { |
| 69 | + log.Printf("Temp dir: %s\n", tempDir) |
| 70 | + } |
| 71 | + |
| 72 | + // Copy all *.go files from the root directory |
| 73 | + goCount, err := copyGoFiles(opts.Dir, tempDir) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to copy go files: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Copy static directory |
| 79 | + staticCount, err := copyStaticDir(opts.Dir, tempDir) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to copy static directory: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + // Create dist directory |
| 85 | + distDir := filepath.Join(tempDir, "dist") |
| 86 | + if err := os.MkdirAll(distDir, 0755); err != nil { |
| 87 | + return fmt.Errorf("failed to create dist directory: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + if opts.Verbose { |
| 91 | + log.Printf("Copied %d go files, %d static files\n", goCount, staticCount) |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func copyStaticDir(srcDir, destDir string) (int, error) { |
| 97 | + // Always create static directory in temp dir |
| 98 | + staticDestDir := filepath.Join(destDir, "static") |
| 99 | + if err := os.MkdirAll(staticDestDir, 0755); err != nil { |
| 100 | + return 0, fmt.Errorf("failed to create static directory: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Copy static/ directory contents if it exists |
| 104 | + staticSrcDir := filepath.Join(srcDir, "static") |
| 105 | + if _, err := os.Stat(staticSrcDir); err == nil { |
| 106 | + return copyDirRecursive(staticSrcDir, staticDestDir) |
| 107 | + } |
| 108 | + |
| 109 | + return 0, nil |
| 110 | +} |
| 111 | + |
| 112 | +func copyGoFiles(srcDir, destDir string) (int, error) { |
| 113 | + entries, err := os.ReadDir(srcDir) |
| 114 | + if err != nil { |
| 115 | + return 0, err |
| 116 | + } |
| 117 | + |
| 118 | + fileCount := 0 |
| 119 | + for _, entry := range entries { |
| 120 | + if entry.IsDir() { |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + if strings.HasSuffix(entry.Name(), ".go") { |
| 125 | + srcPath := filepath.Join(srcDir, entry.Name()) |
| 126 | + destPath := filepath.Join(destDir, entry.Name()) |
| 127 | + |
| 128 | + if err := copyFile(srcPath, destPath); err != nil { |
| 129 | + return 0, fmt.Errorf("failed to copy %s: %w", entry.Name(), err) |
| 130 | + } |
| 131 | + fileCount++ |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return fileCount, nil |
| 136 | +} |
| 137 | + |
| 138 | +func TsunamiRun(opts BuildOpts) error { |
| 139 | + if err := TsunamiBuild(opts); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + return fmt.Errorf("TsunamiRun not implemented yet") |
| 144 | +} |
0 commit comments