Skip to content

Commit

Permalink
clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSimpleZ committed Jun 26, 2021
1 parent 37c29b8 commit 350de60
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 88 deletions.
15 changes: 15 additions & 0 deletions .gitignore
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/
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"mode": "auto",
"program": "${fileDirname}",
"args": [
"tar"
"targetx"
],
"cwd": "./test1"
"cwd": "./example/subfolder"
}
]
}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM hello-world
16 changes: 0 additions & 16 deletions Makefile

This file was deleted.

3 changes: 3 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PHONY: targetx
targetx:
docker build ../
3 changes: 3 additions & 0 deletions example/subfolder/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PHONY: targetx
targety:
echo "Hello Y"
2 changes: 2 additions & 0 deletions go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=
121 changes: 51 additions & 70 deletions main.go
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())
}
}

0 comments on commit 350de60

Please sign in to comment.