-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
38e388d
commit afd5ffe
Showing
16 changed files
with
302 additions
and
46 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,5 @@ | ||
bin | ||
release | ||
.vscode | ||
.idea | ||
.idea | ||
out |
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,4 @@ | ||
packages: | ||
github.com/codeengio/idi/writer: | ||
config: | ||
all: True |
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,6 +1,6 @@ | ||
APP=idi | ||
MODULE := github.com/codeengio/idi | ||
VERSION := v0.1 | ||
VERSION := 1.0.0 | ||
|
||
.PHONY: clean bin test | ||
|
||
|
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,3 @@ | ||
package main | ||
|
||
//go:generate mockery --all --log-level=debug --disable-version-string --inpackage |
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
package generator | ||
|
||
import ( | ||
"errors" | ||
"github.com/codeengio/idi/mocks/github.com/codeengio/idi/writer" | ||
"github.com/codeengio/idi/tests" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/mock" | ||
"testing" | ||
) | ||
|
||
func TestApp_GenerateNew(t *testing.T) { | ||
w := &writer.MockWriter{} | ||
|
||
w.On("WriteTemplate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). | ||
Return(nil) | ||
|
||
appGen := NewApp(zerolog.Logger{}, w) | ||
err := appGen.GenerateNew( | ||
"app", | ||
"example.com/module/app", | ||
map[string]string{"README.md": "templates/readme.md.tmpl"}, | ||
tests.GetFS(), | ||
) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if len(w.Calls) != 1 { | ||
t.Fatalf("expected 1 call, got=%d", len(w.Calls)) | ||
} | ||
} | ||
|
||
func TestApp_GenerateNew_EmptyTemplates(t *testing.T) { | ||
w := &writer.MockWriter{} | ||
appGen := NewApp(zerolog.Logger{}, w) | ||
err := appGen.GenerateNew("app", "example.com/module/app", map[string]string{}, tests.GetFS()) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if len(w.Calls) > 0 { | ||
t.Fatalf("should not have any calls") | ||
} | ||
} | ||
|
||
func TestApp_GenerateNew_WriteError(t *testing.T) { | ||
w := &writer.MockWriter{} | ||
|
||
w.On("WriteTemplate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). | ||
Return(errors.New("write error")) | ||
|
||
appGen := NewApp(zerolog.Logger{}, w) | ||
err := appGen.GenerateNew( | ||
"app", | ||
"example.com/module/app", | ||
map[string]string{"README.md": "templates/readme.md.tmpl"}, | ||
tests.GetFS(), | ||
) | ||
if err == nil { | ||
t.Fatalf("should have errored") | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("{{.AppName}}") | ||
} |
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,13 @@ | ||
package tests | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
) | ||
|
||
//go:embed data/* | ||
var data embed.FS | ||
|
||
var GetFS = func() fs.FS { | ||
return data | ||
} |
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,46 @@ | ||
package writer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/rs/zerolog" | ||
"html/template" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type FS struct { | ||
logger zerolog.Logger | ||
} | ||
|
||
func NewFS(logger zerolog.Logger) *FS { | ||
return &FS{logger: logger} | ||
} | ||
|
||
func (fs *FS) WriteTemplate(appDir, templateName, outFileName string, templateFS fs.FS, args map[string]string) error { | ||
tmpl, err := template.ParseFS(templateFS, templateName) | ||
if err != nil { | ||
fs.logger.Error().Err(err).Msg("error parsing template") | ||
return err | ||
} | ||
|
||
fp := fmt.Sprintf("%s/%s", appDir, outFileName) | ||
if err := os.MkdirAll(filepath.Dir(fp), 0770); err != nil { | ||
fs.logger.Error().Err(err).Msg("error creating directory") | ||
return err | ||
} | ||
|
||
f, err := os.Create(fp) | ||
if err != nil { | ||
fs.logger.Error().Err(err).Msg("error creating file") | ||
return err | ||
} | ||
|
||
err = tmpl.Execute(f, args) | ||
if err != nil { | ||
fs.logger.Error().Err(err).Msg("error executing template") | ||
return err | ||
} | ||
|
||
return err | ||
} |
Oops, something went wrong.