Skip to content

Commit 2708c8c

Browse files
author
jld3103
authored
Add config file (#51)
Add config file
2 parents e1014a0 + da322eb commit 2708c8c

File tree

5 files changed

+95
-8
lines changed

5 files changed

+95
-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: 20 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"
@@ -44,11 +45,11 @@ var crossCompile = false
4445
var engineCachePath string
4546

4647
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)")
48+
buildCmd.PersistentFlags().StringVarP(&buildTarget, "target", "t", config.BuildTargetDefault, "The main entry-point file of the application.")
49+
buildCmd.PersistentFlags().StringVarP(&buildBranch, "branch", "b", config.BuildBranchDefault, "The 'go-flutter' version to use. (@master or @v0.20.0 for example)")
4950
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.")
51+
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)")
52+
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.")
5253
buildCmd.PersistentFlags().BoolVar(&buildDocker, "docker", false, "Compile in Docker container only. No need to install go")
5354
buildCmd.AddCommand(buildLinuxCmd)
5455
buildCmd.AddCommand(buildLinuxSnapCmd)
@@ -304,6 +305,21 @@ func buildInDocker(targetOS string, vmArguments []string) {
304305
}
305306

306307
func buildNormal(targetOS string, vmArguments []string) {
308+
if buildTarget == config.BuildTargetDefault && config.GetConfig().Target != "" {
309+
buildTarget = config.GetConfig().Target
310+
}
311+
if buildBranch == config.BuildBranchDefault && config.GetConfig().Branch != "" {
312+
buildBranch = config.GetConfig().Branch
313+
}
314+
if buildCachePath == config.BuildCachePathDefault && config.GetConfig().CachePath != "" {
315+
buildCachePath = config.GetConfig().CachePath
316+
}
317+
if buildOpenGlVersion == config.BuildOpenGlVersionDefault && config.GetConfig().OpenGL != "" {
318+
buildOpenGlVersion = config.GetConfig().OpenGL
319+
}
320+
if !buildDocker && config.GetConfig().Docker {
321+
buildDocker = config.GetConfig().Docker
322+
}
307323
checkForMainDesktop()
308324
crossCompile = targetOS != runtime.GOOS
309325
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"bufio"
55
"fmt"
6+
"github.com/go-flutter-desktop/hover/internal/config"
67
"io"
78
"os"
89
"os/exec"
@@ -20,10 +21,10 @@ import (
2021
var runObservatoryPort string
2122

2223
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.")
24+
runCmd.Flags().StringVarP(&buildTarget, "target", "t", config.BuildTargetDefault, "The main entry-point file of the application.")
25+
runCmd.Flags().StringVarP(&buildBranch, "branch", "b", config.BuildBranchDefault, "The 'go-flutter' version to use. (@master or @v0.20.0 for example)")
26+
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)")
27+
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.")
2728
runCmd.Flags().StringVarP(&runObservatoryPort, "observatory-port", "", "50300", "The observatory port used to connect hover to VM services (hot-reload/debug/..)")
2829
runCmd.Flags().BoolVar(&buildOmitEmbedder, "omit-embedder", false, "Don't (re)compile 'go-flutter' source code, useful when only working with Dart code")
2930
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
const BuildTargetDefault = "lib/main_desktop.dart"
14+
const BuildBranchDefault = ""
15+
const BuildCachePathDefault = ""
16+
const BuildOpenGlVersionDefault = "3.3"
17+
18+
// Config contains the parsed contents of hover.yaml
19+
type Config struct {
20+
loaded bool
21+
Target string
22+
Branch string
23+
CachePath string `yaml:"cache-path"`
24+
OpenGL string
25+
Docker bool
26+
}
27+
28+
var config = Config{}
29+
30+
// GetConfig returns the working directory hover.yaml as a Config
31+
func GetConfig() Config {
32+
if !config.loaded {
33+
c, err := ReadConfigFile(filepath.Join(build.BuildPath, "hover.yaml"))
34+
if err != nil {
35+
return config
36+
}
37+
config = *c
38+
config.loaded = true
39+
}
40+
return config
41+
}
42+
43+
// ReadConfigFile reads a .yaml file at a path and return a correspond
44+
// Config struct
45+
func ReadConfigFile(configPath string) (*Config, error) {
46+
file, err := os.Open(configPath)
47+
if err != nil {
48+
if os.IsNotExist(err) {
49+
return nil, errors.Wrap(err, "Error: No hover.yaml file found")
50+
}
51+
return nil, errors.Wrap(err, "Failed to open hover.yaml")
52+
}
53+
defer file.Close()
54+
55+
var config Config
56+
err = yaml.NewDecoder(file).Decode(&config)
57+
if err != nil {
58+
return nil, errors.Wrap(err, "Failed to decode hover.yaml")
59+
}
60+
return &config, nil
61+
}

0 commit comments

Comments
 (0)