Skip to content

Commit d5e1191

Browse files
author
Joyer Huang
committed
add args support and refFiles supports
1 parent db669ca commit d5e1191

File tree

1 file changed

+73
-39
lines changed

1 file changed

+73
-39
lines changed

cmd/goscript.go

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
"path"
1818
"runtime"
1919
"strings"
20+
"io"
21+
"bufio"
22+
"regexp"
2023
)
2124

2225
// Error exit status
@@ -32,15 +35,59 @@ var ENVIRON []string
3235
var fShared = flag.Bool("shared", false,
3336
"whether the script is used on a mixed network of machines or "+
3437
"systems from a shared filesystem")
35-
36-
var hasLineInterpreter = flag.Bool("li", false, "wheter the script has the first line as a #! comment. Defaults to false")
38+
3739

3840
func usage() {
3941
flag.PrintDefaults()
4042
os.Exit(ERROR)
4143
}
4244
// ===
4345

46+
func error(s string) {
47+
fmt.Printf("Error: %s\n", s)
48+
os.Exit(1)
49+
}
50+
51+
func warn(s string) {
52+
fmt.Printf("Warnning: %s\n", s)
53+
}
54+
55+
func isource(dst , src string) (refFiles []string) {
56+
refFiles = make([]string, 1)
57+
file1, err := os.Open(src, os.O_RDONLY, 0)
58+
if err != nil {
59+
error(fmt.Sprintf("Can't open %s", src))
60+
}
61+
defer file1.Close()
62+
63+
os.Remove(dst)
64+
file2, err := os.Open(dst, os.O_WRONLY | os.O_CREAT, 0644)
65+
if err != nil {
66+
error(fmt.Sprintf("Can't open %s", flag.Args()[1]))
67+
}
68+
defer file2.Close()
69+
70+
bufFile1 := bufio.NewReader(file1)
71+
bufFile2 := bufio.NewWriter(file2)
72+
defer bufFile2.Flush()
73+
head, _ := bufFile1.ReadString('\n')
74+
if len(head) >= 2 && head[0:2] != "#!" {
75+
//error("First Line: " + head)
76+
bufFile2.WriteString(head + "\n")
77+
} else {
78+
bufFile2.WriteString("\n")
79+
}
80+
refline, _ := bufFile1.ReadString('\n')
81+
if len(refline) >= 5 && refline[0:5] != "///<>" {
82+
bufFile2.WriteString(refline + "\n")
83+
} else {
84+
refFiles = regexp.MustCompile("[_a-zA-Z]+\\.go").FindAllString(refline, 1000)
85+
bufFile2.WriteString(refline + "\n")
86+
}
87+
io.Copy(bufFile2, bufFile1)
88+
return
89+
}
90+
4491

4592
func main() {
4693
var binaryDir, binaryPath string
@@ -71,7 +118,10 @@ Flags:
71118
binaryDir = path.Join(scriptDir, ".go", runtime.GOOS+"_"+runtime.GOARCH)
72119
}
73120
ext := path.Ext(scriptName)
74-
binaryPath = path.Join(binaryDir, strings.TrimRight(scriptName, ext))
121+
binaryPath = path.Join(binaryDir, scriptName[0:len(scriptName) - len(ext)])
122+
// warn(ext)
123+
// warn(binaryDir)
124+
// warn(binaryPath)
75125

76126
// Check directory
77127
if ok := Exist(binaryDir); !ok {
@@ -86,7 +136,7 @@ Flags:
86136
scriptMtime := getTime(scriptPath)
87137
binaryMtime := getTime(binaryPath)
88138

89-
if scriptMtime == binaryMtime {
139+
if scriptMtime <= binaryMtime {
90140
goto _run
91141
}
92142
}
@@ -98,20 +148,21 @@ Flags:
98148
}*/
99149

100150
// === Compile and link
101-
scriptMtime := getTime(scriptPath)
102-
if *hasLineInterpreter {
103-
comment(scriptPath, true)
104-
}
151+
// scriptMtime := getTime(scriptPath)
152+
153+
//comment(scriptPath)
154+
iscriptPath := scriptPath + ".i"
155+
refFiles := isource(iscriptPath, scriptPath)
105156
compiler, linker, archExt := toolchain()
106157

107158
ENVIRON = os.Environ()
108159
objectPath := path.Join(binaryDir, "_go_."+archExt)
109160

110-
cmdArgs := []string{path.Base(compiler), "-o", objectPath, scriptPath}
161+
cmdArgs := []string{path.Base(compiler), "-o", objectPath, iscriptPath}
162+
cmdArgs = append(cmdArgs, refFiles...)
111163
exitCode := run(compiler, cmdArgs, "")
112-
if *hasLineInterpreter {
113-
comment(scriptPath, false)
114-
}
164+
//comment(scriptPath, false)
165+
os.Remove(iscriptPath)
115166
if exitCode != 0 {
116167
os.Exit(exitCode)
117168
}
@@ -122,8 +173,8 @@ Flags:
122173
}
123174

124175
// Set mtime of executable just like the source file
125-
//setTime(scriptPath, scriptMtime)
126-
setTime(binaryPath, scriptMtime)
176+
// setTime(scriptPath, scriptMtime)
177+
// setTime(binaryPath, scriptMtime)
127178

128179
// Cleaning
129180
/*if err := os.Remove(objectPath); err != nil {
@@ -132,7 +183,14 @@ Flags:
132183
}*/
133184

134185
_run:
135-
exitCode = run(binaryPath, []string{scriptPath}, "")
186+
// for a,v := range flag.Args() {
187+
// fmt.Print(a)
188+
// fmt.Println(v)
189+
// }
190+
// normArgs := make([]string, len(flag.Args()) + 1)
191+
// // normArgs = append(normArgs, binaryPath)
192+
// normArgs = append(normArgs, flag.Args()...)
193+
exitCode = run(binaryPath, /*normArgs*/ flag.Args()[:], scriptDir)
136194
os.Exit(exitCode)
137195
}
138196

@@ -163,30 +221,6 @@ func setTime(filename string, mtime int64) {
163221
_time(filename, mtime)
164222
}
165223

166-
// Comments or comments out the line interpreter.
167-
func comment(filename string, ok bool) {
168-
file, err := os.Open(filename, os.O_WRONLY, 0)
169-
if err != nil {
170-
goto _error
171-
}
172-
defer file.Close()
173-
174-
if ok {
175-
if _, err = file.Write([]byte("//")); err != nil {
176-
goto _error
177-
}
178-
} else {
179-
if _, err = file.Write([]byte("#!")); err != nil {
180-
goto _error
181-
}
182-
}
183-
184-
return
185-
186-
_error:
187-
fmt.Fprintf(os.Stderr, "Could not write: %s\n", err)
188-
os.Exit(ERROR)
189-
}
190224

191225
// Checks if exist a file.
192226
func Exist(name string) bool {

0 commit comments

Comments
 (0)