Skip to content

Commit

Permalink
Fix invalid XML template for metainfo, causing MacOS to fail on speci…
Browse files Browse the repository at this point in the history
…al chars
  • Loading branch information
dragetd authored Apr 18, 2024
1 parent 7918978 commit 272bec3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
21 changes: 11 additions & 10 deletions cmd/metawriter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"fmt"
"html"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -39,16 +40,16 @@ func writeMetaFiles() {
}

func replacePlaceholders(text string) string {
text = strings.Replace(text, "${LAUNCHER_BINARY}", os.Getenv("LAUNCHER_BINARY"), -1)
text = strings.Replace(text, "${LAUNCHER_BINARY_EXT}", os.Getenv("LAUNCHER_BINARY_EXT"), -1)
text = strings.Replace(text, "${LAUNCHER_VENDOR_NAME}", launcherConfig.VendorName, -1)
text = strings.Replace(text, "${LAUNCHER_BRANDING_NAME}", launcherConfig.BrandingName, -1)
text = strings.Replace(text, "${LAUNCHER_BRANDING_NAME_SHORT}", launcherConfig.BrandingNameShort, -1)
text = strings.Replace(text, "${LAUNCHER_REVERSE_DNS_PRODUCT_ID}", launcherConfig.ReverseDnsProductId, -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_MAJOR}", strconv.Itoa(launcherConfig.ProductVersion.Major), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_MINOR}", strconv.Itoa(launcherConfig.ProductVersion.Minor), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_PATCH}", strconv.Itoa(launcherConfig.ProductVersion.Patch), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_BUILD}", strconv.Itoa(launcherConfig.ProductVersion.Build), -1)
text = strings.Replace(text, "${LAUNCHER_BINARY}", html.EscapeString(os.Getenv("LAUNCHER_BINARY"))), -1)
text = strings.Replace(text, "${LAUNCHER_BINARY_EXT}", html.EscapeString(os.Getenv("LAUNCHER_BINARY_EXT")), -1)
text = strings.Replace(text, "${LAUNCHER_VENDOR_NAME}", html.EscapeString(launcherConfig.VendorName), -1)
text = strings.Replace(text, "${LAUNCHER_BRANDING_NAME}", html.EscapeString(launcherConfig.BrandingName), -1)
text = strings.Replace(text, "${LAUNCHER_BRANDING_NAME_SHORT}", html.EscapeString(launcherConfig.BrandingNameShort), -1)
text = strings.Replace(text, "${LAUNCHER_REVERSE_DNS_PRODUCT_ID}", html.EscapeString(launcherConfig.ReverseDnsProductId), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_MAJOR}", strconv.Itoa(html.EscapeString(launcherConfig.ProductVersion.Major)), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_MINOR}", strconv.Itoa(html.EscapeString(launcherConfig.ProductVersion.Minor)), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_PATCH}", strconv.Itoa(html.EscapeString(launcherConfig.ProductVersion.Patch)), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_BUILD}", strconv.Itoa(html.EscapeString(launcherConfig.ProductVersion.Build)), -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_SEMANTIC}", versionSemantic, -1)
text = strings.Replace(text, "${LAUNCHER_VERSION_FULL}", versionFull, -1)
return text
Expand Down
36 changes: 36 additions & 0 deletions cmd/metawriter/replace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"os"
"testing"
)

// TestReplacePlaceholders checks if the placeholders are replaced and escaped correctly
func TestReplacePlaceholders(t *testing.T) {
os.Setenv("LAUNCHER_BINARY", "launcher&binary")
launcherConfig = &config.LauncherConfig{
VendorName: "Example<Vendor>",
BrandingName: "Brand\"Name",
BrandingNameShort: "Short'Name",
ReverseDnsProductId: "com.example>product",
ProductVersion: config.ProductVersionStruct{
Major: 1,
Minor: 2,
Patch: 3,
Build: 4,
},
}

versionSemantic = "1.2.3"
versionFull = "1.2.3.4"

// Simulating a template with all placeholders
template := `${LAUNCHER_BINARY} ${LAUNCHER_BINARY_EXT} ${LAUNCHER_VENDOR_NAME} ${LAUNCHER_BRANDING_NAME} ${LAUNCHER_BRANDING_NAME_SHORT} ${LAUNCHER_REVERSE_DNS_PRODUCT_ID} ${LAUNCHER_VERSION_MAJOR} ${LAUNCHER_VERSION_MINOR} ${LAUNCHER_VERSION_PATCH} ${LAUNCHER_VERSION_BUILD} ${LAUNCHER_VERSION_SEMANTIC} ${LAUNCHER_VERSION_FULL}`

expected := `launcher&amp;binary ${LAUNCHER_BINARY_EXT} Example&lt;Vendor&gt; Brand&quot;Name Short&apos;Name com.example&gt;product 1 2 3 4 1.2.3 1.2.3.4`
result := replacePlaceholders(template)

if result != expected {
t.Errorf("Expected '%s', got '%s'", expected, result)
}
}

0 comments on commit 272bec3

Please sign in to comment.