This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
1089 lines (941 loc) · 24.2 KB
/
main.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
cli "github.com/codegangsta/cli"
homedir "github.com/mitchellh/go-homedir"
gx "github.com/whyrusleeping/gx/gxutil"
. "github.com/whyrusleeping/stump"
)
const updateProgressFile = "gx-workspace-update.json"
var cwd string
var pm *gx.PM
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
app := cli.NewApp()
app.Name = "gx-workspace"
app.Author = "lgierth"
app.Usage = "Tool for management of gx dependency trees"
app.Version = "0.0.0"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "turn on verbose output",
},
}
app.Before = func(c *cli.Context) error {
Verbose = c.Bool("verbose")
return nil
}
mcwd, err := os.Getwd()
if err != nil {
Fatal("failed to get cwd:", err)
}
lcwd, err := filepath.EvalSymlinks(mcwd)
if err != nil {
Fatal("failed to resolve symlinks of cdw:", err)
}
cwd = lcwd
app.Commands = []cli.Command{
BubbleListCommand,
UpdateCommand,
}
if err := app.Run(os.Args); err != nil {
Fatal(err)
}
}
var BubbleListCommand = cli.Command{
Name: "bubble-list",
Usage: "list all packages affected by an update of the named package",
Action: func(c *cli.Context) error {
var pkg gx.Package
err := gx.LoadPackageFile(&pkg, gx.PkgFileName)
if err != nil {
return err
}
if !c.Args().Present() {
return fmt.Errorf("must pass a package name")
}
touched, err := getTodoList(&pkg, c.Args())
if err != nil {
return err
}
for _, p := range touched {
fmt.Println(p)
}
return nil
},
}
func getTodoList(root *gx.Package, names []string) ([]string, error) {
var touched []string
// XXX This cache map might hide legitimate updates where
// at the first pass we might not yet know we need to update the package.
memo := make(map[string]bool)
var checkRec func(pkg *gx.Package) (bool, error)
checkRec = func(pkg *gx.Package) (bool, error) {
var needsUpd bool
err := pkg.ForEachDep(func(dep *gx.Dependency, pkg *gx.Package) error {
for _, name := range names {
if dep.Name == name {
needsUpd = true
break
}
}
val, ok := memo[dep.Hash]
if ok {
needsUpd = val || needsUpd
} else {
nu, err := checkRec(pkg)
if err != nil {
return err
}
memo[dep.Hash] = nu
needsUpd = nu || needsUpd
}
return nil
})
if err != nil {
return false, err
}
if needsUpd {
touched = append(touched, pkg.Name)
}
return needsUpd, nil
}
needs, err := checkRec(root)
if err != nil {
return nil, err
}
if !needs {
return nil, fmt.Errorf("named package not in dependency tree")
}
return touched, nil
}
var UpdateCommand = cli.Command{
Name: "update",
Usage: "manage updating a package throughout the dependency tree",
Subcommands: []cli.Command{
updateStartCmd,
updateNextCmd,
updatePushCmd,
updateUndoCmd,
updateRunCmd,
},
Before: func(c *cli.Context) error {
gxconf, err := gx.LoadConfig()
if err != nil {
return err
}
ourpm, err := gx.NewPM(gxconf)
if err != nil {
return err
}
pm = ourpm
return nil
},
}
type UpdateInfo struct {
Roots []string
Changes map[string]string
Todo []string
Current string
Done []string
Skipped []string
GoPath string
Branch string
PullRequests map[string]string
}
func EnumerateAllChildPackages(pkg *gx.Package) ([]string, error) {
out := make(map[string]struct{})
if err := enumerateAllChildPackages(pkg, out); err != nil {
return nil, err
}
var aggr []string
for k := range out {
aggr = append(aggr, k)
}
return aggr, nil
}
func enumerateAllChildPackages(pkg *gx.Package, pkgs map[string]struct{}) error {
return pkg.ForEachDep(func(dep *gx.Dependency, pkg *gx.Package) error {
// TODO: subtle bug here where we could miss some packages if they appear as
// dependencies in one version of a package, but not in another version with the
// same name. probably can worry about this later with a 'normalize' command
if _, ok := pkgs[dep.Name]; ok {
return nil
}
pkgs[dep.Name] = struct{}{}
return enumerateAllChildPackages(pkg, pkgs)
})
}
var updateStartCmd = cli.Command{
Name: "start",
Usage: "begin an update of packages throughout the tree",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "temp-gopath",
},
cli.BoolFlag{
Name: "all",
},
cli.BoolFlag{
Name: "skip-failed-clones",
},
},
Action: func(c *cli.Context) error {
var pkg gx.Package
err := gx.LoadPackageFile(&pkg, gx.PkgFileName)
if err != nil {
return err
}
if len(c.Args()) == 0 && !c.Bool("all") {
return fmt.Errorf("must pass at least one package name")
}
names := c.Args()
if c.Bool("all") {
allpkgs, err := EnumerateAllChildPackages(&pkg)
if err != nil {
return err
}
names = allpkgs
}
if _, err := os.Stat(updateProgressFile); err == nil {
return fmt.Errorf("update already in progress")
}
var ui UpdateInfo
var gopath string
updatename := randomString(6)
if c.Bool("temp-gopath") {
randgp, err := homedir.Expand(filepath.Join("~", ".gx", "update-"+updatename))
if err != nil {
return err
}
gopath = randgp
} else {
gopath = os.Getenv("GOPATH")
}
ui.GoPath = gopath
ui.Branch = "gx/update-" + updatename
err = os.Setenv("GOPATH", ui.GoPath)
if err != nil {
return err
}
err = os.Setenv("GOBIN", filepath.Join(ui.GoPath, "bin"))
if err != nil {
return err
}
fmt.Printf("> Working in GOPATH=%s\n", ui.GoPath)
fmt.Println("> Running 'gx install'")
gxinst := exec.Command("gx", "install")
gxinst.Dir = cwd
gxinst.Stdout = os.Stdout
gxinst.Stderr = os.Stderr
if err = gxinst.Run(); err != nil {
return fmt.Errorf("error installing gx deps: %s", err)
}
ui.Changes = map[string]string{}
ui.Done = []string{}
ui.Skipped = []string{}
for _, name := range names {
skip, err := syncRepo(c, pkg, &ui, name)
if err != nil {
return err
}
if !skip {
ui.Roots = append(ui.Roots, name)
}
}
touched, err := getTodoList(&pkg, ui.Roots)
if err != nil {
return fmt.Errorf("getTodoList failed: %s", err)
}
ui.Todo = touched
fmt.Printf("> Will change %d packages: %s\n", len(ui.Todo), strings.Join(ui.Todo, ", "))
fmt.Printf("> Run `gx-workspace update next` to continue.\n")
return writeUpdateProgress(&ui)
},
}
func syncRepo(c *cli.Context, parentpkg gx.Package, ui *UpdateInfo, name string) (bool, error) {
pkg, err := LoadDepByName(parentpkg, name)
if err != nil {
return false, err
}
dir, err := PkgDir(pkg)
if err != nil {
return false, err
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := gitClone(GxDvcsImport(pkg), dir); err != nil {
finalErr := fmt.Errorf("error cloning: %s", err)
if c.Bool("skip-failed-clones") {
fmt.Printf("WARNING: %n", finalErr)
return true, nil
} else {
return false, finalErr
}
}
} else {
if err := gitPull(dir); err != nil {
return false, fmt.Errorf("error pulling latest: %s", err)
}
}
p := filepath.Join(dir, ".gx", "lastpubver")
data, err := ioutil.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("WARNING: skipping non-gx package %q\n", name)
return true, nil
}
return false, err
}
pubver := strings.Fields(string(data))
if len(pubver) != 2 {
return false, fmt.Errorf("error parsing hash from %s", p)
}
ui.Changes[name] = pubver[1]
ipath, err := gx.InstallPath(pkg.Language, "", true)
if err != nil {
return false, err
}
fmt.Printf("> Running InstallPackage(%s)\n", ui.Changes[name])
_, err = pm.InstallPackage(ui.Changes[name], ipath)
if err != nil {
return false, err
}
return false, nil
}
const letterBytes = "abcdefghijklmnopqrstuvwxyz1234567890"
func randomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func readUpdateProgress() (*UpdateInfo, error) {
fi, err := os.Open("gx-workspace-update.json")
if err != nil {
return nil, err
}
defer fi.Close()
var ui UpdateInfo
err = json.NewDecoder(fi).Decode(&ui)
if err != nil {
return nil, err
}
return &ui, nil
}
func writeUpdateProgress(ui *UpdateInfo) error {
fi, err := os.Create("gx-workspace-update.json")
if err != nil {
return err
}
defer fi.Close()
data, err := json.MarshalIndent(ui, "", " ")
if err != nil {
return err
}
_, err = fi.Write(data)
return err
}
var updateNextCmd = cli.Command{
Name: "next",
Usage: "execute the next step in the update process",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "no-test",
Usage: "skip testing phase",
},
},
Action: updateNext,
}
func updateStepTwo(ui *UpdateInfo) error {
var current gx.Package
err := gx.LoadPackageFile(¤t, filepath.Join(ui.Current, gx.PkgFileName))
if err != nil {
return err
}
changed := true
for _, name := range ui.Skipped {
if name == current.Name {
changed = false
}
}
// We don't want to publish the root package.
if changed && len(ui.Todo) > 0 {
name, hash, err := publishAndRelease(ui.Current, ui.Branch)
if err != nil {
return err
}
ui.Changes[name] = hash
fmt.Printf("> Published package %s @ %s\n", ui.Current, hash)
fmt.Printf("> For pinning: curl -X POST -F \"ghurl=%s\" http://mars.i.ipfs.team:9444/pin_package\n", GxDvcsImport(¤t))
} else if changed {
err = gitCheckout(ui.Current, ui.Branch)
if err != nil {
return err
}
fmt.Printf("> Running 'git add package.json' in %s\n", ui.Current)
add := exec.Command("git", "add", "package.json")
add.Dir = ui.Current
add.Stdout = os.Stdout
add.Stderr = os.Stderr
if err = add.Run(); err != nil {
return fmt.Errorf("error during git add: %s", err)
}
fmt.Printf("> Running 'git commit' in %s\n", ui.Current)
msg := "gx: update " + strings.Join(ui.Roots, ", ")
commitcmd := exec.Command("git", "commit", "-m", msg)
commitcmd.Dir = ui.Current
commitcmd.Stdout = os.Stdout
commitcmd.Stderr = os.Stderr
if err = commitcmd.Run(); err != nil {
return fmt.Errorf("error during git commit: %s", err)
}
} else {
dir, err := PkgDir(¤t)
if err != nil {
return err
}
data, err := ioutil.ReadFile(filepath.Join(dir, ".gx", "lastpubver"))
if err != nil {
return err
}
ui.Changes[current.Name] = strings.Fields(string(data))[1]
fmt.Printf("> Skipping %s, it wasn't changed.\n", ui.Current)
}
done := len(ui.Done) + len(ui.Skipped)
total := done + len(ui.Todo)
if len(ui.Todo) > 0 {
fmt.Printf("> Progress: %d of %d packages, next: %s\n", done, total, ui.Todo[0])
fmt.Printf("> Run `gx-workspace update next` to continue.\n")
} else {
fmt.Printf("> Progress: %d of %d packages, finished.\n", done, total)
fmt.Printf("> You can now safely remove gx-workspace-update.json.\n")
}
ui.Current = ""
return nil
}
func updateStepOne(ui *UpdateInfo, notest bool) error {
if len(ui.Todo) == 0 {
fmt.Printf("> We're done here.\n")
fmt.Printf("> You can now safely remove gx-workspace-update.json.\n")
return nil
}
fmt.Printf("updating package %s\n", ui.Todo[0])
var pkg gx.Package
if err := gx.LoadPackageFile(&pkg, gx.PkgFileName); err != nil {
return err
}
var dir string
if ui.Todo[0] == pkg.Name {
wd, err := os.Getwd()
if err != nil {
return err
}
dir, err = PkgDir(&pkg)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(dir), 0755)
if err != nil {
return err
}
fmt.Printf("> Running Symlink(%s, %s)\n", wd, dir)
err = os.Symlink(wd, dir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
} else {
dep, err := LoadDepByName(pkg, ui.Todo[0])
if err != nil {
return err
}
dir, err = PkgDir(dep)
if err != nil {
return err
}
if _, err := os.Stat(dir); err != nil {
if !os.IsNotExist(err) {
return err
}
err := gitClone(GxDvcsImport(dep), dir)
if err != nil {
return fmt.Errorf("error cloning: %s", err)
}
} else {
if err := gitPull(dir); err != nil {
return err
}
}
}
changed, err := updatePackage(dir, ui.Changes)
if err != nil {
return err
}
if changed {
err = checkPackage(dir, notest)
if err != nil {
return err
}
ui.Done = append(ui.Done, ui.Todo[0])
fmt.Printf("> Changed %s at %s\n", ui.Todo[0], dir)
fmt.Printf("> Please verify before the change gets published and released.\n")
} else {
ui.Skipped = append(ui.Skipped, ui.Todo[0])
fmt.Printf("> Going to skip %s, it doesn't need to be changed.\n", ui.Todo[0])
}
fmt.Printf("> Run `gx-workspace update next` to continue.\n")
ui.Todo = ui.Todo[1:]
ui.Current = dir
return nil
}
func updateNext(c *cli.Context) error {
ui, err := readUpdateProgress()
if err != nil {
return err
}
err = os.Setenv("GOPATH", ui.GoPath)
if err != nil {
return err
}
err = os.Setenv("GOBIN", filepath.Join(ui.GoPath, "bin"))
if err != nil {
return err
}
fmt.Printf("> Working in GOPATH=%s\n", ui.GoPath)
if ui.Current == "" {
if err := updateStepOne(ui, c.Bool("no-test")); err != nil {
return err
}
} else {
if err := updateStepTwo(ui); err != nil {
return err
}
}
if err := writeUpdateProgress(ui); err != nil {
return err
}
return nil
}
var updateUndoCmd = cli.Command{
Name: "undo",
Usage: "put the last 'done' item back on the todo list",
Action: func(c *cli.Context) error {
ui, err := readUpdateProgress()
if err != nil {
return err
}
if len(ui.Done) == 0 {
fmt.Println("nothing to undo")
return nil
}
ui.Todo = append([]string{ui.Done[len(ui.Done)-1]}, ui.Todo...)
ui.Done = ui.Done[:len(ui.Done)-1]
ui.Current = ""
if err := writeUpdateProgress(ui); err != nil {
return err
}
return nil
},
}
func hasChangesSincePublish(dir string) (bool, error) {
return false, nil
}
func gitClone(url string, dir string) error {
pdir := filepath.Dir(dir)
err := os.MkdirAll(pdir, 0775)
if err != nil {
return err
}
if strings.HasPrefix(url, "github.com/") {
url = "git@github.com:" + strings.TrimPrefix(url, "github.com/")
} else {
url = "https://" + url
}
fmt.Printf("> Running 'git clone %s %s'\n", url, dir)
clonecmd := exec.Command("git", "clone", url, dir)
clonecmd.Stdout = os.Stdout
clonecmd.Stderr = os.Stderr
if err := clonecmd.Run(); err != nil {
return fmt.Errorf("error during git clone: %s", err)
}
return nil
}
func gitPull(dir string) error {
fmt.Printf("> Running 'git pull origin master' in %s\n", dir)
pullcmd := exec.Command("git", "pull", "origin", "master")
pullcmd.Dir = dir
pullcmd.Stdout = os.Stdout
pullcmd.Stderr = os.Stderr
if err := pullcmd.Run(); err != nil {
return fmt.Errorf("error during git clone: %s", err)
}
return nil
}
func gitCheckout(dir string, branch string) error {
fmt.Printf("> Running 'git checkout -B %s'\n", branch)
cocmd := exec.Command("git", "checkout", "-B", branch)
cocmd.Dir = dir
cocmd.Stdout = os.Stdout
cocmd.Stderr = os.Stderr
if err := cocmd.Run(); err != nil {
return fmt.Errorf("error during git checkout: %s", err)
}
return nil
}
func publishAndRelease(dir string, branch string) (string, string, error) {
fmt.Printf("> Running 'gx-go uw'\n")
uwcmd := exec.Command("gx-go", "uw")
uwcmd.Stdout = os.Stdout
uwcmd.Stderr = os.Stderr
uwcmd.Dir = dir
if err := uwcmd.Run(); err != nil {
return "", "", fmt.Errorf("error undoing dependency rewrite pre-publish: %s", err)
}
pfpath := filepath.Join(dir, gx.PkgFileName)
var pkg gx.Package
err := gx.LoadPackageFile(&pkg, pfpath)
if err != nil {
return "", "", err
}
if pkg.ReleaseCmd == "" {
return "", "", fmt.Errorf("%s at %s does not have releaseCmd set", pkg.Name, pfpath)
}
err = gitCheckout(dir, branch)
if err != nil {
return "", "", fmt.Errorf("error during git checkout: %s", err)
}
fmt.Printf("> Running 'gx release patch'\n")
cmd := exec.Command("gx", "release", "patch")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err = cmd.Run()
if err != nil {
return "", "", err
}
data, err := ioutil.ReadFile(filepath.Join(dir, ".gx", "lastpubver"))
if err != nil {
return "", "", err
}
nhash := strings.Fields(string(data))[1]
ipath, err := gx.InstallPath(pkg.Language, "", true)
if err != nil {
return "", "", err
}
fmt.Printf("> Running InstallPackage(%s)\n", nhash)
_, err = pm.InstallPackage(nhash, ipath)
if err != nil {
return "", "", err
}
return pkg.Name, nhash, nil
}
func updatePackage(dir string, changes map[string]string) (bool, error) {
fmt.Printf("> Working in CWD=%s\n", dir)
pfpath := filepath.Join(dir, gx.PkgFileName)
var pkg gx.Package
err := gx.LoadPackageFile(&pkg, pfpath)
if err != nil {
return false, err
}
fmt.Println("> Running 'gx install'")
gxinst := exec.Command("gx", "install")
gxinst.Dir = dir
gxinst.Stdout = os.Stdout
gxinst.Stderr = os.Stderr
if err := gxinst.Run(); err != nil {
return false, fmt.Errorf("error installing gx deps: %s", err)
}
ipath, err := gx.InstallPath(pkg.Language, "", true)
if err != nil {
return false, err
}
var changed bool
for _, dep := range pkg.Dependencies {
val, ok := changes[dep.Name]
if !ok || val == dep.Hash {
continue
}
chpkg, err := pm.InstallPackage(val, ipath)
if err != nil {
return false, err
}
dep.Version = chpkg.Version
dep.Hash = val
changed = true
}
if !changed {
return false, nil
}
fmt.Printf("> Running SavePackageFile(%s) with updated dependencies.\n", pfpath)
err = gx.SavePackageFile(&pkg, pfpath)
if err != nil {
return false, err
}
fmt.Println("> Running 'gx install'")
gxinst2 := exec.Command("gx", "install")
gxinst2.Dir = dir
gxinst2.Stdout = os.Stdout
gxinst2.Stderr = os.Stderr
if err := gxinst2.Run(); err != nil {
return false, fmt.Errorf("error installing gx deps: %s", err)
}
return true, nil
}
func checkPackage(dir string, notest bool) error {
fmt.Println("> Running 'gx deps dupes'")
dupecmd := exec.Command("gx", "deps", "dupes")
dupecmd.Dir = dir
out, err := dupecmd.Output()
if err != nil {
return fmt.Errorf("error checking dupes: %s", err)
}
lines := bytes.Split(out, []byte("\n"))
if len(lines) > 0 && len(lines[0]) > 0 {
fmt.Println("!! Package has duplicate dependencies after updating: ")
for _, l := range lines {
fmt.Println(string(l))
}
}
if err := checkForMissingDeps(dir); err != nil {
return err
}
if notest {
fmt.Println("> Skipping gx tests")
} else {
fmt.Println("> Running 'go get -d -t ./...'")
gogetd := exec.Command("go", "get", "-d", "-t", "./...")
gogetd.Dir = dir
gogetd.Stdout = os.Stdout
gogetd.Stderr = os.Stderr
if err := gogetd.Run(); err != nil {
return fmt.Errorf("error installing go deps: %s", err)
}
fmt.Println("> Running 'gx test ./...'")
gxtest := exec.Command("gx", "test", "./...")
gxtest.Dir = dir
gxtest.Stdout = os.Stdout
gxtest.Stderr = os.Stderr
if err := gxtest.Run(); err != nil {
return fmt.Errorf("error running tests: %s", err)
}
}
return nil
}
func checkForMissingDeps(dir string) error {
fmt.Printf("> Running 'gx-go rw' in %s\n", dir)
rwcmd := exec.Command("gx-go", "rw")
rwcmd.Dir = dir
rwcmd.Stdout = os.Stdout
rwcmd.Stderr = os.Stderr
if err := rwcmd.Run(); err != nil {
return fmt.Errorf("error rewriting deps: %s", err)
}
fmt.Printf("> Running 'gx-go dvcs-deps' in %s\n", dir)
ddcmd := exec.Command("gx-go", "dvcs-deps")
ddcmd.Dir = dir
out, err := ddcmd.Output()
if err != nil {
Log(out)
return fmt.Errorf("error while checking for missing deps: %s", err)
}
lines := bytes.Split(out, []byte("\n"))
if len(lines) > 0 && len(lines[0]) > 0 {
fmt.Println("!! Package appears to have missing dependencies:")
for _, l := range lines {
fmt.Println(string(l))
}
}
return nil
}
func checkBranch(dir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("error checking branch: %s", err)
}
clean := bytes.Trim(out, " \t\n")
return string(clean), nil
}
func LoadDepByName(pkg gx.Package, name string) (*gx.Package, error) {
if pkg.Name == name {
return &pkg, nil
}
deps, err := pm.EnumerateDependencies(&pkg)
if err != nil {
return nil, err
}
for k, v := range deps {
if v == name {
var dep gx.Package
err = gx.LoadPackage(&dep, pkg.Language, k)
if err != nil {
return nil, err
}
return &dep, nil
}
}
return nil, fmt.Errorf("dependency %s not found", name)
}
func GxDvcsImport(pkg *gx.Package) string {
pkggx := make(map[string]interface{})
_ = json.Unmarshal(pkg.Gx, &pkggx)
return pkggx["dvcsimport"].(string)
}
func PkgDir(pkg *gx.Package) (string, error) {
dir, err := gx.InstallPath(pkg.Language, "", true)
if err != nil {
return "", err
}
return filepath.Join(dir, GxDvcsImport(pkg)), nil
}
func gitRemotes(dir string) ([]string, error) {
buf := new(bytes.Buffer)
remotescmd := exec.Command("git", "remote")
remotescmd.Dir = dir
remotescmd.Stdout = buf
remotescmd.Stderr = os.Stderr
if err := remotescmd.Run(); err != nil {
return nil, fmt.Errorf("error running git remote: %s", err)
}
lines := strings.Split(buf.String(), "\n")
return lines[:len(lines)-1], nil
}
func gitPush(remote string, branch string, dir string) error {
fmt.Printf("> Running 'git push %s %s' in %s\n", remote, branch, dir)
pushcmd := exec.Command("git", "push", "--set-upstream", remote, branch)
pushcmd.Dir = dir
pushcmd.Stdout = os.Stdout
pushcmd.Stderr = os.Stderr
return pushcmd.Run()
}
var updateRunCmd = cli.Command{
Name: "run",
Usage: "run update, looping with user input",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "no-prompt",
},
},
Action: func(c *cli.Context) error {
for {
if err := updateNext(c); err != nil {
return err
}
if !c.Bool("no-prompt") {
fmt.Println("Press enter to continue...")
fmt.Scanln()
}
}
},
}
var updatePushCmd = cli.Command{
Name: "push",
Usage: "push branches of updated packages, and open pull requests",
Action: func(c *cli.Context) error {
ui, err := readUpdateProgress()
if err != nil {
return err
}
if ui.Current != "" || len(ui.Todo) > 1 {
return fmt.Errorf("update not yet finished")
}
err = os.Setenv("GOPATH", ui.GoPath)
if err != nil {
return err
}
err = os.Setenv("GOBIN", filepath.Join(ui.GoPath, "bin"))
if err != nil {
return err
}
fmt.Printf("> Working in GOPATH=%s\n", ui.GoPath)
var pkg gx.Package
err = gx.LoadPackageFile(&pkg, gx.PkgFileName)
if err != nil {