Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #111 from go-generalize/tsuzu/move-to-sub-packages
Browse files Browse the repository at this point in the history
Move main logic to /generator and Refactor it
  • Loading branch information
54m committed Nov 22, 2021
2 parents 79f0ee2 + 181e48c commit 0e4b9ef
Show file tree
Hide file tree
Showing 45 changed files with 994 additions and 894 deletions.
4 changes: 3 additions & 1 deletion .github/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ linters:
- whitespace

issues:
exclude:
- 'shadow: declaration of "err" shadows declaration at'
exclude-rules:
- path: _test\.go
linters:
Expand All @@ -66,4 +68,4 @@ issues:
service:
golangci-lint-version: 1.23.x
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
- echo "here I can run custom commands, but no preparation needed for this repo"
5 changes: 5 additions & 0 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Binary Files
firestore-repo
./bin/firestore-repo
statik/

# etc
Expand Down
3 changes: 1 addition & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ before:
- go mod tidy

builds:
- main: .
- main: cmd/firestore-repo
binary: firestore-repo
ldflags:
- -s -w
- -X github.com/go-generalize/firestore-repo/main.AppVersion={{ .Version }}
env:
- CGO_ENABLED=0

Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
TEST_OPT=""

.PHONY: bootstrap
bootstrap:
mkdir -p bin
GOBIN=$(PWD)/bin go install github.com/golang/mock/mockgen@latest

.PHONY: test
test: goimports
go test ./... -v ${TEST_OPT}
Expand All @@ -18,4 +23,4 @@ lint:

.PHONY: build
build:
go build -o ./bin/firestore-repo .
go build -o ./bin/firestore-repo ./cmd/firestore-repo
58 changes: 58 additions & 0 deletions cmd/firestore-repo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/go-generalize/firestore-repo/generator"
)

var (
isShowVersion = flag.Bool("v", false, "print version")
isSubCollection = flag.Bool("sub-collection", false, "is SubCollection")
disableMeta = flag.Bool("disable-meta", false, "Disable meta embed")
outputDir = flag.String("o", "./", "Specify directory to generate code in")
packageName = flag.String("p", "", "Specify the package name, default is the same as the original package")
mockGenPath = flag.String("mockgen", "mockgen", "Specify mockgen path")
mockOutputPath = flag.String("mock-output", "mock/mock_{{ .GeneratedFileName }}/mock_{{ .GeneratedFileName }}.go", "Specify directory to generate mock code in")
)

func main() {
appVersion := getAppVersion()

if *isShowVersion {
fmt.Printf("Firestore Model Generator: %s\n", appVersion)
return
}

l := flag.NArg()
if l < 1 {
fmt.Fprintln(os.Stderr, "You have to specify the struct name of target")
os.Exit(1)
}

gen, err := generator.NewGenerator(".")

if err != nil {
fmt.Fprintf(os.Stderr, "failed to initialize generator: %+v\n", err)
os.Exit(1)
}
gen.AppVersion = appVersion

structName := flag.Arg(0)

err = gen.Generate(structName, generator.GenerateOption{
OutputDir: *outputDir,
PackageName: *packageName,
MockGenPath: *mockGenPath,
MockOutputPath: *mockOutputPath,
UseMetaField: !*disableMeta,
Subcollection: *isSubCollection,
})

if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate repository for %s: %+v\n", structName, err)
os.Exit(1)
}
}
13 changes: 13 additions & 0 deletions cmd/firestore-repo/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "runtime/debug"

func getAppVersion() string {
bi, ok := debug.ReadBuildInfo()

if !ok {
return "devel"
}

return bi.Main.Version
}
27 changes: 0 additions & 27 deletions constant.go

This file was deleted.

18 changes: 0 additions & 18 deletions field.go

This file was deleted.

38 changes: 24 additions & 14 deletions emulator_test.go → generator/emulator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//go:build emulator
// +build emulator

package main
package generator

import (
"os"
Expand All @@ -19,6 +20,24 @@ func execTest(t *testing.T) {
}
}

func run(t *testing.T, structName string, useMeta, subCollection bool) {
t.Helper()

gen, err := NewGenerator(".")

if err != nil {
t.Fatalf("NewGenerator failed: %+v", err)
}

opt := NewDefaultGenerateOption()
opt.UseMetaField = useMeta
opt.Subcollection = subCollection

if err := gen.Generate(structName, opt); err != nil {
t.Fatalf("Generate failed: %+v", err)
}
}

func TestGenerator(t *testing.T) {
root, err := os.Getwd()

Expand All @@ -31,13 +50,8 @@ func TestGenerator(t *testing.T) {
tr.Fatalf("chdir failed: %+v", err)
}

if err := run("Task", true, false); err != nil {
tr.Fatalf("failed to generate for testfiles/auto: %+v", err)
}

if err := run("Lock", false, false); err != nil {
tr.Fatalf("failed to generate for testfiles/auto: %+v", err)
}
run(t, "Task", false, false)
run(t, "Lock", true, false)

execTest(tr)
})
Expand All @@ -47,13 +61,9 @@ func TestGenerator(t *testing.T) {
tr.Fatalf("chdir failed: %+v", err)
}

if err := run("Task", true, false); err != nil {
tr.Fatalf("failed to generate for testfiles/not_auto: %+v", err)
}
run(t, "Task", false, false)

if err := run("SubTask", true, true); err != nil {
tr.Fatalf("failed to generate for testfiles/not_auto: %+v", err)
}
run(t, "SubTask", false, true)

execTest(tr)
})
Expand Down
Loading

0 comments on commit 0e4b9ef

Please sign in to comment.