Skip to content

Commit fec0c8f

Browse files
authored
[chore] Generate MSI config files to reduce duplication (#1227)
* [chore] generate MSI config files to reduce duplication * use different delimiters to generate template from template * write to file, remove old files * remove old files * remove file printing
1 parent b2a4951 commit fec0c8f

File tree

5 files changed

+85
-173
lines changed

5 files changed

+85
-173
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ generate: generate-sources generate-goreleaser
3030
generate-goreleaser: go
3131
@./scripts/generate-goreleaser.sh -d "${DISTRIBUTIONS}" -b "${BINARIES}" -g ${GO}
3232

33-
generate-sources: go ocb
33+
generate-sources: go ocb generate-msi
3434
@./scripts/build.sh -d "${DISTRIBUTIONS}" -s true -b ${OTELCOL_BUILDER}
3535

36+
generate-msi: go ocb
37+
$(GO) run cmd/msi-generator/main.go -d "${DISTRIBUTIONS}"
38+
3639
goreleaser-verify: goreleaser
3740
@${GORELEASER} release --snapshot --clean
3841

cmd/msi-generator/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"flag"
9+
"fmt"
10+
"log"
11+
"os"
12+
"strings"
13+
"text/template"
14+
)
15+
16+
const (
17+
coreDistro = "otelcol"
18+
contribDistro = "otelcol-contrib"
19+
otlpDistro = "otelcol-otlp"
20+
templateFilename = "cmd/msi-generator/windows-installer.wxs.tmpl"
21+
finalFilename = "windows-installer.wxs"
22+
distroFolder = "distributions"
23+
)
24+
25+
var (
26+
distFlag = flag.String("d", "", "Collector distributions to build")
27+
)
28+
29+
func main() {
30+
flag.Parse()
31+
32+
if len(*distFlag) == 0 {
33+
log.Fatal("no distribution to template")
34+
}
35+
distros := strings.Split(*distFlag, ",")
36+
37+
for _, distro := range distros {
38+
log.Println("Templating MSI installer for distribution: " + distro)
39+
TemplateDist(distro)
40+
}
41+
}
42+
43+
func TemplateDist(dist string) {
44+
switch dist {
45+
case coreDistro, contribDistro:
46+
templateDist(dist, true)
47+
case otlpDistro:
48+
templateDist(dist, false)
49+
default:
50+
log.Println("Unknown distribution: " + dist)
51+
}
52+
}
53+
54+
func templateDist(dist string, addConfig bool) {
55+
// Parse the base template
56+
baseTemplate, err := template.New("base").Delims("<<", ">>").ParseFiles(templateFilename)
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
// Data for the base template
62+
data := map[string]interface{}{
63+
"AddConfig": addConfig,
64+
}
65+
66+
// Execute the base template to generate a new template
67+
var generatedTemplateContent bytes.Buffer
68+
err = baseTemplate.ExecuteTemplate(&generatedTemplateContent, "base", data)
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
err = os.WriteFile(fmt.Sprintf("%s/%s/%s", distroFolder, dist, finalFilename), generatedTemplateContent.Bytes(), 0644)
74+
if err != nil {
75+
panic(err)
76+
}
77+
}

distributions/otelcol-contrib/windows-installer.wxs renamed to cmd/msi-generator/windows-installer.wxs.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<< define "base" ->>
12
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
23
<Product
34
Name="OpenTelemetry Collector ({{ .Version }}) - {{ .Binary }} distribution"
@@ -47,10 +48,12 @@
4748
Name="{{ .Binary }}.exe"
4849
Source="{{ .Binary }}.exe"
4950
KeyPath="yes"/>
51+
<< if .AddConfig >>
5052
<File
5153
Id="config.yaml"
5254
Name="config.yaml"
5355
Source="config.yaml"/>
56+
<< end ->>
5457

5558
<ServiceInstall
5659
Id="Sevice"
@@ -86,3 +89,4 @@
8689
</Directory>
8790
</Product>
8891
</Wix>
92+
<< end >>

distributions/otelcol-otlp/windows-installer.wxs

Lines changed: 0 additions & 84 deletions
This file was deleted.

distributions/otelcol/windows-installer.wxs

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)