134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package rbxbuilder
|
|
|
|
import (
|
|
"ci.itzana.me/itzaname/rbxfile"
|
|
"ci.itzana.me/itzaname/rbxfile/xml"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
type ScriptBuilder struct {
|
|
Options *BuildSettings
|
|
root *rbxfile.Root
|
|
}
|
|
|
|
func (b *ScriptBuilder) loadScript(path string) error {
|
|
scriptName := strings.TrimSuffix(filepath.Base(path), ".lua")
|
|
scriptFilePath := strings.TrimPrefix(path, "/MainModule")
|
|
parentPath := filepath.Dir(path)
|
|
parentName := filepath.Base(parentPath)
|
|
basePath := strings.TrimSuffix(strings.TrimSuffix(path, parentName+string(os.PathSeparator)+filepath.Base(path)), seperator)
|
|
if basePath == "" {
|
|
basePath = parentPath
|
|
}
|
|
|
|
if scriptFilePath == "/main.lua" {
|
|
script := loadScript("MainModule", "ModuleScript", b.Options.Source+scriptFilePath)
|
|
if script != nil {
|
|
b.root.Instances = append(b.root.Instances, script)
|
|
} else {
|
|
return fmt.Errorf("failed to load script: %s", scriptFilePath)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
parentInstance := instanceFromScriptPath(b.root, parentPath)
|
|
parentBaseInstance := instanceFromScriptPath(b.root, basePath)
|
|
if parentBaseInstance == nil {
|
|
return fmt.Errorf("parent and base instance doesn't exist: %s %s", parentPath, basePath)
|
|
}
|
|
if parentInstance == nil {
|
|
path := strings.TrimPrefix(parentPath+seperator+parentName+".lua", "/MainModule")
|
|
script := loadScript(parentName, "ModuleScript", b.Options.Source+path)
|
|
if script != nil {
|
|
if err := parentBaseInstance.AddChild(script); err != nil {
|
|
return err
|
|
}
|
|
parentInstance = script
|
|
} else {
|
|
folder := generateFolder(parentName)
|
|
if err := parentBaseInstance.AddChild(folder); err != nil {
|
|
return err
|
|
}
|
|
parentInstance = folder
|
|
}
|
|
}
|
|
|
|
if scriptName == parentName {
|
|
return nil
|
|
}
|
|
|
|
// Load normal scripts
|
|
script := loadScript(scriptName, "ModuleScript", b.Options.Source+scriptFilePath)
|
|
if script != nil {
|
|
if err := parentInstance.AddChild(script); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return fmt.Errorf("failed to load script: %s", scriptFilePath)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *ScriptBuilder) scriptTree() ([]string, error) {
|
|
var filelist []string
|
|
err := filepath.Walk(b.Options.Source, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !info.IsDir() {
|
|
if filepath.Ext(path) == ".lua" {
|
|
filelist = append(filelist, "/MainModule"+strings.TrimPrefix(path, b.Options.Source))
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
sort.Slice(filelist, func(i, j int) bool {
|
|
return strings.Count(filelist[i], string(os.PathSeparator)) < strings.Count(filelist[j], string(os.PathSeparator))
|
|
})
|
|
|
|
return filelist, err
|
|
|
|
}
|
|
|
|
func (b *ScriptBuilder) createScripts() error {
|
|
tree, err := b.scriptTree()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for i := 0; i < len(tree); i++ {
|
|
if err := b.loadScript(tree[i]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b ScriptBuilder) Build() error {
|
|
b.root = rbxfile.NewRoot()
|
|
|
|
if err := b.createScripts(); err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
|
|
if b.Options.Writer != nil {
|
|
return xml.Serialize(b.Options.Writer, nil, b.root)
|
|
}
|
|
|
|
file, err := os.OpenFile(b.Options.Output, os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open output file")
|
|
}
|
|
defer file.Close()
|
|
|
|
return xml.Serialize(file, nil, b.root)
|
|
}
|