Skip to content

Commit

Permalink
use go1.16 official static embed instead go-bindata embed static files
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Dec 18, 2020
1 parent 238e287 commit 4e80367
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 1,552 deletions.
17 changes: 10 additions & 7 deletions mirc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@ LDFLAGS += -X "github.com/alimy/mir/mirc/v2/version.GitHash=$(shell git rev-pars
RELEASE_ROOT = release
RELEASE_LINUX_AMD64 = $(RELEASE_ROOT)/linux-amd64/mirc
RELEASE_DARWIN_AMD64 = $(RELEASE_ROOT)/darwin-amd64/mirc
RELEASE_DARWIN_ARM64 = $(RELEASE_ROOT)/darwin-arm64/mirc
RELEASE_WINDOWS_AMD64 = $(RELEASE_ROOT)/windows-amd64/mirc

.PHONY: build
build: fmt
go build -ldflags '$(LDFLAGS)' -o mirc main.go

.PHONY: generate
generate:
-rm -f cmd/templates_gen.go
go generate cmd/templates.go
$(GOFMT) -w cmd/templates_gen.go

.PHONY: release
release: linux-amd64 darwin-amd64 windows-x64
release: linux-amd64 darwin-amd64 darwin-arm64 windows-x64
cp ../LICENSE README.md $(RELEASE_LINUX_AMD64)
cp ../LICENSE README.md $(RELEASE_DARWIN_AMD64)
cp ../LICENSE README.md $(RELEASE_DARWIN_ARM64)
cp ../LICENSE README.md $(RELEASE_WINDOWS_AMD64)
cd $(RELEASE_LINUX_AMD64)/.. && rm -f *.zip && zip -r mirc-linux_amd64.zip mirc && cd -
cd $(RELEASE_DARWIN_AMD64)/.. && rm -f *.zip && zip -r mirc-darwin_amd64.zip mirc && cd -
cd $(RELEASE_DARWIN_ARM64)/.. && rm -f *.zip && zip -r mirc-darwin_arm64.zip mirc && cd -
cd $(RELEASE_WINDOWS_AMD64)/.. && rm -f *.zip && zip -r mirc-windows_amd64.zip mirc && cd -

.PHONY: linux-amd64
Expand All @@ -36,6 +33,10 @@ linux-amd64:
darwin-amd64:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_DARWIN_AMD64)/mirc main.go

.PHONY: darwin-arm64
darwin-arm64:
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_DARWIN_ARM64)/mirc main.go

