Skip to content

ecosystem consideration: prefixing files with 'go-' #13

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
merged 7 commits into from
Aug 27, 2019
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
14 changes: 8 additions & 6 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
buildOmitFlutterBundle bool
)

const buildPath = "go"

func init() {
buildCmd.Flags().StringVarP(&buildTarget, "target", "t", "lib/main_desktop.dart", "The main entry-point file of the application.")
buildCmd.Flags().StringVarP(&buildManifest, "manifest", "m", "pubspec.yaml", "Flutter manifest file of the application.")
Expand All @@ -52,7 +54,7 @@ var buildCmd = &cobra.Command{
}

func build(projectName string, targetOS string, vmArguments []string) {
outputDirectoryPath, err := filepath.Abs(filepath.Join("desktop", "build", "outputs", targetOS))
outputDirectoryPath, err := filepath.Abs(filepath.Join(buildPath, "build", "outputs", targetOS))
if err != nil {
fmt.Printf("hover: Failed to resolve absolute path for output directory: %v\n", err)
os.Exit(1)
Expand Down Expand Up @@ -172,11 +174,11 @@ func build(projectName string, targetOS string, vmArguments []string) {
}

err = copy.Copy(
filepath.Join("desktop", "assets"),
filepath.Join(buildPath, "assets"),
filepath.Join(outputDirectoryPath, "assets"),
)
if err != nil {
fmt.Printf("hover: Failed to copy desktop/assets: %v\n", err)
fmt.Printf("hover: Failed to copy %s/assets: %v\n", buildPath, err)
os.Exit(1)
}

Expand Down Expand Up @@ -206,7 +208,7 @@ func build(projectName string, targetOS string, vmArguments []string) {

if buildBranch == "" {

currentTag, err := enginecache.CurrentGoFlutterTag(wd)
currentTag, err := enginecache.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
if err != nil {
fmt.Printf("hover: %v\n", err)
os.Exit(1)
Expand All @@ -230,7 +232,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
} else {
// when the buildBranch is empty and the currentTag is a release.
// Check if the 'go-flutter' needs updates.
enginecache.CheckFoGoFlutterUpdate(wd, currentTag)
enginecache.CheckFoGoFlutterUpdate(filepath.Join(wd, buildPath), currentTag)
}

} else {
Expand Down Expand Up @@ -261,7 +263,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
fmt.Sprintf("-ldflags=%s", strings.Join(ldflags, " ")),
dotSlash+"cmd",
)
cmdGoBuild.Dir = filepath.Join(wd, "desktop")
cmdGoBuild.Dir = filepath.Join(wd, buildPath)
cmdGoBuild.Env = append(os.Environ(),
"GO111MODULE=on",
"CGO_LDFLAGS="+cgoLdflags,
Expand Down
53 changes: 51 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -71,13 +74,59 @@ Fail:
}

func assertHoverInitialized() {
_, err := os.Stat("desktop")
_, err := os.Stat(buildPath)
if os.IsNotExist(err) {
fmt.Println("hover: Directory 'desktop' is missing, did you run `hover init` in this project?")
if hoverMigration() {
return
}
fmt.Println("hover: Directory '" + buildPath + "' is missing, did you run `hover init` in this project?")
os.Exit(1)
}
if err != nil {
fmt.Printf("hover: Failed to detect directory desktop: %v\n", err)
os.Exit(1)
}
}

func hoverMigration() bool {
oldBuildPath := "desktop"
file, err := os.Open(filepath.Join(oldBuildPath, "go.mod"))
if err != nil {
return false
}
defer file.Close()

fmt.Println("hover: ⚠ Found older hover directory layout, hover is now expecting a 'go' directory instead of 'desktop'.")
fmt.Println("hover: ⚠ To migrate, rename the 'desktop' directory to 'go'.")
fmt.Printf("hover: Let hover do the migration? ")

if askForConfirmation() {
err := os.Rename(oldBuildPath, buildPath)
if err != nil {
fmt.Printf("hover: Migration failed: %v\n", err)
return false
}
fmt.Printf("hover: Migration success\n")
return true
}

return false
}

// askForConfirmation asks the user for confirmation.
func askForConfirmation() bool {
fmt.Printf("[Y/n]: ")
in := bufio.NewReader(os.Stdin)
s, err := in.ReadString('\n')
if err != nil {
panic(err)
}

s = strings.TrimSpace(s)
s = strings.ToLower(s)

if s == "y" || s == "yes" || s == "" {
return true
}
return false
}
16 changes: 8 additions & 8 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ var initCmd = &cobra.Command{
projectPath := args[0]
assertInFlutterProject()

err := os.Mkdir("desktop", 0775)
err := os.Mkdir(buildPath, 0775)
if err != nil {
if os.IsExist(err) {
fmt.Println("hover: A file or directory named `desktop` already exists. Cannot continue init.")
fmt.Println("hover: A file or directory named `" + buildPath + "` already exists. Cannot continue init.")
os.Exit(1)
}
}

desktopCmdPath := filepath.Join("desktop", "cmd")
desktopCmdPath := filepath.Join(buildPath, "cmd")
err = os.Mkdir(desktopCmdPath, 0775)
if err != nil {
fmt.Printf("hover: Failed to create `%s`: %v\n", desktopCmdPath, err)
os.Exit(1)
}

desktopAssetsPath := filepath.Join("desktop", "assets")
desktopAssetsPath := filepath.Join(buildPath, "assets")
err = os.Mkdir(desktopAssetsPath, 0775)
if err != nil {
fmt.Printf("hover: Failed to create `%s`: %v\n", desktopAssetsPath, err)
Expand All @@ -53,16 +53,16 @@ var initCmd = &cobra.Command{
copyAsset("app/main.go", filepath.Join(desktopCmdPath, "main.go"))
copyAsset("app/options.go", filepath.Join(desktopCmdPath, "options.go"))
copyAsset("app/icon.png", filepath.Join(desktopAssetsPath, "icon.png"))
copyAsset("app/gitignore", filepath.Join("desktop", ".gitignore"))
copyAsset("app/gitignore", filepath.Join(buildPath, ".gitignore"))

wd, err := os.Getwd()
if err != nil {
fmt.Printf("hover: Failed to get working dir: %v\n", err)
os.Exit(1)
}

cmdGoModInit := exec.Command(goBin, "mod", "init", projectPath+"/desktop")
cmdGoModInit.Dir = filepath.Join(wd, "desktop")
cmdGoModInit := exec.Command(goBin, "mod", "init", projectPath+"/"+buildPath)
cmdGoModInit.Dir = filepath.Join(wd, buildPath)
cmdGoModInit.Env = append(os.Environ(),
"GO111MODULE=on",
)
Expand All @@ -75,7 +75,7 @@ var initCmd = &cobra.Command{
}

cmdGoModTidy := exec.Command(goBin, "mod", "tidy")
cmdGoModTidy.Dir = filepath.Join(wd, "desktop")
cmdGoModTidy.Dir = filepath.Join(wd, buildPath)
fmt.Println(cmdGoModTidy.Dir)
cmdGoModTidy.Env = append(os.Environ(),
"GO111MODULE=on",
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var runCmd = &cobra.Command{
}

func runAndAttach(projectName string, targetOS string) {
cmdApp := exec.Command(dotSlash + filepath.Join("desktop", "build", "outputs", targetOS, projectName))
cmdApp := exec.Command(dotSlash + filepath.Join(buildPath, "build", "outputs", targetOS, projectName))
cmdFlutterAttach := exec.Command("flutter", "attach")

stdoutApp, err := cmdApp.StdoutPipe()
Expand Down
6 changes: 3 additions & 3 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func upgradeGoFlutter(targetOS string, engineCachePath string) (err error) {
}

cmdGoGetU := exec.Command(goBin, "get", "-u", "github.com/go-flutter-desktop/go-flutter"+buildBranch)
cmdGoGetU.Dir = filepath.Join(wd, "desktop")
cmdGoGetU.Dir = filepath.Join(wd, buildPath)
cmdGoGetU.Env = append(os.Environ(),
"GO111MODULE=on",
"CGO_LDFLAGS="+cgoLdflags,
Expand All @@ -82,7 +82,7 @@ func upgradeGoFlutter(targetOS string, engineCachePath string) (err error) {
}

cmdGoModDownload := exec.Command(goBin, "mod", "download")
cmdGoModDownload.Dir = filepath.Join(wd, "desktop")
cmdGoModDownload.Dir = filepath.Join(wd, buildPath)
cmdGoModDownload.Env = append(os.Environ(),
"GO111MODULE=on",
)
Expand All @@ -95,7 +95,7 @@ func upgradeGoFlutter(targetOS string, engineCachePath string) (err error) {
return
}

currentTag, err := enginecache.CurrentGoFlutterTag(wd)
currentTag, err := enginecache.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
if err != nil {
fmt.Printf("hover: %v\n", err)
os.Exit(1)
Expand Down
8 changes: 4 additions & 4 deletions internal/enginecache/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func flutterRequiredEngineVersion() string {
// for the current project. If the last update comes back to more than X days,
// fetch the last Github release semver. If the Github semver is more recent
// than the current one, display the update notice.
func CheckFoGoFlutterUpdate(wd string, currentTag string) {
cachedGoFlutterCheckPath := filepath.Join(wd, "desktop", ".last_goflutter_check")
func CheckFoGoFlutterUpdate(goDirectoryPath string, currentTag string) {
cachedGoFlutterCheckPath := filepath.Join(goDirectoryPath, ".last_goflutter_check")
cachedGoFlutterCheckBytes, err := ioutil.ReadFile(cachedGoFlutterCheckPath)
if err != nil && !os.IsNotExist(err) {
fmt.Printf("hover: Failed to read the go-flutter last update check: %v\n", err)
Expand Down Expand Up @@ -113,8 +113,8 @@ func CheckFoGoFlutterUpdate(wd string, currentTag string) {
}

// CurrentGoFlutterTag retrieve the semver of go-flutter in 'go.mod'
func CurrentGoFlutterTag(wd string) (currentTag string, err error) {
goModPath := filepath.Join(wd, "desktop", "go.mod")
func CurrentGoFlutterTag(goDirectoryPath string) (currentTag string, err error) {
goModPath := filepath.Join(goDirectoryPath, "go.mod")
goModBytes, err := ioutil.ReadFile(goModPath)
if err != nil && !os.IsNotExist(err) {
err = errors.Wrap(err, "Failed to read the 'go.mod' file: %v")
Expand Down