Skip to content

Commit

Permalink
Replace init() by hardcoded standard package names (#29)
Browse files Browse the repository at this point in the history
* chore: generate std packages mapping

* chore: add Makefile
  • Loading branch information
ldez authored Jan 11, 2021
1 parent 8f975be commit 7eb50f3
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
.gitcookies
.idea/
.vscode/
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: clean generate test build

BIN_OUTPUT := $(if $(filter $(shell go env GOOS), windows), dist/gci.exe, dist/gci)

default: clean generate test build

clean:
@echo BIN_OUTPUT: ${BIN_OUTPUT}
rm -rf dist/ cover.out

build: clean
go build -v -trimpath -o ${BIN_OUTPUT} .

test: clean
go test -v -cover ./...

generate:
go generate ./...
91 changes: 91 additions & 0 deletions internal/generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"path/filepath"
"sort"
"strings"

"golang.org/x/tools/go/packages"
)

const (
Expand All @@ -28,29 +26,11 @@ var (
importStartFlag = []byte(`
import (
`)

importEndFlag = []byte(`
)
`)
standardPackages = make(map[string]struct{})
)

func init() {
pkgs, err := packages.Load(nil, "std")
if err != nil {
panic(err)
}

for _, p := range pkgs {
standardPackages[p.PkgPath] = struct{}{}
}
}

func isStandardPackage(pkg string) bool {
_, ok := standardPackages[pkg]
return ok
}

type FlagSet struct {
LocalFlag string
DoWrite, DoDiff *bool
Expand Down
161 changes: 161 additions & 0 deletions pkg/gci/std.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7eb50f3

Please sign in to comment.