-
Notifications
You must be signed in to change notification settings - Fork 110
/
main.go
1468 lines (1249 loc) · 30.8 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"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"text/tabwriter"
"github.com/blang/semver"
cli "github.com/urfave/cli/v2"
gx "github.com/whyrusleeping/gx/gxutil"
filter "github.com/whyrusleeping/json-filter"
progmeter "github.com/whyrusleeping/progmeter"
log "github.com/whyrusleeping/stump"
)
var (
cwd string
pm *gx.PM
)
const PkgFileName = gx.PkgFileName
func LoadPackageFile(path string) (*gx.Package, error) {
if path == PkgFileName {
root, err := gx.GetPackageRoot()
if err != nil {
return nil, err
}
path = filepath.Join(root, PkgFileName)
}
var pkg gx.Package
err := gx.LoadPackageFile(&pkg, path)
if err != nil {
return nil, err
}
if pkg.GxVersion == "" {
pkg.GxVersion = gx.GxVersion
}
if pkg.SubtoolRequired {
found, err := gx.IsSubtoolInstalled(pkg.Language)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("package requires a subtool (gx-%s) and none was found", pkg.Language)
}
}
dephashes := make(map[string]string)
for _, dep := range pkg.Dependencies {
if pkgname := dephashes[dep.Hash]; pkgname != "" {
return nil, fmt.Errorf("have two packages with a hash of %s (%s, %s)", dep.Hash, dep.Name, pkgname)
}
dephashes[dep.Hash] = dep.Name
}
return &pkg, nil
}
func main() {
cfg, err := gx.LoadConfig()
if err != nil {
log.Fatal(err)
}
pm, err = gx.NewPM(cfg)
if err != nil {
log.Fatal(err)
}
app := cli.NewApp()
app.Authors = []*cli.Author{
&cli.Author{
Name: "whyrusleeping",
},
}
app.Version = gx.GxVersion
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Usage: "print verbose logging information",
},
}
app.Before = func(c *cli.Context) error {
log.Verbose = c.Bool("verbose")
gcwd, err := os.Getwd()
if err != nil {
return err
}
cwd = gcwd
return nil
}
app.Usage = "gx is a packaging tool that uses ipfs"
app.Commands = []*cli.Command{
&CleanCommand,
&DepsCommand,
&GetCommand,
&ImportCommand,
&DiffCommand,
&InitCommand,
&InstallCommand,
&LockInstallCommand,
&PublishCommand,
&ReleaseCommand,
&RepoCommand,
&UpdateCommand,
&VersionCommand,
&ViewCommand,
&SetCommand,
&TestCommand,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func checkLastPubVer() string {
out, err := ioutil.ReadFile(filepath.Join(cwd, ".gx", "lastpubver"))
if err != nil {
return ""
}
parts := bytes.Split(out, []byte{':'})
return string(parts[0])
}
var PublishCommand = cli.Command{
Name: "publish",
Usage: "publish a package",
Description: `publish a package into ipfs using a locally running daemon.
'publish' bundles up all files associated with the package (respecting
.gitignore and .gxignore files), adds them to ipfs, and writes out the
resulting package hash.
By default, you cannot publish a package without updating the version
number. This is a soft requirement and can be skipped by specifying the
-f or --force flag.
`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "allow publishing without bumping version",
},
},
Action: func(c *cli.Context) error {
if gx.UsingGateway {
log.Log("gx cannot publish using public gateways.")
log.Log("please run an ipfs node and try again.")
return nil
}
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
if !c.Bool("force") {
if pkg.Version == checkLastPubVer() {
log.Fatal("please update your packages version before publishing. (use -f to skip)")
}
}
_, err = doPublish(pkg)
return err
},
}
func doPublish(pkg *gx.Package) (string, error) {
if !pm.ShellOnline() {
return "", fmt.Errorf("ipfs daemon isn't running")
}
err := gx.TryRunHook("pre-publish", pkg.Language, pkg.SubtoolRequired)
if err != nil {
return "", err
}
hash, err := pm.PublishPackage(cwd, &pkg.PackageBase)
if err != nil {
return hash, fmt.Errorf("publishing: %s", err)
}
log.Log("package %s published with hash: %s", pkg.Name, hash)
// write out version hash
err = writeLastPub(pkg.Version, hash)
if err != nil {
return hash, err
}
err = gx.TryRunHook("post-publish", pkg.Language, pkg.SubtoolRequired, hash)
return hash, err
}
func writeLastPub(vers string, hash string) error {
err := os.MkdirAll(".gx", 0755)
if err != nil {
return err
}
fi, err := os.Create(".gx/lastpubver")
if err != nil {
return fmt.Errorf("failed to create version file: %s", err)
}
defer fi.Close()
log.VLog("writing published version to .gx/lastpubver")
_, err = fmt.Fprintf(fi, "%s: %s\n", vers, hash)
if err != nil {
return fmt.Errorf("failed to write version file: %s", err)
}
return nil
}
var ImportCommand = cli.Command{
Name: "import",
Usage: "import a package as a dependency",
Description: `Download packages and add them as a dependency in package.json.
EXAMPLE
> gx import QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH
> gx import github.com/libp2p/go-libp2p
In the last example, Gx will check the ".gx/lastpubver"
file in the repository to find which hash to import.
`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Value: true,
Usage: "download imported package to global store",
},
&cli.BoolFlag{
Name: "local",
Usage: "install packages locally (equal to --global=false)",
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("import requires a package reference")
}
global := c.Bool("global")
if c.Bool("local") {
global = false
}
pm.SetGlobal(global)
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
depname := c.Args().First()
cdep := pkg.FindDep(depname)
if cdep != nil {
return fmt.Errorf("package %s already imported as %s", cdep.Hash, cdep.Name)
}
dephash, err := pm.ResolveDepName(depname)
if err != nil {
return err
}
ipath, err := gx.InstallPath(pkg.Language, "", global)
if err != nil {
return err
}
npkg, err := pm.InstallPackage(dephash, ipath)
if err != nil {
return fmt.Errorf("(install): %s", err)
}
pm.ProgMeter.Stop()
if pkg.FindDep(npkg.Name) != nil {
s := fmt.Sprintf("package with name %s already imported, continue?", npkg.Name)
if !yesNoPrompt(s, false) {
return nil
}
log.Log("continuing, please note some things may not work as expected")
}
ndep := &gx.Dependency{
Author: npkg.Author,
Hash: dephash,
Name: npkg.Name,
Version: npkg.Version,
}
pkg.Dependencies = append(pkg.Dependencies, ndep)
err = gx.SavePackageFile(pkg, PkgFileName)
if err != nil {
return fmt.Errorf("writing pkgfile: %s", err)
}
err = gx.TryRunHook("post-import", npkg.Language, npkg.SubtoolRequired, dephash)
if err != nil {
return fmt.Errorf("running post-import: %s", err)
}
return nil
},
}
var InstallCommand = cli.Command{
Name: "install",
Usage: "install this package",
Aliases: []string{"i"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Value: true,
Usage: "install package in global namespace",
},
&cli.BoolFlag{
Name: "local",
Usage: "install packages locally (equal to --global=false)",
},
&cli.BoolFlag{
Name: "save",
Usage: "write installed packages as deps in package.json",
},
&cli.BoolFlag{
Name: "nofancy",
Usage: "write minimal output",
},
},
Action: func(c *cli.Context) error {
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
save := c.Bool("save")
global := c.Bool("global")
if c.Bool("local") {
global = false
}
pm.SetGlobal(global)
pm.ProgMeter = progmeter.NewProgMeter(c.Bool("nofancy"))
if c.NArg() == 0 {
cwd, err := os.Getwd()
if err != nil {
return err
}
err = gx.TryRunHook("req-check", pkg.Language, pkg.SubtoolRequired, cwd)
if err != nil {
return err
}
ipath, err := gx.InstallPath(pkg.Language, cwd, global)
if err != nil {
return err
}
err = pm.InstallDeps(pkg, ipath)
if err != nil {
return fmt.Errorf("install deps: %s", err)
}
return nil
}
ipath, err := gx.InstallPath(pkg.Language, "", global)
if err != nil {
return err
}
for _, p := range c.Args().Slice() {
phash, err := pm.ResolveDepName(p)
if err != nil {
return fmt.Errorf("resolving package '%s': %s", p, err)
}
if p != phash {
log.VLog("%s resolved to %s", p, phash)
}
ndep, err := pm.ImportPackage(ipath, p)
if err != nil {
return fmt.Errorf("importing package '%s': %s", p, err)
}
if save {
foundPackage := false
for _, d := range pkg.Dependencies {
if d.Hash == ndep.Hash {
foundPackage = true
}
}
if !foundPackage {
pkg.Dependencies = append(pkg.Dependencies, ndep)
}
}
}
if save {
err := gx.SavePackageFile(pkg, PkgFileName)
if err != nil {
return err
}
}
return nil
},
}
var GetCommand = cli.Command{
Name: "get",
Usage: "download a package",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "o",
Usage: "specify output dir name",
},
},
Action: func(c *cli.Context) error {
if !c.Args().Present() {
return fmt.Errorf("no package specified")
}
pkg, err := pm.ResolveDepName(c.Args().First())
if err != nil {
return err
}
out := c.String("o")
if out == "" {
out = filepath.Join(cwd, pkg)
}
log.Log("writing package to:", out)
_, err = pm.GetPackageTo(pkg, out)
if err != nil {
return fmt.Errorf("fetching package: %s", err)
}
return nil
},
}
var InitCommand = cli.Command{
Name: "init",
Usage: "initialize a package in the current working directory",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lang",
Usage: "specify primary language of new package",
},
},
Action: func(c *cli.Context) error {
var pkgname string
if c.NArg() > 0 {
pkgname = c.Args().First()
} else {
pkgname = filepath.Base(cwd)
}
lang := c.String("lang")
if !c.IsSet("lang") {
lang = promptUser("what language will the project be in?")
}
log.Log("initializing package %s...", pkgname)
err := pm.InitPkg(cwd, pkgname, lang, func(p *gx.Package) {
p.Bugs.Url = promptUser("where should users go to report issues?")
})
if err != nil {
return fmt.Errorf("init error: %s", err)
}
return nil
},
}
var UpdateCommand = cli.Command{
Name: "update",
Usage: "update a package dependency",
ArgsUsage: "[oldref] [newref]",
Description: `Update a package to a specified ref.
EXAMPLE:
Update 'myPkg' to a given version (referencing it by package name):
$ gx update myPkg QmPZ6gM12JxshKzwSyrhbEmyrsi7UaMrnoQZL6mdrzSfh1
or reference it by hash:
$ export OLDHASH=QmdTTcAwxWhHLruoZtowxuqua1e5GVkYzxziiYPDn4vWJb
$ export NEWHASH=QmPZ6gM12JxshKzwSyrhbEmyrsi7UaMrnoQZL6mdrzSfh1
$ gx update $OLDHASH $NEWHASH
`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Value: true,
Usage: "install new package in global namespace",
},
&cli.BoolFlag{
Name: "local",
Usage: "install packages locally (equal to --global=false)",
},
&cli.BoolFlag{
Name: "with-deps",
Usage: "experimental feature to recursively update child deps too",
},
},
Action: func(c *cli.Context) error {
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
log.Fatal("error: ", err)
}
var existing, target string
switch c.NArg() {
case 0:
log.Fatal("update requires two arguments, current and target")
case 1:
target = c.Args().Get(0)
case 2:
existing = c.Args().Get(0)
target = c.Args().Get(1)
default:
log.Log("ignoring extra arguments: %s", c.Args().Slice()[2:])
}
trgthash, err := pm.ResolveDepName(target)
if err != nil {
return err
}
global := c.Bool("global")
if c.Bool("local") {
global = false
}
ipath, err := gx.InstallPath(pkg.Language, cwd, global)
if err != nil {
return err
}
npkg, err := pm.InstallPackage(trgthash, ipath)
if err != nil {
log.Fatal("(installpackage) : ", err)
}
if existing == "" {
existing = npkg.Name
}
var oldhash string
olddep := pkg.FindDep(existing)
if olddep == nil {
log.Fatal("unknown package: ", existing)
}
oldhash = olddep.Hash
log.Log("updating %s to version %s (%s)", olddep.Name, npkg.Version, trgthash)
if npkg.Name != olddep.Name {
prompt := fmt.Sprintf(`Target package has a different name than new package:
old: %s (%s)
new: %s (%s)
continue?`, olddep.Name, olddep.Hash, npkg.Name, trgthash)
if !yesNoPrompt(prompt, false) {
log.Fatal("refusing to update package with different names")
}
}
log.VLog("running pre update hook...")
err = gx.TryRunHook("pre-update", pkg.Language, pkg.SubtoolRequired, existing)
if err != nil {
return err
}
if c.Bool("with-deps") {
err := RecursiveDepUpdate(pkg, oldhash, trgthash)
if err != nil {
return err
}
} else {
log.VLog("checking for potential package naming collisions...")
err = updateCollisionCheck(pkg, olddep, trgthash, nil, make(map[string]struct{}))
if err != nil {
log.Fatal("update sanity check: ", err)
}
log.VLog(" - no collisions found for updated package")
}
olddep.Hash = trgthash
olddep.Version = npkg.Version
err = gx.SavePackageFile(pkg, PkgFileName)
if err != nil {
return fmt.Errorf("writing package file: %s", err)
}
log.VLog("running post update hook...")
err = gx.TryRunHook("post-update", pkg.Language, pkg.SubtoolRequired, oldhash, trgthash)
if err != nil {
return err
}
log.VLog("update complete!")
return nil
},
}
func updateCollisionCheck(ipkg *gx.Package, idep *gx.Dependency, trgt string, chain []string, skip map[string]struct{}) error {
return ipkg.ForEachDep(func(dep *gx.Dependency, pkg *gx.Package) error {
if _, ok := skip[dep.Hash]; ok {
return nil
}
if dep == idep {
return nil
}
skip[dep.Hash] = struct{}{}
if (dep.Name == idep.Name && dep.Hash != trgt) || (dep.Hash == idep.Hash && dep.Name != idep.Name) {
log.Log("dep %s also imports %s (%s)", strings.Join(chain, "/"), dep.Name, dep.Hash)
return nil
}
return updateCollisionCheck(pkg, idep, trgt, append(chain, dep.Name), skip)
})
}
var VersionCommand = cli.Command{
Name: "version",
Usage: "view or modify this package's version",
Description: `view or modify this package's version
run without any arguments, will print the current semver of this package.
if an argument is given, it will be parsed as a semver; if that succeeds,
the version will be set to that exactly. If the argument is not a semver,
it should be one of three things: "major", "minor", or "patch". Passing
any of those three will bump the corresponding segment of the semver up
by one.
EXAMPLE:
> gx version
0.4.0
> gx version patch
updated version to 0.4.1
> gx version major
updated version to 1.0.0
> gx version 2.5.7
updated version to 2.5.7
`,
Action: func(c *cli.Context) (outerr error) {
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
if !c.Args().Present() {
fmt.Println(pkg.Version)
return
}
return updateVersion(pkg, c.Args().First())
},
}
func updateVersion(pkg *gx.Package, nver string) (outerr error) {
if nver == "" {
return fmt.Errorf("must specify version with non-zero length")
}
defer func() {
err := gx.SavePackageFile(pkg, PkgFileName)
if err != nil {
outerr = err
}
}()
// if argument is a semver, set version to it
_, err := semver.Make(nver)
if err == nil {
pkg.Version = nver
return nil
}
v, err := semver.Make(pkg.Version)
if err != nil {
return err
}
switch nver {
case "major":
v.Major++
v.Minor = 0
v.Patch = 0
v.Pre = nil // reset prerelase info
case "minor":
v.Minor++
v.Patch = 0
v.Pre = nil
case "patch":
v.Patch++
v.Pre = nil
default:
if nver[0] == 'v' {
nver = nver[1:]
}
newver, err := semver.Make(nver)
if err != nil {
log.Error(err)
return
}
v = newver
}
log.Log("updated version to: %s", v)
pkg.Version = v.String()
return nil
}
var ViewCommand = cli.Command{
Name: "view",
Usage: "view package information",
Description: `view can be used to print out information in the package.json
of this package, or a dependency specified either by name or hash.
EXAMPLE:
> gx view language
go
> gx view .
{
"language": "go",
"name": "gx",
"version": "0.2.0
}
> gx view go-libp2p gx.dvcsimport
"github.com/ipfs/go-libp2p"
> gx view '.gxDependencies[0].name'
go-multihash
> gx view '.gxDependencies[.name=go-multiaddr].hash'
QmWLfU4tstw2aNcTykDm44xbSTCYJ9pUJwfhQCKGwckcHx
`,
Action: func(c *cli.Context) error {
if !c.Args().Present() {
log.Fatal("must specify at least a query")
}
var cfg map[string]interface{}
if c.NArg() == 2 {
pkg, err := LoadPackageFile(gx.PkgFileName)
if err != nil {
return err
}
ref := c.Args().Get(0)
dep := pkg.FindDep(ref)
if dep == nil {
return fmt.Errorf("no dep referenced by %s", ref)
}
err = gx.LoadPackage(&cfg, pkg.Language, dep.Hash)
if err != nil {
return err
}
} else {
root, err := gx.GetPackageRoot()
if err != nil {
return err
}
err = gx.LoadPackageFile(&cfg, filepath.Join(root, PkgFileName))
if err != nil {
return err
}
}
queryStr := c.Args().Get(c.NArg() - 1)
val, err := filter.Get(cfg, queryStr)
if err != nil {
return err
}
jsonPrint(val)
return nil
},
}
var depDotCommand = cli.Command{
Name: "dot",
Usage: "generate dot graph of the package tree",
Action: func(c *cli.Context) error {
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
dt, err := genDepsTree(pm, pkg)
if err != nil {
log.Fatal(err)
}
toProcess := make([]*depTreeNode, 0, 32)
rank := make(map[string]int)
name := func(d *gx.Dependency) string {
hash := d.Hash
if len(hash) > 6 {
hash = hash[0:6]
}
return fmt.Sprintf("\"%s@%s\"", d.Name, hash)
}
bfs := func(dt *depTreeNode, depth int) {
toProcess = append(toProcess, dt.children...)
rank[name(dt.this)] = depth
}
depth := 0
bfs(dt, depth)
for len(toProcess) > 0 {
depth++
current := toProcess
toProcess = make([]*depTreeNode, 0, 32)
for _, dt := range current {
bfs(dt, depth)
}
}
fmt.Println("digraph G {")
fmt.Println("rankdir = TB;")
fmt.Println("subgraph {")
toPrint := make([]*depTreeNode, 0, 32)
printed := make(map[string]struct{})
printNode := func(dt *depTreeNode) {
if _, ok := printed[dt.this.Hash]; ok {
return
}
printed[dt.this.Hash] = struct{}{}
toPrint = append(toPrint, dt.children...)
for _, c := range dt.children {
fmt.Printf("%s -> %s\n", name(dt.this), name(c.this))
}
}
printNode(dt)
for len(toPrint) > 0 {
current := toPrint
toPrint = make([]*depTreeNode, 0, 32)
for _, dt := range current {
printNode(dt)
}
}
reverseRank := make([][]string, depth+1)
for n, r := range rank {
reverseRank[r] = append(reverseRank[r], n)
}
for _, r := range reverseRank {
fmt.Printf("{rank = same;")
for _, n := range r {
fmt.Printf(" %s;", n)
}
fmt.Printf("}\n")
}
fmt.Println("}")
fmt.Println("}")
return nil
},
}
var depCheckCommand = cli.Command{
Name: "check",
Usage: "perform comprehensive sanity check and note any packaging issues",
Action: func(c *cli.Context) error {
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
success, err := check(pkg)
if err != nil {
return err
}
if success {
os.Exit(0)
} else {
os.Exit(1)
}
return nil
},
}
var LockInstallCommand = cli.Command{
Name: "lock-install",
Usage: "Install deps from lockfile into vendor",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "nofancy",
Usage: "write minimal output",
},
},
Action: func(c *cli.Context) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
var lck gx.LockFile
if err := gx.LoadLockFile(&lck, filepath.Join(cwd, gx.LckFileName)); err != nil {
return err
}
pm.ProgMeter = progmeter.NewProgMeter(c.Bool("nofancy"))
if err := pm.InstallLock(lck.Lock, cwd); err != nil {
return fmt.Errorf("install deps: %s", err)
}
return nil
},
}
var CleanCommand = cli.Command{
Name: "clean",
Usage: "cleanup unused packages in vendor directory",
Description: `deletes any package in the 'vendor/gx' directory
that is not a dependency of this package.
use '--dry-run' to print packages that would be deleted without actually
removing them.
`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Usage: "print out things to be removed without removing them",
},
},
Action: func(c *cli.Context) error {
pkg, err := LoadPackageFile(PkgFileName)
if err != nil {
return err
}
dry := c.Bool("dry-run")
good, err := pm.EnumerateDependencies(pkg)
if err != nil {
return err
}
ipath, err := gx.InstallPath(pkg.Language, cwd, false)
if err != nil {
return err
}
vdir := filepath.Join(ipath, "gx", "ipfs")
dirinfos, err := ioutil.ReadDir(vdir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
for _, di := range dirinfos {
if !strings.HasPrefix(di.Name(), "Qm") {
continue
}