-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dapeng
committed
May 25, 2024
1 parent
7a86ebb
commit 4363587
Showing
43 changed files
with
1,599 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
.DS_Store | ||
|
||
test-app | ||
coverage.txt | ||
coverage.txt | ||
|
||
demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
"path" | ||
) | ||
|
||
func CreateCommand() *cli.Command { | ||
return &cli.Command{ | ||
Name: "create", | ||
Usage: "[-t ${template} [-m ${modName}]] ${appName}", | ||
Description: "create a gone app", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "template", | ||
Aliases: []string{"t"}, | ||
Value: "web", | ||
Usage: "template: only support web、web+mysql, more will be supported in the future", | ||
}, | ||
|
||
&cli.StringFlag{ | ||
Name: "mod", | ||
Aliases: []string{"m"}, | ||
Usage: "modName", | ||
}, | ||
}, | ||
Action: action, | ||
} | ||
} | ||
|
||
func action(c *cli.Context) error { | ||
return doAction(c.String("template"), c.String("mod"), c.Args().Get(0)) | ||
} | ||
|
||
func doAction(template, modName, appName string) error { | ||
tplName, app, mod, err := paramsProcess(template, appName, modName) | ||
if err != nil { | ||
return err | ||
} | ||
return copyAndReplace(f, | ||
path.Join(".", tplName), | ||
app, | ||
map[string]string{ | ||
"${{appName}}": app, | ||
"demo-structure": mod, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli/v2" | ||
"testing" | ||
) | ||
|
||
func TestAction(t *testing.T) { | ||
app := cli.App{ | ||
Commands: []*cli.Command{ | ||
CreateCommand(), | ||
}, | ||
} | ||
|
||
err := app.Run([]string{"", "create"}) | ||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package app | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"github.com/gone-io/gone/templates" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
var f = templates.F | ||
|
||
func copyAndReplace(f embed.FS, source, target string, replacement map[string]string) error { | ||
err := os.MkdirAll(target, 0766) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dir, err := f.ReadDir(source) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, entry := range dir { | ||
from := path.Join(source, entry.Name()) | ||
to := path.Join(target, strings.TrimSuffix(entry.Name(), ".tpl")) | ||
|
||
if entry.IsDir() { | ||
err = copyAndReplace( | ||
f, | ||
from, | ||
to, | ||
replacement, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
file, err := os.OpenFile(to, os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer func(file *os.File) { | ||
err := file.Close() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}(file) | ||
|
||
data, err := f.ReadFile(from) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range replacement { | ||
data = bytes.ReplaceAll(data, []byte(k), []byte(v)) | ||
} | ||
|
||
_, err = file.Write(data) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func paramsProcess(template, appName, modName string) (string, string, string, error) { | ||
if template != "web" && template != "web+mysql" { | ||
return "", "", "", fmt.Errorf("only support web、web+mysql, more will be supported in the future") | ||
} | ||
|
||
if appName == "" { | ||
appName = "demo" | ||
} | ||
|
||
if modName == "" { | ||
modName = appName | ||
} | ||
return template, appName, modName, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package app | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func deleteFilesInDirectory(dir string) error { | ||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
return fmt.Errorf("failed to read directory: %v", err) | ||
} | ||
|
||
for _, file := range files { | ||
if !file.IsDir() { | ||
err := os.Remove(filepath.Join(dir, file.Name())) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete file %s: %v", file.Name(), err) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
//go:embed testdata/from/* | ||
var testF embed.FS | ||
|
||
func Test_copyAndReplace(t *testing.T) { | ||
_ = deleteFilesInDirectory("testdata/to/") | ||
err := copyAndReplace(testF, "testdata/from", "testdata/to/", map[string]string{ | ||
"test": "x-test", | ||
}) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_paramsProcess(t *testing.T) { | ||
tpl, m, app, err := paramsProcess("", "", "") | ||
assert.Error(t, err) | ||
|
||
tpl, m, app, err = paramsProcess("web", "", "") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "web", tpl) | ||
assert.Equal(t, "demo", m) | ||
assert.Equal(t, "demo", app) | ||
} |
Oops, something went wrong.