diff --git a/assets/app/hover.yaml b/assets/app/hover.yaml new file mode 100644 index 00000000..a609fda9 --- /dev/null +++ b/assets/app/hover.yaml @@ -0,0 +1,8 @@ +target: lib/main_desktop.dart +# Change to "@latest" to download the latest go-flutter version on every build: +branch: "" +# https://github.com/go-flutter-desktop/go-flutter/issues/184: +# cache-path: "/home/YOURUSERNAME/.cache/" +# Uncomment this line if you have trouble with your OpenGL driver (https://github.com/go-flutter-desktop/go-flutter/issues/272): +# opengl: "none" +docker: false \ No newline at end of file diff --git a/cmd/build.go b/cmd/build.go index 19e36d13..db452014 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -17,6 +17,7 @@ import ( "github.com/go-flutter-desktop/hover/cmd/packaging" "github.com/go-flutter-desktop/hover/internal/androidmanifest" "github.com/go-flutter-desktop/hover/internal/build" + "github.com/go-flutter-desktop/hover/internal/config" "github.com/go-flutter-desktop/hover/internal/enginecache" "github.com/go-flutter-desktop/hover/internal/fileutils" "github.com/go-flutter-desktop/hover/internal/log" @@ -44,11 +45,11 @@ var crossCompile = false var engineCachePath string func init() { - buildCmd.PersistentFlags().StringVarP(&buildTarget, "target", "t", "lib/main_desktop.dart", "The main entry-point file of the application.") - buildCmd.PersistentFlags().StringVarP(&buildBranch, "branch", "b", "", "The 'go-flutter' version to use. (@master or @v0.20.0 for example)") + buildCmd.PersistentFlags().StringVarP(&buildTarget, "target", "t", config.BuildTargetDefault, "The main entry-point file of the application.") + buildCmd.PersistentFlags().StringVarP(&buildBranch, "branch", "b", config.BuildBranchDefault, "The 'go-flutter' version to use. (@master or @v0.20.0 for example)") buildCmd.PersistentFlags().BoolVar(&buildDebug, "debug", false, "Build a debug version of the app.") - buildCmd.PersistentFlags().StringVarP(&buildCachePath, "cache-path", "", "", "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll (defaults to the standard user cache directory)") - buildCmd.PersistentFlags().StringVar(&buildOpenGlVersion, "opengl", "3.3", "The OpenGL version specified here is only relevant for external texture plugin (i.e. video_plugin).\nIf 'none' is provided, texture won't be supported. Note: the Flutter Engine still needs a OpenGL compatible context.") + buildCmd.PersistentFlags().StringVarP(&buildCachePath, "cache-path", "", config.BuildCachePathDefault, "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll (defaults to the standard user cache directory)") + buildCmd.PersistentFlags().StringVar(&buildOpenGlVersion, "opengl", config.BuildOpenGlVersionDefault, "The OpenGL version specified here is only relevant for external texture plugin (i.e. video_plugin).\nIf 'none' is provided, texture won't be supported. Note: the Flutter Engine still needs a OpenGL compatible context.") buildCmd.PersistentFlags().BoolVar(&buildDocker, "docker", false, "Compile in Docker container only. No need to install go") buildCmd.AddCommand(buildLinuxCmd) buildCmd.AddCommand(buildLinuxSnapCmd) @@ -304,6 +305,21 @@ func buildInDocker(targetOS string, vmArguments []string) { } func buildNormal(targetOS string, vmArguments []string) { + if buildTarget == config.BuildTargetDefault && config.GetConfig().Target != "" { + buildTarget = config.GetConfig().Target + } + if buildBranch == config.BuildBranchDefault && config.GetConfig().Branch != "" { + buildBranch = config.GetConfig().Branch + } + if buildCachePath == config.BuildCachePathDefault && config.GetConfig().CachePath != "" { + buildCachePath = config.GetConfig().CachePath + } + if buildOpenGlVersion == config.BuildOpenGlVersionDefault && config.GetConfig().OpenGL != "" { + buildOpenGlVersion = config.GetConfig().OpenGL + } + if !buildDocker && config.GetConfig().Docker { + buildDocker = config.GetConfig().Docker + } checkForMainDesktop() crossCompile = targetOS != runtime.GOOS buildDocker = crossCompile || buildDocker diff --git a/cmd/init.go b/cmd/init.go index ae83b194..0e92557f 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -64,6 +64,7 @@ var initCmd = &cobra.Command{ fileutils.CopyAsset("app/options.go", filepath.Join(desktopCmdPath, "options.go"), fileutils.AssetsBox) fileutils.CopyAsset("app/icon.png", filepath.Join(desktopAssetsPath, "icon.png"), fileutils.AssetsBox) fileutils.CopyAsset("app/gitignore", filepath.Join(build.BuildPath, ".gitignore"), fileutils.AssetsBox) + fileutils.CopyAsset("app/hover.yaml", filepath.Join(build.BuildPath, "hover.yaml"), fileutils.AssetsBox) initializeGoModule(projectPath) log.Printf("Available plugin for this project:") diff --git a/cmd/run.go b/cmd/run.go index 74263c4a..1adcb1c6 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -3,6 +3,7 @@ package cmd import ( "bufio" "fmt" + "github.com/go-flutter-desktop/hover/internal/config" "io" "os" "os/exec" @@ -20,10 +21,10 @@ import ( var runObservatoryPort string func init() { - runCmd.Flags().StringVarP(&buildTarget, "target", "t", "lib/main_desktop.dart", "The main entry-point file of the application.") - runCmd.Flags().StringVarP(&buildBranch, "branch", "b", "", "The 'go-flutter' version to use. (@master or @v0.20.0 for example)") - runCmd.Flags().StringVarP(&buildCachePath, "cache-path", "", "", "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll (defaults to the standard user cache directory)") - runCmd.Flags().StringVar(&buildOpenGlVersion, "opengl", "3.3", "The OpenGL version specified here is only relevant for external texture plugin (i.e. video_plugin).\nIf 'none' is provided, texture won't be supported. Note: the Flutter Engine still needs a OpenGL compatible context.") + runCmd.Flags().StringVarP(&buildTarget, "target", "t", config.BuildTargetDefault, "The main entry-point file of the application.") + runCmd.Flags().StringVarP(&buildBranch, "branch", "b", config.BuildBranchDefault, "The 'go-flutter' version to use. (@master or @v0.20.0 for example)") + runCmd.Flags().StringVarP(&buildCachePath, "cache-path", "", config.BuildCachePathDefault, "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll (defaults to the standard user cache directory)") + runCmd.Flags().StringVar(&buildOpenGlVersion, "opengl", config.BuildOpenGlVersionDefault, "The OpenGL version specified here is only relevant for external texture plugin (i.e. video_plugin).\nIf 'none' is provided, texture won't be supported. Note: the Flutter Engine still needs a OpenGL compatible context.") runCmd.Flags().StringVarP(&runObservatoryPort, "observatory-port", "", "50300", "The observatory port used to connect hover to VM services (hot-reload/debug/..)") runCmd.Flags().BoolVar(&buildOmitEmbedder, "omit-embedder", false, "Don't (re)compile 'go-flutter' source code, useful when only working with Dart code") runCmd.Flags().BoolVar(&buildOmitFlutterBundle, "omit-flutter", false, "Don't (re)compile the current Flutter project, useful when only working with Golang code (plugin)") diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 00000000..c170fa57 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,61 @@ +package config + +import ( + "os" + "path/filepath" + + "github.com/pkg/errors" + "gopkg.in/yaml.v2" + + "github.com/go-flutter-desktop/hover/internal/build" +) + +const BuildTargetDefault = "lib/main_desktop.dart" +const BuildBranchDefault = "" +const BuildCachePathDefault = "" +const BuildOpenGlVersionDefault = "3.3" + +// Config contains the parsed contents of hover.yaml +type Config struct { + loaded bool + Target string + Branch string + CachePath string `yaml:"cache-path"` + OpenGL string + Docker bool +} + +var config = Config{} + +// GetConfig returns the working directory hover.yaml as a Config +func GetConfig() Config { + if !config.loaded { + c, err := ReadConfigFile(filepath.Join(build.BuildPath, "hover.yaml")) + if err != nil { + return config + } + config = *c + config.loaded = true + } + return config +} + +// ReadConfigFile reads a .yaml file at a path and return a correspond +// Config struct +func ReadConfigFile(configPath string) (*Config, error) { + file, err := os.Open(configPath) + if err != nil { + if os.IsNotExist(err) { + return nil, errors.Wrap(err, "Error: No hover.yaml file found") + } + return nil, errors.Wrap(err, "Failed to open hover.yaml") + } + defer file.Close() + + var config Config + err = yaml.NewDecoder(file).Decode(&config) + if err != nil { + return nil, errors.Wrap(err, "Failed to decode hover.yaml") + } + return &config, nil +}