-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
104 lines (85 loc) · 2.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import "flag"
import "fmt"
import "io/ioutil"
import "os"
import "os/exec"
const (
help_text string = `
Usage: gmake [OPTION]...
A very lightweight build tool.
--help display this help and exit
--version output version information and exit
`
version_text = `
gmake (aisola/gmake) 0.1
Copyright (C) 2014 Abram C. Isola.
This program comes with ABSOLUTELY NO WARRANTY; for details see
LICENSE. This is free software, and you are welcome to redistribute
it under certain conditions in LICENSE.
`
)
var AST []*Directive
func getDirective(dname string) *Directive {
for i := 0; i < len(AST); i++ {
if AST[i].Name == dname {
return AST[i]
}
}
return nil
}
func combiner(strs []string) string {
var fullstr string
for i := 0; i < len(strs); i++ {
fullstr = fullstr + strs[i] + " "
}
return fullstr
}
// Starts processing
func main() {
help := flag.Bool("help", false, help_text)
version := flag.Bool("version", false, version_text)
flag.Parse()
if *help {
fmt.Println(help_text)
os.Exit(0)
} else if *version {
fmt.Println(version_text)
os.Exit(0)
} else {
// get contents
buf, err := ioutil.ReadFile("GMakefile")
if err != nil {
fmt.Println("gmake: fatal: could not read GMakefile")
return
}
// scan then parse
_, tokens := Lexer("GMAKE", string(buf))
AST = Parse("GMAKE", tokens)
args := flag.Args()
var dir *Directive
if len(args) == 0 {
dir = getDirective("all")
if dir == nil {
fmt.Println("gmake: fatal: no all directive defined")
os.Exit(1)
}
} else {
dir = getDirective(args[0])
if dir == nil {
fmt.Printf("gmake: fatal: no '%s' directive defined")
os.Exit(1)
}
}
for i := 0; i < len(dir.Commands); i++ {
fmt.Println(combiner(dir.Commands[i].Parts))
cm, parts := dir.Commands[i].Parts[0], dir.Commands[i].Parts[1:]
cmd := exec.Command(cm, parts...)
err := cmd.Run()
if err != nil {
fmt.Printf("gmake: fatal: '%s'", err)
os.Exit(1)
}
}
}
}