Skip to content

Commit 1b9e8ae

Browse files
committed
Go: Modify Autobuild to return an array of scripts that were run
1 parent bbc359e commit 1b9e8ae

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

go/extractor/autobuilder/autobuilder.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ func checkExtractorRun() bool {
5454
}
5555

5656
// tryBuildIfExists tries to run the command `cmd args...` if the file `buildFile` exists and is not
57-
// a directory. Returns true if the command was successful and false if not.
58-
func tryBuildIfExists(buildFile, cmd string, args ...string) bool {
59-
if util.FileExists(buildFile) {
57+
// a directory. Returns values indicating whether the script succeeded as well as whether the script was found.
58+
func tryBuildIfExists(buildFile, cmd string, args ...string) (scriptSuccess bool, scriptFound bool) {
59+
scriptSuccess = false
60+
scriptFound = util.FileExists(buildFile)
61+
if scriptFound {
6062
log.Printf("%s found.\n", buildFile)
61-
return tryBuild(cmd, args...)
63+
scriptSuccess = tryBuild(cmd, args...)
6264
}
63-
return false
65+
return
6466
}
6567

6668
// tryBuild tries to run `cmd args...`, returning true if successful and false if not.
@@ -92,11 +94,23 @@ var BuildScripts = []BuildScript{
9294
// Autobuild attempts to detect build systems based on the presence of build scripts from the
9395
// list in `BuildScripts` and run the corresponding command. This may invoke zero or more
9496
// build scripts in the order given by `BuildScripts`.
95-
func Autobuild() bool {
97+
func Autobuild() (scriptSuccess bool, scriptsExecuted []string) {
98+
scriptSuccess = false
99+
scriptsExecuted = []string{}
100+
96101
for _, script := range BuildScripts {
97-
if tryBuildIfExists(script.Filename, script.Tool) {
98-
return true
102+
// Try to run the build script
103+
success, scriptFound := tryBuildIfExists(script.Filename, script.Tool)
104+
105+
// If it was found, we attempted to run it: add it to the array.
106+
if scriptFound {
107+
scriptsExecuted = append(scriptsExecuted, script.Filename)
108+
}
109+
// If it was successfully executed, we stop here.
110+
if success {
111+
scriptSuccess = true
112+
return
99113
}
100114
}
101-
return false
115+
return
102116
}

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func setGopath(root string) {
325325
func buildWithoutCustomCommands(modMode project.ModMode) (shouldInstallDependencies bool) {
326326
shouldInstallDependencies = false
327327
// try to run a build script
328-
scriptSucceeded := autobuilder.Autobuild()
328+
scriptSucceeded, _ := autobuilder.Autobuild()
329329

330330
// If there is no build script we could invoke successfully or there are still dependency errors;
331331
// we'll try to install dependencies ourselves in the normal Go way.

go/extractor/cli/go-build-runner/go-build-runner.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package main
22

33
import (
4-
"github.com/github/codeql-go/extractor/util"
54
"log"
65
"os"
76
"os/exec"
87
"path/filepath"
98
"runtime"
109

10+
"github.com/github/codeql-go/extractor/util"
11+
1112
"github.com/github/codeql-go/extractor/autobuilder"
1213
)
1314

1415
func main() {
1516
// check if a build command has successfully extracted something
1617
autobuilder.CheckExtracted = true
17-
if autobuilder.Autobuild() {
18+
scriptSuccess, _ := autobuilder.Autobuild()
19+
if scriptSuccess {
1820
return
1921
}
2022

0 commit comments

Comments
 (0)