Skip to content

Commit d260889

Browse files
committed
Add config file
1 parent e1014a0 commit d260889

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-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+
const buildTargetDefault = "lib/main_desktop.dart"
48+
const buildBranchDefault = ""
49+
const buildCachePathDefault = ""
50+
const 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
)
12+
13+
// Config contains the parsed contents of hover.yaml
14+
type Config struct {
15+
loaded bool
16+
Target string
17+
Branch string
18+
CachePath string `yaml:"cache-path"`
19+
OpenGL string
20+
Docker bool
21+
}
22+
23+
var config = Config{}
24+
25+
// GetConfig returns the working directory hover.yaml as a Config
26+
func GetConfig() Config {
27+
if !config.loaded {
28+
c, err := ReadConfigFile(filepath.Join(build.BuildPath, "hover.yaml"))
29+
if err != nil {
30+
return config
31+
}
32+
config = *c
33+
config.loaded = true
34+
}
35+
return config
36+
}
37+
38+
// ReadConfigFile reads a .yaml file at a path and return a correspond
39+
// Config struct
40+
func ReadConfigFile(configPath string) (*Config, error) {
41+
file, err := os.Open(configPath)
42+
if err != nil {
43+
if os.IsNotExist(err) {
44+
return nil, errors.Wrap(err, "Error: No hover.yaml file found")
45+
}
46+
return nil, errors.Wrap(err, "Failed to open hover.yaml")
47+
}
48+
defer file.Close()
49+
50+
var config Config
51+
err = yaml.NewDecoder(file).Decode(&config)
52+
if err != nil {
53+
return nil, errors.Wrap(err, "Failed to decode hover.yaml")
54+
}
55+
return &config, nil
56+
}

0 commit comments

Comments
 (0)