add a way to add arbitrary plist contents

This commit is contained in:
2025-05-03 14:53:51 -04:00
parent 8405a8d01b
commit c56d5058ec
4 changed files with 87 additions and 2 deletions

View File

@@ -13,12 +13,12 @@ Create a macOS Application from an executable (like a Go binary)
To install `appify`: To install `appify`:
```bash ```bash
go get github.com/machinebox/appify go get git.yetaga.in/alazyreader/appify
``` ```
## Usage ## Usage
``` ```bash
appify -name "My Go Application" -icon ./icon.png /path/to/bin appify -name "My Go Application" -icon ./icon.png /path/to/bin
``` ```

13
main.go
View File

@@ -29,6 +29,7 @@ func run() error {
version = flag.String("version", "1.0", "app version") version = flag.String("version", "1.0", "app version")
identifier = flag.String("id", "", "bundle identifier") identifier = flag.String("id", "", "bundle identifier")
icon = flag.String("icon", "", "icon image file (.icns|.png|.jpg|.jpeg)") icon = flag.String("icon", "", "icon image file (.icns|.png|.jpg|.jpeg)")
extraPlist = flag.String("plist", "", "path to file with additional plist dict key/value items")
) )
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
@@ -70,6 +71,13 @@ func run() error {
if id == "" { if id == "" {
id = *author + "." + *name id = *author + "." + *name
} }
if *extraPlist != "" {
b, err := os.ReadFile(*extraPlist)
if err != nil {
return errors.Wrap(err, "error reading plist location: "+*extraPlist)
}
*extraPlist = string(b)
}
info := infoListData{ info := infoListData{
Name: *name, Name: *name,
Executable: filepath.Join("MacOS", appname), Executable: filepath.Join("MacOS", appname),
@@ -77,6 +85,7 @@ func run() error {
Version: *version, Version: *version,
InfoString: *name + " by " + *author, InfoString: *name + " by " + *author,
ShortVersionString: *version, ShortVersionString: *version,
AdditionalPList: *extraPlist,
} }
if *icon != "" { if *icon != "" {
iconPath, err := prepareIcons(*icon, resouresPath) iconPath, err := prepareIcons(*icon, resouresPath)
@@ -150,6 +159,7 @@ type infoListData struct {
InfoString string InfoString string
ShortVersionString string ShortVersionString string
IconFile string IconFile string
AdditionalPList string
} }
const infoPlistTemplate = `<?xml version="1.0" encoding="UTF-8"?> const infoPlistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
@@ -176,6 +186,9 @@ const infoPlistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<key>CFBundleIconFile</key> <key>CFBundleIconFile</key>
<string>{{ .IconFile }}</string> <string>{{ .IconFile }}</string>
{{- end }} {{- end }}
{{- if .AdditionalPList -}}
{{ .AdditionalPList -}}
{{- end }}
</dict> </dict>
</plist> </plist>
` `

View File

@@ -51,6 +51,45 @@ func Test(t *testing.T) {
} }
} }
func TestWithPListInjection(t *testing.T) {
is := is.New(t)
out, err := exec.Command("./appify",
"-name", "Test",
"-icon", "testdata/machina-square.png",
"-plist", "testdata/additionalplist.xml",
"testdata/app").CombinedOutput()
t.Logf("%q", string(out))
is.NoErr(err)
defer os.RemoveAll("Test.app")
actualAppHash := filehash(t, "testdata/app")
type file struct {
path string
perm string
hash string
}
for _, f := range []file{
{path: "Test.app", perm: "drwxr-xr-x"},
{path: "Test.app/Contents", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/MacOS", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/MacOS/Test.app", perm: "-rwxr-xr-x", hash: actualAppHash},
{path: "Test.app/Contents/Info.plist", perm: "-rw-r--r--", hash: "dd977257c7e77dc7739bb4ae70e5f697"},
{path: "Test.app/Contents/README", perm: "-rw-r--r--", hash: "afeb10df47c7f189b848ae44a54e7e06"},
{path: "Test.app/Contents/Resources", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/Resources/icon.icns", perm: "-rw-r--r--", hash: "23bdc36475094ed8886f319811d3a182"},
} {
t.Run(f.path, func(t *testing.T) {
is := is.New(t)
info, err := os.Stat(f.path)
is.NoErr(err)
is.Equal(info.Mode().String(), f.perm) // perm
if f.hash != "" {
actual := filehash(t, f.path)
is.Equal(actual, f.hash) // hash
}
})
}
}
// filehash gets an md5 hash of the file at path. // filehash gets an md5 hash of the file at path.
func filehash(t *testing.T, path string) string { func filehash(t *testing.T, path string) string {
is := is.New(t) is := is.New(t)

33
testdata/additionalplist.xml vendored Normal file
View File

@@ -0,0 +1,33 @@
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>jpg</string>
<string>JPG</string>
<string>jpeg</string>
<string>JPEG</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>jpeg.icns</string>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>image/jpeg</string>
</array>
<key>CFBundleTypeName</key>
<string>JPEG Image</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>JPEG</string>
<string>????</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
</array>
<key>LSTypeIsPackage</key>
<false/>
</dict>
</array>