Wrapper tool hardening and cleanup

This commit is contained in:
2026-05-13 19:06:24 +02:00
parent 53cb1e6996
commit 7d286551b3
19 changed files with 1376 additions and 149 deletions
+416 -90
View File
@@ -6,7 +6,9 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
@@ -294,6 +296,9 @@ func runBuild(ctx context) error {
if err != nil {
return err
}
if err := runBuildModulePreflight(ctx, p, nil); err != nil {
return err
}
result, err := pipeline.Build(p)
if err != nil {
@@ -321,12 +326,19 @@ func runBuildModule(ctx context) error {
return err
}
result, err := pipeline.BuildModuleWithProgress(p, func(message string) {
progress := func(message string) {
if ctx.logLevel == logLevelQuiet {
return
}
spin.linebreak()
fmt.Fprintf(ctx.stdout, "[build-module] %s\n", message)
}
if err := runBuildModulePreflight(ctx, p, progress); err != nil {
return err
}
result, err := pipeline.BuildModuleWithProgress(p, func(message string) {
progress(message)
})
if err != nil {
return err
@@ -338,6 +350,222 @@ func runBuildModule(ctx context) error {
return nil
}
func runBuildModulePreflight(ctx context, p *project.Project, progress func(string)) error {
if progress == nil {
progress = func(string) {}
}
if err := refreshBuildModuleManifest(ctx, p, progress); err != nil {
return err
}
if err := prepareBuildModuleCompiler(ctx, p, progress); err != nil {
return err
}
return nil
}
func refreshBuildModuleManifest(ctx context, p *project.Project, progress func(string)) error {
manifestPath := p.HAKManifestPath()
if override := strings.TrimSpace(os.Getenv("SOW_HAK_MANIFEST_PATH")); override != "" {
manifestPath = override
if !filepath.IsAbs(manifestPath) {
manifestPath = filepath.Join(p.Root, manifestPath)
}
}
if envEnabled("SOW_MODULE_SKIP_HAK_MANIFEST_REFRESH") {
if _, err := os.Stat(manifestPath); err != nil {
return fmt.Errorf("SOW_MODULE_SKIP_HAK_MANIFEST_REFRESH is set, but %s does not exist", manifestPath)
}
progress(fmt.Sprintf("Using existing hak manifest at %s; skipping published sow-assets refresh.", relPathFromRoot(p.Root, manifestPath)))
return nil
}
progress("Refreshing hak list from the latest published sow-assets manifest...")
if err := runProjectScript(ctx, p, []string{"scripts", "fetch-hak-manifest"}, manifestPath); err != nil {
return err
}
if _, err := pipeline.ApplyHAKManifest(p, manifestPath); err != nil {
return err
}
return nil
}
func prepareBuildModuleCompiler(ctx context, p *project.Project, progress func(string)) error {
if envEnabled("SOW_MODULE_SKIP_NSS_COMPILATION") {
progress("SOW_MODULE_SKIP_NSS_COMPILATION is set; skipping NWScript compiler resolution and NSS compilation for this run.")
return nil
}
if !projectHasScriptSources(p) {
return nil
}
compilerPathEnv := p.ScriptCompilerPathEnv()
explicitCompiler := strings.TrimSpace(os.Getenv(compilerPathEnv))
if explicitCompiler != "" {
if scriptCompilerRunnable(explicitCompiler) {
return nil
}
if fileExists(explicitCompiler) {
return disableBuildModuleCompiler(compilerPathEnv, progress, fmt.Sprintf("Configured NWScript compiler is present but cannot start: %s", explicitCompiler))
}
}
if _, ok := findUsableBuildModuleCompiler(p); ok {
return nil
}
installScript := projectScriptPath(p.Root, "scripts", "install-script-compiler")
if !fileExists(installScript) {
return disableBuildModuleCompiler(compilerPathEnv, progress, "NWScript compiler not found locally and automatic installer script is unavailable.")
}
progress("NWScript compiler not found locally. Installing it now...")
if err := runProjectScript(ctx, p, []string{"scripts", "install-script-compiler"}); err != nil {
return disableBuildModuleCompiler(compilerPathEnv, progress, "Automatic NWScript compiler install failed.")
}
if _, ok := findUsableBuildModuleCompiler(p); ok {
return nil
}
return disableBuildModuleCompiler(compilerPathEnv, progress, "Installed NWScript compiler but it still cannot start.")
}
func disableBuildModuleCompiler(compilerPathEnv string, progress func(string), reason string) error {
if err := os.Setenv("SOW_MODULE_SKIP_NSS_COMPILATION", "1"); err != nil {
return fmt.Errorf("set SOW_MODULE_SKIP_NSS_COMPILATION: %w", err)
}
progress("Skipping NWScript compilation for this build. " + reason)
if compilerPathEnv != "" {
_ = os.Unsetenv(compilerPathEnv)
}
return nil
}
func projectHasScriptSources(p *project.Project) bool {
for _, rel := range p.Inventory.ScriptFiles {
if strings.EqualFold(filepath.Ext(rel), ".nss") {
return true
}
}
return false
}
func findUsableBuildModuleCompiler(p *project.Project) (string, bool) {
candidates := make([]string, 0, 2+len(p.ScriptCompilerSearchPaths()))
if configured := p.ScriptCompilerPath(); configured != "" {
candidates = append(candidates, configured)
}
if configured := strings.TrimSpace(os.Getenv(p.ScriptCompilerPathEnv())); configured != "" {
candidates = append(candidates, configured)
}
candidates = append(candidates, p.ScriptCompilerSearchPaths()...)
for _, candidate := range candidates {
if compiler, ok := resolveRunnableCompilerCandidate(candidate); ok {
return compiler, true
}
}
return "", false
}
func resolveRunnableCompilerCandidate(candidate string) (string, bool) {
if candidate == "" {
return "", false
}
if binary, ok := strings.CutPrefix(candidate, "PATH:"); ok {
compiler, err := exec.LookPath(binary)
if err != nil {
return "", false
}
if scriptCompilerRunnable(compiler) {
return compiler, true
}
return "", false
}
if !fileExists(candidate) {
return "", false
}
if scriptCompilerRunnable(candidate) {
return candidate, true
}
return "", false
}
func scriptCompilerRunnable(path string) bool {
if !fileExists(path) {
return false
}
cmd := exec.Command(path, "--help")
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
return cmd.Run() == nil
}
func runProjectScript(ctx context, p *project.Project, scriptParts []string, args ...string) error {
if len(scriptParts) == 0 {
return errors.New("project script path is required")
}
scriptPath := projectScriptPath(p.Root, scriptParts...)
if !fileExists(scriptPath) {
return fmt.Errorf("required project script is missing: %s", scriptPath)
}
var cmd *exec.Cmd
switch filepath.Ext(scriptPath) {
case ".ps1":
powershell := "powershell"
if runtime.GOOS != "windows" {
powershell = "pwsh"
}
commandArgs := []string{"-NoProfile", "-File", scriptPath}
commandArgs = append(commandArgs, args...)
cmd = exec.Command(powershell, commandArgs...)
default:
cmd = exec.Command(scriptPath, args...)
}
cmd.Dir = p.Root
cmd.Stdout = ctx.stderr
cmd.Stderr = ctx.stderr
return cmd.Run()
}
func projectScriptPath(root string, pathParts ...string) string {
parts := append([]string{root}, pathParts...)
base := filepath.Join(parts...)
if runtime.GOOS == "windows" {
if strings.HasSuffix(base, ".ps1") || strings.HasSuffix(base, ".sh") {
return base
}
return base + ".ps1"
}
if strings.HasSuffix(base, ".ps1") || strings.HasSuffix(base, ".sh") {
return base
}
return base + ".sh"
}
func envEnabled(name string) bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv(name))) {
case "1", "true", "yes", "on":
return true
default:
return false
}
}
func fileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
func relPathFromRoot(root, path string) string {
rel, err := filepath.Rel(root, path)
if err != nil {
return filepath.ToSlash(path)
}
return filepath.ToSlash(rel)
}
type buildHAKOptions struct {
filteredHAKs []string
filteredArchives []string
@@ -530,6 +758,161 @@ func (c *buildHAKConsole) relPath(path string) string {
return filepath.ToSlash(rel)
}
type topdataConsole struct {
stdout io.Writer
projectRoot string
projectName string
commandName string
commandLabel string
level logLevel
spinnerEnabled bool
}
func newTopdataConsole(ctx context, p *project.Project, commandName string) *topdataConsole {
return &topdataConsole{
stdout: ctx.stdout,
projectRoot: p.Root,
projectName: p.Config.Module.Name,
commandName: commandName,
commandLabel: topdataCommandLabel(commandName),
level: ctx.logLevel,
spinnerEnabled: isInteractiveTTY(ctx.stderr) && strings.TrimSpace(os.Getenv("CI")) == "" && ctx.logLevel == logLevelNormal,
}
}
func topdataCommandLabel(commandName string) string {
switch commandName {
case "validate-topdata":
return "Validate Topdata"
case "build-topdata":
return "Build Topdata"
case "build-top-package":
return "Build Top Package"
case "compare-topdata":
return "Compare Topdata"
case "build-wiki":
return "Build Wiki"
case "deploy-wiki":
return "Deploy Wiki"
default:
return commandName
}
}
func (c *topdataConsole) progress(message string) {
if phase := c.phaseLabel(message); phase != "" {
spin.update(c.commandLabel + ": " + phase)
}
if c.level != logLevelDebug {
return
}
spin.linebreak()
fmt.Fprintf(c.stdout, "[debug] %s\n", message)
}
func (c *topdataConsole) phaseLabel(message string) string {
switch {
case strings.HasPrefix(message, "Building native topdata outputs"):
return "building native outputs"
case strings.HasPrefix(message, "Topdata outputs are current"):
return "checking cached outputs"
case strings.HasPrefix(message, "Compiled topdata outputs are current"):
return "refreshing top package"
case strings.HasPrefix(message, "Packaging compiled topdata resources into "):
return "packaging HAK"
case strings.HasPrefix(message, "Preparing compiled TLK release output "):
return "packaging TLK"
case strings.HasPrefix(message, "Building native wiki pages"):
return "building wiki pages"
case strings.HasPrefix(message, "NodeBB wiki plan: "):
return "planning wiki deploy"
case strings.HasPrefix(message, "Collecting "):
return "collecting pages"
default:
return ""
}
}
func (c *topdataConsole) emitValidationResult(report topdata.ValidationReport, topdataRoot string) {
spin.linebreak()
fmt.Fprintln(c.stdout, "Validate Topdata ----------")
fmt.Fprintf(c.stdout, "project: %s\n", c.projectName)
fmt.Fprintf(c.stdout, "topdata root: %s\n", c.relPath(topdataRoot))
fmt.Fprintf(c.stdout, "topdata files: %d\n", report.Files)
fmt.Fprintf(c.stdout, "data files: %d\n", report.DataFiles)
fmt.Fprintf(c.stdout, "tlk files: %d\n", report.TLKFiles)
if warnings := report.WarningCount(); warnings > 0 {
fmt.Fprintf(c.stdout, "warnings: %d\n", warnings)
}
fmt.Fprintln(c.stdout, "topdata validation: ok")
}
func (c *topdataConsole) emitPackageResult(result topdata.PackageResult) {
spin.linebreak()
fmt.Fprintf(c.stdout, "%s ----------\n", c.commandLabel)
fmt.Fprintf(c.stdout, "project: %s\n", c.projectName)
fmt.Fprintf(c.stdout, "mode: %s\n", result.Mode)
fmt.Fprintf(c.stdout, "topdata 2da output: %s\n", c.relPath(result.Output2DADir))
fmt.Fprintf(c.stdout, "topdata tlk output: %s\n", c.relPath(result.OutputTLKDir))
fmt.Fprintf(c.stdout, "top package hak: %s\n", c.relPath(result.OutputHAKPath))
fmt.Fprintf(c.stdout, "top package tlk: %s\n", c.relPath(result.OutputTLKPath))
fmt.Fprintf(c.stdout, "2da files: %d\n", result.Files2DA)
fmt.Fprintf(c.stdout, "tlk files: %d\n", result.FilesTLK)
fmt.Fprintf(c.stdout, "top package resources: %d\n", result.HAKResources)
fmt.Fprintf(c.stdout, "top package assets: %d\n", result.AssetFiles)
if result.WikiOutputDir != "" {
fmt.Fprintf(c.stdout, "wiki output: %s\n", c.relPath(result.WikiOutputDir))
fmt.Fprintf(c.stdout, "wiki pages: %d\n", result.WikiPages)
fmt.Fprintf(c.stdout, "wiki status: %s\n", result.WikiStatus)
}
}
func (c *topdataConsole) emitCompareResult(result topdata.CompareResult) {
spin.linebreak()
fmt.Fprintln(c.stdout, "Compare Topdata ----------")
fmt.Fprintf(c.stdout, "project: %s\n", c.projectName)
fmt.Fprintf(c.stdout, "mode: %s\n", result.Mode)
fmt.Fprintf(c.stdout, "checked 2da files: %d\n", result.Compared2DA)
fmt.Fprintf(c.stdout, "checked tlk files: %d\n", result.ComparedTLK)
fmt.Fprintf(c.stdout, "native-pass: %d\n", result.NativePass)
fmt.Fprintf(c.stdout, "not-yet-canonical: %d\n", result.NotYetCanonical)
fmt.Fprintf(c.stdout, "native-mismatch: %d\n", result.NativeMismatch)
fmt.Fprintln(c.stdout, "topdata compare: ok")
}
func (c *topdataConsole) emitWikiBuildResult(outputDir string, pageCount int, status string) {
spin.linebreak()
fmt.Fprintln(c.stdout, "Build Wiki ----------")
fmt.Fprintf(c.stdout, "project: %s\n", c.projectName)
fmt.Fprintf(c.stdout, "wiki output: %s\n", c.relPath(outputDir))
fmt.Fprintf(c.stdout, "wiki pages: %d\n", pageCount)
fmt.Fprintf(c.stdout, "wiki status: %s\n", status)
}
func (c *topdataConsole) emitWikiDeployResult(localPages, created, updated, skipped, stale, drifted int, manifest string) {
spin.linebreak()
fmt.Fprintln(c.stdout, "Deploy Wiki ----------")
fmt.Fprintf(c.stdout, "project: %s\n", c.projectName)
fmt.Fprintf(c.stdout, "local pages: %d\n", localPages)
fmt.Fprintf(c.stdout, "created: %d\n", created)
fmt.Fprintf(c.stdout, "updated: %d\n", updated)
fmt.Fprintf(c.stdout, "skipped: %d\n", skipped)
fmt.Fprintf(c.stdout, "stale: %d\n", stale)
fmt.Fprintf(c.stdout, "drifted: %d\n", drifted)
fmt.Fprintf(c.stdout, "manifest: %s\n", c.relPath(manifest))
}
func (c *topdataConsole) relPath(path string) string {
if path == "" {
return path
}
rel, err := filepath.Rel(c.projectRoot, path)
if err != nil {
return filepath.ToSlash(path)
}
return filepath.ToSlash(rel)
}
func formatCount(value int) string {
raw := strconv.Itoa(value)
if value < 1000 && value > -1000 {
@@ -1145,21 +1528,14 @@ func runValidateTopData(ctx context) error {
return err
}
console := newTopdataConsole(ctx, p, "validate-topdata")
report := topdata.ValidateProject(p)
emitTopdataValidationReport(ctx.stderr, report)
if report.HasErrors() {
return fmt.Errorf("topdata validation failed with %d error(s)", report.ErrorCount())
}
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "topdata root: %s\n", p.TopDataSourceDir())
fmt.Fprintf(ctx.stdout, "topdata files: %d\n", report.Files)
fmt.Fprintf(ctx.stdout, "data files: %d\n", report.DataFiles)
fmt.Fprintf(ctx.stdout, "tlk files: %d\n", report.TLKFiles)
if warnings := report.WarningCount(); warnings > 0 {
fmt.Fprintf(ctx.stdout, "warnings: %d\n", warnings)
}
fmt.Fprintf(ctx.stdout, "topdata validation: ok\n")
console.emitValidationResult(report, p.TopDataSourceDir())
return nil
}
@@ -1174,31 +1550,16 @@ func runBuildTopData(ctx context) error {
return err
}
result, err := topdata.BuildAndPackageWithOptions(p, opts, func(message string) {
if ctx.logLevel == logLevelQuiet {
return
}
fmt.Fprintf(ctx.stdout, "[build-topdata] %s\n", message)
})
console := newTopdataConsole(ctx, p, "build-topdata")
spin.configure(ctx.stderr, console.spinnerEnabled)
spin.start("Build Topdata: starting")
defer spin.stop()
result, err := topdata.BuildAndPackageWithOptions(p, opts, console.progress)
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "mode: %s\n", result.Mode)
fmt.Fprintf(ctx.stdout, "topdata 2da output: %s\n", result.Output2DADir)
fmt.Fprintf(ctx.stdout, "topdata tlk output: %s\n", result.OutputTLKDir)
fmt.Fprintf(ctx.stdout, "top package hak: %s\n", result.OutputHAKPath)
fmt.Fprintf(ctx.stdout, "top package tlk: %s\n", result.OutputTLKPath)
fmt.Fprintf(ctx.stdout, "2da files: %d\n", result.Files2DA)
fmt.Fprintf(ctx.stdout, "tlk files: %d\n", result.FilesTLK)
fmt.Fprintf(ctx.stdout, "top package resources: %d\n", result.HAKResources)
fmt.Fprintf(ctx.stdout, "top package assets: %d\n", result.AssetFiles)
if result.WikiOutputDir != "" {
fmt.Fprintf(ctx.stdout, "wiki output: %s\n", result.WikiOutputDir)
fmt.Fprintf(ctx.stdout, "wiki pages: %d\n", result.WikiPages)
fmt.Fprintf(ctx.stdout, "wiki status: %s\n", result.WikiStatus)
}
console.emitPackageResult(result)
return nil
}
@@ -1217,31 +1578,16 @@ func runBuildTopPackage(ctx context) error {
return fmt.Errorf("--wiki is not supported with build-top-package; use build-topdata when wiki generation is required")
}
result, err := topdata.BuildPackage(p, func(message string) {
if ctx.logLevel == logLevelQuiet {
return
}
fmt.Fprintf(ctx.stdout, "[build-top-package] %s\n", message)
})
console := newTopdataConsole(ctx, p, "build-top-package")
spin.configure(ctx.stderr, console.spinnerEnabled)
spin.start("Build Top Package: starting")
defer spin.stop()
result, err := topdata.BuildPackage(p, console.progress)
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "mode: %s\n", result.Mode)
fmt.Fprintf(ctx.stdout, "topdata 2da output: %s\n", result.Output2DADir)
fmt.Fprintf(ctx.stdout, "topdata tlk output: %s\n", result.OutputTLKDir)
fmt.Fprintf(ctx.stdout, "top package hak: %s\n", result.OutputHAKPath)
fmt.Fprintf(ctx.stdout, "top package tlk: %s\n", result.OutputTLKPath)
fmt.Fprintf(ctx.stdout, "2da files: %d\n", result.Files2DA)
fmt.Fprintf(ctx.stdout, "tlk files: %d\n", result.FilesTLK)
fmt.Fprintf(ctx.stdout, "top package resources: %d\n", result.HAKResources)
fmt.Fprintf(ctx.stdout, "top package assets: %d\n", result.AssetFiles)
if result.WikiOutputDir != "" {
fmt.Fprintf(ctx.stdout, "wiki output: %s\n", result.WikiOutputDir)
fmt.Fprintf(ctx.stdout, "wiki pages: %d\n", result.WikiPages)
fmt.Fprintf(ctx.stdout, "wiki status: %s\n", result.WikiStatus)
}
console.emitPackageResult(result)
return nil
}
@@ -1268,24 +1614,16 @@ func runCompareTopData(ctx context) error {
return err
}
result, err := topdata.Compare(p, func(message string) {
if ctx.logLevel == logLevelQuiet {
return
}
fmt.Fprintf(ctx.stdout, "[compare-topdata] %s\n", message)
})
console := newTopdataConsole(ctx, p, "compare-topdata")
spin.configure(ctx.stderr, console.spinnerEnabled)
spin.start("Compare Topdata: starting")
defer spin.stop()
result, err := topdata.Compare(p, console.progress)
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "mode: %s\n", result.Mode)
fmt.Fprintf(ctx.stdout, "checked 2da files: %d\n", result.Compared2DA)
fmt.Fprintf(ctx.stdout, "checked tlk files: %d\n", result.ComparedTLK)
fmt.Fprintf(ctx.stdout, "native-pass: %d\n", result.NativePass)
fmt.Fprintf(ctx.stdout, "not-yet-canonical: %d\n", result.NotYetCanonical)
fmt.Fprintf(ctx.stdout, "native-mismatch: %d\n", result.NativeMismatch)
fmt.Fprintf(ctx.stdout, "topdata compare: ok\n")
console.emitCompareResult(result)
return nil
}
@@ -1307,20 +1645,16 @@ func runBuildWiki(ctx context) error {
return err
}
result, err := topdata.BuildWikiWithOptions(p, opts, func(message string) {
if ctx.logLevel == logLevelQuiet {
return
}
fmt.Fprintf(ctx.stdout, "[build-wiki] %s\n", message)
})
console := newTopdataConsole(ctx, p, "build-wiki")
spin.configure(ctx.stderr, console.spinnerEnabled)
spin.start("Build Wiki: starting")
defer spin.stop()
result, err := topdata.BuildWikiWithOptions(p, opts, console.progress)
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "wiki output: %s\n", result.OutputDir)
fmt.Fprintf(ctx.stdout, "wiki pages: %d\n", result.PageCount)
fmt.Fprintf(ctx.stdout, "wiki status: %s\n", result.Status)
console.emitWikiBuildResult(result.OutputDir, result.PageCount, result.Status)
return nil
}
@@ -1350,24 +1684,16 @@ func runDeployWiki(ctx context) error {
return err
}
result, err := topdata.DeployWikiWithOptions(p, opts, func(message string) {
if ctx.logLevel == logLevelQuiet {
return
}
fmt.Fprintf(ctx.stdout, "[deploy-wiki] %s\n", message)
})
console := newTopdataConsole(ctx, p, "deploy-wiki")
spin.configure(ctx.stderr, console.spinnerEnabled)
spin.start("Deploy Wiki: starting")
defer spin.stop()
result, err := topdata.DeployWikiWithOptions(p, opts, console.progress)
if err != nil {
return err
}
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "local pages: %d\n", result.LocalPages)
fmt.Fprintf(ctx.stdout, "created: %d\n", result.Created)
fmt.Fprintf(ctx.stdout, "updated: %d\n", result.Updated)
fmt.Fprintf(ctx.stdout, "skipped: %d\n", result.Skipped)
fmt.Fprintf(ctx.stdout, "stale: %d\n", result.Stale)
fmt.Fprintf(ctx.stdout, "drifted: %d\n", result.Drifted)
fmt.Fprintf(ctx.stdout, "manifest: %s\n", result.Manifest)
console.emitWikiDeployResult(result.LocalPages, result.Created, result.Updated, result.Skipped, result.Stale, result.Drifted, result.Manifest)
return nil
}