.PHONY: windows-x64
windows-x64:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o $(RELEASE_WINDOWS_AMD64)/mirc main.go
Expand All @@ -55,6 +56,7 @@ check-debug-all:
rm -rf $$target; \
./mirc new --mir ../../ --style $$target --dst $$target; \
cd $$target; \
make mod-tidy; \
make generate; \
cd -; \
echo ""; \
Expand All @@ -67,6 +69,7 @@ check-release-all:
rm -rf $$target; \
./mirc new --style $$target --dst $$target; \
cd $$target; \
make mod-tidy; \
make generate; \
cd -; \
echo ""; \
Expand Down
4 changes: 2 additions & 2 deletions mirc/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func genProject(ctx *tmplCtx, dstPath string, tmpls map[string]tmplInfo) error {
break
}
if assetInfo.isTmpl {
t, err := tmpl.Parse(MustAssetString(assetInfo.name))
t, err := tmpl.Parse(string(assetInfo.MustBytes()))
if err != nil {
break
}
if err = t.Execute(file, ctx); err != nil {
break
}
} else {
if _, err = file.Write(MustAsset(assetInfo.name)); err != nil {
if _, err = file.Write(assetInfo.MustBytes()); err != nil {
break
}
}
Expand Down
159 changes: 86 additions & 73 deletions mirc/cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

package cmd

//go:generate go-bindata -nomemcopy -pkg=${GOPACKAGE} -ignore=README.md -prefix=templates -debug=false -o=templates_gen.go templates/...
import (
"embed"
)

//go:embed templates
var fs embed.FS

// tmplCtx template context for generate project
type tmplCtx struct {
Expand All @@ -18,96 +23,104 @@ type tmplInfo struct {
isTmpl bool
}

func (t tmplInfo) MustBytes() []byte {
data, err := fs.ReadFile(t.name)
if err != nil {
panic(err)
}
return data
}

// tmplFiles map of generated file's assets info
var tmplFiles = map[string]map[string]tmplInfo{
"gin": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"gin_go_mod.tmpl", true},
"main.go": {"gin_main.tmpl", false},
"mirc/main.go": {"gin_mirc_main.tmpl", true},
"mirc/routes/site.go": {"gin_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"gin_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"gin_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/gin_go_mod.tmpl", true},
"main.go": {"templates/gin_main.tmpl", false},
"mirc/main.go": {"templates/gin_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/gin_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/gin_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/gin_mirc_routes_site_v2.tmpl", false},
},
"chi": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"chi_go_mod.tmpl", true},
"main.go": {"chi_main.tmpl", false},
"mirc/main.go": {"chi_mirc_main.tmpl", true},
"mirc/routes/site.go": {"chi_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"chi_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"chi_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/chi_go_mod.tmpl", true},
"main.go": {"templates/chi_main.tmpl", false},
"mirc/main.go": {"templates/chi_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/chi_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/chi_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/chi_mirc_routes_site_v2.tmpl", false},
},
"mux": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"mux_go_mod.tmpl", true},
"main.go": {"mux_main.tmpl", false},
"mirc/main.go": {"mux_mirc_main.tmpl", true},
"mirc/routes/site.go": {"mux_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"mux_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"mux_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/mux_go_mod.tmpl", true},
"main.go": {"templates/mux_main.tmpl", false},
"mirc/main.go": {"templates/mux_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/mux_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/mux_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/mux_mirc_routes_site_v2.tmpl", false},
},
"echo": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"echo_go_mod.tmpl", true},
"main.go": {"echo_main.tmpl", false},
"mirc/main.go": {"echo_mirc_main.tmpl", true},
"mirc/routes/site.go": {"echo_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"echo_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"echo_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/echo_go_mod.tmpl", true},
"main.go": {"templates/echo_main.tmpl", false},
"mirc/main.go": {"templates/echo_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/echo_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/echo_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/echo_mirc_routes_site_v2.tmpl", false},
},
"iris": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"iris_go_mod.tmpl", true},
"main.go": {"iris_main.tmpl", false},
"mirc/main.go": {"iris_mirc_main.tmpl", true},
"mirc/routes/site.go": {"iris_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"iris_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"iris_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/iris_go_mod.tmpl", true},
"main.go": {"templates/iris_main.tmpl", false},
"mirc/main.go": {"templates/iris_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/iris_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/iris_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/iris_mirc_routes_site_v2.tmpl", false},
},
"fiber": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"fiber_go_mod.tmpl", true},
"main.go": {"fiber_main.tmpl", false},
"mirc/main.go": {"fiber_mirc_main.tmpl", true},
"mirc/routes/site.go": {"fiber_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"fiber_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"fiber_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/fiber_go_mod.tmpl", true},
"main.go": {"templates/fiber_main.tmpl", false},
"mirc/main.go": {"templates/fiber_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/fiber_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/fiber_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/fiber_mirc_routes_site_v2.tmpl", false},
},
"fiber-v2": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"fiber_v2_go_mod.tmpl", true},
"main.go": {"fiber_v2_main.tmpl", false},
"mirc/main.go": {"fiber_v2_mirc_main.tmpl", true},
"mirc/routes/site.go": {"fiber_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"fiber_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"fiber_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/fiber_v2_go_mod.tmpl", true},
"main.go": {"templates/fiber_v2_main.tmpl", false},
"mirc/main.go": {"templates/fiber_v2_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/fiber_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/fiber_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/fiber_mirc_routes_site_v2.tmpl", false},
},
"macaron": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"macaron_go_mod.tmpl", true},
"main.go": {"macaron_main.tmpl", false},
"mirc/main.go": {"macaron_mirc_main.tmpl", true},
"mirc/routes/site.go": {"macaron_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"macaron_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"macaron_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/macaron_go_mod.tmpl", true},
"main.go": {"templates/macaron_main.tmpl", false},
"mirc/main.go": {"templates/macaron_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/macaron_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/macaron_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/macaron_mirc_routes_site_v2.tmpl", false},
},
"httprouter": {
"Makefile": {"makefile.tmpl", false},
"README.md": {"readme.tmpl", false},
"go.mod": {"httprouter_go_mod.tmpl", true},
"main.go": {"httprouter_main.tmpl", false},
"mirc/main.go": {"httprouter_mirc_main.tmpl", true},
"mirc/routes/site.go": {"httprouter_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"httprouter_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"httprouter_mirc_routes_site_v2.tmpl", false},
"Makefile": {"templates/makefile.tmpl", false},
"README.md": {"templates/readme.tmpl", false},
"go.mod": {"templates/httprouter_go_mod.tmpl", true},
"main.go": {"templates/httprouter_main.tmpl", false},
"mirc/main.go": {"templates/httprouter_mirc_main.tmpl", true},
"mirc/routes/site.go": {"templates/httprouter_mirc_routes_site.tmpl", false},
"mirc/routes/v1/site.go": {"templates/httprouter_mirc_routes_site_v1.tmpl", false},
"mirc/routes/v2/site.go": {"templates/httprouter_mirc_routes_site_v2.tmpl", false},
},
}
5 changes: 5 additions & 0 deletions mirc/cmd/templates/makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ build: fmt
run: fmt
go run main.go

.PHONY: mod-tidy
mod-tidy:
@go mod download
@go mod tidy

.PHONY: generate
generate:
@go generate mirc/main.go
Expand Down
Loading

0 comments on commit 4e80367

Please sign in to comment.