You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
938 B
Go
53 lines
938 B
Go
4 years ago
|
package gen
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"text/template"
|
||
|
|
||
|
"zero/tools/goctl/util"
|
||
|
)
|
||
|
|
||
|
func GenerateMakefile(goFile, namespace string) error {
|
||
|
relPath, err := util.PathFromGoSrc()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
movePath, err := getMovePath()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
out, err := util.CreateIfNotExist("Makefile")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer out.Close()
|
||
|
|
||
|
t := template.Must(template.New("makefile").Parse(makefileTemplate))
|
||
|
return t.Execute(out, map[string]string{
|
||
|
"rootRelPath": movePath,
|
||
|
"relPath": relPath,
|
||
|
"exeFile": util.FileNameWithoutExt(goFile),
|
||
|
"namespace": namespace,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func getMovePath() (string, error) {
|
||
|
relPath, err := util.PathFromGoSrc()
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
var builder strings.Builder
|
||
|
for range strings.Split(relPath, "/") {
|
||
|
builder.WriteString("../")
|
||
|
}
|
||
|
|
||
|
if move := builder.String(); len(move) == 0 {
|
||
|
return ".", nil
|
||
|
} else {
|
||
|
return move, nil
|
||
|
}
|
||
|
}
|