cmd/gio: generate appID if not specified

Use the Go import path to create an appID based on the domain name
plus the last directory location in the import path.

Signed-off-by: Greg Pomerantz <gmp.gio@wow.st>
This commit is contained in:
Greg Pomerantz
2019-09-10 18:28:43 -04:00
committed by Elias Naur
parent 86b231ca28
commit a68d97f947
3 changed files with 74 additions and 9 deletions
+40 -4
View File
@@ -14,7 +14,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
@@ -27,7 +26,7 @@ var (
archNames = flag.String("arch", "", "specify architecture(s) to include (arm, arm64, amd64).")
buildMode = flag.String("buildmode", "exe", "specify buildmode (archive, exe)")
destPath = flag.String("o", "", "output file or directory.\nFor -target ios or tvos, use the .app suffix to target simulators.")
appID = flag.String("appid", "org.gioui.app", "app identifier (for -buildmode=exe)")
appID = flag.String("appid", "", "app identifier (for -buildmode=exe)")
version = flag.Int("version", 1, "app version (for -buildmode=exe)")
printCommands = flag.Bool("x", false, "print the commands")
keepWorkdir = flag.Bool("work", false, "print the name of the temporary work directory and do not delete it when exiting.")
@@ -75,15 +74,16 @@ func mainErr() error {
return fmt.Errorf("invalid -buildmode %s\n", *buildMode)
}
// Find package name.
name, err := runCmd(exec.Command("go", "list", "-f", "{{.ImportPath}}", pkg))
pkgPath, err := runCmd(exec.Command("go", "list", "-f", "{{.ImportPath}}", pkg))
if err != nil {
return fmt.Errorf("gio: %v", err)
}
name = path.Base(name)
dir, err := runCmd(exec.Command("go", "list", "-f", "{{.Dir}}", pkg))
if err != nil {
return fmt.Errorf("gio: %v", err)
}
elems := strings.Split(pkgPath, "/")
name := elems[len(elems)-1]
bi := &buildInfo{
name: name,
pkg: pkg,
@@ -92,6 +92,10 @@ func mainErr() error {
dir: dir,
version: *version,
}
if bi.appID == "" {
bi.appID = appIDFromPackage(pkgPath)
}
switch *target {
case "js":
bi.archs = []string{"wasm"}
@@ -114,6 +118,38 @@ func mainErr() error {
return nil
}
func appIDFromPackage(pkgPath string) string {
elems := strings.Split(pkgPath, "/")
domain := strings.Split(elems[0], ".")
name := ""
if len(elems) > 1 {
name = "." + elems[len(elems)-1]
}
if len(elems) < 2 && len(domain) < 2 {
name = "." + domain[0]
domain[0] = "localhost"
} else {
for i := 0; i < len(domain)/2; i++ {
opp := len(domain) - 1 - i
domain[i], domain[opp] = domain[opp], domain[i]
}
}
pkgDomain := strings.Join(domain, ".")
appid := []rune(pkgDomain + name)
// a Java-language-style package name may contain upper- and lower-case
// letters and underscores with individual parts separated by '.'.
// https://developer.android.com/guide/topics/manifest/manifest-element
for i, c := range appid {
if !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' ||
c == '_' || c == '.') {
appid[i] = '_'
}
}
return string(appid)
}
func build(bi *buildInfo) error {
tmpDir, err := ioutil.TempDir("", "gio-")
if err != nil {