-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] go.mod & go.work FilePath ReplaceDirective support
- Loading branch information
1 parent
6b208eb
commit e599cdf
Showing
30 changed files
with
640 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* Copyright 2019 The Bazel Authors. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func copyTree(destRoot, srcRoot string) error { | ||
return filepath.Walk(srcRoot, func(src string, info os.FileInfo, e error) (err error) { | ||
if e != nil { | ||
return e | ||
} | ||
rel, err := filepath.Rel(srcRoot, src) | ||
if err != nil { | ||
return err | ||
} | ||
if rel == "." { | ||
return nil | ||
} | ||
dest := filepath.Join(destRoot, rel) | ||
|
||
if info.IsDir() { | ||
return os.Mkdir(dest, 0o777) | ||
} else { | ||
r, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() | ||
w, err := os.Create(dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if cerr := w.Close(); err == nil && cerr != nil { | ||
err = cerr | ||
} | ||
}() | ||
_, err = io.Copy(w, r) | ||
return err | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"go/build" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
type GoModDownloadResult struct { | ||
Dir string | ||
Sum string | ||
Error string | ||
} | ||
|
||
func isModeCacherwSupported() bool { | ||
// Check whether -modcacherw is supported. | ||
// Assume that fetch_repo was built with the same version of Go we're running. | ||
modcacherw := false | ||
for _, tag := range build.Default.ReleaseTags { | ||
if tag == "go1.14" { | ||
modcacherw = true | ||
break | ||
} | ||
} | ||
|
||
return modcacherw | ||
} | ||
func findGoPath() string { | ||
// Locate the go binary. If GOROOT is set, we'll use that one; otherwise, | ||
// we'll use PATH. | ||
goPath := "go" | ||
if runtime.GOOS == "windows" { | ||
goPath += ".exe" | ||
} | ||
if goroot, ok := os.LookupEnv("GOROOT"); ok { | ||
goPath = filepath.Join(goroot, "bin", goPath) | ||
} | ||
return goPath | ||
} | ||
|
||
func runGoModDownload(dl *GoModDownloadResult, dest string, importpath string, version string) error { | ||
buf := &bytes.Buffer{} | ||
bufErr := &bytes.Buffer{} | ||
cmd := exec.Command(findGoPath(), "mod", "download", "-json") | ||
cmd.Dir = dest | ||
if isModeCacherwSupported() { | ||
cmd.Args = append(cmd.Args, "-modcacherw") | ||
} | ||
|
||
if version != "" && importpath != "" { | ||
cmd.Args = append(cmd.Args, importpath+"@"+version) | ||
} | ||
|
||
cmd.Stdout = buf | ||
cmd.Stderr = bufErr | ||
fmt.Printf("Running: %s %s\n", cmd.Path, strings.Join(cmd.Args, " ")) | ||
dlErr := cmd.Run() | ||
if dlErr != nil { | ||
if _, ok := dlErr.(*exec.ExitError); !ok { | ||
if bufErr.Len() > 0 { | ||
return fmt.Errorf("!%s %s: %s", cmd.Path, strings.Join(cmd.Args, " "), bufErr.Bytes()) | ||
} else { | ||
return fmt.Errorf("!!%s %s: %v", cmd.Path, strings.Join(cmd.Args, " "), dlErr) | ||
} | ||
} | ||
} | ||
|
||
// Parse the JSON output. | ||
if err := json.Unmarshal(buf.Bytes(), &dl); err != nil { | ||
if bufErr.Len() > 0 { | ||
return fmt.Errorf("3%s %s: %s", cmd.Path, strings.Join(cmd.Args, " "), bufErr.Bytes()) | ||
} else { | ||
return fmt.Errorf("4%s %s: error parsing JSON: %v error: %v", cmd.Path, strings.Join(cmd.Args, " "), buf, err) | ||
} | ||
} | ||
if dl.Error != "" { | ||
return errors.New(dl.Error) | ||
} | ||
if dlErr != nil { | ||
return dlErr | ||
} | ||
|
||
fmt.Printf("Downloaded: %s\n", dl.Dir) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.