Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ckpt
  • Loading branch information
vangent committed Nov 16, 2018
commit cf840b5f6eeeddb49d09c376c4609d203b9b7235
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/mattn/goveralls v0.0.2 // indirect
github.com/opencensus-integrations/ocsql v0.1.1
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v0.9.0 // indirect
github.com/prometheus/common v0.0.0-20181015124227-bcb74de08d37 // indirect
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d // indirect
Expand Down
3 changes: 2 additions & 1 deletion samples/guestbook/wire_gen.go

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

33 changes: 32 additions & 1 deletion wire/cmd/wire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"golang.org/x/tools/go/types/typeutil"
)

const usage = "usage: wire [gen] [PKG] | wire show [...] | wire check [...]"
const usage = "usage: wire [gen] [PKG] | wire show [...] | wire check [...] | wire diff [...]"

func main() {
var err error
Expand All @@ -51,6 +51,10 @@ func main() {
err = check(".")
case len(os.Args) > 2 && os.Args[1] == "check":
err = check(os.Args[2:]...)
case len(os.Args) == 2 && os.Args[1] == "diff":
err = diff(".")
case len(os.Args) > 2 && os.Args[1] == "diff":
err = diff(os.Args[2:]...)
case len(os.Args) == 2:
err = generate(os.Args[1])
case len(os.Args) == 3 && os.Args[1] == "gen":
Expand Down Expand Up @@ -85,6 +89,33 @@ func generate(pkg string) error {
return out.Commit()
}

// diff runs the diff subcommand. Given a package, diff will generate
// the content for the wire_gen.go file, and output the diff against the
// existing file.
func diff(pkgs ...string) error {
wd, err := os.Getwd()
if err != nil {
return err
}
for _, pkg := range pkgs {
fmt.Println(pkg)
out, errs := wire.Generate(context.Background(), wd, os.Environ(), pkg)
if len(errs) > 0 {
logErrors(errs)
return errors.New("generate failed")
}
if len(out.Content) == 0 {
// No Wire directives, don't write anything.
fmt.Fprintln(os.Stderr, "wire: no injector found for", pkg)
continue
}
if err := out.Diff(os.Stderr); err != nil {
return err
}
}
return nil
}

// show runs the show subcommand.
//
// Given one or more packages, show will find all the provider sets
Expand Down
12 changes: 12 additions & 0 deletions wire/internal/wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go/printer"
"go/token"
"go/types"
"io"
"io/ioutil"
"path/filepath"
"sort"
Expand All @@ -34,6 +35,7 @@ import (
"unicode"
"unicode/utf8"

"github.com/pmezard/go-difflib/difflib"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
Expand All @@ -53,6 +55,16 @@ func (gen GeneratedFile) Commit() error {
return ioutil.WriteFile(gen.Path, gen.Content, 0666)
}

// Diff writes a diff of the current file on disk vs the generated file to out.
func (gen GeneratedFile) Diff(out io.Writer) error {
// Assume the current file is empty if we can't read it.
cur, _ := ioutil.ReadFile(gen.Path)
return difflib.WriteUnifiedDiff(out, difflib.UnifiedDiff{
A: difflib.SplitLines(string(cur)),
B: difflib.SplitLines(string(gen.Content)),
})
}

// Generate performs dependency injection for a single package,
// returning the gofmt'd Go source code. The package pattern is defined
// by the underlying build system. For the go tool, this is described at
Expand Down