|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/onkernel/cli/pkg/templates" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + DIR_PERM = 0755 // rwxr-xr-x |
| 14 | + FILE_PERM = 0644 // rw-r--r-- |
| 15 | +) |
| 16 | + |
| 17 | +// CopyTemplateFiles copies all files and directories from the specified embedded template |
| 18 | +// into the target application path. It uses the given language and template names |
| 19 | +// to locate the template inside the embedded filesystem. |
| 20 | +// |
| 21 | +// - appPath: filesystem path where the files should be written (the project directory) |
| 22 | +// - language: language subdirectory (e.g., "typescript") |
| 23 | +// - template: template subdirectory (e.g., "sample-app") |
| 24 | +// |
| 25 | +// The function will recursively walk through the embedded template directory and |
| 26 | +// replicate all files and folders in appPath. If a file named "_gitignore" is encountered, |
| 27 | +// it is renamed to ".gitignore" in the output, to work around file embedding limitations. |
| 28 | +// |
| 29 | +// Returns an error if the template path is invalid, empty, or if any file operations fail. |
| 30 | +func CopyTemplateFiles(appPath, language, template string) error { |
| 31 | + // Build the template path within the embedded FS (e.g., "typescript/sample-app") |
| 32 | + templatePath := filepath.Join(language, template) |
| 33 | + |
| 34 | + // Check if the template exists and is non-empty |
| 35 | + entries, err := fs.ReadDir(templates.FS, templatePath) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("template not found: %s/%s", language, template) |
| 38 | + } |
| 39 | + if len(entries) == 0 { |
| 40 | + return fmt.Errorf("template directory is empty: %s/%s", language, template) |
| 41 | + } |
| 42 | + |
| 43 | + // Walk through the embedded template directory and copy contents |
| 44 | + return fs.WalkDir(templates.FS, templatePath, func(path string, d fs.DirEntry, err error) error { |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + // Determine the path relative to the root of the template |
| 50 | + relPath, err := filepath.Rel(templatePath, path) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + // Skip the template root directory itself |
| 56 | + if relPath == "." { |
| 57 | + return nil |
| 58 | + } |
| 59 | + |
| 60 | + destPath := filepath.Join(appPath, relPath) |
| 61 | + |
| 62 | + if d.IsDir() { |
| 63 | + return os.MkdirAll(destPath, DIR_PERM) |
| 64 | + } |
| 65 | + |
| 66 | + // Read the file content from the embedded filesystem |
| 67 | + content, err := fs.ReadFile(templates.FS, path) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to read template file %s: %w", path, err) |
| 70 | + } |
| 71 | + |
| 72 | + // Rename _gitignore to .gitignore in the destination |
| 73 | + if filepath.Base(destPath) == "_gitignore" { |
| 74 | + destPath = filepath.Join(filepath.Dir(destPath), ".gitignore") |
| 75 | + } |
| 76 | + |
| 77 | + // Write the file to disk in the target project directory |
| 78 | + if err := os.WriteFile(destPath, content, FILE_PERM); err != nil { |
| 79 | + return fmt.Errorf("failed to write file %s: %w", destPath, err) |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | + }) |
| 84 | +} |
0 commit comments