build controller + types is ok

This commit is contained in:
Adrien Marquès 2018-07-07 16:35:20 +02:00
parent c3bcf40e43
commit 5cc1e091e9
4 changed files with 174 additions and 5 deletions

View File

@ -25,7 +25,7 @@ func Info(s ...string) string {
func Title(s string) { func Title(s string) {
title_index++ title_index++
fmt.Printf("\n%s (%d) %s %s\n", Color(33, ">>", false), title_index, s, Color(33, "<<", false)) fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), title_index, s, Color(33, "<<", false))
} }

View File

@ -1,9 +1,72 @@
package main package main
import (
"fmt"
"git.xdrm.io/go/aicra/clifmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Builds controllers as plugins (.so) // Builds controllers as plugins (.so)
// from the sources in the @in folder // from the sources in the @in folder
// recursively and generate .so files // recursively and generate .so files
// into the @out folder with the same structure // into the @out folder with the same structure
func buildControllers(in string, out string) error { func buildControllers(in string, out string) error {
clifmt.Title("compile controllers")
/* (1) Create build folder */
clifmt.Align(" . create output folder")
err := os.MkdirAll(out, os.ModePerm)
if err != nil {
return err
}
fmt.Printf("ok\n")
/* (1) List recursively */
sources := []string{}
err = filepath.Walk(in, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, "i.go") {
sources = append(sources, path)
}
return nil
})
if err != nil {
return err
}
/* (2) Print files */
for _, infile := range sources {
// 1. process output file name
rel, _ := filepath.Rel(in, infile)
outfile := strings.Replace(rel, ".go", ".so", 1)
outfile = fmt.Sprintf("%s/%s", out, outfile)
clifmt.Align(fmt.Sprintf(" . compile %s", clifmt.Color(33, rel)))
// 3. compile
stdout, err := exec.Command("go",
"build", "-buildmode=plugin",
"-o", outfile,
infile,
).Output()
// 4. success
if err == nil {
fmt.Printf("ok\n")
continue
}
// 5. debug error
fmt.Printf("error\n")
if len(stdout) > 0 {
fmt.Printf("%s\n%s\n%s\n", clifmt.Color(31, "-=-"), stdout, clifmt.Color(31, "-=-"))
}
}
return nil return nil
} }

71
cmd/aicra/build-types.go Normal file
View File

@ -0,0 +1,71 @@
package main
import (
"fmt"
"git.xdrm.io/go/aicra/clifmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Builds types as plugins (.so)
// from the sources in the @in folder
// recursively and generate .so files
// into the @out folder with the same structure
func buildTypes(in string, out string) error {
clifmt.Title("compile types")
/* (1) Create build folder */
clifmt.Align(" . create output folder")
err := os.MkdirAll(out, os.ModePerm)
if err != nil {
return err
}
fmt.Printf("ok\n")
/* (1) List recursively */
types := []string{}
err = filepath.Walk(in, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, "/main.go") {
types = append(types, filepath.Base(filepath.Dir(path)))
}
return nil
})
if err != nil {
return err
}
/* (2) Print files */
for _, name := range types {
// 1. process output file name
infile := filepath.Join(in, name, "main.go")
outfile := filepath.Join(out, fmt.Sprintf("%s.so", name))
clifmt.Align(fmt.Sprintf(" . compile %s", clifmt.Color(33, name)))
// 3. compile
stdout, err := exec.Command("go",
"build", "-buildmode=plugin",
"-o", outfile,
infile,
).Output()
// 4. success
if err == nil {
fmt.Printf("ok\n")
continue
}
// 5. debug error
fmt.Printf("error\n")
if len(stdout) > 0 {
fmt.Printf("%s\n%s\n%s\n", clifmt.Color(31, "-=-"), stdout, clifmt.Color(31, "-=-"))
}
}
return nil
}

View File

@ -60,7 +60,7 @@ func main() {
/* (3) Check path are existing dirs /* (3) Check path are existing dirs
---------------------------------------------------------*/ ---------------------------------------------------------*/
clifmt.Title("Check files") clifmt.Title("file check")
/* (1) Project path */ /* (1) Project path */
clifmt.Align(" . project root") clifmt.Align(" . project root")
@ -97,4 +97,39 @@ func main() {
return return
} }
/* (4) Compile
---------------------------------------------------------*/
/* (1) Create build output dir */
buildPath := filepath.Join(projectPath, ".build")
clifmt.Align(" . create build folder")
err = os.MkdirAll(buildPath, os.ModePerm)
if err != nil {
fmt.Printf("error\n\n")
fmt.Printf("%s the directory %s cannot be created, check permissions.", clifmt.Warn(), clifmt.Color(33, buildPath))
return
}
fmt.Printf("ok\n")
/* (2) Compile controllers */
if compileControllers {
err = buildControllers(cPath, filepath.Join(projectPath, ".build/controller"))
if err != nil {
fmt.Printf("%s compilation error: %s\n", clifmt.Warn(), err)
}
}
/* (3) Compile types */
if compileTypes {
err = buildTypes(tPath, filepath.Join(projectPath, ".build/types"))
if err != nil {
fmt.Printf("%s compilation error: %s\n", clifmt.Warn(), err)
}
}
/* (4) finished */
fmt.Printf("\n[ %s ] files are located inside the %s directory inside the project folder\n",
clifmt.Color(32, "finished"),
clifmt.Color(33, ".build"),
)
} }