Skip to content

Commit e6ddc15

Browse files
committed
feat: Support for C programs
1 parent 04a8aa1 commit e6ddc15

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

cmd/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
)
99

1010
func main() {
11-
if err := pro.CheckArgs(); err != nil {
11+
language, projectName, err := pro.CheckArgs()
12+
if err != nil {
1213
log.Fatal(err)
1314
}
1415

@@ -19,13 +20,14 @@ func main() {
1920
}
2021

2122
p := pro.Project{
22-
ProjectName: os.Args[1],
2323
AbsolutePath: currentWorkingDir,
24+
Language: language,
25+
ProjectName: projectName,
2426
}
2527

2628
if err := p.CreateProjectStructure(); err != nil {
2729
log.Fatalf("Error creating project structure: %v", err)
2830
}
2931

30-
log.Printf("Project %s created successfully", p.ProjectName)
32+
log.Printf("Project %s created successfully", p.ProjectName)
3133
}

internal/program.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package internal
33
import (
44
_ "embed"
55
"errors"
6+
"flag"
7+
"fmt"
68
"log"
79
"os"
810
"os/exec"
@@ -20,7 +22,7 @@ const (
2022
//go:embed templates/.gitignore.tmpl
2123
var gitignoreTmpl string
2224

23-
//go:embed templates/main.cpp.tmpl
25+
//go:embed templates/main.tmpl
2426
var mainTmpl string
2527

2628
//go:embed templates/Makefile.tmpl
@@ -35,6 +37,7 @@ var readmeTmpl string
3537
type Project struct {
3638
ProjectName string
3739
AbsolutePath string
40+
Language string
3841
}
3942

4043
func (p *Project) CreateProjectStructure() error {
@@ -86,15 +89,15 @@ func (p *Project) CreateProjectStructure() error {
8689
return err
8790
}
8891

89-
// Create main.cpp file
92+
// Create main file
9093
mainPath := filepath.Join(projectPath, srcPath)
91-
if err := p.CreateFileFromTemplate(mainTmpl, mainPath, "main.cpp", nil); err != nil {
92-
log.Printf("Error creating main.cpp file: %v\n", err)
94+
if err := p.CreateFileFromTemplate(mainTmpl, mainPath, "main."+p.Language, map[string]string{"Language": p.Language}); err != nil {
95+
log.Printf("Error creating main.%s file: %v\n", p.Language, err)
9396
return err
9497
}
9598

9699
// Create Makefile
97-
if err := p.CreateFileFromTemplate(makefileTmpl, projectPath, "Makefile", nil); err != nil {
100+
if err := p.CreateFileFromTemplate(makefileTmpl, projectPath, "Makefile", map[string]string{"Language": p.Language}); err != nil {
98101
log.Printf("Error creating Makefile: %v\n", err)
99102
return err
100103
}
@@ -173,11 +176,43 @@ func ExecuteCmd(name string, args []string, dir string) error {
173176
return nil
174177
}
175178

176-
func CheckArgs() error {
177-
// Check if there are enough arguments
178-
if len(os.Args) != 2 {
179-
return errors.New("Usage: bluecpprint <project_name>")
179+
func usage() {
180+
fmt.Fprintf(os.Stderr, "Usage: bluecpprint --language=[LANGUAGE] [PROJECT_NAME]\n")
181+
fmt.Fprintf(os.Stderr, "Options:")
182+
fmt.Fprintf(os.Stderr, "\n --language=[LANG]")
183+
fmt.Fprintf(os.Stderr, " Specify the programming language. Choose between 'c' for C or 'cpp' for C++. The project files will change accordingly.\n")
184+
}
185+
186+
func valudateLanguage(lang string) error {
187+
if lang == "" {
188+
return errors.New("Language is required")
189+
}
190+
191+
if lang != "c" && lang != "cpp" {
192+
return errors.New("Invalid language. Allowed values are: c, cpp")
180193
}
181194

182195
return nil
183196
}
197+
198+
func printUsageAndError(msg string) (string, string, error) {
199+
flag.Usage()
200+
return "", "", errors.New(msg)
201+
}
202+
203+
func CheckArgs() (string, string, error) {
204+
language := flag.String("language", "", "")
205+
flag.Usage = usage
206+
flag.Parse()
207+
208+
if len(flag.Args()) < 1 {
209+
return printUsageAndError("Language and Project Name are required")
210+
}
211+
projectName := flag.Arg(0)
212+
213+
if err := valudateLanguage(*language); err != nil {
214+
return printUsageAndError(err.Error())
215+
}
216+
217+
return *language, projectName, nil
218+
}

internal/templates/Makefile.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CC=g++
1+
{{ if eq .Language "cpp" -}}CC=g++{{ else -}}CC=gcc{{ end }}
22
FLAGS=-O2 -Wall -Wextra -Wfatal-errors -pedantic
33
BIN_DIR=bin
44
SRC_DIR=src
@@ -7,10 +7,11 @@ INCLUDE_DIR=include
77
all: build
88

99
build:
10-
@$(CC) $(FLAGS) -I$(INCLUDE_DIR) $(SRC_DIR)/*.cpp -o $(BIN_DIR)/main
10+
@if [ ! -d $(BIN_DIR) ]; then mkdir $(BIN_DIR); fi
11+
@$(CC) $(FLAGS) -I$(INCLUDE_DIR) $(SRC_DIR)/*.{{.Language}} -o $(BIN_DIR)/main
1112

1213
clean:
13-
@rm -f $(BIN_DIR)/*
14+
@rm -rf $(BIN_DIR)
1415

1516
run:
1617
@./$(BIN_DIR)/main

internal/templates/main.cpp.tmpl

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/templates/main.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{ if eq .Language "cpp" -}}
2+
#include <iostream>
3+
{{ else -}}
4+
#include <stdio.h>
5+
{{ end }}
6+
int main (int argc, char *argv[])
7+
{
8+
{{- if eq .Language "cpp" }}
9+
std::cout << "Hello World" << std::endl;
10+
{{- else }}
11+
printf("Hello World\n");
12+
{{- end }}
13+
return 0;
14+
}

0 commit comments

Comments
 (0)