Skip to content

Commit

Permalink
Merge pull request #42 from dmcgowan/revert-module-path
Browse files Browse the repository at this point in the history
Revert "Add module name to protobuild file"
  • Loading branch information
estesp authored Sep 9, 2021
2 parents ae399d3 + 093e76c commit d483f63
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 92 deletions.
14 changes: 4 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io/ioutil"
"log"
"path/filepath"

"github.com/pelletier/go-toml"
)
Expand Down Expand Up @@ -73,13 +72,8 @@ func newDefaultConfig() config {
}
}

func readConfig(path string) (config, string, error) {
configFile, err := filepath.Abs(path)
if err != nil {
log.Fatalln(err)
}

p, err := ioutil.ReadFile(configFile)
func readConfig(path string) (config, error) {
p, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -89,8 +83,8 @@ func readConfig(path string) (config, string, error) {
}

if c.Version != configVersion {
return config{}, "", fmt.Errorf("unknown file version %v; please upgrade to %v", c.Version, configVersion)
return config{}, fmt.Errorf("unknown file version %v; please upgrade to %v", c.Version, configVersion)
}

return c, filepath.Dir(configFile), nil
return c, nil
}
90 changes: 8 additions & 82 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -51,7 +50,7 @@ func init() {
func main() {
flag.Parse()

c, configRoot, err := readConfig(configPath)
c, err := readConfig(configPath)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -61,74 +60,21 @@ func main() {
log.Fatalln(err)
}

globalGopath, err := gopathSrc()
gopath, err := gopathSrc()
if err != nil {
log.Fatalln(err)
}

module, err := gomodule()
gopathCurrent, err := gopathCurrent()
if err != nil {
log.Fatalln(err)
}

var (
gopath string
outputDir string
)
// For some reason, the golang protobuf generator makes the god awful
// decision to output the files relative to the gopath root. It doesn't do
// this only in the case where you give it ".".
outputDir := filepath.Join(gopathCurrent, "src")

// If module is provided, create a temporary gopath, place the module
// into that path and rewrite the paths
if module != "" {
tmpgopath, err := ioutil.TempDir("", "protobuild-")
if err != nil {
log.Fatalln(err)
}
defer os.RemoveAll(tmpgopath)

gopath = gopathJoin(tmpgopath, "src")
outputDir = gopath

sympath := gopathJoin(gopath, module)
if err := os.MkdirAll(filepath.Dir(sympath), 0755); err != nil {
log.Fatalln(err)
}

if err := os.Symlink(configRoot, sympath); err != nil {
log.Fatalln(err)
}

// Update pkgInfos to new gopath
for i, pkg := range pkgInfos {
relPath, err := filepath.Rel(configRoot, pkg.Dir)
if err != nil {
log.Fatalln(err)
}
pkgInfos[i].Dir = filepath.Join(outputDir, module, relPath)

for j, f := range pkg.ProtoFiles {
relPath, err = filepath.Rel(configRoot, f)
if err != nil {
log.Fatalln(err)
}
pkg.ProtoFiles[j] = filepath.Join(outputDir, module, relPath)
}
pkgInfos[i].ProtoFiles = pkg.ProtoFiles

}
} else {
gopath = globalGopath

currentGopath, err := gopathCurrent()
if err != nil {
log.Fatalln(err)
}

// For some reason, the golang protobuf generator makes the god awful
// decision to output the files relative to the gopath root. It doesn't do
// this only in the case where you give it ".".
outputDir = filepath.Join(currentGopath, "src")

}
// Index overrides by target import path
overrides := map[string]struct {
Prefixes []string
Expand Down Expand Up @@ -213,7 +159,7 @@ func main() {
// handle packages that we want to have as an include root from any of
// the gopaths.
for _, pkg := range c.Includes.Packages {
includes = append(includes, gopathJoin(globalGopath, pkg))
includes = append(includes, gopathJoin(gopath, pkg))
}

includes = append(includes, gopath)
Expand Down Expand Up @@ -433,26 +379,6 @@ func gopathJoin(gopath, element string) string {
return strings.Join(elements, string(filepath.ListSeparator))
}

// gomodule returns the name of the current module if called from a go module
func gomodule() (string, error) {
cmd := exec.Command("go", "list", "-m", "-json")
out, err := cmd.Output()
if err != nil {
return "", err
}
var listOutput struct {
Path string
GoMod string
}
if err := json.Unmarshal(out, &listOutput); err != nil {
return "", err
}
if listOutput.GoMod != "" {
return listOutput.Path, nil
}
return "", nil
}

// descriptorProto returns the full path to google/protobuf/descriptor.proto
// which might be different depending on whether it was installed. The argument
// is the list of paths to check.
Expand Down

0 comments on commit d483f63

Please sign in to comment.