-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/common build utils #22
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e95f717
refactor(build): Refactor common utils + Bower builder
elldritch 6284822
refactor(build): Refactor Composer builder
elldritch c73cf5d
refactor(go): Refactor Go builder
elldritch 1f499e5
refactor(mvn): Refactor Maven builder
elldritch 8feac38
refactor(nodejs): Refactor Nodejs builder
elldritch c76b0d4
refactor(ruby): Refactor Ruby builder
elldritch f21c9ab
refactor(sbt): Refactor SBT builder
elldritch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,143 @@ | ||
package build | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
logging "github.com/op/go-logging" | ||
) | ||
|
||
var commonLogger = logging.MustGetLogger("common") | ||
|
||
// Utilities for finding files and manifests | ||
func hasFile(elem ...string) (bool, error) { | ||
_, err := os.Stat(filepath.Join(elem...)) | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return !os.IsNotExist(err), err | ||
} | ||
|
||
func orPredicates(predicates ...fileChecker) fileChecker { | ||
return func(path string) (bool, error) { | ||
for _, predicate := range predicates { | ||
ok, err := predicate(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
if ok { | ||
return ok, nil | ||
} | ||
} | ||
return false, nil | ||
} | ||
} | ||
|
||
type fileChecker func(path string) (bool, error) | ||
|
||
func findAncestor(stopWhen fileChecker, path string) (string, bool, error) { | ||
absPath, err := filepath.Abs(path) | ||
if absPath == string(filepath.Separator) { | ||
return "", false, nil | ||
} | ||
if err != nil { | ||
return "", false, err | ||
} | ||
stop, err := stopWhen(absPath) | ||
if err != nil { | ||
return "", false, err | ||
} | ||
if stop { | ||
return absPath, true, nil | ||
} | ||
return findAncestor(stopWhen, filepath.Dir(path)) | ||
} | ||
|
||
// Utilities around `exec.Command` | ||
func run(name string, arg ...string) (string, string, error) { | ||
var stderr bytes.Buffer | ||
cmd := exec.Command(name, arg...) | ||
cmd.Stderr = &stderr | ||
stdout, err := cmd.Output() | ||
return string(stdout), stderr.String(), err | ||
} | ||
|
||
func runInDir(dir string, name string, arg ...string) (string, string, error) { | ||
var stderr bytes.Buffer | ||
cmd := exec.Command(name, arg...) | ||
cmd.Dir = dir | ||
cmd.Stderr = &stderr | ||
stdout, err := cmd.Output() | ||
return string(stdout), stderr.String(), err | ||
} | ||
|
||
// Utilities for debug logging | ||
func runLogged(logger *logging.Logger, dir string, name string, arg ...string) (string, string, error) { | ||
cmd := strings.Join(append([]string{name}, arg...), " ") | ||
logger.Debugf("Running `%s`...", cmd) | ||
stdout, stderr, err := runInDir(dir, name, arg...) | ||
if err != nil { | ||
logger.Debugf("Running `%s` failed: %#v %#v", cmd, err, stderr) | ||
return "", "", fmt.Errorf("running `%s` failed: %#v %#v", cmd, err, stderr) | ||
} | ||
logger.Debugf("Done running `%s`: %#v %#v", stdout, stderr) | ||
return stdout, stderr, nil | ||
} | ||
|
||
func parseLogged(logger *logging.Logger, file string, v interface{}) error { | ||
return parseLoggedWithUnmarshaller(logger, file, v, json.Unmarshal) | ||
} | ||
|
||
type unmarshaller func(data []byte, v interface{}) error | ||
|
||
func parseLoggedWithUnmarshaller(logger *logging.Logger, file string, v interface{}, unmarshal unmarshaller) error { | ||
logger.Debugf("Parsing %s...", file) | ||
|
||
contents, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
logger.Debugf("Error reading %s: %s", file, err.Error()) | ||
return err | ||
} | ||
err = unmarshal(contents, v) | ||
if err != nil { | ||
logger.Debugf("Error parsing %s: %#v %#v", file, err, contents) | ||
return err | ||
} | ||
|
||
logger.Debugf("Done parsing %s.", file) | ||
return nil | ||
} | ||
|
||
// Utilities for detecting which binary to use | ||
type versionResolver func(cmd string) (string, error) | ||
|
||
func whichWithResolver(cmds []string, getVersion versionResolver) (string, string, error) { | ||
for _, cmd := range cmds { | ||
version, err := getVersion(cmd) | ||
if err == nil { | ||
return cmd, version, nil | ||
} | ||
commonLogger.Debugf("Tried resolving `%s` but did not work: %#v %#v", cmd, err, version) | ||
} | ||
return "", "", errors.New("could not resolve version") | ||
} | ||
|
||
func which(versionFlags string, cmds ...string) (string, string, error) { | ||
return whichWithResolver(cmds, func(cmd string) (string, error) { | ||
stdout, stderr, err := run(cmd, strings.Split(versionFlags, " ")...) | ||
if err != nil { | ||
return "", err | ||
} | ||
if stdout == "" { | ||
return stderr, nil | ||
} | ||
return stdout, nil | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome. is this backwards compatible? we should be OK w/ breaking compatibility until 1.0