Skip to content

Commit d56e7a7

Browse files
committed
feat: add README template
1 parent e256b6c commit d56e7a7

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

internal/program.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ var makefileTmpl string
2929
//go:embed templates/.clang-format.tmpl
3030
var clangFormatTmpl string
3131

32+
//go:embed templates/README.md.tmpl
33+
var readmeTmpl string
34+
3235
type Project struct {
3336
ProjectName string
3437
AbsolutePath string
@@ -78,30 +81,36 @@ func (p *Project) CreateProjectStructure() error {
7881
}
7982

8083
// Create .gitignore file
81-
if err := p.CreateFileFromTemplate(gitignoreTmpl, projectPath, ".gitignore"); err != nil {
84+
if err := p.CreateFileFromTemplate(gitignoreTmpl, projectPath, ".gitignore", nil); err != nil {
8285
log.Printf("Error creating .gitignore file: %v\n", err)
8386
return err
8487
}
8588

8689
// Create main.cpp file
8790
mainPath := filepath.Join(projectPath, srcPath)
88-
if err := p.CreateFileFromTemplate(mainTmpl, mainPath, "main.cpp"); err != nil {
91+
if err := p.CreateFileFromTemplate(mainTmpl, mainPath, "main.cpp", nil); err != nil {
8992
log.Printf("Error creating main.cpp file: %v\n", err)
9093
return err
9194
}
9295

9396
// Create Makefile
94-
if err := p.CreateFileFromTemplate(makefileTmpl, projectPath, "Makefile"); err != nil {
97+
if err := p.CreateFileFromTemplate(makefileTmpl, projectPath, "Makefile", nil); err != nil {
9598
log.Printf("Error creating Makefile: %v\n", err)
9699
return err
97100
}
98101

99102
// Create clang-format file
100-
if err := p.CreateFileFromTemplate(clangFormatTmpl, projectPath, ".clang-format"); err != nil {
103+
if err := p.CreateFileFromTemplate(clangFormatTmpl, projectPath, ".clang-format", nil); err != nil {
101104
log.Printf("Error creating clang-format: %v\n", err)
102105
return err
103106
}
104107

108+
// Create README file
109+
if err := p.CreateFileFromTemplate(readmeTmpl, projectPath, "README.md", map[string]string{"ProjectName": p.ProjectName}); err != nil {
110+
log.Printf("Error creating readme file: %v\n", err)
111+
return err
112+
}
113+
105114
// Initialize the project with a commit
106115
if err := ExecuteCmd("git", []string{"add", "-A"}, projectPath); err != nil {
107116
log.Printf("Error adding files to git repository: %v\n", err)
@@ -116,7 +125,7 @@ func (p *Project) CreateProjectStructure() error {
116125
return nil
117126
}
118127

119-
func (p *Project) CreateFileFromTemplate(tmplContent string, destinationPath string, filename string) error {
128+
func (p *Project) CreateFileFromTemplate(tmplContent string, destinationPath string, filename string, data interface{}) error {
120129
// read template file
121130
tmpl, err := template.New(filename).Parse(tmplContent)
122131
if err != nil {
@@ -132,7 +141,7 @@ func (p *Project) CreateFileFromTemplate(tmplContent string, destinationPath str
132141
}
133142
defer generatedFile.Close()
134143

135-
err = tmpl.Execute(generatedFile, nil)
144+
err = tmpl.Execute(generatedFile, data)
136145
if err != nil {
137146
log.Printf("Error executing template: %v", err)
138147
}

internal/templates/.gitignore.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
# Binaries directory
35+
bin

internal/templates/README.md.tmpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Project {{.ProjectName}}
2+
3+
Description goes here.
4+
5+
## Build the project
6+
7+
### Compile
8+
9+
Binaries will be placed inside the `bin` directory. To build the project just run:
10+
```bash
11+
make
12+
```
13+
14+
### Build & run
15+
16+
To compile and run the application:
17+
```bash
18+
make run
19+
```
20+
21+
### Clean `bin` directory
22+
23+
Clean the `bin` directory using the following command:
24+
```bash
25+
make clean
26+
```

0 commit comments

Comments
 (0)