Skip to content

Commit

Permalink
[refactor] CODEGEN.md -> SCAFFOLD.md and clean print log
Browse files Browse the repository at this point in the history
  • Loading branch information
igtm committed Jan 14, 2022
1 parent f3e187f commit 37df57a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ brews:

# Your app's description.
# Default is empty.
description: "Simple Markdown-Driven Code Generator written by Go"
description: "Simple Markdown-Driven Scaffolding tool written by Go"

# Custom install script for brew.
# Default is 'bin.install "program"'.
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Manaita

Simple Markdown-Driven Code Generator written by Go
Simple Markdown-Driven Scaffolding tool written by Go

Write your scaffolding code on `CODEGEN.md` and generate files using the scaffold.
Write your scaffolding code on `SCAFFOLD.md` and generate files using the scaffold.

Template file is Markdown format. so you can see it on Github and easily understand what will be generated.
Scaffold file is Markdown format. so you can see it on Github and easily understand what will be generated.

![manaita](./docs/manaita.png "manaita")

Expand All @@ -24,7 +24,7 @@ go install github.com/igtm/manaita@latest

# Getting Started

1. put `CODEGEN.md` on your directory
1. put `SCAFFOLD.md` on your directory

````
---
Expand Down Expand Up @@ -55,14 +55,14 @@ print("bar.py")
# Usage

```shell
manaita -c ./path/to/CODEGEN.md -p key=value
manaita -c ./path/to/SCAFFOLD.md -p key=value
```

Available options:

```
-c specify markdown template file path. default name is 'CODEGEN.md'
-p specify parameters for code gen. these must be defined on markdown e.g. '-p foo=bar,fizz=buzz'
-c specify markdown scaffold file path. default name is 'SCAFFOLD.md'
-p specify parameters for scaffold template. these must be defined on markdown e.g. '-p foo=bar,fizz=buzz'
```

Available template params:
Expand Down
File renamed without changes.
56 changes: 32 additions & 24 deletions manaita.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
)

const (
DefaultCodeGenFileName = "CODEGEN.md"
YamlMetaStartCode = "---"
DestFileNameStartCode = "# "
CodeTemplateStartCode = "```"
DefaultScaffoldFileName = "SCAFFOLD.md"
DeprecatedDefaultScaffoldFileName = "CODEGEN.md" // deprecated
YamlMetaStartCode = "---"
DestFileNameStartCode = "# "
CodeTemplateStartCode = "```"
)

var (
c = flag.String("c", DefaultCodeGenFileName, "template markdown file path")
c = flag.String("c", DefaultScaffoldFileName, "scaffold markdown file path")
p = flag.String("p", "", "template params")

funcMap = template.FuncMap{
Expand All @@ -43,24 +44,32 @@ var (
func main() {
flag.Parse()

errlog := log.New(os.Stderr, "ERROR: ", 0)

envMap := envToMap()
currentDir, _ := os.Getwd()
givenParamMap := paramToMap(*p)

codegenFileName := fmt.Sprintf("%s/%s", currentDir, *c)
scaffoldFileName := fmt.Sprintf("%s/%s", currentDir, *c)

codegenFile, err := os.Open(codegenFileName)
die(err)
defer codegenFile.Close()
scaffoldFile, err := os.Open(scaffoldFileName)
if err != nil {
scaffoldFile, err = os.Open(DeprecatedDefaultScaffoldFileName)
}
if err != nil {
errlog.Println(fmt.Errorf("cannot find '%s'", DefaultScaffoldFileName))
return
}
defer scaffoldFile.Close()

sc := bufio.NewScanner(codegenFile)
sc := bufio.NewScanner(scaffoldFile)

var foundMeta bool
var endMeta bool
var metaStr string
var metaData map[string]interface{}
paramMap := make(map[string]string)
var code, dest string
var code, dest, destFileName string
var searchCode bool
var foundCode bool
for sc.Scan() {
Expand Down Expand Up @@ -119,7 +128,8 @@ func main() {
}
if strings.Contains(loc, DestFileNameStartCode) {
// dest filename
destFileName := strings.Replace(loc, DestFileNameStartCode, "", -1)
destFileName = strings.Replace(loc, DestFileNameStartCode, "", -1)
destFileName = strings.Trim(destFileName, " ")
// compile filename
tmpl := template.Must(template.New("").Funcs(funcMap).Parse(destFileName))
var compiledDest bytes.Buffer
Expand All @@ -138,20 +148,27 @@ func main() {
// recursively make directories
os.MkdirAll(filepath.Dir(dest), os.ModePerm)
destFile, err := os.Create(dest)
die(err)
if err != nil {
errlog.Println(fmt.Errorf("cannot Create '%s'", dest))
return
}
defer destFile.Close()
fmt.Println(dest)
fmt.Println(destFileName)

tmpl := template.Must(template.New("").Funcs(funcMap).Parse(code))
err = tmpl.Execute(destFile, map[string]interface{}{
"Env": envMap,
"Params": paramMap,
})
die(err)
if err != nil {
errlog.Println(fmt.Errorf("cannot write code to file '%s'", dest))
return
}
searchCode = false
foundCode = false
code = ""
dest = ""
destFileName = ""
continue
}
// start
Expand All @@ -163,17 +180,8 @@ func main() {
}
}
}

die(sc.Err())

}

func die(err error) {
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
}
func envToMap() map[string]string {
envMap := make(map[string]string)

Expand Down

0 comments on commit 37df57a

Please sign in to comment.