Commit 7c2d1115 authored by Oscar Campos's avatar Oscar Campos
Browse files

feat: removed Python generation scriopt and move build to Magefile

parent a00ff223
all: clean generate build
clean:
rm gdnative/*.gen.* || true
go clean
generate:
go generate
build:
go build -x ./gdnative/...
.PHONY: all
// +build ignore
package main
import (
"os"
"github.com/magefile/mage/mage"
)
func main() {
os.Exit(mage.Main())
}
#!/usr/bin/python
import os
import shlex
import subprocess
LOCAL_PATH = os.path.abspath(os.path.dirname(__file__))
DOC_PATH = os.path.join(LOCAL_PATH, '..', 'doc')
# Check if the "doc" folder is present where we can fetch all of the godot
# documentation to include it as Go documentation as part of class generation.
if not os.getenv('NODOC'):
if os.path.exists(DOC_PATH):
print('Godot documentation found. Pulling the latest changes...')
os.chdir(DOC_PATH)
subprocess.run(shlex.split('git pull origin master'))
os.chdir(LOCAL_PATH)
else:
print('Godot documentation not found. Pulling the latest changes...')
os.makedirs(DOC_PATH)
os.chdir(DOC_PATH)
cmds = [
'git init',
'git remote add -f origin https://github.com/godotengine/godot.git', # noqa
'git config core.sparseCheckout true',
'echo "doc/classes" >> {}'.format(
os.path.join(DOC_PATH, '.git', 'info', 'sparse-checkout')
),
'git pull origin master'
]
for cmd in cmds:
subprocess.run(shlex.split(cmd))
os.chdir(LOCAL_PATH)
# clean the gdnative directory
print('Cleaning previous generation...')
subprocess.run(shlex.split('rm gdnative/*.gen.*'), capture_output=True)
env = os.environ.copy()
env.update({'API_PATH': os.path.join(LOCAL_PATH, '..')})
cmd = 'go run -v {}'.format(os.path.join(LOCAL_PATH, 'generate', 'main.go'))
subprocess.run(shlex.split(cmd), env=env)
......@@ -2,6 +2,7 @@ module gitlab.com/pimpam-games-studio/gdnative-go
require (
github.com/fatih/camelcase v1.0.0 // indirect
github.com/magefile/mage v1.9.0
github.com/pinzolo/casee v0.0.0-20160729104318-956b6baf666a
github.com/vitaminwater/cgo.wchar v0.0.0-20160320123332-5dd6f4be3f2a
)
......
// +build mage
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var ldflags = "-s -w"
var Default = Generate
func init() {
// make sure we use Go 1.11 modules even if the source lives inside GOPATH
os.Setenv("GO111MODULE", "on")
}
// Generate Go Bindings
func Generate() error {
mg.SerialDeps(RetrieveGodotDocumentation, Clean)
apiPath := getCurrentFilePath()
generateScript := filepath.Join(getCurrentFilePath(), "cmd", "generate", "main.go")
err := sh.RunWith(map[string]string{"API_PATH": apiPath}, "go", "run", "-v", generateScript)
if err != nil {
return fmt.Errorf("could not genearate Go bindings: %w", err)
}
return Build()
}
// Clean cleans previous generations
func Clean() error {
log.Println("Cleaning previous generation...")
return sh.Rm("./gdnative/*.gen.*")
}
// RetrieveGodotDocumentation retrieves latest Godot documentation to attach docstrings
func RetrieveGodotDocumentation() error {
localPath := getCurrentFilePath()
docPath := filepath.Join(localPath, "doc")
_, found := os.Stat(docPath)
if found == nil {
os.Chdir(docPath)
log.Println("Godot documentation found. Pulling latest changes...")
if err := sh.Run("git", "pull", "origin", "master"); err != nil {
return fmt.Errorf("could not pull latest Godot documentation from git: %w", err)
}
os.Chdir(localPath)
return nil
}
log.Println("Godot documentation not found. Cloning the repository...")
if err := os.MkdirAll(docPath, 0766); err != nil {
return fmt.Errorf("could not create a new directory on the disk: %w", err)
}
os.Chdir(docPath)
if err := sh.Run("git", "init"); err != nil {
return fmt.Errorf("could not execute git init: %w", err)
}
if err := sh.Run("git", "remote", "add", "-f", "origin", "https://github.com/godotengine/godot.git"); err != nil {
return fmt.Errorf("could not set origin remote for documentation: %w", err)
}
if err := sh.Run("git", "config", "core.sparseCheckout", "true"); err != nil {
return fmt.Errorf("could not activate core.sparseCheckout: %w", err)
}
sparseCheckoutsConfigFile := filepath.Join(".", ".git", "info", "sparse-checkout")
writeErr := ioutil.WriteFile(sparseCheckoutsConfigFile, []byte("doc/classes"), 0655)
if writeErr != nil {
return fmt.Errorf("could not write .git/info/sparse-checkout file: %w", writeErr)
}
if err := sh.Run("git", "pull", "origin", "master"); err != nil {
return fmt.Errorf("error while pulling: %w", err)
}
return nil
}
// Build builds the library to make sure everything is fine
func Build() error {
return sh.Run("go", "build", "-ldflags", ldflags, "-x", "./gdnative...")
}
// getCurrentFilePath constructs and returns the current file path on the drive
func getCurrentFilePath() string {
_, filename, _, ok := runtime.Caller(0)
if !ok {
log.Fatal("could not get current file path")
}
return filepath.Join(filepath.Dir(filename))
}
package main
// Generate the wrapper
//go:generate ./cmd/generate.py
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment