diff --git a/Makefile b/Makefile index 086946f..85f34b9 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,14 @@ CUR_SHA=$(shell git log -n1 --pretty='%h') CUR_BRANCH=$(shell git branch --show-current) VERSION=$(shell git describe --exact-match --tags $(CUR_SHA) 2>/dev/null || echo $(CUR_BRANCH)-$(CUR_SHA)) +GIT_PATH:=github.com/metal3d/goreorder +PACKAGE:=$(GIT_PATH)/... +COMMAND_PACKAGE:=$(GIT_PATH)/cmd/goreorder + +DIST_CC:=podman run +DIST_CC_OPTS:=--rm -i --userns keep-id -v $(PWD):/go/src/github.com/metal3d/goreorder:z -w /go/src/github.com/metal3d/goreorder -e CGO_ENABLED=0 docker.io/golang:1.18 CC=go CC_OPTS=-ldflags "-X main.version=$(VERSION)" -PACKAGE:=goreorder/cmd/goreorder install: go install -v $(CC_OPTS) $(PACKAGE) @@ -17,29 +22,64 @@ uninstall: dev-build: go build -v $(CC_OPTS) ./cmd/*.go +.ONESHELL: dist: clean-dist mkdir -p dist $(MAKE) dist/goreorder-linux-amd64 $(MAKE) dist/goreorder-darwin-amd64 $(MAKE) dist/goreorder-windows-amd64.exe $(MAKE) dist/goreorder-freebsd-amd64 + # stripping + strip dist/goreorder-linux-amd64 || true + strip dist/goreorder-darwin-amd64 || true + strip dist/goreorder-windows-amd64.exe || true + strip dist/goreorder-freebsd-amd64 || true + # sign + for i in $$(find dist -type f); do + echo "signing $$i" + echo "sha256: $$(sha256sum $$i)" >> $$i.sha256 + done + dist/goreorder-linux-amd64: - GOOS=linux GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@ + @mkdir -p dist +ifeq ($(strip $(_CNT)),true) + GOOS=linux GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE) +else + $(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@ +endif dist/goreorder-darwin-amd64: - GOOS=darwin GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@ + @mkdir -p dist +ifeq ($(strip $(_CNT)),true) + GOOS=darwin GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE) +else + $(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@ +endif dist/goreorder-windows-amd64.exe: - GOOS=windows GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@ + @mkdir -p dist +ifeq ($(strip $(_CNT)),true) + GOOS=windows GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE) +else + $(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@ +endif dist/goreorder-freebsd-amd64: - GOOS=freebsd GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@ - + @mkdir -p dist +ifeq ($(strip $(_CNT)),true) + GOOS=freebsd GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE) +else + $(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@ +endif clean-dist: rm -rf dist clean: clean-dist rm -f ./goreorder + + +test: + go test ./ordering -v -cover diff --git a/README.md b/README.md index 27d06ea..f036bcb 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Get release or use `go install github.com/metal3d/goreorder@latest` If you want to install from source: ``` -go install -v github.com/metal3d/goreorder/cmd/ +go install -v github.com/metal3d/goreorder/cmd/... ``` You can also get this repository and type: diff --git a/cmd/goreorder/main.go b/cmd/goreorder/main.go index fa9e3f3..da51528 100644 --- a/cmd/goreorder/main.go +++ b/cmd/goreorder/main.go @@ -3,13 +3,14 @@ package main import ( "flag" "fmt" - "goreorder/ordering" "io/ioutil" "log" "os" "os/exec" "path/filepath" "strings" + + "github.com/metal3d/goreorder/ordering" ) var version = "master" // changed at compilation time diff --git a/go.mod b/go.mod index bf002be..ac5db17 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module goreorder +module github.com/metal3d/goreorder go 1.16 diff --git a/ordering/main.go b/ordering/main.go index 756d5dc..ce0d31a 100644 --- a/ordering/main.go +++ b/ordering/main.go @@ -76,6 +76,11 @@ func Parse(filename, formatCommand string) (map[string][]*GoType, map[string][]* if d.Tok == token.TYPE { for _, spec := range d.Specs { if s, ok := spec.(*ast.TypeSpec); ok { + // is it a struct? + if s.Type.(*ast.StructType) == nil { + // no... skip + continue + } typeDef := &GoType{ Name: s.Name.Name, OpeningLine: fset.Position(d.Pos()).Line, diff --git a/ordering/main_test.go b/ordering/main_test.go new file mode 100644 index 0000000..ebaece2 --- /dev/null +++ b/ordering/main_test.go @@ -0,0 +1,122 @@ +package ordering + +import ( + "os" + "path/filepath" + "testing" +) + +const exampleSourceCode = `package main + +// comment 1 +// comment 2 +type Foo struct { + // comment 3 + idfoo int + // comment 4 + namefoo string +} + +func (b *Bar) BadlyBar(){ + print("BadlyBar") +} + +// FooMethod1 comment +func (f *Foo) FooMethod1() { + print("FooMethod1") +} + +type Bar struct { + // comment 5 + idbar int + // comment 6 + namebar string +} + +func NewFoo() *Foo { + return nil +} + +func NewBar() *Bar { + return nil +} +` + +const expectedSource = `package main + +type Bar struct { + // comment 5 + idbar int + // comment 6 + namebar string +} + +func NewBar() *Bar { + return nil +} + +func (b *Bar) BadlyBar() { + print("BadlyBar") +} + +// comment 1 +// comment 2 +type Foo struct { + // comment 3 + idfoo int + // comment 4 + namefoo string +} + +func NewFoo() *Foo { + return nil +} + +// FooMethod1 comment +func (f *Foo) FooMethod1() { + print("FooMethod1") +} +` + +func setup() string { + // write exampleSourceCode in a temporary file and return the filename + dirname, err := os.MkdirTemp("", "goreorder-") + if err != nil { + panic(err) + } + filename := filepath.Join(dirname, "example.go") + file, err := os.Create(filename) + if err != nil { + panic(err) + } + defer file.Close() + _, err = file.WriteString(exampleSourceCode) + if err != nil { + panic(err) + } + + return filename + +} + +func teardown(filename string) { + // remove the temporary file + os.Remove(filename) +} + +func TestReorder(t *testing.T) { + filename := setup() + defer teardown(filename) + + // reorder the file + content, err := ReorderSource(filename, "gofmt", true) + if err != nil { + t.Error(err) + } + + // check the content + if content != expectedSource { + t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedSource, content) + } + +}