/
gxpkg.ss
1185 lines (1071 loc) · 37 KB
/
gxpkg.ss
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
;;; -*- Gerbil -*-
;;; © vyzo
;;; The Gerbil Package Manager
;;;
;;; Usage:
;;; gxpkg action arg ....
;;; Actions:
;;; install pkg[@tag] ...
;;; update pkg[@tag] ...
;;; uninstall pkg ...
;;; link pkg src
;;; unlink pkg ...
;;; build pkg ...
;;; clean pkg ...
;;; list
;;; retag
;;; search kw ...
;;; Packages:
;;; github.com/user/package -- github based packages
;;; gitlab.com/user/package -- gitlab based packages
;;; bitbucket.org/user/package -- bitbucket based packages
;;; all -- action applies to all packages where sensible to do so
;;; TODO: add private repos support
(import :gerbil/gambit
:std/cli/getopt
:std/format
:std/iter
:std/misc/process
:std/misc/template
:std/net/request
:std/pregexp
:std/sort
(only-in :std/srfi/1 reverse!)
(only-in :std/srfi/13 string-trim)
:std/sugar
./env)
(export main
;; script api
pkg-root-dir
pkg-install pkg-uninstall pkg-update
pkg-link pkg-unlink
pkg-build pkg-clean
pkg-list
pkg-retag
pkg-plist pkg-dependents pkg-dependents*)
(def (main . args)
(def force-flag
(flag 'force "-f" "--force"
help: "force the action"))
(def install-cmd
(command 'install help: "install one or more packages"
global-env-flag
(rest-arguments 'pkg help: "package to install; use @tag to checkout a specific tag")))
(def uninstall-cmd
(command 'uninstall help: "uninstall one or more packages"
global-env-flag force-flag
(rest-arguments 'pkg help: "package to uninstall")))
(def update-cmd
(command 'update help: "update one or more packages"
global-env-flag
(rest-arguments 'pkg help: "package to update; use @tag to checkout a specific tag; all for all packages")))
(def link-cmd
(command 'link help: "link a local development package"
global-env-flag
(argument 'pkg help: "package to link")
(argument 'src help: "path to package source directory")))
(def unlink-cmd
(command 'unlink help: "unlink one or more local development packages"
global-env-flag force-flag
(rest-arguments 'pkg help: "package to unlink")))
(def build-cmd
(command 'build help: "rebuild one or more packages and their dependents"
global-env-flag
(flag 'build-release "-R" "--release" help: "build released (static) executables")
(flag 'build-optimized "-O" "--optimized" help: "build full program optimized executables")
(flag 'build-debug "-g" "--debug" help: "build with debug symbols")
(rest-arguments 'pkg help: "package to build; all for all packages, omit to build in current directory")))
(def clean-cmd
(command 'clean help: "clean compilation artefacts from one or more packages"
global-env-flag
(rest-arguments 'pkg help: "package to clean; all for all packages, omit to clean in current directory")))
(def new-cmd
(command 'new help: "create a new package template in the current directory"
(option 'package "-p" "--package"
help: "the package prefix for your project; defaults to the current username")
(option 'name "-n" "--name"
help: "the package name; defaults to the current directory name")
(option 'link "-l" "--link"
help: "link this package with a public package name; for example: github.com/your-user/your-package")))
(def deps-cmd
(command 'deps help: "manage dependencies for the current project"
(flag 'add "-a" "--add"
help: "add dependencies")
(flag 'install "-i" "--install"
help: "install dependencies")
(flag 'update "-u" "--update"
help: "update dependencies")
(flag 'remove "-r" "--remove"
help: "remove dependencies")
(rest-arguments 'deps
help: "the list of dependencies to add, update or remove; empty for all; if no flags are specified it displays current deps")))
(def list-cmd
(command 'list
global-env-flag
help: "list installed packages"))
(def retag-cmd
(command 'retag
global-env-flag
help: "retag installed packages"))
(def search-cmd
(command 'search help: "search the package directory"
global-env-flag
(option 'directory "-d" "--directory"
help: "A specific directory to use; by default the mighty-gerbils directory and all user configured directories are searched")
(flag 'as-list "--list"
help: "Print the results as a list, do not format it")
(rest-arguments 'keywords help: "keywords to search for, as a boolean and")))
(def dir-cmd
(command 'dir help: "manage the directory list"
global-env-flag
(flag 'add "-a" "--add"
help: "add a directory to the list of searched directories")
(flag 'remove "-r" "--remove"
help: "remove a directory from the list")
(rest-arguments 'directories
help: "the directory to add or remove; the directory can be a fully qualified https url to the package-list or a github repo of the form github.com/some-org/some-repo")))
(def env-cmd
(command 'env help: "execute a command within the local package context"
(argument 'command help: "the command to execute")
(rest-arguments 'command-args help: "the command arguments")))
(call-with-getopt gxpkg-main args
program: "gxpkg"
help: "The Gerbil Package Manager"
new-cmd
build-cmd
clean-cmd
deps-cmd
link-cmd
unlink-cmd
install-cmd
uninstall-cmd
update-cmd
list-cmd
retag-cmd
search-cmd
dir-cmd
env-cmd))
(def (gxpkg-main cmd opt)
(let-hash opt
(case cmd
((new)
(pkg-new .package .name .link))
((build)
(build-pkgs .pkg .?build-release .?build-optimized .?build-debug .?global))
((clean)
(clean-pkgs .pkg .?global))
((deps)
(manage-deps .deps .?add .?install .?update .?remove .?global))
((link)
(link-pkg .pkg .src .?global))
((unlink)
(unlink-pkgs .pkg .?force .?global))
((install)
(install-pkgs .pkg .?global))
((uninstall)
(uninstall-pkgs .pkg .?force .?global))
((update)
(update-pkgs .pkg .?global))
((list)
(list-pkgs .?global))
((retag)
(retag-pkgs .?global))
((search)
(search-pkgs .keywords .directory .?as-list))
((dir)
(manage-dirs .directories .?add .?remove .?global))
((env)
(env-exec .command .command-args .?global)))))
;;; commands
(def (env-exec command args global?)
(unless global?
(setup-local-pkg-env! #t))
(setup-local-path!)
(invoke command args
stdin-redirection: #f
stdout-redirection: #f
stderr-redirection: #f))
(defrules fold-pkgs ()
((_ pkgs action action-arg ...)
(let lp ((rest pkgs) (result #f))
(match rest
([pkg . rest]
(if (action pkg action-arg ...)
(lp rest #t)
(lp rest result)))
(else
result)))))
(defrules fold-pkgs-retag ()
((_ pkgs action action-arg ...)
(when (fold-pkgs pkgs action action-arg ...)
(pkg-retag))))
(def (install-pkgs pkgs global?)
(unless global?
(setup-local-pkg-env! #t))
(fold-pkgs-retag pkgs pkg-install))
(def (uninstall-pkgs pkgs force? global?)
(unless global?
(setup-local-pkg-env! #t))
(fold-pkgs-retag pkgs pkg-uninstall force?))
(def (update-pkgs pkgs global?)
(unless global?
(setup-local-pkg-env! #t))
(when (fold-pkgs pkgs pkg-update)
;; the package dependencies might have changed, so install them
(for-each
(lambda (pkg)
(if (equal? pkg "all")
(for-each pkg-install-deps (pkg-list))
(pkg-install-deps pkg)))
pkgs)
;; rebuild packages
(for-each pkg-build pkgs)
(pkg-retag)))
(def (link-pkg pkg src global?)
(unless global?
(setup-local-pkg-env! #t))
(pkg-link pkg src))
(def (unlink-pkgs pkgs force? global?)
(unless global?
(setup-local-pkg-env! #t))
(for-each (cut pkg-unlink <> force?) pkgs))
(def (build-pkgs pkgs release? optimized? debug? global?)
(unless global?
(setup-local-pkg-env! #t))
(when release?
(setenv "GERBIL_BUILD_RELEASE" "t"))
(when optimized?
(setenv "GERBIL_BUILD_OPTIMIZED" "t"))
(when debug?
(setenv "GERBIL_BUILD_DEBUG" "t"))
(if (null? pkgs)
;; do local build
(pkg-build "." #f)
(for-each pkg-build pkgs)))
(def (clean-pkgs pkgs global?)
(unless global?
(setup-local-pkg-env! #t))
(if (null? pkgs)
;; do local clean
(pkg-clean ".")
(for-each pkg-clean pkgs)))
(def (list-pkgs global?)
(unless global?
(setup-local-pkg-env! #t))
(for (pkg (pkg-list))
(let (tag (pkg-tag-get pkg))
(display pkg)
(when tag
(display* "@" tag))
(newline))))
(def (retag-pkgs global?)
(unless global?
(setup-local-pkg-env! #t))
(pkg-retag))
(def (search-pkgs keywords dir as-list?)
(pkg-search keywords dir as-list?))
(def (manage-dirs dirs add? remove? global?)
(pkg-directory-manage dirs add? remove? global?))
(def (manage-deps deps add? install? update? remove? global?)
(unless global?
(setup-local-pkg-env! #t))
(pkg-deps-manage deps add? install? update? remove?))
(def (setup-local-path!)
(let* (($GERBIL_PATH (gerbil-path))
($PATH (getenv "PATH"))
($PATH (string-append $GERBIL_PATH "/bin" ":" $PATH)))
(setenv "PATH" $PATH)))
;;; action implementation -- script api
(def +root-dir+
(delay (gerbil-path)))
(def +pkg-root-dir+
(delay (path-expand "pkg" (force +root-dir+))))
(def +pkg-lib-dir+
(delay (path-expand "lib" (force +root-dir+))))
(def +pkg-lib-static-dir+
(delay (path-expand "static" (force +pkg-lib-dir+))))
(def +pkg-bin-dir+
(delay (path-expand "bin" (force +root-dir+))))
(def pkg-root-dir
(let (once
(delay
(begin
(create-directory* (force +root-dir+))
(create-directory* (force +pkg-root-dir+))
(create-directory* (force +pkg-lib-dir+))
(create-directory* (force +pkg-lib-static-dir+))
(create-directory* (force +pkg-bin-dir+)))))
(lambda ()
(force once)
(force +pkg-root-dir+))))
(def (pkg-new package-prefix package-name maybe-link)
(def prefix (or package-prefix
(getenv "USER" #f)
(error "Package prefix not specified with -p or --package, and USER not defined")))
(def name (or package-name
(path-strip-directory (path-normalize* (current-directory)))))
(def (create-template file template . args)
(call-with-output-file file
(lambda (output)
(apply write-template template output args))))
(create-template "gerbil.pkg" gerbil.pkg-template
package: prefix)
(create-directory name)
(create-template (path-expand "main.ss" name) main.ss-template
name: name)
(create-template (path-expand "lib.ss" name) lib.ss-template)
(create-template [path: "build.ss" permissions: #o755] build.ss-template
name: name)
(create-template ".gitignore" gitignore-template)
(create-template "Makefile" Makefile-template name: name)
(when maybe-link
(pkg-link maybe-link (current-directory))))
(def (pkg+tag pkg)
(let* ((pt (string-split pkg #\@))
(pkg (car pt))
(tag (let (kdr (cdr pt))
(and (not (null? kdr))
(car kdr)))))
(values pkg tag)))
(def (pkg-install pkg)
(let* (((values pkg tag) (pkg+tag pkg))
(current-tag (pkg-tag-get pkg)))
(def (install-it tag)
(pkg-fetch pkg tag)
(pkg-install-deps pkg)
(pkg-build pkg #f))
(if current-tag
(cond
((pkg-tag-incompatible? current-tag tag)
(error "Package already installed with an incompatible tag" pkg tag current-tag))
((pkg-tag-choose current-tag tag)
=> install-it)
(else
(install-it tag)))
(install-it tag))))
(def (pkg-install-deps pkg)
(let* ((plist (pkg-plist pkg))
(deps (pgetq depend: plist [])))
(for-each pkg-install deps)))
(def (pkg-uninstall pkg (force? #f))
(let* ((root (pkg-root-dir))
(dest (path-expand pkg root)))
(and (file-exists? dest)
(not (file-symbolic-link? dest))
(begin
(unless force?
(let (deps (pkg-dependents pkg))
(unless (null? deps)
(error "Refuse to uninstall package; orphaned dependencies" deps))))
(pkg-clean pkg)
(displayln "... uninstall " pkg)
(run-process ["rm" "-rf" (path-normalize* dest)]
coprocess: void)
(let (tagf (pkg-tag-file pkg))
(when (file-exists? tagf)
(delete-file tagf)))
#t))))
(def (pkg-update pkg)
(cond
((equal? pkg "all")
(fold-pkgs (pkg-list) pkg-update))
((or (string-prefix? "github.com/" pkg)
(string-prefix? "gitlab.com/" pkg)
(string-prefix? "bitbucket.org/" pkg))
(pkg-update-git pkg))
(else
(error "Unknown package provider" pkg))))
(def (pkg-update-git pkg)
(let* ((root (pkg-root-dir))
((values pkg tag) (pkg+tag pkg))
(dest (path-expand pkg root)))
(unless (file-exists? dest)
(error "Cannot update unknown package" pkg))
(and (not (file-symbolic-link? dest))
(pkg-fetch-git pkg tag))))
(def (pkg-fetch pkg tag)
(cond
((or (string-prefix? "github.com/" pkg)
(string-prefix? "gitlab.com/" pkg)
(string-prefix? "bitbucket.org/" pkg))
(pkg-fetch-git pkg tag))
(else
(error "Unknown package provider" pkg))))
(def (pkg-fetch-git pkg tag)
(let* ((root (pkg-root-dir))
(dest (path-expand pkg root)))
(if (file-exists? dest)
(begin
(displayln "... fetching " pkg)
(run-process ["git" "fetch" "-q"]
directory: dest
coprocess: void
stdout-redirection: #f))
(let ((path (path-directory dest))
(clone-url (git-clone-url pkg)))
(displayln "... cloning " pkg)
(create-directory* path)
(run-process ["git" "clone" "-q" clone-url]
directory: path
coprocess: void
stdout-redirection: #f)))
(when tag
(displayln "... checking out " tag)
(run-process ["git" "checkout" "-q" tag]
directory: dest
coprocess: void
stdout-redirection: #f)
(call-with-output-file (pkg-tag-file pkg)
(cut write tag <>)))
(let (tag (pkg-tag-get pkg))
(when (not (pkg-tag-semver? tag))
(displayln "... pulling")
(run-process ["git" "pull" "-q" "origin" tag]
directory: dest
coprocess: void
stdout-redirection: #f)))))
(def (pkg-tag-file pkg)
(let* ((root (pkg-root-dir))
(dest (path-expand pkg root)))
(string-append dest ".tag")))
(def (pkg-tag-get pkg)
(let ((tagf (pkg-tag-file pkg))
(top (path-expand pkg (pkg-root-dir))))
(cond
((file-exists? tagf)
(call-with-input-file tagf read))
((file-exists? top)
(run-process ["git" "branch" "--show-current"]
directory: top
coprocess: read-line))
(else #f))))
(def (pkg-tag-incompatible? current other)
(cond
((or (not current) (not other)) #f)
((and (pkg-tag-semver? current)
(pkg-tag-semver? other))
#f)
((or (member current '("master" "main"))
(member other '("master" "main")))
#f)
(else
(not (equal? current other)))))
;; Note: in this implementation of semver, we always keep the greatest version.
;; We don't pay attention to majors and we consider master/main to be the frontier.
(def (pkg-tag-choose current other)
(cond
((equal? current other)
current)
((not other)
current)
((member current '("master" "main"))
current)
((member other '("master" "main"))
other)
(else
(let ((current-version (pkg-tag-semver current))
(other-version (pkg-tag-semver other)))
(let lp ((current-rest current-version)
(other-rest other-version))
(match current-rest
([current-hd . current-rest]
(match other-rest
([other-hd . other-rest]
(cond
((= current-hd other-hd)
(lp current-rest other-rest))
((> current-hd other-hd)
current)
(else other)))
(else current)))
(else other)))))))
(def +rx-semver+
(pregexp "v(\\d+\\.)*\\d+"))
(def (pkg-tag-semver? tag)
(pregexp-match +rx-semver+ tag))
(def (pkg-tag-semver tag)
(map string->number
(string-split (substring tag 1 (string-length tag)) ; drop the v
#\.)))
(def (pkg-link pkg src)
(let* ((root (pkg-root-dir))
(dest (path-expand pkg root)))
(when (file-exists? dest)
(error "Cannot link package; destination already exists" pkg dest))
;; We could do some sanity checks about the source package:
;; - make sure the source exists, and is a directory
;; - make sure it's a valid pkg with a build.ss
;; - install dependencies of the package (as listed in gerbil.pkg)
;; - etc ...
;; ... but where to stop, so just trust the user and link!
(create-directory* (path-directory dest))
(create-symbolic-link src dest)))
(def (pkg-unlink pkg (force? #f))
(let* ((root (pkg-root-dir))
(dest (path-expand pkg root)))
(when (file-exists? dest)
(unless (file-symbolic-link? dest)
(error "Cannot unlink package; not a symbolic link" dest))
(unless force?
(let (deps (pkg-dependents pkg))
(unless (null? deps)
(error "Refuse to unlink package; orphaned dependencies" deps))))
(pkg-clean pkg)
(delete-file dest))))
(def (pkg-build pkg (dependents? #t))
(def build-options
(let* ((options [])
(options (if (getenv "GERBIL_BUILD_RELEASE" #f)
(cons "--release" options)
options))
(options (if (getenv "GERBIL_BUILD_OPTIMIZED" #f)
(cons "--optimized" options)
options))
(options (if (getenv "GERBIL_BUILD_DEBUG" #f)
(cons "--debug" options)
options)))
options))
(cond
((equal? pkg "all")
(let* ((pkgs (pkg-list))
(deps (map (cut pkg-dependents* <> pkgs) pkgs))
(pkgs+deps (map cons pkgs deps))
(sorted (sort pkgs+deps (lambda (pa pb) (member (car pb) (cdr pa))))))
(for-each (cut pkg-build <> #f) (map car sorted))))
((equal? pkg ".")
(displayln "... build in current directory")
(pkg-manifest! pkg)
(let (build.ss (path-expand "build.ss" (current-directory)))
(run-process [build.ss "compile" build-options ...]
stdout-redirection: #f)))
(else
(let* ((root (pkg-root-dir))
(path (path-expand pkg root))
(_ (unless (file-exists? path)
(error "Cannot build unknown package" pkg)))
(build.ss (pkg-build-script pkg)))
(displayln "... build " pkg)
(pkg-manifest! pkg)
(run-process [build.ss "compile" build-options ...]
directory: path
coprocess: void
stdout-redirection: #f)
(when dependents?
(for-each pkg-build (pkg-dependents pkg)))))))
(def (pkg-manifest! pkg)
(let* (((values pkg _) (pkg+tag pkg))
(plist (pkg-plist pkg))
(deps (pgetq depend: plist []))
(deps
(let recur ((rest deps) (result []))
(match rest
([dep . rest]
(let ((values dep _) (pkg+tag dep))
;; check for external package manager installed deps (eg NiX)
(if (file-exists? (pkg-plist-path dep))
(let* ((plist (pkg-plist dep))
(deps (pgetq depend: plist [])))
(recur rest (recur deps (cons dep result))))
;; just record the dep, we don't have the pkg contents
;; for transitive
(recur rest (cons dep result)))))
(else
(remove-duplicates result)))))
(manifests
(let lp ((rest deps) (result []))
(match rest
([dep . rest]
(let (manifest
(call-with-input-file
(path-expand (string-append dep ".manifest")
(pkg-root-dir))
read))
(lp rest (append result manifest))))
(else
(remove-duplicates result)))))
(gerbil-version
(cons "Gerbil" (gerbil-version-string)))
(gambit-version
(cons "Gambit" (system-version-string)))
(write-version-manifest
(lambda (manifest1 output)
(pretty-print
`(def version-manifest
(quote
,(remove-duplicates
(cons* manifest1 gerbil-version gambit-version manifests))))
output)))
(write-pkg-manifest
(lambda (manifest1 output)
(pretty-print
(remove-duplicates (cons* manifest1 gerbil-version gambit-version manifests))
output))))
(if (equal? pkg ".")
(let* ((version
(if (file-exists? ".git")
(run-process ["git" "describe" "--tags" "--always"]
coprocess: read-line)
"unknown"))
(manifest1
(cons (path-strip-directory
(path-normalize (current-directory)))
version)))
(call-with-output-file [path: "manifest.ss" create: 'maybe truncate: #t]
(cut write-version-manifest manifest1 <>)))
(let (pkg-path (path-expand pkg (pkg-root-dir)))
(when (file-exists? pkg-path)
(let* ((version
(run-process ["git" "describe" "--tags" "--always"]
directory: pkg-path
coprocess: read-line))
(manifest1
(cons pkg version)))
(call-with-output-file [path: (path-expand "manifest.ss" pkg-path)
create: 'maybe truncate: #t]
(cut write-version-manifest manifest1 <>))
(call-with-output-file [path: (string-append pkg-path ".manifest")
create: 'maybe truncate: #t]
(cut write-pkg-manifest manifest1 <>))))))))
(def (pkg-clean pkg)
(cond
((equal? pkg "all")
(let* ((pkgs (pkg-list))
(deps (map (cut pkg-dependents* <> pkgs) pkgs))
(pkgs+deps (map cons pkgs deps))
(sorted (sort pkgs+deps (lambda (pa pb) (member (car pb) (cdr pa))))))
(for-each (cut pkg-clean <>) (map car sorted))))
((equal? pkg ".")
(displayln "... clean current package")
(let (build.ss (path-expand "build.ss" (current-directory)))
(run-process [build.ss "clean"]
stdout-redirection: #f)))
(else
(let* ((root (pkg-root-dir))
(path (path-expand pkg root))
(_ (unless (file-exists? path)
(error "Cannot build unknown package" pkg)))
(build.ss (pkg-build-script pkg)))
(displayln "... clean " pkg)
(run-process [build.ss "clean"]
directory: path
coprocess: void
stdout-redirection: #f)))))
(def (pkg-list)
(def root (pkg-root-dir))
(def (walk dir pkgpath)
(for-each
(lambda (file)
(let* ((path (path-expand file dir))
(gerbil.pkg (path-expand "gerbil.pkg" path))
(pkgpath
(if (string-empty? pkgpath)
file
(string-append pkgpath "/" file))))
(cond
((file-exists? gerbil.pkg)
(yield pkgpath))
((file-directory? path)
(walk path pkgpath)))))
(directory-files dir)))
(for/collect (pkg (cut walk root "")) pkg))
(def (pkg-retag)
(let* ((root (pkg-root-dir))
(dirs (filter (lambda (file) (file-directory? (path-expand file root)))
(directory-files root))))
(displayln "... tagging packages")
(run-process ["gxtags" dirs ...]
directory: root)))
;; package directory search
(def (pkg-search keywords dir as-list?)
(def (search lst)
(def (try-match kw)
(let (rx (pregexp (string-append "(?i:" kw ")")))
(lambda (pkg desc)
(or (pregexp-match rx pkg) (pregexp-match rx desc)))))
(def matching
(map try-match keywords))
(let lp ((rest lst) (result []))
(match rest
([hd . rest]
(match hd
([pkg . plist]
(let (description (pgetq description: plist))
(if (andmap (lambda (matches?) (matches? pkg description))
matching)
(lp rest (cons (cons pkg description) result))
(lp rest result))))
(else
(lp rest result))))
(else
(reverse result)))))
(def (display-pkgs alst)
(if as-list?
(pretty-print alst)
(for ([pkg . desc] alst)
(displayln pkg ": " desc))))
(let (alst (if dir (pkg-directory-list dir) (pkg-directory-list-all)))
(let (matches (search alst))
(display-pkgs matches))))
(def +mighty-gerbils-pkg-directory+
"github.com/mighty-gerbils/gerbil-directory")
(def (pkg-directory-url dir)
(cond
((string-prefix? "https://" dir)
dir)
((string-prefix? "github.com/" dir)
(let (repo (substring dir (string-index dir #\/) (string-length dir)))
(string-append "https://raw.githubusercontent.com" repo "/main/package-list")))
(else
(error "bad directory" dir))))
(def (pkg-directory-user-dirs-path)
(path-expand "pkg/directory-list" (path-expand "~/.gerbil")))
(def (pkg-directory-local-dirs-path)
(path-expand "pkg/directory-list" (gerbil-path)))
(def (pkg-directory-dirs)
(let* ((user-dir (pkg-directory-user-dirs-path))
(local-dir (pkg-directory-local-dirs-path))
(user-dirs
(if (file-exists? user-dir)
(call-with-input-file user-dir read)
[])))
(remove-duplicates
(cond
((equal? user-dir local-dir)
user-dirs)
((file-exists? local-dir)
(append (call-with-input-file local-dir read)
user-dirs))
(else
user-dirs)))))
(def (pkg-directory-user-dirs (get-path pkg-directory-user-dirs-path))
(let (dir (get-path))
(if (file-exists? dir)
(call-with-input-file dir read)
[])))
(def (pkg-directory-local-dirs)
(pkg-directory-user-dirs pkg-directory-local-dirs-path))
(def (pkg-directory-dirs-add add-dirs global?)
(let* ((current
(if global?
(pkg-directory-user-dirs)
(pkg-directory-local-dirs)))
(new
(let lp ((rest add-dirs) (new []))
(match rest
([dir . rest]
(if (or (member dir current)
(member dir new))
(lp rest new)
(lp rest (cons dir new))))
(else
(remove-duplicates
(append current (reverse new))))))))
(call-with-output-file
(if global?
(pkg-directory-user-dirs-path)
(pkg-directory-local-dirs-path))
(cut write new <>))))
(def (pkg-directory-dirs-remove remove-dirs global?)
(let* ((current
(if global?
(pkg-directory-user-dirs)
(pkg-directory-local-dirs)))
(new
(let lp ((rest current) (new []))
(match rest
([dir . rest]
(if (member dir remove-dirs)
(lp rest new)
(lp rest (cons dir new))))
(else
(reverse new))))))
(call-with-output-file
(if global?
(pkg-directory-user-dirs-path)
(pkg-directory-local-dirs-path))
(cut write new <>))))
(def (pkg-directory-urls)
(let* ((default-dirs [+mighty-gerbils-pkg-directory+])
(other-dirs (pkg-directory-dirs))
(all-dirs (remove-duplicates (append default-dirs other-dirs))))
(map pkg-directory-url all-dirs)))
(def (pkg-directory-list-all)
(remove-duplicates
(for/fold (result []) (url (pkg-directory-urls))
(let (req (with-catch
(lambda (exn)
(displayln/err "*** WARNING error retrieving packages from " url
": " (or (error-message exn) "(unknown error)"))
#f)
(cut http-get url redirect: #t)))
(if (and req (fx= (request-status req) 200))
(let (pkgs (with-catch
(lambda (exn)
(displayln/err "*** WARNING error retrieving packages from "
(request-url req)
": " (or (error-message exn) "(unknown error)"))
[])
(lambda () (call-with-input-string (request-text req) read))))
(append result pkgs))
(begin
(displayln/err "error retrieving packages from " url
": " (request-status-text req))
result))))))
(def (pkg-directory-list dir)
(let* ((url (pkg-directory-url dir))
(req (http-get url redirect: #t)))
(if (fx= (request-status req) 200)
(call-with-input-string (request-text req) read)
(error "error retrieving packages" url (request-status-text req)))))
;; package directory management
(def (pkg-directory-manage dirs add? remove? global?)
(cond
((null? dirs)
(if (or add? remove?)
(error "no directory specified")
(let (user-dirs (pkg-directory-dirs))
(for (dir user-dirs)
(let (url (pkg-directory-url dir))
(if (equal? dir url)
(displayln dir)
(displayln dir " -> " url)))))))
((and add? remove?)
(error "do you want to add or remove"))
(add?
(pkg-directory-dirs-add dirs global?))
(remove?
(pkg-directory-dirs-remove dirs global?))
(else
(for (dir dirs)
(pretty-print (pkg-directory-list dir))))))
;; package dependency management
(def (pkg-deps-manage deps add? install? update? remove?)
(let* ((plist (pkg-plist "."))
(current-deps (pgetq depend: plist [])))
(def (add-dep! dep)
(let ((values xpkg _) (pkg+tag dep))
(let lp ((rest current-deps))
(match rest
([hd . rest]
(let ((values dpkg _) (pkg+tag hd))
(if (equal? xpkg dpkg)
(set! (car rest) dep)
(lp rest))))
(else
(set! current-deps (append current-deps [dep])))))))
(def (remove-dep! dep)
(let ((values xpkg _) (pkg+tag dep))
(set! current-deps
(filter (lambda (hd)
(let ((values dpkg _) (pkg+tag hd))
(not (equal? dpkg xpkg))))
current-deps))))
(def (write-deps!)
(let (hd (member depend: plist))
(if hd
(set! (car (cdr hd)) current-deps)
(set! plist (append plist [depend: current-deps]))))
(call-with-output-file (path-expand "gerbil.pkg" (current-directory))
(cut pretty-print plist <>)))
(if (null? deps)
(cond
(add? (error "nothing to add"))
(remove? (error "nothing to remove"))
(install?
(install-pkgs current-deps #t))
(update?
(update-pkgs current-deps #t))
(else
(for-each displayln current-deps)))
(cond
((and add? remove?)
(error "cannot both add and remove"))
((and remove? install?)
(error "cannot both remove and install"))
((and add? update?)
(error "cannot both add and update"))
(add?
(for (dep deps)
(add-dep! dep))
(write-deps!)
(when install?
(install-pkgs deps #t)))
(update?
(update-pkgs deps #t))
(remove?
(for (dep deps)
(remove-dep! dep))
(write-deps!))
(else
(error "unspecified action; use --add, --update or --remove"))))))
;;; internal
(def +pkg-plist+
(make-hash-table))
(def (pkg-plist pkg)
(cond
((equal? pkg ".")
(let* ((gerbil.pkg (path-expand "gerbil.pkg" (current-directory)))
(plist (call-with-input-file gerbil.pkg read)))
(if (eof-object? plist) [] plist)))
((hash-get +pkg-plist+ pkg))
(else
(let* ((gerbil.pkg (pkg-plist-path pkg))
(_ (unless (file-exists? gerbil.pkg)
(error "bad package; missing gerbil.pkg" pkg)))
(plist (call-with-input-file gerbil.pkg read))
(plist (if (eof-object? plist) [] plist)))
(hash-put! +pkg-plist+ pkg plist)
plist))))
(def (pkg-plist-path pkg)