Skip to content

Commit 4f5861a

Browse files
author
jld3103
authored
Merge branch 'master' into feature/packaging/darwin-pkg
2 parents b17687d + 1d570a2 commit 4f5861a

16 files changed

+1064
-135
lines changed

assets/app/main_desktop.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'package:flutter/foundation.dart'
2+
show debugDefaultTargetPlatformOverride;
3+
import 'package:flutter/material.dart';
4+
5+
import './main.dart';
6+
7+
void main() {
8+
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
9+
runApp(MyApp());
10+
}

assets/plugin/README.md.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# {{.pluginName}}
2+
3+
This Go package implements the host-side of the Flutter [{{.pluginName}}](https://{{.urlVSCRepo}}) plugin.
4+
5+
## Usage
6+
7+
Import as:
8+
9+
```go
10+
import {{.pluginName}} "{{.urlVSCRepo}}/go"
11+
```
12+
13+
Then add the following option to your go-flutter [application options](https://github.com/go-flutter-desktop/go-flutter/wiki/Plugin-info):
14+
15+
```go
16+
flutter.AddPlugin(&{{.pluginName}}.{{.structName}}{}),
17+
```

assets/plugin/import.go.tmpl.tmpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
// DO NOT EDIT, this file is generated by hover at compile-time for the {{.pluginName}} plugin.
4+
5+
import (
6+
flutter "github.com/go-flutter-desktop/go-flutter"
7+
{{.pluginName}} "{{.urlVSCRepo}}/go"
8+
)
9+
10+
func init() {
11+
// Only the init function can be tweaked by plugin maker.
12+
options = append(options, flutter.AddPlugin(&{{.pluginName}}.{{.structName}}{}))
13+
}

assets/plugin/plugin.go.tmpl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package {{.pluginName}}
2+
3+
import (
4+
flutter "github.com/go-flutter-desktop/go-flutter"
5+
"github.com/go-flutter-desktop/go-flutter/plugin"
6+
)
7+
8+
const channelName = "{{.pluginName}}"
9+
10+
// {{.structName}} implements flutter.Plugin and handles method.
11+
type {{.structName}} struct{}
12+
13+
var _ flutter.Plugin = &{{.structName}}{} // compile-time type check
14+
15+
// InitPlugin initializes the plugin.
16+
func (p *{{.structName}}) InitPlugin(messenger plugin.BinaryMessenger) error {
17+
channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})
18+
channel.HandleFunc("getPlatformVersion", p.handlePlatformVersion)
19+
return nil
20+
}
21+
22+
func (p *{{.structName}}) handlePlatformVersion(arguments interface{}) (reply interface{}, err error) {
23+
return "go-flutter " + flutter.PlatformVersion, nil
24+
}

cmd/build.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import (
1414
"github.com/otiai10/copy"
1515
"github.com/spf13/cobra"
1616

17+
"github.com/go-flutter-desktop/hover/internal/build"
1718
"github.com/go-flutter-desktop/hover/internal/enginecache"
19+
"github.com/go-flutter-desktop/hover/internal/fileutils"
1820
"github.com/go-flutter-desktop/hover/internal/log"
19-
"github.com/go-flutter-desktop/hover/internal/versioncheck"
20-
"github.com/go-flutter-desktop/hover/internal/build"
2121
"github.com/go-flutter-desktop/hover/internal/pubspec"
2222
"github.com/go-flutter-desktop/hover/internal/androidmanifest"
23+
"github.com/go-flutter-desktop/hover/internal/versioncheck"
2324
"github.com/go-flutter-desktop/hover/cmd/packaging"
2425
)
2526

@@ -158,6 +159,31 @@ var buildWindowsCmd = &cobra.Command{
158159
},
159160
}
160161

