-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37c29b8
commit 350de60
Showing
9 changed files
with
79 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ |
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 @@ | ||
FROM hello-world |
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,3 @@ | ||
PHONY: targetx | ||
targetx: | ||
docker build ../ |
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,3 @@ | ||
PHONY: targetx | ||
targety: | ||
echo "Hello Y" |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module maker | ||
|
||
go 1.16 | ||
|
||
require github.com/creack/pty v1.1.13 // indirect |
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,2 @@ | ||
github.com/creack/pty v1.1.13 h1:rTPnd/xocYRjutMfqide2zle1u96upp1gm6eUHKi7us= | ||
github.com/creack/pty v1.1.13/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= |
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 |
---|---|---|
@@ -1,109 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"regexp" | ||
|
||
"github.com/creack/pty" | ||
) | ||
|
||
func runMake(dir string, args ...string) error { | ||
dirCommand := []string{"-C", dir} | ||
allArgs := append(dirCommand, args...) | ||
cmd := exec.Command("make", allArgs...) | ||
|
||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
stderr, err := cmd.StderrPipe() | ||
func makefileHasTarget(target string, path string) (bool, error) { | ||
fileContent, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
log.Fatal(err) | ||
return false, err | ||
} | ||
|
||
scannerOut := bufio.NewScanner(stdout) | ||
scannerOut.Split(bufio.ScanLines) | ||
|
||
scannerErr := bufio.NewScanner(stderr) | ||
scannerErr.Split(bufio.ScanLines) | ||
|
||
go func() { | ||
for scannerOut.Scan() { | ||
m := scannerOut.Text() | ||
fmt.Println(m) | ||
} | ||
}() | ||
|
||
for scannerErr.Scan() { | ||
m := scannerErr.Text() | ||
fmt.Println(m) | ||
} | ||
|
||
stringMatch := fmt.Sprintf(`\n?%s:`, target) | ||
|
||
return regexp.Match(stringMatch, fileContent) | ||
} | ||
|
||
if err := cmd.Wait(); err != nil { | ||
log.Fatal(err) | ||
func exists(name string) (bool, error) { | ||
_, err := os.Stat(name) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return false, nil | ||
} | ||
|
||
return nil | ||
return err == nil, err | ||
} | ||
|
||
func findMakeFileWithTarget(cwd, target string) (string, error) { | ||
hasMakefile, err := exists(cwd + "Makefile") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if hasMakefile { | ||
commandExists, err := makefileHasTarget(target, cwd + "Makefile") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
func checkIfCommandExists(command string, path string) (bool, error) { | ||
fileContent, err := ioutil.ReadFile(path) | ||
|
||
if os.IsNotExist(err) { | ||
return false, nil | ||
} else if err != nil { | ||
return false, err | ||
if commandExists { | ||
return cwd, nil | ||
} | ||
} | ||
fileContentString := string(fileContent) | ||
stringMatch := fmt.Sprintf("PHONY:%s\n%s:", command, command) | ||
if strings.Contains(strings.ReplaceAll(fileContentString, " ", ""), stringMatch) { | ||
return true, nil | ||
|
||
if isGitRoot, _ := exists(cwd + ".git"); !isGitRoot { | ||
return findMakeFileWithTarget("./." + cwd, target) | ||
} | ||
|
||
return false, nil | ||
|
||
return "", errors.New("Could not find a makefile with target: " + target) | ||
} | ||
|
||
func runMake(dir string, args ...string) error { | ||
dirCommand := []string{"-C", dir} | ||
allArgs := append(dirCommand, args...) | ||
cmd := exec.Command("make", allArgs...) | ||
|
||
f, err := pty.Start(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
func runRecurse(cwd string) error { | ||
commandExists, err := checkIfCommandExists(os.Args[1], cwd + "Makefile") | ||
if err != nil { | ||
return err | ||
} else if commandExists { | ||
err := runMake(cwd, os.Args[1:]...) | ||
return err | ||
} else if _, err := os.Stat(cwd + ".git"); os.IsNotExist(err) { | ||
err := runRecurse("./." + cwd) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return errors.New("Could not find a makefile with target: " + os.Args[1]) | ||
} | ||
io.Copy(os.Stdout, f) | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if len(os.Args[1:]) < 1 { | ||
fmt.Println("You must provide a make target") | ||
return | ||
log.Fatalln("You must provide a make target") | ||
} | ||
|
||
makeFileDir, err := findMakeFileWithTarget("./", os.Args[1]) | ||
if err != nil { | ||
log.Fatalln("Could not find a makefile:", err.Error()) | ||
} | ||
err := runRecurse("./") | ||
|
||
err = runMake(makeFileDir, os.Args[1:]...) | ||
if err != nil { | ||
fmt.Println("An error occurred while running make:", err.Error()) | ||
log.Fatalln("Could not run makefile:", err.Error()) | ||
} | ||
} |