-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
63 lines (51 loc) · 1.53 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
strcase "github.com/iancoleman/strcase"
)
func replaceContentCases(content string, name string) (result string) {
result = strings.ReplaceAll(content, "{{name}}", name)
result = strings.ReplaceAll(result, "{{nameCamel}}", strcase.ToLowerCamel(name))
result = strings.ReplaceAll(result, "{{NameCamel}}", strcase.ToCamel(name))
result = strings.ReplaceAll(result, "{{nameKebab}}", strcase.ToKebab(name))
result = strings.ReplaceAll(result, "{{NameKebab}}", strcase.ToScreamingKebab(name))
result = strings.ReplaceAll(result, "{{Name}}", strings.Title(name))
result = strings.ReplaceAll(result, "{{NAME}}", strings.ToUpper(name))
return result
}
func CreateFiles(config CreateFileConfig) (results []CreateFileResult) {
var files []string
err := filepath.Walk(config.baseTemplatePath, func(path string, info os.FileInfo, err error) error {
if path != config.baseTemplatePath && !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
log.Fatalln(err)
}
var wg sync.WaitGroup
for _, filePath := range files {
wg.Add(1)
go func(file string) {
defer wg.Done()
input, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalln(err)
}
content := string(input)
results = append(
results, CreateFile(
replaceContentCases(strings.Replace(file, config.baseTemplatePath, config.basePath, 1), config.name),
replaceContentCases(content, config.name),
))
}(filePath)
}
wg.Wait()
return results
}