Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/metadataapi #2813

Merged
merged 16 commits into from Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ More detailed release notes can be found on the [releases page](https://github.c
### Added

* Add SetIcon method on ToolbarAction (#2475)
* Access compiled app metadata using new `App.Metadata()` method

### Changed

Expand All @@ -18,7 +19,7 @@ More detailed release notes can be found on the [releases page](https://github.c

### Fixed

*
* SendNotification does not show app name on Windows (#1940)


## 2.1.1 - 22 October 2021
Expand Down
19 changes: 19 additions & 0 deletions app.go
Expand Up @@ -60,6 +60,11 @@ type App interface {

// Lifecycle returns a type that allows apps to hook in to lifecycle events.
Lifecycle() Lifecycle

// Metadata returns the application metadata that was set at compile time.
//
// Since: 2.2
Metadata() AppMetadata
}

var app App
Expand All @@ -84,6 +89,20 @@ func CurrentApp() App {
return app
}

// AppMetadata captures the build metadata for an application.
//
// Since: 2.2
type AppMetadata struct {
// ID is the unique ID of this application, used by many distribution platforms.
ID string
// Name is the human friendly name of this app.
Name string
// Version represents the version of this application, normally following semantic versioning.
Version string
// Build is the build number of this app, some times appended to the version number.
Build int
}

// Lifecycle represents the various phases that an app can transition through.
//
// Since: 2.1
Expand Down
4 changes: 2 additions & 2 deletions app/app_windows.go
Expand Up @@ -74,9 +74,9 @@ var scriptNum = 0
func (a *fyneApp) SendNotification(n *fyne.Notification) {
title := escapeNotificationString(n.Title)
content := escapeNotificationString(n.Content)
appID := a.UniqueID() // TODO once we have an app name compiled in this could be improved
appID := a.UniqueID()
if appID == "" || strings.Index(appID, "missing-id") == 0 {
appID = "Fyne app"
appID = a.Metadata().Name
}

script := fmt.Sprintf(notificationTemplate, title, content, appID)
Expand Down
30 changes: 30 additions & 0 deletions app/meta.go
@@ -0,0 +1,30 @@
package app

import (
"strconv"

"fyne.io/fyne/v2"
intapp "fyne.io/fyne/v2/internal/app"
)

var (
meta fyne.AppMetadata
)

func init() {
build, err := strconv.Atoi(intapp.MetaBuild)
andydotxyz marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
build = 1
}

meta = fyne.AppMetadata{
ID: intapp.MetaID,
Name: intapp.MetaName,
Version: intapp.MetaVersion,
Build: build,
}
}

func (a *fyneApp) Metadata() fyne.AppMetadata {
return meta
}
4 changes: 4 additions & 0 deletions app_test.go
Expand Up @@ -57,6 +57,10 @@ func (dummyApp) Lifecycle() Lifecycle {
return nil
}

func (dummyApp) Metadata() AppMetadata {
return AppMetadata{}
}

func TestSetCurrentApp(t *testing.T) {
a := &dummyApp{}
SetCurrentApp(a)
Expand Down
30 changes: 27 additions & 3 deletions cmd/fyne/internal/commands/build.go
Expand Up @@ -13,6 +13,9 @@ type builder struct {
os, srcdir string
release bool
tags []string

id, name, version string
buildNum int
}

func (b *builder) build() error {
Expand All @@ -27,14 +30,17 @@ func (b *builder) build() error {
env = append(env, "CGO_CFLAGS=-mmacosx-version-min=10.11", "CGO_LDFLAGS=-mmacosx-version-min=10.11")
}

meta := b.generateMetaLDFlags()
if goos == "windows" {
if b.release {
args = append(args, "-ldflags", "-s -w -H=windowsgui")
args = append(args, "-ldflags", "-s -w -H=windowsgui "+meta)
} else {
args = append(args, "-ldflags", "-H=windowsgui")
args = append(args, "-ldflags", "-H=windowsgui "+meta)
}
} else if b.release {
args = append(args, "-ldflags", "-s -w")
args = append(args, "-ldflags", "-s -w "+meta)
} else {
args = append(args, "-ldflags", meta)
}

// handle build tags
Expand All @@ -60,6 +66,24 @@ func (b *builder) build() error {
return err
}

func (b *builder) generateMetaLDFlags() string {
var vars []string
andydotxyz marked this conversation as resolved.
Show resolved Hide resolved
if b.id != "" {
vars = append(vars, fmt.Sprintf("-X 'fyne.io/fyne/v2/internal/app.MetaID=%s'", b.id))
}
if b.name != "" {
vars = append(vars, fmt.Sprintf("-X 'fyne.io/fyne/v2/internal/app.MetaName=%s'", b.name))
}
if b.version != "" {
vars = append(vars, fmt.Sprintf("-X 'fyne.io/fyne/v2/internal/aapp.MetaVersion=%s'", b.version))
andydotxyz marked this conversation as resolved.
Show resolved Hide resolved
}
if b.buildNum != 0 {
vars = append(vars, fmt.Sprintf("-X 'fyne.io/fyne/v2/internal/app.MetaBuild=%d'", b.buildNum))
Jacalz marked this conversation as resolved.
Show resolved Hide resolved
}

return strings.Join(vars, " ")
}

func targetOS() string {
osEnv, ok := os.LookupEnv("GOOS")
if ok {
Expand Down
5 changes: 5 additions & 0 deletions cmd/fyne/internal/commands/package.go
Expand Up @@ -187,6 +187,11 @@ func (p *Packager) buildPackage() error {
srcdir: p.srcDir,
release: p.release,
tags: tags,

id: p.appID,
name: p.name,
version: p.appVersion,
buildNum: p.appBuild,
}

return b.build()
Expand Down
9 changes: 9 additions & 0 deletions internal/app/meta.go
@@ -0,0 +1,9 @@
package app

// these internal variables are set by the fyne build command so that the "FyneApp.toml" data is readable at runtime.
var (
MetaID = "com.example"
MetaName = "Fyne App"
MetaVersion = "1.0.0"
MetaBuild = "1"
)
4 changes: 4 additions & 0 deletions test/testapp.go
Expand Up @@ -86,6 +86,10 @@ func (a *testApp) Lifecycle() fyne.Lifecycle {
return a.lifecycle
}

func (a *testApp) Metadata() fyne.AppMetadata {
return fyne.AppMetadata{} // just dummy data
}

func (a *testApp) lastAppliedTheme() fyne.Theme {
a.propertyLock.Lock()
defer a.propertyLock.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions theme/themedtestapp.go
Expand Up @@ -64,6 +64,10 @@ func (t *themedApp) Lifecycle() fyne.Lifecycle {
return nil
}

func (t *themedApp) Metadata() fyne.AppMetadata {
return fyne.AppMetadata{}
}

func (t *themedApp) PrimaryColor() string {
return ColorBlue
}
Expand Down