Skip to content

Add support for darwin-bundle and darwin-pkg #42

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/otiai10/copy"
"github.com/spf13/cobra"

"github.com/go-flutter-desktop/hover/cmd/packaging"
"github.com/go-flutter-desktop/hover/internal/build"
"github.com/go-flutter-desktop/hover/internal/enginecache"
"github.com/go-flutter-desktop/hover/internal/fileutils"
"github.com/go-flutter-desktop/hover/internal/log"
"github.com/go-flutter-desktop/hover/internal/pubspec"
"github.com/go-flutter-desktop/hover/internal/versioncheck"
"github.com/go-flutter-desktop/hover/internal/androidmanifest"
"github.com/go-flutter-desktop/hover/internal/versioncheck"
"github.com/go-flutter-desktop/hover/cmd/packaging"
)

var dotSlash = string([]byte{'.', filepath.Separator})
Expand Down Expand Up @@ -53,6 +54,8 @@ func init() {
buildCmd.AddCommand(buildLinuxSnapCmd)
buildCmd.AddCommand(buildLinuxDebCmd)
buildCmd.AddCommand(buildDarwinCmd)
buildCmd.AddCommand(buildDarwinBundleCmd)
buildCmd.AddCommand(buildDarwinPkgCmd)
buildCmd.AddCommand(buildWindowsCmd)
rootCmd.AddCommand(buildCmd)
}
Expand Down Expand Up @@ -114,6 +117,38 @@ var buildDarwinCmd = &cobra.Command{
},
}

var buildDarwinBundleCmd = &cobra.Command{
Use: "darwin-bundle",
Short: "Build a desktop release for darwin and package it for OSX bundle",
Run: func(cmd *cobra.Command, args []string) {
assertHoverInitialized()
packaging.AssertPackagingFormatInitialized("darwin-bundle")

if !packaging.DockerInstalled() {
os.Exit(1)
}

buildNormal("darwin", nil)
packaging.BuildDarwinBundle()
},
}

var buildDarwinPkgCmd = &cobra.Command{
Use: "darwin-pkg",
Short: "Build a desktop release for darwin and package it for OSX pkg installer",
Run: func(cmd *cobra.Command, args []string) {
assertHoverInitialized()
packaging.AssertPackagingFormatInitialized("darwin-pkg")

if !packaging.DockerInstalled() {
os.Exit(1)
}

buildNormal("darwin", nil)
packaging.BuildDarwinPkg()
},
}

var buildWindowsCmd = &cobra.Command{
Use: "windows",
Short: "Build a desktop release for windows",
Expand Down Expand Up @@ -523,7 +558,7 @@ func buildCommand(targetOS string, vmArguments []string, outputBinaryPath string
pubspec.GetPubSpec().Version,
currentTag,
pubspec.GetPubSpec().Name,
androidOrganizationName()))
androidmanifest.AndroidOrganizationName()))

