The execution operating system (linux, windows, etc.)
$GOFILE
The base name of the file.
+ $GOLINE
+ The line number of the directive in the source file.
$GOPACKAGE
The name of the package of the file containing the directive.
$DOLLAR
"strconv"
"strings"
"unicode"
- "unicode/utf8"
)
var cmdGenerate = &Command{
The execution operating system (linux, windows, etc.)
$GOFILE
The base name of the file.
+ $GOLINE
+ The line number of the directive in the source file.
$GOPACKAGE
The name of the package of the file containing the directive.
$DOLLAR
file string // base name of file.
pkg string
commands map[string][]string
- lineNum int
+ lineNum int // current line number.
}
// run runs the generators in the current file.
}
// Substitute environment variables.
for i, word := range words {
- words[i] = g.expandEnv(word)
+ words[i] = os.Expand(word, g.expandVar)
}
return words
}
panic(stop)
}
-// expandEnv expands any $XXX invocations in word.
-func (g *Generator) expandEnv(word string) string {
- if !strings.ContainsRune(word, '$') {
- return word
+// expandVar expands the $XXX invocation in word. It is called
+// by os.Expand.
+func (g *Generator) expandVar(word string) string {
+ switch word {
+ case "GOARCH":
+ return runtime.GOARCH
+ case "GOOS":
+ return runtime.GOOS
+ case "GOFILE":
+ return g.file
+ case "GOLINE":
+ return fmt.Sprint(g.lineNum)
+ case "GOPACKAGE":
+ return g.pkg
+ case "DOLLAR":
+ return "$"
+ default:
+ return os.Getenv(word)
}
- var buf bytes.Buffer
- var w int
- var r rune
- for i := 0; i < len(word); i += w {
- r, w = utf8.DecodeRuneInString(word[i:])
- if r != '$' {
- buf.WriteRune(r)
- continue
- }
- w += g.identLength(word[i+w:])
- envVar := word[i+1 : i+w]
- var sub string
- switch envVar {
- case "GOARCH":
- sub = runtime.GOARCH
- case "GOOS":
- sub = runtime.GOOS
- case "GOFILE":
- sub = g.file
- case "GOPACKAGE":
- sub = g.pkg
- case "DOLLAR":
- sub = "$"
- default:
- sub = os.Getenv(envVar)
- }
- buf.WriteString(sub)
- }
- return buf.String()
}
// identLength returns the length of the identifier beginning the string.
if ! ./testgo generate ./testdata/generate/test3.go > testdata/std.out; then
echo "go generate ./testdata/generate/test3.go failed to run"
ok=false
-elif ! grep "$GOARCH test3.go p xyzp/test3.go/123" testdata/std.out > /dev/null; then
+elif ! grep "$GOARCH test3.go:7 pabc xyzp/test3.go/123" testdata/std.out > /dev/null; then
echo "go generate ./testdata/generate/test3.go generated wrong output"
ok=false
fi
TEST 'go generate run flag'
if ! ./testgo generate -run y.s ./testdata/generate/test4.go > testdata/std.out; then
- echo "go test -run y.s ./testdata/generate/test4.go failed to run"
+ echo "go test -run yes ./testdata/generate/test4.go failed to run"
ok=false
elif ! grep "yes" testdata/std.out > /dev/null; then
- echo "go generate -run y.s ./testdata/generate/test4.go did not select yes"
+ echo "go generate -run yes ./testdata/generate/test4.go did not select yes"
ok=false
elif grep "no" testdata/std.out > /dev/null; then
- echo "go generate -run y.s ./testdata/generate/test4.go selected no"
+ echo "go generate -run yes ./testdata/generate/test4.go selected no"
ok=false
fi