Skip to content

Commit da6511a

Browse files
committed
Add config file
1 parent e1014a0 commit da6511a

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed

assets/app/hover.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target: lib/main_desktop.dart
2+
# Change to "@latest" to download the latest go-flutter version on every build:
3+
branch: ""
4+
# https://github.com/go-flutter-desktop/go-flutter/issues/184:
5+
# cache-path: "/home/YOURUSERNAME/.cache/"
6+
# Uncomment this line if you have trouble with your OpenGL driver (https://github.com/go-flutter-desktop/go-flutter/issues/272):
7+
# opengl: "none"
8+
docker: false

cmd/build.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/go-flutter-desktop/hover/cmd/packaging"
1818
"github.com/go-flutter-desktop/hover/internal/androidmanifest"
1919
"github.com/go-flutter-desktop/hover/internal/build"
20+
"github.com/go-flutter-desktop/hover/internal/config"
2021
"github.com/go-flutter-desktop/hover/internal/enginecache"
2122
"github.com/go-flutter-desktop/hover/internal/fileutils"
2223
"github.com/go-flutter-desktop/hover/internal/log"
@@ -43,12 +44,17 @@ const clangBinName = "o32-clang"
4344
var crossCompile = false
4445
var engineCachePath string
4546

47+
var buildTargetDefault = "lib/main_desktop.dart"
48+
var buildBranchDefault = ""
49+
var buildCachePathDefault = ""
50+
var buildOpenGlVersionDefault = "3.3"
51+
4652
func init() {
47-
buildCmd.PersistentFlags().StringVarP(&buildTarget, "target", "t", "lib/main_desktop.dart", "The main entry-point file of the application.")
48-
buildCmd.PersistentFlags().StringVarP(&buildBranch, "branch", "b", "", "The 'go-flutter' version to use. (@master or @v0.20.0 for example)")
53+
buildCmd.PersistentFlags().StringVarP(&buildTarget, "target", "t", buildTargetDefault, "The main entry-point file of the application.")
54+
buildCmd.PersistentFlags().StringVarP(&buildBranch, "branch", "b", buildBranchDefault, "The 'go-flutter' version to use. (@master or @v0.20.0 for example)")
4955
buildCmd.PersistentFlags().BoolVar(&buildDebug, "debug", false, "Build a debug version of the app.")
50-
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)")
51-
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.")
56+
buildCmd.PersistentFlags().StringVarP(&buildCachePath, "cache-path", "", buildCachePathDefault, "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll (defaults to the standard user cache directory)")
57+
buildCmd.PersistentFlags().StringVar(&buildOpenGlVersion, "opengl", 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.")
5258
buildCmd.PersistentFlags().BoolVar(&buildDocker, "docker", false, "Compile in Docker container only. No need to install go")
5359
buildCmd.AddCommand(buildLinuxCmd)
5460
buildCmd.AddCommand(buildLinuxSnapCmd)
@@ -304,6 +310,21 @@ func buildInDocker(targetOS string, vmArguments []string) {
304310
}
305311

306312
func buildNormal(targetOS string, vmArguments []string) {
313+
if buildTarget == buildTargetDefault && config.GetConfig().Target != "" {
314+
buildTarget = config.GetConfig().Target
315+
}
316+
if buildBranch == buildBranchDefault && config.GetConfig().Branch != "" {
317+
buildBranch = config.GetConfig().Branch
318+
}
319+
if buildCachePath == buildCachePathDefault && config.GetConfig().CachePath != "" {
320+
buildCachePath = config.GetConfig().CachePath
321+
}
322+
if buildOpenGlVersion == buildOpenGlVersionDefault && config.GetConfig().OpenGL != "" {
323+
buildOpenGlVersion = config.GetConfig().OpenGL
324+
}
325+
if !buildDocker && config.GetConfig().Docker {
326+
buildDocker = config.GetConfig().Docker
327+
}
307328
checkForMainDesktop()
308329
crossCompile = targetOS != runtime.GOOS
309330
buildDocker = crossCompile || buildDocker

cmd/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var initCmd = &cobra.Command{
6464
fileutils.CopyAsset("app/options.go", filepath.Join(desktopCmdPath, "options.go"), fileutils.AssetsBox)
6565
fileutils.CopyAsset("app/icon.png", filepath.Join(desktopAssetsPath, "icon.png"), fileutils.AssetsBox)
6666
fileutils.CopyAsset("app/gitignore", filepath.Join(build.BuildPath, ".gitignore"), fileutils.AssetsBox)
67+
fileutils.CopyAsset("app/hover.yaml", filepath.Join(build.BuildPath, "hover.yaml"), fileutils.AssetsBox)
6768

6869
initializeGoModule(projectPath)
6970
log.Printf("Available plugin for this project:")

cmd/run.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
var runObservatoryPort string
2121

2222
func init() {
23-
runCmd.Flags().StringVarP(&buildTarget, "target", "t", "lib/main_desktop.dart", "The main entry-point file of the application.")
24-
runCmd.Flags().StringVarP(&buildBranch, "branch", "b", "", "The 'go-flutter' version to use. (@master or @v0.20.0 for example)")
25-
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)")
26-
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.")
23+
runCmd.Flags().StringVarP(&buildTarget, "target", "t", buildTargetDefault, "The main entry-point file of the application.")
24+
runCmd.Flags().StringVarP(&buildBranch, "branch", "b", buildBranchDefault, "The 'go-flutter' version to use. (@master or @v0.20.0 for example)")
25+
runCmd.Flags().StringVarP(&buildCachePath, "cache-path", "", buildCachePathDefault, "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll (defaults to the standard user cache directory)")
26+
runCmd.Flags().StringVar(&buildOpenGlVersion, "opengl", 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.")
2727
runCmd.Flags().StringVarP(&runObservatoryPort, "observatory-port", "", "50300", "The observatory port used to connect hover to VM services (hot-reload/debug/..)")
2828
runCmd.Flags().BoolVar(&buildOmitEmbedder, "omit-embedder", false, "Don't (re)compile 'go-flutter' source code, useful when only working with Dart code")
2929
runCmd.Flags().BoolVar(&buildOmitFlutterBundle, "omit-flutter", false, "Don't (re)compile the current Flutter project, useful when only working with Golang code (plugin)")

internal/config/config.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/pkg/errors"
8+
"gopkg.in/yaml.v2"
9+
10+
"github.com/go-flutter-desktop/hover/internal/build"
11+
"github.com/go-flutter-desktop/hover/internal/log"
12+
)
13+
14+
// Config contains the parsed contents of hover.yaml
15+
type Config struct {
16+
loaded bool
17+
Target string
18+
Branch string
19+
CachePath string `yaml:"cache-path"`
20+
OpenGL string
21+
Docker bool
22+
}
23+
24+
var config = Config{}
25+
26+
// GetConfig returns the working directory hover.yaml as a Config
27+
func GetConfig() Config {
28+
if !config.loaded {
29+
c, err := ReadConfigFile(filepath.Join(build.BuildPath, "hover.yaml"))
30+
if err != nil {
31+
log.Errorf("%v", err)
32+
log.Errorf("This command should be run from the root of your Flutter project.")
33+
os.Exit(1)
34+
}
35+
config = *c
36+
config.loaded = true
37+
}
38+
return config
39+
}
40+
41+
// ReadConfigFile reads a .yaml file at a path and return a correspond
42+
// Config struct
43+
func ReadConfigFile(configPath string) (*Config, error) {
44+
file, err := os.Open(configPath)
45+
if err != nil {
46+
if os.IsNotExist(err) {
47+
return nil, errors.Wrap(err, "Error: No hover.yaml file found")
48+
}
49+
return nil, errors.Wrap(err, "Failed to open hover.yaml")
50+
}
51+
defer file.Close()
52+
53+
var config Config
54+
err = yaml.NewDecoder(file).Decode(&config)
55+
if err != nil {
56+
return nil, errors.Wrap(err, "Failed to decode hover.yaml")
57+
}
58+
// avoid checking for the flutter dependencies for out of ws directories
59+
if configPath != "hover.yaml" {
60+
return &config, nil
61+
}
62+
return &config, nil
63+
}

0 commit comments

Comments
 (0)