-
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
539d6b4
commit ca5fb08
Showing
6 changed files
with
183 additions
and
0 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,113 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"go/format" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Println("need 1 argument: <file.go:line>") | ||
os.Exit(1) | ||
} | ||
|
||
bits := strings.SplitN(os.Args[1], ":", 2) | ||
src, err := os.Open(bits[0]) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
defer func() { _ = src.Close() }() | ||
|
||
start, err := strconv.Atoi(bits[1]) | ||
if err != nil { | ||
fmt.Println("invalid line:", err) | ||
os.Exit(1) | ||
} | ||
|
||
i := 0 | ||
|
||
var yamlData []byte | ||
scanner := bufio.NewScanner(src) | ||
|
||
for scanner.Scan() { | ||
i++ | ||
if i <= start { | ||
continue | ||
} | ||
|
||
line := scanner.Text() | ||
if !strings.HasPrefix(line, "//") { | ||
break | ||
} | ||
|
||
if line == "//" { | ||
line = "" | ||
} else if strings.HasPrefix(line, "// ") { | ||
line = strings.TrimPrefix(line, "// ") | ||
} else if strings.HasPrefix(line, "//\t") { | ||
line = strings.TrimPrefix(line, "//\t") | ||
} | ||
|
||
yamlData = append(yamlData, line...) | ||
yamlData = append(yamlData, "\n"...) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Println("scan fail:", err) | ||
os.Exit(1) | ||
} | ||
|
||
log.Printf("yamlData:\n%s\n---", string(yamlData)) | ||
|
||
var j job | ||
if err := yaml.Unmarshal(yamlData, &j); err != nil { | ||
fmt.Println("format fail:", err) | ||
os.Exit(1) | ||
} | ||
|
||
if err := j.do(); err != nil { | ||
fmt.Println("job fail:", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
type job struct { | ||
Dest string `yaml:"dest"` | ||
Vars map[string]interface{} `yaml:"vars"` | ||
Template string `yaml:"template"` | ||
} | ||
|
||
func (j job) do() error { | ||
tpl, err := template.New(j.Dest).Parse(j.Template) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := tpl.Execute(&buf, j.Vars); err != nil { | ||
return err | ||
} | ||
|
||
source := append([]byte("// Code generated by Bang.go DO NOT EDIT.\n\n"), buf.Bytes()...) | ||
content, err := format.Source(source) | ||
if err != nil { | ||
return errors.Wrapf(err, "format fail: %s", string(source)) | ||
} | ||
|
||
if err := os.WriteFile(j.Dest, content, 0644); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,26 @@ | ||
package main | ||
|
||
//go:generate bang $GOFILE:$GOLINE | ||
// dest: example_generated.go | ||
// vars: | ||
// package: main | ||
// items: | ||
// - a | ||
// - b | ||
// - c | ||
// template: > | ||
// package {{.package}} | ||
// | ||
// func main() { | ||
// println("123: {{.vars.items}}") | ||
// } | ||
// | ||
// {{range $k, $v := .items}} | ||
// func f{{$k}}() { | ||
// print("{{$v}}") | ||
// } | ||
// {{end}} | ||
|
||
func main() { | ||
// noop | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
#!/bin/bash | ||
set -ex | ||
cd $( dirname $0 ) | ||
|
||
( | ||
cd .. | ||
go build | ||
go install | ||
) | ||
|
||
go generate . |
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,8 @@ | ||
module github.com/tada-team/bang | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/pkg/errors v0.9.1 | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b | ||
) |
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,6 @@ | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |