aboutsummaryrefslogtreecommitdiffstats
path: root/binapigen
diff options
context:
space:
mode:
authorNathan Skrzypczak <nathan.skrzypczak@gmail.com>2020-08-21 17:25:53 +0200
committerOndrej Fabry <ofabry@cisco.com>2020-09-03 15:11:13 +0000
commitc94a962279858fb13eaacc689f47aed358373e44 (patch)
tree0cade1807c10ed53bf7c1b623f4d26da639356f6 /binapigen
parent42d11af03300fe0a3476c32ad8c70297862d9320 (diff)
Improve doc & fix import ordering
This also updates /binapi and adds a new make command to generate api files out of a local vpp repo clone Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com> Change-Id: Iff7965388a74ecd21af80f10b5a59d4ed8da6340
Diffstat (limited to 'binapigen')
-rw-r--r--binapigen/generator.go24
-rw-r--r--binapigen/vppapi/util.go19
2 files changed, 33 insertions, 10 deletions
diff --git a/binapigen/generator.go b/binapigen/generator.go
index a6f1615..e5eed5a 100644
--- a/binapigen/generator.go
+++ b/binapigen/generator.go
@@ -223,6 +223,13 @@ func (g *GenFile) Content() ([]byte, error) {
return g.injectImports(g.buf.Bytes())
}
+func getImportClass(importPath string) int {
+ if !strings.Contains(importPath, ".") {
+ return 0 /* std */
+ }
+ return 1 /* External */
+}
+
// injectImports parses source, injects import block declaration with all imports and return formatted
func (g *GenFile) injectImports(original []byte) ([]byte, error) {
// Parse source code
@@ -259,7 +266,12 @@ func (g *GenFile) injectImports(original []byte) ([]byte, error) {
}
// Sort imports by import path
sort.Slice(importPaths, func(i, j int) bool {
- return importPaths[i].Path < importPaths[j].Path
+ ci := getImportClass(importPaths[i].Path)
+ cj := getImportClass(importPaths[j].Path)
+ if ci == cj {
+ return importPaths[i].Path < importPaths[j].Path
+ }
+ return ci < cj
})
// Inject new import block into parsed AST
if len(importPaths) > 0 {
@@ -275,14 +287,20 @@ func (g *GenFile) injectImports(original []byte) ([]byte, error) {
}
// Prepare the import block
impDecl := &ast.GenDecl{Tok: token.IMPORT, TokPos: pos, Lparen: pos, Rparen: pos}
- for _, importPath := range importPaths {
+ for i, importPath := range importPaths {
var name *ast.Ident
if importPath.Name == "_" || strings.Contains(importPath.Path, ".") {
name = &ast.Ident{Name: importPath.Name, NamePos: pos}
}
+ value := strconv.Quote(importPath.Path)
+ if i < len(importPaths)-1 {
+ if getImportClass(importPath.Path) != getImportClass(importPaths[i+1].Path) {
+ value += "\n"
+ }
+ }
impDecl.Specs = append(impDecl.Specs, &ast.ImportSpec{
Name: name,
- Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(importPath.Path), ValuePos: pos},
+ Path: &ast.BasicLit{Kind: token.STRING, Value: value, ValuePos: pos},
EndPos: pos,
})
}
diff --git a/binapigen/vppapi/util.go b/binapigen/vppapi/util.go
index 87f2e55..3db8ef5 100644
--- a/binapigen/vppapi/util.go
+++ b/binapigen/vppapi/util.go
@@ -26,7 +26,9 @@ import (
)
const (
- VPPVersionEnvVar = "VPP_VERSION"
+ VPPVersionEnvVar = "VPP_VERSION"
+ VPPDirEnvVar = "VPP_DIR"
+ versionScriptPath = "./src/scripts/version"
)
// ResolveVPPVersion resolves version of the VPP for target directory.
@@ -35,7 +37,7 @@ const (
func ResolveVPPVersion(apidir string) string {
// check env variable override
if ver := os.Getenv(VPPVersionEnvVar); ver != "" {
- logrus.Debugf("VPP version was manually set to %q via %s env var", ver, VPPVersionEnvVar)
+ logrus.Infof("VPP version was manually set to %q via %s env var", ver, VPPVersionEnvVar)
return ver
}
@@ -60,7 +62,7 @@ func ResolveVPPVersion(apidir string) string {
if err != nil {
logrus.Warnf("resolving VPP version from version script failed: %v", err)
} else {
- logrus.Debugf("resolved VPP version from version script: %v", version)
+ logrus.Infof("resolved VPP version from version script: %v", version)
return version
}
}
@@ -85,14 +87,13 @@ func GetVPPVersionInstalled() (string, error) {
return strings.TrimSpace(string(out)), nil
}
-const versionScriptPath = "./src/scripts/version"
-
// GetVPPVersionRepo retrieves VPP version using script in repo directory.
func GetVPPVersionRepo(repoDir string) (string, error) {
- if _, err := os.Stat(versionScriptPath); err != nil {
+ scriptPath := path.Join(repoDir, versionScriptPath)
+ if _, err := os.Stat(scriptPath); err != nil {
return "", err
}
- cmd := exec.Command(versionScriptPath)
+ cmd := exec.Command(scriptPath)
cmd.Dir = repoDir
out, err := cmd.CombinedOutput()
if err != nil {
@@ -102,6 +103,10 @@ func GetVPPVersionRepo(repoDir string) (string, error) {
}
func findGitRepoRootDir(dir string) (string, error) {
+ if conf := os.Getenv(VPPDirEnvVar); conf != "" {
+ logrus.Infof("VPP directory was manually set to %q via %s env var", conf, VPPDirEnvVar)
+ return conf, nil
+ }
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Dir = dir
out, err := cmd.CombinedOutput()