-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support reading .ico icon files if passed to packaging #3008
Changes from 3 commits
c09bfc7
e5d136e
0663302
58f0dc8
9f344de
ae1f95f
a504bc6
dcf7fd3
fb36bdd
f88c300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4,15 +4,17 @@ import ( | |||||||
"errors" | ||||||||
"flag" | ||||||||
"fmt" | ||||||||
"image" | ||||||||
_ "image/jpeg" // import image encodings | ||||||||
_ "image/png" // import image encodings | ||||||||
"image/png" // import image encodings | ||||||||
"io/ioutil" | ||||||||
"log" | ||||||||
"os" | ||||||||
"path/filepath" | ||||||||
"strconv" | ||||||||
"strings" | ||||||||
|
||||||||
_ "github.com/biessek/golang-ico" // import image encodings | ||||||||
"github.com/urfave/cli/v2" | ||||||||
"golang.org/x/mod/modfile" | ||||||||
"golang.org/x/mod/module" | ||||||||
|
@@ -347,6 +349,13 @@ func (p *Packager) validate() error { | |||||||
if !util.Exists(p.icon) { | ||||||||
return errors.New("Missing application icon at \"" + p.icon + "\"") | ||||||||
} | ||||||||
if strings.ToLower(filepath.Ext(p.icon)) != ".png" { | ||||||||
tmp, err := normaliseIcon(p.icon) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
p.icon = tmp | ||||||||
} | ||||||||
|
||||||||
p.appID, err = validateAppID(p.appID, p.os, p.name, p.release) | ||||||||
if err != nil { | ||||||||
|
@@ -412,6 +421,35 @@ func mergeMetadata(p *appData, data *metadata.FyneApp) { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
// normaliseIcon takes a non-png image file and converts it to PNG for use in packaging. | ||||||||
// Successful conversion will return a path to the new file. | ||||||||
// Any errors that occur will be returned with an empty string for new path. | ||||||||
func normaliseIcon(path string) (string, error) { | ||||||||
// convert icon | ||||||||
img, err := os.Open(path) | ||||||||
if err != nil { | ||||||||
return "", fmt.Errorf("failed to open source image: %w", err) | ||||||||
} | ||||||||
defer img.Close() | ||||||||
srcImg, _, err := image.Decode(img) | ||||||||
if err != nil { | ||||||||
return "", fmt.Errorf("failed to decode source image: %w", err) | ||||||||
} | ||||||||
|
||||||||
tmpPath := filepath.Join(os.TempDir(), "fyne-ico-tmp.png") | ||||||||
out, err := os.Create(tmpPath) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using ioutil.TempFile() instead?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used that originally, but it's not in Go 1.14 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about that? It seems to exist in Go 1.14 if you look at this: https://pkg.go.dev/io/ioutil@go1.14#TempFile. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh good catch, I must have got the two confused sorry. |
||||||||
if err != nil { | ||||||||
return "", fmt.Errorf("failed to open image output file: %w", err) | ||||||||
} | ||||||||
|
||||||||
err = png.Encode(out, srcImg) | ||||||||
if err != nil { | ||||||||
return "", fmt.Errorf("failed to encode icon: %w", err) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable |
||||||||
} | ||||||||
|
||||||||
return tmpPath, out.Close() | ||||||||
} | ||||||||
|
||||||||
func validateAppID(appID, os, name string, release bool) (string, error) { | ||||||||
// old darwin compatibility | ||||||||
if os == "darwin" { | ||||||||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to remove the file once it is not necessary anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good idea.
I don't think we have a hook for the processed completing at this time.
Can you think of a clean way to do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doPackage use
defer p.removeBuild(files)
. Maybe make doPackage and validate return a[]string
of files that need to be cleaned and set a defer in the caller to those function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could create a folder in the temp directory using https://pkg.go.dev/io/ioutil#TempDir and save everything there when we create temporary files. We then simply make sure to remove the whole folder before exiting the command (we could probably even hook up the interup signal https://www.tutorialspoint.com/how-to-handle-signals-in-golang so that it removes it before exiting using ctrl+c).