add powershell build script template

Fri, 19 Dec 2025 23:02:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 19 Dec 2025 23:02:38 +0100
changeset 9
eef1439666f7
parent 8
2e9b3f85c949
child 10
a4537989ed09

add powershell build script template

UwBuild.psm1 file | annotate | diff | comparison | revisions
build.ps1.template file | annotate | diff | comparison | revisions
src/main.go file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UwBuild.psm1	Fri Dec 19 23:02:38 2025 +0100
@@ -0,0 +1,4 @@
+function UwBuildShell {
+    &{Import-Module "C:\Program Files\Microsoft Visual Studio\18\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" }
+    Enter-VsDevShell a295baa3
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/build.ps1.template	Fri Dec 19 23:02:38 2025 +0100
@@ -0,0 +1,147 @@
+# -------------------------
+# Utility functions
+# -------------------------
+
+function Is-Platform {
+    param([string]$Name)
+    return $PLATFORM -contains $Name
+}
+
+function Not-IsPlatform {
+    param([string]$Name)
+    return -not ($PLATFORM -contains $Name)
+}
+
+# -------------------------
+# Platform (fixed)
+# -------------------------
+
+Write-Host "detect platform... windows"
+
+$PLATFORM = @('windows')
+$PLATFORM_NAME = 'windows'
+
+# -------------------------
+# Result directories
+# -------------------------
+
+New-Item -ItemType Directory -Force -Path result | Out-Null
+$result_dir = (Resolve-Path result).Path
+
+$configure_log = "configure.log"
+$compile_log   = "compile.log"
+$check_log     = "check.log"
+
+$result_file = Join-Path $result_dir "result.json"
+
+"[" | Out-File -FilePath $result_file -Encoding utf8
+
+# -------------------------
+# Source directory
+# -------------------------
+
+Set-Location src
+
+{{ $nbuild := len .Build }}
+{{ range $i, $build := .Build}}
+
+# Build isplatform="{{ .IsPlatform }}" not="{{ .NotPlatform }}"
+while ($true) {
+
+    $log_dir = Join-Path $result_dir "build{{$i}}"
+    New-Item -ItemType Directory -Force -Path $log_dir | Out-Null
+    if (-not $?) {
+        Write-Error "failed to create log dir $log_dir"
+        exit 1
+    }
+
+    {{- if .IsPlatform}}
+    if (Not-IsPlatform "{{ $build.IsPlatform }}") {
+        break
+    }
+    {{- end}}
+
+    {{- range $build.NotList}}
+    if (Is-Platform "{{.}}") {
+        break
+    }
+    {{- end}}
+
+    # -------------------------
+    # configure
+    # -------------------------
+
+    $configure = $null
+    {{- if $build.Configure}}
+@"
+{{ $build.Configure }}
+"@ | Out-File uwbuild-configure.ps1 -Encoding utf8
+
+    .\uwbuild-configure.ps1 *> (Join-Path $log_dir $configure_log)
+
+    $configure = $LASTEXITCODE
+    {{- end}}
+
+    # -------------------------
+    # compile
+    # -------------------------
+
+    $compile = $null
+    {{- if $build.Compile}}
+@"
+{{ $build.Compile }}
+"@ | Out-File uwbuild-compile.ps1 -Encoding utf8
+
+    .\uwbuild-compile.ps1 *> (Join-Path $log_dir $compile_log)
+
+    $compile = $LASTEXITCODE
+    {{- end}}
+
+    # -------------------------
+    # check
+    # -------------------------
+
+    $check = $null
+    {{- if $build.Check}}
+@"
+{{ $build.Check }}
+"@ | Out-File uwbuild-check.ps1 -Encoding utf8
+
+    .\uwbuild-check.ps1 *> (Join-Path $log_dir $check_log)
+
+    $check = $LASTEXITCODE
+    {{- end}}
+
+    # -------------------------
+    # write result
+    # -------------------------
+
+    Add-Content $result_file "{ `"build`": `"{{$i}}`","
+
+    {{- if $build.Name}}
+    Add-Content $result_file "  `"name`": `"{{$build.Name}}`","
+    {{- end}}
+
+    if ($null -ne $configure) {
+        Add-Content $result_file "  `"configure`": `"$configure`","
+    }
+    if ($null -ne $compile) {
+        Add-Content $result_file "  `"compile`": `"$compile`","
+    }
+    if ($null -ne $check) {
+        Add-Content $result_file "  `"check`": `"$check`""
+    }
+
+    {{- if eq $i (sub $nbuild 1)}}
+    Add-Content $result_file "}"
+    {{ else }}
+    Add-Content $result_file "},"
+    {{- end}}
+
+    break
+}
+
+# End Build
+{{ end}}
+
+"]" | Add-Content $result_file
--- a/src/main.go	Fri Dec 19 22:38:10 2025 +0100
+++ b/src/main.go	Fri Dec 19 23:02:38 2025 +0100
@@ -106,6 +106,11 @@
 		log.Fatal(err)
 	}
 
+	templateStr2, err := os.ReadFile("build.ps1.template")
+	if err != nil {
+		log.Fatal(err)
+	}
+
 	funcs := template.FuncMap{
 		"add": func(i int, n int) int {
 			return i + n
@@ -120,6 +125,11 @@
 		log.Fatal(err)
 	}
 
+	tpl2, err := template.New("").Funcs(funcs).Parse(string(templateStr2))
+	if err != nil {
+		log.Fatal(err)
+	}
+
 	err = os.Mkdir("tmp", 0755)
 	if err != nil && !os.IsExist(err) {
 		log.Fatal(err)
@@ -141,7 +151,7 @@
 			continue
 		}
 
-		if err := create_repo_archive(&repo, tpl, tmp); err != nil {
+		if err := create_repo_archive(&repo, tpl, tpl2, tmp); err != nil {
 			log.Fatal(err)
 		}
 
@@ -154,7 +164,7 @@
 	}
 }
 
-func create_repo_archive(repo *Repository, tpl *template.Template, tmp_dir string) error {
+func create_repo_archive(repo *Repository, tpl *template.Template, tpl2 *template.Template, tmp_dir string) error {
 	// create a build directory, that contains the repository source
 	// and build scripts
 	buildPath := path.Join(tmp_dir, "build")
@@ -229,8 +239,20 @@
 	}
 	file.Chmod(0755)
 
+	file2, err := os.Create(path.Join(buildPath, "build.ps1"))
+	if err != nil {
+		log.Print("cannot create build.ps1")
+		return err
+	}
+	defer file2.Close()
+	err = tpl2.Execute(file2, repo)
+	if err != nil {
+		log.Print("cannot execute build.ps1 template")
+		return err
+	}
+
 	// create tarball
-	cmd = exec.Command("tar", "cvf", "../build.tar", "src", "build.sh")
+	cmd = exec.Command("tar", "cvf", "../build.tar", "src", "build.sh", "build.ps1")
 	cmd.Dir = buildPath
 	if err := cmd.Run(); err != nil {
 		log.Print("tar error")

mercurial