Skip to content

Commit a626790

Browse files
committed
Merge branch 'cleanup-packaging-variables'
2 parents b51a1d2 + fdb3749 commit a626790

File tree

10 files changed

+74
-96
lines changed

10 files changed

+74
-96
lines changed

cmd/packaging/darwin-bundle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var DarwinBundleTask = &packagingTask{
1515
},
1616
executableFiles: []string{},
1717
flutterBuildOutputDirectory: "{{.applicationName}} {{.version}}.app/Contents/MacOS",
18-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
18+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
1919
outputFileName := fmt.Sprintf("%s %s.app", applicationName, version)
2020
err := os.MkdirAll(filepath.Join(tmpPath, outputFileName, "Contents", "Resources"), 0755)
2121
if err != nil {

cmd/packaging/darwin-dmg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var DarwinDmgTask = &packagingTask{
1212
dependsOn: map[*packagingTask]string{
1313
DarwinBundleTask: "dmgdir",
1414
},
15-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
15+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
1616
outputFileName := fmt.Sprintf("%s %s.dmg", applicationName, version)
1717
cmdLn := exec.Command("ln", "-sf", "/Applications", "dmgdir/Applications")
1818
cmdLn.Dir = tmpPath

cmd/packaging/darwin-pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var DarwinPkgTask = &packagingTask{
1717
"darwin-pkg/PackageInfo.tmpl": "flat/base.pkg/PackageInfo.tmpl",
1818
"darwin-pkg/Distribution.tmpl": "flat/Distribution.tmpl",
1919
},
20-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
20+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
2121
outputFileName := fmt.Sprintf("%s %s.pkg", applicationName, version)
2222

2323
payload, err := os.OpenFile(filepath.Join("flat", "base.pkg", "Payload"), os.O_RDWR|os.O_CREATE, 0755)

cmd/packaging/linux-appimage.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"strings"
89

910
copy "github.com/otiai10/copy"
1011

@@ -16,16 +17,16 @@ var LinuxAppImageTask = &packagingTask{
1617
packagingFormatName: "linux-appimage",
1718
templateFiles: map[string]string{
1819
"linux-appimage/AppRun.tmpl": "AppRun.tmpl",
19-
"linux/app.desktop.tmpl": "{{.strippedApplicationName}}.desktop.tmpl",
20+
"linux/app.desktop.tmpl": "{{.packageName}}.desktop.tmpl",
2021
},
2122
executableFiles: []string{
2223
".",
2324
"AppRun",
24-
"{{.strippedApplicationName}}.desktop",
25+
"{{.packageName}}.desktop",
2526
},
26-
linuxDesktopFileIconPath: "{{.strippedApplicationName}}",
27+
linuxDesktopFileIconPath: "{{.packageName}}",
2728
flutterBuildOutputDirectory: "build",
28-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
29+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
2930
sourceIconPath := filepath.Join(tmpPath, "build", "assets", "icon.png")
3031
iconDir := filepath.Join(tmpPath, "usr", "share", "icons", "hicolor", "256x256", "apps")
3132
if _, err := os.Stat(iconDir); os.IsNotExist(err) {
@@ -35,12 +36,12 @@ var LinuxAppImageTask = &packagingTask{
3536
os.Exit(1)
3637
}
3738
}
38-
err := copy.Copy(sourceIconPath, filepath.Join(tmpPath, fmt.Sprintf("%s.png", strippedApplicationName)))
39+
err := copy.Copy(sourceIconPath, filepath.Join(tmpPath, fmt.Sprintf("%s.png", packageName)))
3940
if err != nil {
4041
log.Errorf("Failed to copy icon root dir: %v", err)
4142
os.Exit(1)
4243
}
43-
err = copy.Copy(sourceIconPath, filepath.Join(iconDir, fmt.Sprintf("%s.png", strippedApplicationName)))
44+
err = copy.Copy(sourceIconPath, filepath.Join(iconDir, fmt.Sprintf("%s.png", packageName)))
4445
if err != nil {
4546
log.Errorf("Failed to copy icon dir: %v", err)
4647
os.Exit(1)
@@ -58,7 +59,7 @@ var LinuxAppImageTask = &packagingTask{
5859
if err != nil {
5960
return "", err
6061
}
61-
return fmt.Sprintf("%s-%s-x86_64.AppImage", strippedApplicationName, version), nil
62+
return fmt.Sprintf("%s-%s-x86_64.AppImage", strings.ReplaceAll(applicationName, " ", "_"), version), nil
6263
},
6364
requiredTools: map[string][]string{
6465
"linux": {"appimagetool"},

cmd/packaging/linux-deb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ var LinuxDebTask = &packagingTask{
1919
"usr/share/applications/{{.executableName}}.desktop",
2020
},
2121
linuxDesktopFileExecutablePath: "/usr/lib/{{.packageName}}/{{.executableName}}",
22-
linuxDesktopFileIconPath: "/usr/lib/{{.packageName}}/assets/icon.png",
22+
linuxDesktopFileIconPath: "/usr/lib/{{.packageName}}/assets/icon",
2323
flutterBuildOutputDirectory: "usr/lib/{{.packageName}}",
24-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
24+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
2525
outputFileName := fmt.Sprintf("%s_%s_amd64.deb", packageName, version)
2626
cmdDpkgDeb := exec.Command("dpkg-deb", "--build", ".", outputFileName)
2727
cmdDpkgDeb.Dir = tmpPath

cmd/packaging/linux-pkg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ var LinuxPkgTask = &packagingTask{
1919
"src/usr/share/applications/{{.executableName}}.desktop",
2020
},
2121
linuxDesktopFileExecutablePath: "/usr/lib/{{.packageName}}/{{.executableName}}",
22-
linuxDesktopFileIconPath: "/usr/lib/{{.packageName}}/assets/icon.png",
22+
linuxDesktopFileIconPath: "/usr/lib/{{.packageName}}/assets/icon",
2323
flutterBuildOutputDirectory: "src/usr/lib/{{.packageName}}",
24-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
24+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
2525
cmdMakepkg := exec.Command("makepkg")
2626
cmdMakepkg.Dir = tmpPath
2727
cmdMakepkg.Stdout = os.Stdout

cmd/packaging/linux-rpm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ var LinuxRpmTask = &packagingTask{
1919
"BUILDROOT/{{.packageName}}-{{.version}}-{{.release}}.x86_64/usr/share/applications/{{.executableName}}.desktop",
2020
},
2121
linuxDesktopFileExecutablePath: "/usr/lib/{{.packageName}}/{{.executableName}}",
22-
linuxDesktopFileIconPath: "/usr/lib/{{.packageName}}/assets/icon.png",
22+
linuxDesktopFileIconPath: "/usr/lib/{{.packageName}}/assets/icon",
2323
flutterBuildOutputDirectory: "BUILD/{{.packageName}}-{{.version}}-{{.release}}.x86_64/usr/lib/{{.packageName}}",
24-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
24+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
2525
cmdRpmbuild := exec.Command("rpmbuild", "--define", fmt.Sprintf("_topdir %s", tmpPath), "--define", "_unpackaged_files_terminate_build 0", "-ba", fmt.Sprintf("./SPECS/%s.spec", packageName))
2626
cmdRpmbuild.Dir = tmpPath
2727
cmdRpmbuild.Stdout = os.Stdout

cmd/packaging/linux-snap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var LinuxSnapTask = &packagingTask{
1414
"linux/app.desktop.tmpl": "snap/local/{{.executableName}}.desktop.tmpl",
1515
},
1616
linuxDesktopFileExecutablePath: "/{{.executableName}}",
17-
linuxDesktopFileIconPath: "/icon.png",
17+
linuxDesktopFileIconPath: "/icon",
1818
flutterBuildOutputDirectory: "build",
19-
packagingFunction: func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) {
19+
packagingFunction: func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) {
2020
cmdSnapcraft := exec.Command("snapcraft")
2121
cmdSnapcraft.Dir = tmpPath
2222
cmdSnapcraft.Stdout = os.Stdout

cmd/packaging/packaging.go

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ package packaging
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/go-flutter-desktop/hover/internal/androidmanifest"
7+
"github.com/go-flutter-desktop/hover/internal/pubspec"
68
"io/ioutil"
79
"os"
810
"os/exec"
911
"path/filepath"
1012
"runtime"
1113
"strings"
12-
"sync"
1314
"text/template"
1415

1516
"github.com/otiai10/copy"
1617

17-
"github.com/go-flutter-desktop/hover/internal/androidmanifest"
1818
"github.com/go-flutter-desktop/hover/internal/build"
1919
"github.com/go-flutter-desktop/hover/internal/config"
2020
"github.com/go-flutter-desktop/hover/internal/fileutils"
2121
"github.com/go-flutter-desktop/hover/internal/log"
22-
"github.com/go-flutter-desktop/hover/internal/pubspec"
2322
)
2423

2524
var packagingPath = filepath.Join(build.BuildPath, "packaging")
@@ -55,19 +54,19 @@ func getTemporaryBuildDirectory(projectName string, packagingFormat string) stri
5554
}
5655

5756
type packagingTask struct {
58-
packagingFormatName string // Name of the packaging format: OS-TYPE
59-
dependsOn map[*packagingTask]string // Packaging tasks this task depends on
60-
templateFiles map[string]string // Template files to copy over on init
61-
executableFiles []string // Files that should be executable
62-
linuxDesktopFileExecutablePath string // Path of the executable for linux .desktop file (only set on linux)
63-
linuxDesktopFileIconPath string // Path of the icon for linux .desktop file (only set on linux)
64-
generateBuildFiles func(packageName, path string) // Generate dynamic build files. Operates in the temporary directory
65-
generateInitFiles func(packageName, path string) // Generate dynamic init files
66-
extraTemplateData func(packageName, path string) map[string]string // Update the template data on build. This is used for inserting values that are generated on init
67-
flutterBuildOutputDirectory string // Path to copy the build output of the app to. Operates in the temporary directory
68-
packagingFunction func(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release string) (string, error) // Function that actually packages the app. Needs to check for OS specific tools etc. . Returns the path of the packaged file
69-
skipAssertInitialized bool // Set to true when a task doesn't need to be initialized.
70-
requiredTools map[string][]string // Map of list of tools required to package per OS
57+
packagingFormatName string // Name of the packaging format: OS-TYPE
58+
dependsOn map[*packagingTask]string // Packaging tasks this task depends on
59+
templateFiles map[string]string // Template files to copy over on init
60+
executableFiles []string // Files that should be executable
61+
linuxDesktopFileExecutablePath string // Path of the executable for linux .desktop file (only set on linux)
62+
linuxDesktopFileIconPath string // Path of the icon for linux .desktop file (only set on linux)
63+
generateBuildFiles func(packageName, path string) // Generate dynamic build files. Operates in the temporary directory
64+
generateInitFiles func(packageName, path string) // Generate dynamic init files
65+
extraTemplateData func(packageName, path string) map[string]string // Update the template data on build. This is used for inserting values that are generated on init
66+
flutterBuildOutputDirectory string // Path to copy the build output of the app to. Operates in the temporary directory
67+
packagingFunction func(tmpPath, applicationName, packageName, executableName, version, release string) (string, error) // Function that actually packages the app. Needs to check for OS specific tools etc. . Returns the path of the packaged file
68+
skipAssertInitialized bool // Set to true when a task doesn't need to be initialized.
69+
requiredTools map[string][]string // Map of list of tools required to package per OS
7170
}
7271

7372
func (t *packagingTask) AssertSupported() {
@@ -93,61 +92,6 @@ func (t *packagingTask) AssertSupported() {
9392
}
9493
}
9594

96-
var templateData map[string]string
97-
var once sync.Once
98-
99-
var (
100-
// TODO: these all need documentation
101-
projectName string
102-
version string
103-
release string
104-
description string
105-
organizationName string
106-
author string
107-
applicationName string
108-
strippedApplicationName string
109-
executableName string
110-
packageName string
111-
license string
112-
)
113-
114-
// TODO: it doesn't make sense to have a global "once" function as method on packagingTask
115-
func (t *packagingTask) initData(fullVersion string) map[string]string {
116-
once.Do(func() {
117-
projectName = pubspec.GetPubSpec().Name
118-
version = strings.Split(fullVersion, "+")[0]
119-
if strings.Contains(fullVersion, "+") {
120-
release = strings.Split(fullVersion, "+")[1]
121-
} else {
122-
release = strings.ReplaceAll(fullVersion, ".", "")
123-
}
124-
description = pubspec.GetPubSpec().Description
125-
organizationName = androidmanifest.AndroidOrganizationName()
126-
author = pubspec.GetPubSpec().GetAuthor()
127-
applicationName = config.GetConfig().GetApplicationName(projectName)
128-
strippedApplicationName = strings.ReplaceAll(applicationName, " ", "_")
129-
executableName = config.GetConfig().GetExecutableName(projectName)
130-
packageName = config.GetConfig().GetPackageName(projectName)
131-
license = config.GetConfig().GetLicense()
132-
templateData = map[string]string{
133-
"projectName": projectName,
134-
"version": version,
135-
"release": release,
136-
"description": description,
137-
"organizationName": organizationName,
138-
"author": author,
139-
"applicationName": applicationName,
140-
"strippedApplicationName": strippedApplicationName,
141-
"executableName": executableName,
142-
"packageName": packageName,
143-
"license": license,
144-
}
145-
templateData["iconPath"] = executeStringTemplate(t.linuxDesktopFileIconPath, templateData)
146-
templateData["executablePath"] = executeStringTemplate(t.linuxDesktopFileExecutablePath, templateData)
147-
})
148-
return templateData
149-
}
150-
15195
func (t *packagingTask) Name() string {
15296
return strings.SplitN(t.packagingFormatName, "-", 2)[1]
15397
}
@@ -174,7 +118,7 @@ func (t *packagingTask) init(ignoreAlreadyExists bool) {
174118
}
175119
if t.generateInitFiles != nil {
176120
log.Infof("Generating dynamic init files")
177-
t.generateInitFiles(config.GetConfig().GetPackageName(projectName), dir)
121+
t.generateInitFiles(config.GetConfig().GetPackageName(pubspec.GetPubSpec().Name), dir)
178122
}
179123
log.Infof("go/packaging/%s has been created. You can modify the configuration files and add it to git.", t.packagingFormatName)
180124
log.Infof(fmt.Sprintf("You now can package the %s using `%s`", strings.Split(t.packagingFormatName, "-")[0], log.Au().Magenta("hover build "+t.packagingFormatName)))
@@ -185,18 +129,46 @@ func (t *packagingTask) init(ignoreAlreadyExists bool) {
185129
}
186130

187131
func (t *packagingTask) Pack(fullVersion string) {
188-
t.initData(fullVersion)
189-
t.pack(fullVersion)
132+
projectName := pubspec.GetPubSpec().Name
133+
version := strings.Split(fullVersion, "+")[0]
134+
var release string
135+
if strings.Contains(fullVersion, "+") {
136+
release = strings.Split(fullVersion, "+")[1]
137+
} else {
138+
release = strings.ReplaceAll(fullVersion, ".", "")
139+
}
140+
description := pubspec.GetPubSpec().Description
141+
organizationName := androidmanifest.AndroidOrganizationName()
142+
author := pubspec.GetPubSpec().GetAuthor()
143+
applicationName := config.GetConfig().GetApplicationName(projectName)
144+
executableName := config.GetConfig().GetExecutableName(projectName)
145+
packageName := config.GetConfig().GetPackageName(projectName)
146+
license := config.GetConfig().GetLicense()
147+
templateData := map[string]string{
148+
"projectName": projectName,
149+
"version": version,
150+
"release": release,
151+
"description": description,
152+
"organizationName": organizationName,
153+
"author": author,
154+
"applicationName": applicationName,
155+
"executableName": executableName,
156+
"packageName": packageName,
157+
"license": license,
158+
}
159+
templateData["iconPath"] = executeStringTemplate(t.linuxDesktopFileIconPath, templateData)
160+
templateData["executablePath"] = executeStringTemplate(t.linuxDesktopFileExecutablePath, templateData)
161+
t.pack(templateData, packageName, projectName, applicationName, executableName, version, release)
190162
}
191163

192-
func (t *packagingTask) pack(fullVersion string) {
164+
func (t *packagingTask) pack(templateData map[string]string, packageName, projectName, applicationName, executableName, version, release string) {
193165
if t.extraTemplateData != nil {
194166
for key, value := range t.extraTemplateData(packageName, packagingFormatPath(t.packagingFormatName)) {
195167
templateData[key] = value
196168
}
197169
}
198170
for task := range t.dependsOn {
199-
task.pack(fullVersion)
171+
task.pack(templateData, packageName, projectName, applicationName, executableName, version, release)
200172
}
201173
tmpPath := getTemporaryBuildDirectory(projectName, t.packagingFormatName)
202174
defer func() {
@@ -243,7 +215,7 @@ func (t *packagingTask) pack(fullVersion string) {
243215
os.Exit(1)
244216
}
245217

246-
relativeOutputFilePath, err := t.packagingFunction(tmpPath, applicationName, strippedApplicationName, packageName, executableName, version, release)
218+
relativeOutputFilePath, err := t.packagingFunction(tmpPath, applicationName, packageName, executableName, version, release)
247219
if err != nil {
248220
log.Errorf("%v", err)
249221
log.Warnf("Packaging is very experimental and has mostly been tested on Linux.")
@@ -259,6 +231,11 @@ func (t *packagingTask) pack(fullVersion string) {
259231
log.Errorf("Could not move %s file: %v", outputFileName, err)
260232
os.Exit(1)
261233
}
234+
err = os.Chmod(outputFilePath, 0755)
235+
if err != nil {
236+
log.Errorf("Could not change file permissions for %s: %v", outputFileName, err)
237+
os.Exit(1)
238+
}
262239
}
263240

264241
func (t *packagingTask) AssertInitialized() {

0 commit comments

Comments
 (0)