Skip to content

Commit 74bbb5f

Browse files
authored
Merge pull request #112 from go-flutter-desktop/fix/config-pubspec-read
Fix config/pubspec reading
2 parents 3b600ee + 48ede49 commit 74bbb5f

File tree

6 files changed

+69
-54
lines changed

6 files changed

+69
-54
lines changed

cmd/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func initBuildParameters(targetOS string) {
223223
}
224224

225225
if buildVersionNumber == "" {
226-
buildVersionNumber = pubspec.GetPubSpec().Version
226+
buildVersionNumber = pubspec.GetPubSpec().GetVersion()
227227
}
228228

229229
if buildSkipEngineDownload {
@@ -414,7 +414,7 @@ func buildGoBinary(targetOS string, vmArguments []string) {
414414
}
415415
}
416416

417-
buildCommandString := buildCommand(targetOS, vmArguments, build.OutputBinaryPath(config.GetConfig().ExecutableName(pubspec.GetPubSpec().Name), targetOS))
417+
buildCommandString := buildCommand(targetOS, vmArguments, build.OutputBinaryPath(config.GetConfig().GetExecutableName(pubspec.GetPubSpec().Name), targetOS))
418418
cmdGoBuild := exec.Command(buildCommandString[0], buildCommandString[1:]...)
419419
cmdGoBuild.Dir = filepath.Join(wd, build.BuildPath)
420420
cmdGoBuild.Env = append(os.Environ(),

cmd/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ var initCmd = &cobra.Command{
7070
fileutils.CopyAsset("app/icon.png", filepath.Join(desktopAssetsPath, "icon.png"), fileutils.AssetsBox())
7171
fileutils.CopyAsset("app/gitignore", filepath.Join(build.BuildPath, ".gitignore"), fileutils.AssetsBox())
7272
fileutils.ExecuteTemplateFromAssetsBox("app/hover.yaml.tmpl", filepath.Join(build.BuildPath, "hover.yaml"), fileutils.AssetsBox(), map[string]string{
73-
"applicationName": emptyConfig.ApplicationName(projectName),
74-
"executableName": emptyConfig.ExecutableName(projectName),
75-
"packageName": emptyConfig.PackageName(projectName),
73+
"applicationName": emptyConfig.GetApplicationName(projectName),
74+
"executableName": emptyConfig.GetExecutableName(projectName),
75+
"packageName": emptyConfig.GetPackageName(projectName),
7676
})
7777

7878
initializeGoModule(projectPath)

cmd/packaging/packaging.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ func (t *packagingTask) getTemplateData(projectName, buildVersion string) map[st
8282
"version": buildVersion,
8383
"release": strings.Split(buildVersion, ".")[0],
8484
"arch": runtime.GOARCH,
85-
"description": pubspec.GetPubSpec().Description,
85+
"description": pubspec.GetPubSpec().GetDescription(),
8686
"organizationName": androidmanifest.AndroidOrganizationName(),
87-
"author": config.GetConfig().Author(),
88-
"applicationName": config.GetConfig().ApplicationName(projectName),
89-
"executableName": config.GetConfig().ExecutableName(projectName),
90-
"packageName": config.GetConfig().PackageName(projectName),
91-
"license": config.GetConfig().License(),
87+
"author": pubspec.GetPubSpec().GetAuthor(),
88+
"applicationName": config.GetConfig().GetApplicationName(projectName),
89+
"executableName": config.GetConfig().GetExecutableName(projectName),
90+
"packageName": config.GetConfig().GetPackageName(projectName),
91+
"license": config.GetConfig().GetLicense(),
9292
}
9393
templateData["iconPath"] = executeStringTemplate(t.linuxDesktopFileIconPath, templateData)
9494
templateData["executablePath"] = executeStringTemplate(t.linuxDesktopFileExecutablePath, templateData)
@@ -182,7 +182,7 @@ func (t *packagingTask) Pack(buildVersion string) {
182182
fileutils.CopyTemplateDir(packagingFormatPath(t.packagingFormatName), filepath.Join(tmpPath), t.getTemplateData(projectName, buildVersion))
183183
if t.generateBuildFiles != nil {
184184
log.Infof("Generating dynamic build files")
185-
t.generateBuildFiles(config.GetConfig().PackageName(projectName), tmpPath)
185+
t.generateBuildFiles(config.GetConfig().GetPackageName(projectName), tmpPath)
186186
}
187187

188188
for _, file := range t.executableFiles {
@@ -204,9 +204,9 @@ func (t *packagingTask) Pack(buildVersion string) {
204204
runPackaging(tmpPath, packagingScript)
205205
var outputFileName string
206206
if t.outputFileUsesApplicationName {
207-
outputFileName += config.GetConfig().ApplicationName(projectName)
207+
outputFileName += config.GetConfig().GetApplicationName(projectName)
208208
} else {
209-
outputFileName += config.GetConfig().PackageName(projectName)
209+
outputFileName += config.GetConfig().GetPackageName(projectName)
210210
}
211211
if t.outputFileContainsVersion {
212212
if t.outputFileUsesApplicationName {

cmd/publish-plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ import (
100100
match = []string{"", "origin"}
101101
}
102102

103-
tag := "go/v" + pubspec.GetPubSpec().Version
103+
tag := "go/v" + pubspec.GetPubSpec().GetVersion()
104104

105-
log.Infof("Your plugin at version '%s' is ready to be publish as a golang module.", pubspec.GetPubSpec().Version)
105+
log.Infof("Your plugin at version '%s' is ready to be publish as a golang module.", pubspec.GetPubSpec().GetVersion())
106106
log.Infof("Please run: `%s`", log.Au().Magenta("git tag "+tag))
107107
log.Infof(" `%s`", log.Au().Magenta("git push "+match[1]+" "+tag))
108108

internal/config/config.go

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package config
22

33
import (
44
"os"
5-
"os/user"
65
"path/filepath"
76
"strings"
87

@@ -11,7 +10,6 @@ import (
1110

1211
"github.com/go-flutter-desktop/hover/internal/build"
1312
"github.com/go-flutter-desktop/hover/internal/log"
14-
"github.com/go-flutter-desktop/hover/internal/pubspec"
1513
)
1614

1715
// BuildTargetDefault Default build target file
@@ -29,62 +27,44 @@ const BuildOpenGlVersionDefault = "3.3"
2927
// Config contains the parsed contents of hover.yaml
3028
type Config struct {
3129
loaded bool
32-
applicationName string `yaml:"application-name"`
33-
executableName string `yaml:"executable-name"`
34-
packageName string `yaml:"package-name"`
35-
license string
30+
ApplicationName string `yaml:"application-name"`
31+
ExecutableName string `yaml:"executable-name"`
32+
PackageName string `yaml:"package-name"`
33+
License string
3634
Target string
3735
Branch string
3836
CachePath string `yaml:"cache-path"`
3937
OpenGL string
4038
Engine string `yaml:"engine-version"`
4139
}
4240

43-
func (c Config) ApplicationName(projectName string) string {
44-
if c.applicationName == "" {
41+
func (c Config) GetApplicationName(projectName string) string {
42+
if c.ApplicationName == "" {
4543
return projectName
4644
}
47-
return c.applicationName
45+
return c.ApplicationName
4846
}
4947

50-
func (c Config) ExecutableName(projectName string) string {
51-
if c.executableName == "" {
48+
func (c Config) GetExecutableName(projectName string) string {
49+
if c.ExecutableName == "" {
5250
return strings.ReplaceAll(projectName, " ", "")
5351
}
54-
return c.executableName
52+
return c.ExecutableName
5553
}
5654

57-
func (c Config) PackageName(projectName string) string {
58-
if c.packageName == "" {
55+
func (c Config) GetPackageName(projectName string) string {
56+
if c.PackageName == "" {
5957
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(projectName, "-", ""), "_", ""), " ", "")
6058
}
61-
return c.packageName
59+
return c.PackageName
6260
}
6361

64-
func (c Config) License() string {
65-
if c.license == "" {
66-
log.Warnf("Missing/Empty `license` field in go/hover.yaml.")
67-
log.Warnf("Please add it otherwise you may publish your app with a wrong license.")
68-
log.Warnf("Continuing with `NOASSERTION` as a placeholder license.")
69-
return "NOASSERTION"
62+
func (c Config) GetLicense() string {
63+
if len(c.License) == 0 {
64+
c.License = "NOASSERTION"
65+
PrintMissingField("license", "go/hover.yaml", c.License)
7066
}
71-
return c.license
72-
}
73-
74-
func (c Config) Author() string {
75-
author := pubspec.GetPubSpec().Author
76-
if author == "" {
77-
log.Warnf("Missing author field in pubspec.yaml")
78-
log.Warnf("Please add the `author` field to your pubspec.yaml")
79-
u, err := user.Current()
80-
if err != nil {
81-
log.Errorf("Couldn't get current user: %v", err)
82-
os.Exit(1)
83-
}
84-
author = u.Username
85-
log.Printf("Using this username from system instead: %s", author)
86-
}
87-
return author
67+
return c.License
8868
}
8969

9070
var config = Config{}
@@ -121,3 +101,7 @@ func ReadConfigFile(configPath string) (*Config, error) {
121101
}
122102
return &config, nil
123103
}
104+
105+
func PrintMissingField(name, file, def string) {
106+
log.Warnf("Missing/Empty `%s` field in %s. Please add it or otherwise you may publish your app with a wrong %s. Continuing with `%s` as a placeholder %s.", name, file, name, def, name)
107+
}

internal/pubspec/pubspec.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package pubspec
22

33
import (
44
"fmt"
5+
"github.com/go-flutter-desktop/hover/internal/config"
56
"os"
7+
"os/user"
68

79
"gopkg.in/yaml.v2"
810

@@ -20,6 +22,35 @@ type PubSpec struct {
2022
Flutter map[string]interface{}
2123
}
2224

25+
func (p PubSpec) GetDescription() string {
26+
if len(p.Description) == 0 {
27+
p.Description = "A flutter app made with go-flutter"
28+
config.PrintMissingField("description", "pubspec.yaml", p.Description)
29+
}
30+
return p.Description
31+
}
32+
33+
func (p PubSpec) GetVersion() string {
34+
if len(p.Version) == 0 {
35+
p.Version = "0.0.1"
36+
config.PrintMissingField("version", "pubspec.yaml", p.Version)
37+
}
38+
return p.Version
39+
}
40+
41+
func (p PubSpec) GetAuthor() string {
42+
if len(p.Author) == 0 {
43+
u, err := user.Current()
44+
if err != nil {
45+
log.Errorf("Couldn't get current user: %v", err)
46+
os.Exit(1)
47+
}
48+
p.Author = u.Username
49+
config.PrintMissingField("author", "pubspec.yaml", p.Author)
50+
}
51+
return p.Author
52+
}
53+
2354
var pubspec = PubSpec{}
2455

2556
// GetPubSpec returns the working directory pubspec.yaml as a PubSpec

0 commit comments

Comments
 (0)