162+
// checkForMainDesktop checks and adds the lib/main_desktop.dart dart entry
163+
// point if needed
164+
func checkForMainDesktop() {
165+
if buildTarget != "lib/main_desktop.dart" {
166+
return
167+
}
168+
_, err := os.Stat("lib/main_desktop.dart")
169+
if os.IsNotExist(err) {
170+
log.Warnf("Target file \"lib/main_desktop.dart\" not found.")
171+
log.Warnf("Let hover add the \"lib/main_desktop.dart\" file? ")
172+
if askForConfirmation() {
173+
fileutils.CopyAsset("app/main_desktop.dart", filepath.Join("lib", "main_desktop.dart"), assetsBox)
174+
log.Infof("Target file \"lib/main_desktop.dart\" has been created.")
175+
log.Infof(" Depending on your project, you might want to tweak it.")
176+
return
177+
}
178+
log.Printf("You can define a custom traget by using the %s flag.", log.Au().Magenta("--target"))
179+
os.Exit(1)
180+
}
181+
if err != nil {
182+
log.Errorf("Failed to stat lib/main_desktop.dart: %v\n", err)
183+
os.Exit(1)
184+
}
185+
}
186+
161187
func buildInDocker(targetOS string, vmArguments []string) {
162188
crossCompilingDir, err := filepath.Abs(filepath.Join(build.BuildPath, "cross-compiling"))
163189
err = os.MkdirAll(crossCompilingDir, 0755)
@@ -257,6 +283,7 @@ func buildInDocker(targetOS string, vmArguments []string) {
257283
}
258284

259285
func buildNormal(targetOS string, vmArguments []string) {
286+
checkForMainDesktop()
260287
crossCompile = targetOS != runtime.GOOS
261288
buildDocker = crossCompile || buildDocker
262289

@@ -305,6 +332,14 @@ func buildNormal(targetOS string, vmArguments []string) {
305332
trackWidgetCreation = "--track-widget-creation"
306333
}
307334

335+
// must be run before `flutter build bundle`
336+
// because `build bundle` will update the file timestamp
337+
runPluginGet, err := shouldRunPluginGet()
338+
if err != nil {
339+
log.Errorf("Failed to check if plugin get should be run: %v.\n", err)
340+
os.Exit(1)
341+
}
342+
308343
cmdFlutterBuild := exec.Command(build.FlutterBin, "build", "bundle",
309344
"--asset-dir", filepath.Join(build.OutputDirectoryPath(targetOS), "flutter_assets"),
310345
"--target", buildTarget,
@@ -322,6 +357,16 @@ func buildNormal(targetOS string, vmArguments []string) {
322357
}
323358
}
324359

360+
if runPluginGet {
361+
log.Printf("listing available plugins:")
362+
if hoverPluginGet(true) {
363+
log.Infof(fmt.Sprintf("run `%s`? ", log.Au().Magenta("hover plugins get")))
364+
if askForConfirmation() {
365+
hoverPluginGet(false)
366+
}
367+
}
368+
}
369+
325370
var engineFile string
326371
switch targetOS {
327372
case "darwin":

cmd/common.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"regexp"
9+
"runtime"
810
"strings"
11+
"time"
912

1013
"github.com/go-flutter-desktop/hover/internal/build"
1114
"github.com/go-flutter-desktop/hover/internal/log"
1215
"github.com/go-flutter-desktop/hover/internal/pubspec"
1316
)
1417

18+
// initBinaries is used to ensure go and flutter exec are found in the
19+
// user's path
1520
func initBinaries() {
1621
var err error
1722
goAvailable := false
@@ -37,13 +42,25 @@ func initBinaries() {
3742
log.Errorf("Failed to lookup 'flutter' executable. Please install flutter.\nhttps://flutter.dev/docs/get-started/install")
3843
os.Exit(1)
3944
}
45+
gitBin, err = exec.LookPath("git")
46+
if err != nil {
47+
log.Warnf("Failed to lookup 'git' executable.")
48+
}
4049
}
4150

4251
// assertInFlutterProject asserts this command is executed in a flutter project
4352
func assertInFlutterProject() {
4453
pubspec.GetPubSpec()
4554
}
4655

56+
// assertInFlutterPluginProject asserts this command is executed in a flutter plugin project
57+
func assertInFlutterPluginProject() {
58+
if _, ok := pubspec.GetPubSpec().Flutter["plugin"]; !ok {
59+
log.Errorf("The directory doesn't appear to contain a plugin package.\nTo create a new plugin, first run `%s`, then run `%s`.", log.Au().Magenta("flutter create --template=plugin"), log.Au().Magenta("hover init-plugin"))
60+
os.Exit(1)
61+
}
62+
}
63+
4764
func assertHoverInitialized() {
4865
_, err := os.Stat(build.BuildPath)
4966
if os.IsNotExist(err) {
@@ -59,6 +76,7 @@ func assertHoverInitialized() {
5976
}
6077
}
6178

79+
// hoverMigration migrates from old hover buildPath directory to the new one ("desktop" -> "go")
6280
func hoverMigration() bool {
6381
oldBuildPath := "desktop"
6482
file, err := os.Open(filepath.Join(oldBuildPath, "go.mod"))
@@ -86,7 +104,7 @@ func hoverMigration() bool {
86104

87105
// askForConfirmation asks the user for confirmation.
88106
func askForConfirmation() bool {
89-
log.Printf("[y/N]: ")
107+
fmt.Print(log.Au().Bold(log.Au().Cyan("hover: ")).String() + "[y/N]? ")
90108
in := bufio.NewReader(os.Stdin)
91109
s, err := in.ReadString('\n')
92110
if err != nil {

cmd/create-plugin.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

cmd/init-plugin.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/go-flutter-desktop/hover/internal/build"
9+
"github.com/go-flutter-desktop/hover/internal/fileutils"
10+
"github.com/go-flutter-desktop/hover/internal/log"
11+
"github.com/go-flutter-desktop/hover/internal/pubspec"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func init() {
16+
rootCmd.AddCommand(createPluginCmd)
17+
}
18+
19+
var createPluginCmd = &cobra.Command{
20+
Use: "init-plugin",
21+
Short: "Initialize a go-flutter plugin in a existing flutter platform plugin",
22+
Args: func(cmd *cobra.Command, args []string) error {
23+
if len(args) != 1 {
24+
return errors.New("requires one argument, the VCS repository path. e.g.: github.com/my-organization/" + pubspec.GetPubSpec().Name + "\n" +
25+
"This path will be used by Golang to fetch the plugin, make sure it correspond to the code repository of the plugin!")
26+
}
27+
return nil
28+
},
29+
Run: func(cmd *cobra.Command, args []string) {
30+
assertInFlutterPluginProject()
31+
32+
vcsPath := args[0]
33+
34+
err := os.Mkdir(build.BuildPath, 0775)
35+
if err != nil {
36+
if os.IsExist(err) {
37+
log.Errorf("A file or directory named `" + build.BuildPath + "` already exists. Cannot continue init-plugin.")
38+
os.Exit(1)
39+
}
40+
log.Errorf("Failed to create '%s' directory: %v", build.BuildPath, err)
41+
os.Exit(1)
42+
}
43+
44+
templateData := map[string]string{
45+
"pluginName": pubspec.GetPubSpec().Name,
46+
"structName": toCamelCase(pubspec.GetPubSpec().Name + "Plugin"),
47+
"urlVSCRepo": vcsPath,
48+
}
49+
50+
fileutils.CopyTemplate("plugin/plugin.go.tmpl", filepath.Join(build.BuildPath, "plugin.go"), assetsBox, templateData)
51+
fileutils.CopyTemplate("plugin/README.md.tmpl", filepath.Join(build.BuildPath, "README.md"), assetsBox, templateData)
52+
fileutils.CopyTemplate("plugin/import.go.tmpl.tmpl", filepath.Join(build.BuildPath, "import.go.tmpl"), assetsBox, templateData)
53+
54+
initializeGoModule(vcsPath)
55+
},
56+
}

cmd/init.go

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package cmd
22

33
import (
44
"errors"
5-
"io"
65
"os"
7-
"os/exec"
86
"path/filepath"
97

108
"github.com/spf13/cobra"
119

1210
"github.com/go-flutter-desktop/hover/internal/build"
11+
"github.com/go-flutter-desktop/hover/internal/fileutils"
1312
"github.com/go-flutter-desktop/hover/internal/log"
1413
"github.com/go-flutter-desktop/hover/internal/pubspec"
1514
)
@@ -43,6 +42,8 @@ var initCmd = &cobra.Command{
4342
log.Errorf("A file or directory named '%s' already exists. Cannot continue init.", build.BuildPath)
4443
os.Exit(1)
4544
}
45+
log.Errorf("Failed to create '%s' directory: %v", build.BuildPath, err)
46+
os.Exit(1)
4647
}
4748

4849
desktopCmdPath := filepath.Join(build.BuildPath, "cmd")
@@ -59,62 +60,13 @@ var initCmd = &cobra.Command{
5960
os.Exit(1)
6061
}
6162

62-
copyAsset("app/main.go", filepath.Join(desktopCmdPath, "main.go"))
63-
copyAsset("app/options.go", filepath.Join(desktopCmdPath, "options.go"))
64-
copyAsset("app/icon.png", filepath.Join(desktopAssetsPath, "icon.png"))
65-
copyAsset("app/gitignore", filepath.Join(build.BuildPath, ".gitignore"))
66-
67-
wd, err := os.Getwd()
68-
if err != nil {
69-
log.Errorf("Failed to get working dir: %v", err)
70-
os.Exit(1)
71-
}
72-
73-
cmdGoModInit := exec.Command(build.GoBin, "mod", "init", projectPath+"/"+build.BuildPath)
74-
cmdGoModInit.Dir = filepath.Join(wd, build.BuildPath)
75-
cmdGoModInit.Env = append(os.Environ(),
76-
"GO111MODULE=on",
77-
)
78-
cmdGoModInit.Stderr = os.Stderr
79-
cmdGoModInit.Stdout = os.Stdout
80-
err = cmdGoModInit.Run()
81-
if err != nil {
82-
log.Errorf("Go mod init failed: %v", err)
83-
os.Exit(1)
84-
}
63+
fileutils.CopyAsset("app/main.go", filepath.Join(desktopCmdPath, "main.go"), assetsBox)
64+
fileutils.CopyAsset("app/options.go", filepath.Join(desktopCmdPath, "options.go"), assetsBox)
65+
fileutils.CopyAsset("app/icon.png", filepath.Join(desktopAssetsPath, "icon.png"), assetsBox)
66+
fileutils.CopyAsset("app/gitignore", filepath.Join(build.BuildPath, ".gitignore"), assetsBox)
8567

86-
cmdGoModTidy := exec.Command(build.GoBin, "mod", "tidy")
87-
cmdGoModTidy.Dir = filepath.Join(wd, build.BuildPath)
88-
log.Printf(cmdGoModTidy.Dir)
89-
cmdGoModTidy.Env = append(os.Environ(),
90-
"GO111MODULE=on",
91-
)
92-
cmdGoModTidy.Stderr = os.Stderr
93-
cmdGoModTidy.Stdout = os.Stdout
94-
err = cmdGoModTidy.Run()
95-
if err != nil {
96-
log.Errorf("Go mod tidy failed: %v", err)
97-
os.Exit(1)
98-
}
68+
initializeGoModule(projectPath)
69+
log.Printf("Available plugin for this project:")
70+
pluginListCmd.Run(cmd, []string{})
9971
},
10072
}
101-
102-
func copyAsset(boxed, to string) {
103-
file, err := os.Create(to)
104-
if err != nil {
105-
log.Errorf("Failed to create %s: %v", to, err)
106-
os.Exit(1)
107-
}
108-
defer file.Close()
109-
boxedFile, err := assetsBox.Open(boxed)
110-
if err != nil {
111-
log.Errorf("Failed to find boxed file %s: %v", boxed, err)
112-
os.Exit(1)
113-
}
114-
defer boxedFile.Close()
115-
_, err = io.Copy(file, boxedFile)
116-
if err != nil {
117-
log.Errorf("Failed to write file %s: %v", to, err)
118-
os.Exit(1)
119-
}
120-
}

0 commit comments

Comments
 (0)