|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "text/template" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + gitignoreTmplPath = "templates/.gitignore.tmpl" |
| 15 | + mainTmplPath = "templates/main.cpp.tmpl" |
| 16 | + srcPath = "src" |
| 17 | + binPath = "bin" |
| 18 | + includePath = "include" |
| 19 | +) |
| 20 | + |
| 21 | +type Project struct { |
| 22 | + ProjectName string |
| 23 | + AbsolutePath string |
| 24 | +} |
| 25 | + |
| 26 | +func (p *Project) CreateProjectStructure() error { |
| 27 | + // Check for AbsolutePath. Create if it does not exist |
| 28 | + if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) { |
| 29 | + if err := os.Mkdir(p.AbsolutePath, 0o754); err != nil { |
| 30 | + log.Printf("Could not create directory: %v", err) |
| 31 | + return err |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Trim whitespaces from ProjectName |
| 36 | + p.ProjectName = strings.TrimSpace(p.ProjectName) |
| 37 | + |
| 38 | + // Create the root project directory |
| 39 | + projectPath := filepath.Join(p.AbsolutePath, p.ProjectName) |
| 40 | + if _, err := os.Stat(projectPath); os.IsNotExist(err) { |
| 41 | + if err := os.MkdirAll(projectPath, 0o751); err != nil { |
| 42 | + log.Printf("Error creating root project directory %v\n", err) |
| 43 | + return err |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Create project structure (src, bin, include directories) |
| 48 | + if err := p.CreatePath(srcPath, projectPath); err != nil { |
| 49 | + log.Printf("Error creating src directory %v\n", err) |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + if err := p.CreatePath(includePath, projectPath); err != nil { |
| 54 | + log.Printf("Error creating include directory %v\n", err) |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + if err := p.CreatePath(binPath, projectPath); err != nil { |
| 59 | + log.Printf("Error creating bin directory %v\n", err) |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Initialize git repository |
| 64 | + if err := ExecuteCmd("git", []string{"init"}, projectPath); err != nil { |
| 65 | + log.Printf("Error initializing git repository %v\n", err) |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + // Create .gitignore file |
| 70 | + if err := p.CreateFileFromTemplate(gitignoreTmplPath, projectPath, ".gitignore"); err != nil { |
| 71 | + log.Printf("Error creating .gitignore file: %v\n", err) |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + // Create main.cpp file |
| 76 | + mainPath := filepath.Join(projectPath, srcPath) |
| 77 | + if err := p.CreateFileFromTemplate(mainTmplPath, mainPath, "main.cpp"); err != nil { |
| 78 | + log.Printf("Error creating main.cpp file: %v\n", err) |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (p *Project) CreateFileFromTemplate(tmplPath string, destinationPath string, filename string) error { |
| 86 | + // read template file |
| 87 | + tmpl, err := template.ParseFiles(tmplPath) |
| 88 | + if err != nil { |
| 89 | + log.Printf("Error parsing template file: %v\n", err) |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + // create the output file |
| 94 | + generatedFile, err := os.Create(filepath.Join(destinationPath, filename)) |
| 95 | + if err != nil { |
| 96 | + log.Printf("Error creating %s file: %v\n", filename, err) |
| 97 | + return err |
| 98 | + } |
| 99 | + defer generatedFile.Close() |
| 100 | + |
| 101 | + err = tmpl.Execute(generatedFile, nil) |
| 102 | + if err != nil { |
| 103 | + log.Printf("Error executing template: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (p *Project) CreatePath(pathToCreate string, projectPath string) error { |
| 110 | + path := filepath.Join(projectPath, pathToCreate) |
| 111 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 112 | + if err := os.MkdirAll(path, 0o751); err != nil { |
| 113 | + log.Printf("Error creating directory %v\n", err) |
| 114 | + return err |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// [https://github.com/Melkeydev/go-blueprint/blob/main/cmd/utils/utils.go]: |
| 122 | +// Melkeydev implementation of ExecuteCmd |
| 123 | +func ExecuteCmd(name string, args []string, dir string) error { |
| 124 | + cmd := exec.Command(name, args...) |
| 125 | + cmd.Dir = dir |
| 126 | + |
| 127 | + if err := cmd.Run(); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func CheckArgs() error { |
| 134 | + // Check if there are enough arguments |
| 135 | + if len(os.Args) != 2 { |
| 136 | + return errors.New("Usage: bluecpprint <project_name>") |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments