Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
Added config-generator example, fixed some shit
  • Loading branch information
chaposcripts committed Jan 25, 2025
1 parent 304bc9d commit 52030da
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.exe
*.exe
example-project/src/lfs.dll
20 changes: 18 additions & 2 deletions bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ import (
"../config"
)

const MODULE_PATTERN = "\n-- Module \"%s\" (from %s)\npackage.preload['%s'] = (function()\n%s\nend);"
const INIT_PATTERN = "\n-- Init (from %s) \nLUBU_ENTRY_POINT = (function()\n%s\nend);\nLUBU_ENTRY_POINT();"

func GenerateModules(basePath string, cfg config.Config) string {
log.Println("Generating modules...")
modulesCode := []string{}
for name, file := range cfg.Modules {
if strings.HasSuffix(file, ".lua") {
modulesCode = append(modulesCode, GenerateLua(basePath, name, file, false))
} else {
modulesCode = append(modulesCode, GenerateDll(basePath, name, file))
}
}
modulesCode = append(modulesCode, GenerateLua(basePath, "init", cfg.Main, true))
return strings.Join(modulesCode, "\n")
}

func Generate(basePath string, cfg config.Config) string {
dllFunc := ""
if IsDllModuleDefined(cfg) {
Expand All @@ -29,7 +46,7 @@ func Generate(basePath string, cfg config.Config) string {

func Bundle(basePath string, cfg config.Config) {
outDir, _ := filepath.Split(cfg.Out)
log.Printf("Writing code to \"%s\"\ndir: %s", cfg.Out, outDir)
log.Printf("Writing code to \"%s\"", cfg.Out)

err := os.MkdirAll(outDir, 0644)
if err != nil {
Expand All @@ -40,5 +57,4 @@ func Bundle(basePath string, cfg config.Config) {
log.Fatalf("Error writing data: %v", err)
}
log.Printf("Done! Output file: %s", cfg.Out)

}
24 changes: 2 additions & 22 deletions bundler/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,16 @@ import (
"log"
"os"
"path/filepath"
"strings"

"../config"
)

const MODULE_PATTERN = "\n-- Module \"%s\" (from %s)\npackage.preload['%s'] = (function()\n%s\nend);"
const INIT_PATTERN = "\n-- Init (from %s) \nLUBU_ENTRY_POINT = (function()\n%s\nend);\nLUBU_ENTRY_POINT();"

func GenerateModules(basePath string, cfg config.Config) string {
log.Println("Generating modules...")
modulesCode := []string{}
for name, file := range cfg.Modules {
if strings.HasSuffix(file, ".lua") {
modulesCode = append(modulesCode, GenerateModule(basePath, name, file, false))
} else {
modulesCode = append(modulesCode, GenerateDll(basePath, name, file))
}
}
modulesCode = append(modulesCode, GenerateModule(basePath, "init", cfg.Main, true))
return strings.Join(modulesCode, "\n")
}

func GenerateModule(basePath, name, file string, isInit bool) string {
func GenerateLua(basePath, name, file string, isMainFile bool) string {
fullPath := filepath.Join(basePath, file)
log.Printf("Bundling \"%s\" module from: %s", name, fullPath)
bytes, err := os.ReadFile(fullPath)
if err != nil {
log.Fatalf("Error reading module \"%s\" (%s):\n\t%v", name, file, err)
}
if isInit {
if isMainFile {
return fmt.Sprintf(INIT_PATTERN, fullPath, string(bytes))
}
return fmt.Sprintf(MODULE_PATTERN, name, fullPath, name, string(bytes))
Expand Down
3 changes: 2 additions & 1 deletion example-project/bundle-confing.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"utils": "src/utils.lua",
"lfs": "src/lfs.dll"
},
"out": "dist/bundled.lua"
"out": "dist/bundled.lua",
"watcher_delay": 250
}
51 changes: 51 additions & 0 deletions example-project/config-generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)

type Config struct {
Main string `json:"main"`
Out string `json:"out"`
Modules map[string]string `json:"modules"`
WatcherDelay float64 `json:"watcher_delay"`
Constants map[string]interface{} `json:"const"`
}

const OUTPUT_CONFIG = "autogen-config.json"

var cfg Config = Config{
Main: "src/init.lua",
WatcherDelay: 250,
Out: "dist/bundled.lua",
Modules: map[string]string{},
Constants: map[string]interface{}{
"VERSION": "0.1-alpha",
},
}

func main() {
fmt.Println("Generating config...")
err := filepath.Walk("./src", func(path string, info os.FileInfo, e error) error {
if !info.IsDir() && e == nil && path != cfg.Main {
_, name := filepath.Split(path)
cfg.Modules[name] = path
fmt.Printf("Added module \"%s\"\n", path)
}
return nil
})
if err != nil {
panic(err)
}
bytes, err := json.Marshal(cfg)
if err != nil {
panic(err)
}

if err := os.WriteFile(OUTPUT_CONFIG, bytes, 0644); err != nil {
panic("Error saving file:" + err.Error())
}
}
40 changes: 22 additions & 18 deletions example-project/dist/bundled.lua

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions example-project/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ local lfs = require('lfs');
local MyMath = require('my-math');
local Utils = require('utils');

if (LUBU_BUNDLED) then ---@diagnostic disable-line
print('Build date:', os.date("%H:%M:%S %d.%m.%y", LUBU_BUNDLED_AT)); ---@diagnostic disable-line
end

print('Script path:', lfs.currentdir());

local numbers = { 1, 10, 71, 22, 39 };
Expand Down
9 changes: 2 additions & 7 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ func checkFile(file, basePath string) bool {
}
modTime := fileStat.ModTime()
oldModTime, oldModTimeExists := lastCheckedTime[filePath]
if !oldModTimeExists {
if !oldModTimeExists || modTime != oldModTime {
lastCheckedTime[filePath] = modTime
return false
}
if modTime != oldModTime {
lastCheckedTime[filePath] = modTime
return true
}
return false
return modTime != oldModTime
}

0 comments on commit 52030da

Please sign in to comment.