Skip to content

Commit

Permalink
Merge pull request #2 from henrylee2cn/master
Browse files Browse the repository at this point in the history
update newest code
  • Loading branch information
letmefly committed Feb 24, 2018
2 parents 26e786a + 908b284 commit 11098e7
Show file tree
Hide file tree
Showing 25 changed files with 922 additions and 290 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,14 @@ NAME:
ant new - Create a new ant project
USAGE:
ant new [options] [arguments...]
or
ant new [options except -app_path] [arguments...] {app_path}
ant new [command options] [arguments...]
OPTIONS:
--app_path value, -a value Specifies the path(relative/absolute) of the project
--script value, -s value The script for code generation(relative/absolute)
--app_path value, -p value The path(relative/absolute) of the project
```

example: `ant new -a myant` or `ant new myant`
example: `ant new -s ./test.ant -p ./myant`

- run project

Expand All @@ -189,10 +188,10 @@ USAGE:
OPTIONS:
--watch_exts value, -x value Specified to increase the listening file suffix (default: ".go", ".ini", ".yaml", ".toml", ".xml")
--app_path value, -a value Specifies the path(relative/absolute) of the project
--app_path value, -p value The path(relative/absolute) of the project
```

example: `ant run -x .yaml -a myant` or `ant run -x .yaml myant`
example: `ant run -x .yaml -p myant` or `ant run -x .yaml myant`

[More Ant Command](https://github.com/henrylee2cn/ant/tree/master/cmd/ant)

Expand Down
11 changes: 5 additions & 6 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,14 @@ NAME:
ant new - Create a new ant project
USAGE:
ant new [options] [arguments...]
or
ant new [options except -app_path] [arguments...] {app_path}
ant new [command options] [arguments...]
OPTIONS:
--app_path value, -a value Specifies the path(relative/absolute) of the project
--script value, -s value The script for code generation(relative/absolute)
--app_path value, -p value The path(relative/absolute) of the project
```

示例:`ant new -a myant` or `ant new myant`
示例:`ant new -p ./myant -s ./test.ant`

- 热编译运行

Expand All @@ -184,7 +183,7 @@ USAGE:
OPTIONS:
--watch_exts value, -x value Specified to increase the listening file suffix (default: ".go", ".ini", ".yaml", ".toml", ".xml")
--app_path value, -a value Specifies the path(relative/absolute) of the project
--app_path value, -p value Specifies the path(relative/absolute) of the project
```

示例:`ant run -x .yaml -a myant` or `ant run -x .yaml myant`
Expand Down
13 changes: 6 additions & 7 deletions cmd/ant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ NAME:
ant new - Create a new ant project
USAGE:
ant new [options] [arguments...]
or
ant new [options except -app_path] [arguments...] {app_path}
ant new [command options] [arguments...]
OPTIONS:
--app_path value, -a value Specifies the path(relative/absolute) of the project
--script value, -s value The script for code generation(relative/absolute)
--app_path value, -p value The path(relative/absolute) of the project
```

example: `ant new -a myant` or `ant new myant`
example: `ant new -s ./test.ant -p ./myant`

- run project

Expand All @@ -45,10 +44,10 @@ USAGE:
OPTIONS:
--watch_exts value, -x value Specified to increase the listening file suffix (default: ".go", ".ini", ".yaml", ".toml", ".xml")
--app_path value, -a value Specifies the path(relative/absolute) of the project
--app_path value, -p value The path(relative/absolute) of the project
```

example: `ant run -x .yaml -a myant` or `ant run -x .yaml myant`
example: `ant run -x .yaml -p myant` or `ant run -x .yaml myant`

## 4. Project Structure

Expand Down
98 changes: 44 additions & 54 deletions cmd/ant/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,67 @@ package create

import (
"bytes"
"io/ioutil"
"io"
"os"
"path/filepath"
"os/exec"

"github.com/henrylee2cn/ant"
"github.com/henrylee2cn/ant/cmd/ant/create/def"
"github.com/henrylee2cn/ant/cmd/ant/create/test"
"github.com/henrylee2cn/ant/cmd/ant/create/tpl"
"github.com/henrylee2cn/ant/cmd/ant/info"
)

var (
projName, projPath []byte
projNameTpl = []byte("{{PROJ_NAME}}")
projPathTpl = []byte("{{PROJ_PATH}}")
)

// CreateProject creates a project.
func CreateProject() {
func CreateProject(scriptFile string) {
os.MkdirAll(info.AbsPath(), os.FileMode(0755))
err := os.Chdir(info.AbsPath())
if err != nil {
ant.Fatalf("[ant] Jump working directory failed: %v", err)
}
{
projName = []byte(info.ProjName())
projPath = []byte(info.ProjPath())
}
err = restoreAssets("./", "")
if err != nil {
ant.Fatalf("[ant] Write project files failed: %v", err)
}
}

// restoreAssets restores an asset under the given directory recursively
func restoreAssets(dir, name string) error {
children, err := AssetDir(name)
// File
if err != nil {
return restoreAsset(dir, name)
if len(scriptFile) == 0 {
def.Create()
format()
return
}
// Dir
for _, child := range children {
err = restoreAssets(dir, filepath.Join(name, child))

// creats base files
tpl.Create()

var r io.Reader
if len(scriptFile) == 0 {
b := test.MustAsset("test.ant")
r = bytes.NewReader(b)
} else {
file, err := os.Open(scriptFile)
if err != nil {
return err
ant.Fatalf("[ant] Write project files failed: %v", err)
}
defer file.Close()
r = file
}
return nil
}

// restoreAsset restores an asset under the given directory
func restoreAsset(dir, name string) error {
data, err := Asset(name)
if err != nil {
return err
}
data = bytes.Replace(data, projNameTpl, projName, -1)
data = bytes.Replace(data, projPathTpl, projPath, -1)
info, err := AssetInfo(name)
if err != nil {
return err
}
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
{
lexer := Lexer{}
lexer.init(r)

parser := Parser{}
parser.init(&lexer)
parser.parse()

codeGen := CodeGen{}
codeGen.init(&parser)
codeGen.genForGolang()
}
return nil

format()
}

// format the codes
func format() {
cmd := exec.Command("gofmt", "-w", "./")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Run()
}
63 changes: 62 additions & 1 deletion cmd/ant/create/data.go → cmd/ant/create/def/tpl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 11098e7

Please sign in to comment.