-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathplugins.go
609 lines (530 loc) · 16.9 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
"gopkg.in/yaml.v2"
"github.com/go-flutter-desktop/hover/internal/build"
"github.com/go-flutter-desktop/hover/internal/fileutils"
"github.com/go-flutter-desktop/hover/internal/log"
"github.com/go-flutter-desktop/hover/internal/modx"
"github.com/go-flutter-desktop/hover/internal/pubspec"
)
const standaloneImplementationListAPI = "https://raw.githubusercontent.com/go-flutter-desktop/plugins/master/list.json"
var (
listAllPluginDependencies bool
tidyPurge bool
dryRun bool
reImport bool
)
func init() {
pluginTidyCmd.Flags().BoolVar(&tidyPurge, "purge", false, "Remove all go platform plugins imports from the project.")
pluginListCmd.Flags().BoolVarP(&listAllPluginDependencies, "all", "a", false, "List all platform plugins dependencies, even the one have no go-flutter support")
pluginGetCmd.Flags().BoolVar(&reImport, "force", false, "Re-import already imported plugins.")
pluginCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "n", false, "Perform a trial run with no changes made.")
pluginCmd.AddCommand(pluginListCmd)
pluginCmd.AddCommand(pluginGetCmd)
pluginCmd.AddCommand(pluginTidyCmd)
rootCmd.AddCommand(pluginCmd)
}
var pluginCmd = &cobra.Command{
Use: "plugins",
Short: "Tools for plugins",
Long: "A collection of commands to help with finding/importing go-flutter implementations of plugins.",
}
// PubSpecLock contains the parsed contents of pubspec.lock
type PubSpecLock struct {
Packages map[string]PubDep
}
// PubDep contains one entry of the pubspec.lock yaml list
type PubDep struct {
Dependency string
Description interface{}
Source string
Version string
// Fields set by hover
name string
android bool
ios bool
desktop bool
// optional description values
path string // correspond to the path field in lock file
host string // correspond to the host field in lock file
// contain a import.go.tmpl file used for import
autoImport bool
// the path/URL to the go code of the plugin is stored
pluginGoSource string
// whether or not the go plugin source code is located on another VCS repo.
standaloneImpl bool
}
func (p PubDep) imported() bool {
pluginImportOutPath := filepath.Join(build.BuildPath, "cmd", fmt.Sprintf("import-%s-plugin.go", p.name))
if _, err := os.Stat(pluginImportOutPath); err == nil {
return true
}
return false
}
func (p PubDep) platforms() []string {
var platforms []string
if p.android {
platforms = append(platforms, "android")
}
if p.ios {
platforms = append(platforms, "ios")
}
if p.desktop {
platforms = append(platforms, build.BuildPath)
}
return platforms
}
var pluginListCmd = &cobra.Command{
Use: "list",
Short: "List golang platform plugins in the application",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.New("does not take arguments")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
assertInFlutterProject()
dependencyList, err := listPlatformPlugin()
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
var hasNewPlugin bool
var hasPlugins bool
for _, dep := range dependencyList {
if !(dep.desktop || listAllPluginDependencies) {
continue
}
if hasPlugins {
fmt.Println("")
}
hasPlugins = true
log.Infof(" - %s", dep.name)
log.Infof(" version: %s", dep.Version)
log.Infof(" platforms: [%s]", strings.Join(dep.platforms(), ", "))
if dep.desktop {
if dep.standaloneImpl {
log.Infof(" source: This go plugin isn't maintained by the official plugin creator.")
}
if dep.imported() {
log.Infof(" import: [OK] The plugin is already imported in the project.")
continue
}
if dep.autoImport || dep.standaloneImpl {
hasNewPlugin = true
log.Infof(" import: [Missing] The plugin can be imported by hover.")
} else {
log.Infof(" import: [Manual import] The plugin is missing the import.go.tmpl file required for hover import.")
}
if dep.path != "" {
log.Infof(" dev: Plugin replaced in go.mod to path: '%s'", dep.path)
}
}
}
if hasNewPlugin {
log.Infof(fmt.Sprintf("run `%s` to import the missing plugins!", log.Au().Magenta("hover plugins get")))
}
},
}
var pluginTidyCmd = &cobra.Command{
Use: "tidy",
Short: "Removes unused platform plugins.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.New("does not take arguments")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var (
err error
gomod *modfile.File
)
assertInFlutterProject()
assertHoverInitialized()
gomod, err = modx.Open(build.BuildPath)
if err != nil {
log.Errorf("failed to open go.mod", err)
os.Exit(1)
}
desktopCmdPath := filepath.Join(build.BuildPath, "cmd")
dependencyList, err := listPlatformPlugin()
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
importedPlugins, err := ioutil.ReadDir(desktopCmdPath)
if err != nil {
log.Errorf("Failed to search for plugins: %v", err)
os.Exit(1)
}
for _, f := range importedPlugins {
isPlugin := strings.HasPrefix(f.Name(), "import-")
isPlugin = isPlugin && strings.HasSuffix(f.Name(), "-plugin.go")
if isPlugin {
pluginName := strings.TrimPrefix(f.Name(), "import-")
pluginName = strings.TrimSuffix(pluginName, "-plugin.go")
pluginImportPath := filepath.Join(desktopCmdPath, f.Name())
pluginInUse := false
for _, dep := range dependencyList {
if dep.name == pluginName {
// plugin in pubspec.lock
pluginInUse = true
break
}
}
if !pluginInUse || tidyPurge {
// clean-up go.mod
pluginImportStr, err := readPluginGoImport(pluginImportPath, pluginName)
// Delete the 'replace' and 'require' import strings from go.mod.
// Not mission critical, if the plugins not correctly removed from
// the go.mod file, the project still works and the plugin is
// successfully removed from the flutter.Application.
if err != nil || pluginImportStr == "" {
log.Warnf("Couldn't clean the '%s' plugin from the 'go.mod' file. Error: %v", pluginName, err)
} else {
if err = modx.RemoveModule(gomod, pluginImportStr); err != nil {
log.Warnf("failed remove %s from %s/go.mod: %v", pluginImportStr, build.BuildPath, err)
}
}
// remove import file
if !dryRun {
if err = os.Remove(pluginImportPath); err != nil {
log.Warnf("Couldn't remove plugin %s: %v", pluginName, err)
continue
}
}
log.Infof(" plugin: [%s] removed", pluginName)
}
}
}
if dryRun {
s, err := modx.Print(gomod)
if err != nil {
log.Errorf("failed to print updated go.mod: %v", err)
os.Exit(1)
}
log.Infof("modified go.mod:\n%s", s)
} else {
err = modx.Replace(build.BuildPath, gomod)
if err != nil {
log.Errorf("failed to update go.mod: %v", err)
os.Exit(1)
}
}
if tidyPurge {
intermediatesDirectoryPath, err := filepath.Abs(filepath.Join(build.BuildPath, "build", "intermediates"))
if err != nil {
log.Errorf("Failed to resolve absolute path for intermediates directory: %v", err)
os.Exit(1)
}
if fileutils.IsDirectory(intermediatesDirectoryPath) {
_ = os.RemoveAll(intermediatesDirectoryPath)
}
}
},
}
var pluginGetCmd = &cobra.Command{
Use: "get",
Short: "Imports missing platform plugins in the application",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.New("does not take arguments")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
assertInFlutterProject()
assertHoverInitialized()
hoverPluginGet(false)
},
}
func hoverPluginGet(dryRun bool) bool {
dependencyList, err := listPlatformPlugin()
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
for _, dep := range dependencyList {
if !dep.desktop {
continue
}
if !dep.autoImport {
log.Infof(" plugin: [%s] couldn't be imported, check the plugin's README for manual instructions", dep.name)
continue
}
if dryRun {
if dep.imported() {
log.Infof(" plugin: [%s] can be updated", dep.name)
} else {
log.Infof(" plugin: [%s] can be imported", dep.name)
}
continue
}
pluginImportOutPath := filepath.Join(build.BuildPath, "cmd", fmt.Sprintf("import-%s-plugin.go", dep.name))
if dep.imported() && !reImport {
pluginImportStr, err := readPluginGoImport(pluginImportOutPath, dep.name)
if err != nil {
log.Warnf("Couldn't read the plugin '%s' import URL", dep.name)
log.Warnf("Fallback to the latest version installed.")
continue
}
if !goGetModuleSuccess(pluginImportStr, dep.Version) {
log.Warnf("Couldn't download version '%s' of plugin '%s'", dep.Version, dep.name)
log.Warnf("Fallback to the latest version installed.")
continue
}
log.Infof(" plugin: [%s] updated", dep.name)
continue
}
if dep.standaloneImpl {
fileutils.DownloadFile(dep.pluginGoSource, pluginImportOutPath)
} else {
autoImportTemplatePath := filepath.Join(dep.pluginGoSource, "import.go.tmpl")
fileutils.CopyFile(autoImportTemplatePath, pluginImportOutPath)
if fileutils.IsDirectory(filepath.Join(dep.pluginGoSource, "dlib")) {
dlibPath, err := filepath.Abs(filepath.Join(dep.pluginGoSource, "dlib"))
if err != nil {
log.Errorf("Failed to resolve absolute path for dlib directory: %v", err)
os.Exit(1)
}
intermediatesDirectoryPath, err := filepath.Abs(filepath.Join(build.BuildPath, "build", "intermediates"))
if err != nil {
log.Errorf("Failed to resolve absolute path for intermediates directory: %v", err)
os.Exit(1)
}
fileutils.CopyDir(dlibPath, intermediatesDirectoryPath)
if fileutils.IsFileExists(filepath.Join(dlibPath, "README.md")) {
readmeName := fmt.Sprintf("README-%s.md", dep.name)
fileutils.CopyFile(filepath.Join(dlibPath, "README.md"), filepath.Join(intermediatesDirectoryPath, readmeName))
_ = os.Remove(filepath.Join(intermediatesDirectoryPath, "README.md"))
}
}
pluginImportStr, err := readPluginGoImport(pluginImportOutPath, dep.name)
if err != nil {
log.Warnf("Couldn't read the plugin '%s' import URL", dep.name)
log.Warnf("Fallback to the latest version available on github.")
continue
}
// if remote plugin, get the correct version
if dep.path == "" {
if !goGetModuleSuccess(pluginImportStr, dep.Version) {
log.Warnf("Couldn't download version '%s' of plugin '%s'", dep.Version, dep.name)
log.Warnf("Fallback to the latest version available on github.")
}
}
// if local plugin
if dep.path != "" {
path, err := filepath.Abs(filepath.Join(dep.path, build.BuildPath))
if err != nil {
log.Errorf("Failed to resolve absolute path for plugin '%s': %v", dep.name, err)
os.Exit(1)
}
// mutation we're applying to go.mod
mut := func(gomod *modfile.File) error {
return gomod.AddReplace(pluginImportStr, "", path, "")
}
if err = modx.Mutate(build.BuildPath, mut); err != nil {
log.Errorf("failed to update go.mod: %v", err)
os.Exit(1)
}
}
log.Infof(" plugin: [%s] imported", dep.name)
}
}
return len(dependencyList) != 0
}
func listPlatformPlugin() ([]PubDep, error) {
onlineList, err := fetchStandaloneImplementationList()
if err != nil {
log.Warnf("Warning, couldn't read the online plugin list: %v", err)
}
pubcachePath, err := findPubcachePath()
if err != nil {
return nil, errors.Wrap(err, "failed to find path for pub-cache")
}
var list []PubDep
pubLock, err := readPubSpecLock()
if err != nil {
log.Infof("Run `%s` (or equivalent) first", log.Au().Magenta("flutter build bundle"))
return nil, err
}
for name, entry := range pubLock.Packages {
entry.name = name
switch i := entry.Description.(type) {
case string:
if i == "flutter" {
continue
}
case map[interface{}]interface{}:
if value, ok := i["path"]; ok {
entry.path = value.(string)
}
if value, ok := i["url"]; ok {
url, err := url.Parse(value.(string))
if err != nil {
return nil, errors.Wrap(err, "failed to parse URL from string %s"+value.(string))
}
entry.host = url.Host
}
}
pluginPath := filepath.Join(pubcachePath, "hosted", entry.host, entry.name+"-"+entry.Version)
if entry.path != "" {
pluginPath = entry.path
}
pluginPubspecPath := filepath.Join(pluginPath, "pubspec.yaml")
pluginPubspec, err := pubspec.ReadPubSpecFile(pluginPubspecPath)
if err != nil {
continue
}
// Non plugin package are likely to contain android/ios folders (even
// through they aren't used).
// To check if the package is really a platform plugin, we need to read
// the pubspec.yaml file. If he contains a Flutter/plugin entry, then
// it's a platform plugin.
if _, ok := pluginPubspec.Flutter["plugin"]; !ok {
continue
}
detectPlatformPlugin := func(platform string) (bool, error) {
platformPath := filepath.Join(pluginPath, platform)
stat, err := os.Stat(platformPath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, errors.Wrapf(err, "failed to stat %s", platformPath)
}
return stat.IsDir(), nil
}
entry.android, err = detectPlatformPlugin("android")
if err != nil {
return nil, err
}
entry.ios, err = detectPlatformPlugin("ios")
if err != nil {
return nil, err
}
entry.desktop, err = detectPlatformPlugin(build.BuildPath)
if err != nil {
return nil, err
}
if entry.desktop {
entry.pluginGoSource = filepath.Join(pluginPath, build.BuildPath)
autoImportTemplate := filepath.Join(entry.pluginGoSource, "import.go.tmpl")
_, err := os.Stat(autoImportTemplate)
entry.autoImport = true
if err != nil {
entry.autoImport = false
if !os.IsNotExist(err) {
return nil, errors.Wrapf(err, "failed to stat %s", autoImportTemplate)
}
}
} else {
// check if the plugin is available in github.com/go-flutter-desktop/plugins
for _, plugin := range onlineList {
if entry.name == plugin.Name {
entry.desktop = true
entry.standaloneImpl = true
entry.autoImport = true
entry.pluginGoSource = plugin.ImportFile
break
}
}
}
list = append(list, entry)
}
return list, nil
}
// readLocal reads pubspec.lock in the current working directory.
func readPubSpecLock() (*PubSpecLock, error) {
p := &PubSpecLock{}
file, err := os.Open("pubspec.lock")
if err != nil {
if os.IsNotExist(err) {
return nil, errors.New("no pubspec.lock file found")
}
return nil, errors.Wrap(err, "failed to open pubspec.lock")
}
defer file.Close()
err = yaml.NewDecoder(file).Decode(p)
if err != nil {
return nil, errors.Wrap(err, "failed to decode pubspec.lock")
}
return p, nil
}
func readPluginGoImport(pluginImportOutPath, pluginName string) (string, error) {
pluginImportBytes, err := ioutil.ReadFile(pluginImportOutPath)
if err != nil && !os.IsNotExist(err) {
return "", err
}
re := regexp.MustCompile(fmt.Sprintf(`\s+%s\s"(\S*)"`, pluginName))
match := re.FindStringSubmatch(string(pluginImportBytes))
if len(match) < 2 {
err = errors.New("Failed to parse the import path, plugin name in the import must have been changed")
return "", err
}
return match[1], nil
}
type onlineList struct {
List []StandaloneImplementation `json:"standaloneImplementation"`
}
// StandaloneImplementation contains the go-flutter compatible plugins that
// aren't merged into original VSC repo.
type StandaloneImplementation struct {
Name string `json:"name"`
ImportFile string `json:"importFile"`
}
func fetchStandaloneImplementationList() ([]StandaloneImplementation, error) {
remoteList := &onlineList{}
client := http.Client{
Timeout: time.Second * 20, // Maximum of 10 secs
}
req, err := http.NewRequest(http.MethodGet, standaloneImplementationListAPI, nil)
if err != nil {
return remoteList.List, err
}
res, err := client.Do(req)
if err != nil {
return remoteList.List, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return remoteList.List, err
}
if res.StatusCode != 200 {
return remoteList.List, errors.New(strings.TrimRight(string(body), "\r\n"))
}
err = json.Unmarshal(body, remoteList)
if err != nil {
return remoteList.List, err
}
return remoteList.List, nil
}
// goGetModuleSuccess updates a module at a version, if it fails, return false.
func goGetModuleSuccess(pluginImportStr, version string) bool {
cmdGoGetU := exec.Command(build.GoBin(), "get", "-u", "-d", pluginImportStr+"@v"+version)
cmdGoGetU.Dir = filepath.Join(build.BuildPath)
cmdGoGetU.Env = append(os.Environ(),
"GOPROXY=direct", // github.com/golang/go/issues/32955 (allows '/' in branch name)
"GO111MODULE=on",
)
cmdGoGetU.Stderr = os.Stderr
cmdGoGetU.Stdout = os.Stdout
return cmdGoGetU.Run() == nil
}