outputCommand := []string{
"go",
Expand Down
143 changes: 0 additions & 143 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package cmd

import (
"bufio"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,15 +13,6 @@ import (
"github.com/go-flutter-desktop/hover/internal/build"
"github.com/go-flutter-desktop/hover/internal/log"
"github.com/go-flutter-desktop/hover/internal/pubspec"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)

var (
goBin string
flutterBin string
dockerBin string
gitBin string
)

// initBinaries is used to ensure go and flutter exec are found in the
Expand Down Expand Up @@ -131,134 +119,3 @@ func askForConfirmation() bool {
}
return false
}

// AndroidManifest is a file that describes the essential information about
// an android app.
type AndroidManifest struct {
Package string `xml:"package,attr"`
}

// androidOrganizationName fetch the android package name (default:
// 'com.example').
// Can by set upon flutter create (--org flag)
//
// If errors occurs when reading the android package name, the string value
// will correspond to 'hover.failed.to.retrieve.package.name'
func androidOrganizationName() string {
// Default value
androidManifestFile := "android/app/src/main/AndroidManifest.xml"

// Open AndroidManifest file
xmlFile, err := os.Open(androidManifestFile)
if err != nil {
log.Errorf("Failed to retrieve the organization name: %v", err)
return "hover.failed.to.retrieve.package.name"
}
defer xmlFile.Close()

byteXMLValue, err := ioutil.ReadAll(xmlFile)
if err != nil {
log.Errorf("Failed to retrieve the organization name: %v", err)
return "hover.failed.to.retrieve.package.name"
}

var androidManifest AndroidManifest
err = xml.Unmarshal(byteXMLValue, &androidManifest)
if err != nil {
log.Errorf("Failed to retrieve the organization name: %v", err)
return "hover.failed.to.retrieve.package.name"
}
javaPackage := strings.Split(androidManifest.Package, ".")
orgName := strings.Join(javaPackage[:len(javaPackage)-1], ".")
if orgName == "" {
return "hover.failed.to.retrieve.package.name"
}
return orgName
}

var camelcaseRegex = regexp.MustCompile("(^[A-Za-z])|_([A-Za-z])")

// toCamelCase take a snake_case string and converts it to camelcase
func toCamelCase(str string) string {
return camelcaseRegex.ReplaceAllStringFunc(str, func(s string) string {
return strings.ToUpper(strings.Replace(s, "_", "", -1))
})
}

// initializeGoModule uses the golang binary to initialize the go module
func initializeGoModule(projectPath string) {
wd, err := os.Getwd()
if err != nil {
log.Errorf("Failed to get working dir: %v\n", err)
os.Exit(1)
}

cmdGoModInit := exec.Command(goBin, "mod", "init", projectPath+"/"+build.BuildPath)
cmdGoModInit.Dir = filepath.Join(wd, build.BuildPath)
cmdGoModInit.Env = append(os.Environ(),
"GO111MODULE=on",
)
cmdGoModInit.Stderr = os.Stderr
cmdGoModInit.Stdout = os.Stdout
err = cmdGoModInit.Run()
if err != nil {
log.Errorf("Go mod init failed: %v\n", err)
os.Exit(1)
}

cmdGoModTidy := exec.Command(goBin, "mod", "tidy")
cmdGoModTidy.Dir = filepath.Join(wd, build.BuildPath)
log.Infof("You can add the '%s' directory to git.", cmdGoModTidy.Dir)
cmdGoModTidy.Env = append(os.Environ(),
"GO111MODULE=on",
)
cmdGoModTidy.Stderr = os.Stderr
cmdGoModTidy.Stdout = os.Stdout
err = cmdGoModTidy.Run()
if err != nil {
log.Errorf("Go mod tidy failed: %v\n", err)
os.Exit(1)
}
}

// findPubcachePath returns the absolute path for the pub-cache or an error.
func findPubcachePath() (string, error) {
var path string
switch runtime.GOOS {
case "darwin", "linux":
home, err := homedir.Dir()
if err != nil {
return "", errors.Wrap(err, "failed to resolve user home dir")
}
path = filepath.Join(home, ".pub-cache")
case "windows":
path = filepath.Join(os.Getenv("APPDATA"), "Pub", "Cache")
}
return path, nil
}

// shouldRunPluginGet checks if the pubspec.yaml file is older than the
// .packages file, if it is the case, prompt the user for a hover plugin get.
func shouldRunPluginGet() (bool, error) {
file1Info, err := os.Stat("pubspec.yaml")
if err != nil {
return false, err
}

file2Info, err := os.Stat(".packages")
if err != nil {
if os.IsNotExist(err) {
return true, nil
}
return false, err
}
modTime1 := file1Info.ModTime()
modTime2 := file2Info.ModTime()

diff := modTime1.Sub(modTime2)

if diff > (time.Duration(0) * time.Second) {
return true, nil
}
return false, nil
}
24 changes: 24 additions & 0 deletions cmd/packaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
func init() {
initPackagingCmd.AddCommand(initLinuxSnapCmd)
initPackagingCmd.AddCommand(initLinuxDebCmd)
initPackagingCmd.AddCommand(initDarwinBundleCmd)
initPackagingCmd.AddCommand(initDarwinPkgCmd)
rootCmd.AddCommand(initPackagingCmd)
}

Expand Down Expand Up @@ -38,3 +40,25 @@ var initLinuxDebCmd = &cobra.Command{
packaging.InitLinuxDeb()
},
}

var initDarwinBundleCmd = &cobra.Command{
Use: "darwin-bundle",
Short: "Create configuration files for OSX bundle packaging",
Run: func(cmd *cobra.Command, args []string) {
assertHoverInitialized()
packaging.DockerInstalled()

packaging.InitDarwinBundle()
},
}

var initDarwinPkgCmd = &cobra.Command{
Use: "darwin-pkg",
Short: "Create configuration files for OSX pkg installer packaging",
Run: func(cmd *cobra.Command, args []string) {
assertHoverInitialized()
packaging.DockerInstalled()

packaging.InitDarwinPkg()
},
}
Loading