diff --git a/cmd/build.go b/cmd/build.go index 45dd621f..907369d0 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -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.") @@ -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) @@ -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) } @@ -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) @@ -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 { @@ -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, diff --git a/cmd/common.go b/cmd/common.go index 8b1c86ef..9ef4e146 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -1,9 +1,12 @@ package cmd import ( + "bufio" "fmt" "os" "os/exec" + "path/filepath" + "strings" "github.com/spf13/cobra" yaml "gopkg.in/yaml.v2" @@ -71,9 +74,12 @@ 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 { @@ -81,3 +87,46 @@ func assertHoverInitialized() { 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 +} diff --git a/cmd/init.go b/cmd/init.go index 913d2de4..555111b6 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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) @@ -53,7 +53,7 @@ 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 { @@ -61,8 +61,8 @@ var initCmd = &cobra.Command{ 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", ) @@ -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", diff --git a/cmd/run.go b/cmd/run.go index 5b7479fc..cfbb90a1 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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() diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 134ac93e..5e7cec8d 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -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, @@ -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", ) @@ -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) diff --git a/internal/enginecache/version.go b/internal/enginecache/version.go index 582a83a5..ce1bd97a 100644 --- a/internal/enginecache/version.go +++ b/internal/enginecache/version.go @@ -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) @@ -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")