@@ -539,8 +539,8 @@ load-module: function [
539
539
delay: no-share: none hdr: spec-of mod
540
540
assert/type [hdr/options [block! none! ]]
541
541
]
542
- block? mod [set /any [hdr: code: ] mod]
543
- ; module/block mod used later for override testing
542
+ block? mod [set /any [hdr: code: ] mod] ; module/block mod used later for override testing
543
+ url? mod [ return none ] ; used by `import` for downloading extensions
544
544
545
545
; Get and process the header
546
546
not hdr [
@@ -579,6 +579,7 @@ load-module: function [
579
579
case /all [
580
580
module? :mod0 [hdr0: spec-of mod0] ; final header
581
581
block? :mod0 [hdr0: first mod0] ; cached preparsed header
582
+ url? :mod0 [hdr0: object [version: 0.0.0 url: :mod0 checksum: none]]
582
583
;assert/type [name0 word! hdr0 object! sum0 [binary! none!]] none
583
584
;not tuple? set/any 'ver0 :hdr0/version [ver0: 0.0.0] ;@@ remove?
584
585
]
@@ -647,6 +648,49 @@ load-module: function [
647
648
reduce [name if module? mod [mod]]
648
649
]
649
650
651
+ locate-extension : function [ name [word! ]] [
652
+ foreach path system/options/module-paths [
653
+ file: append to file! name %.rebx
654
+ if exists? path/: file [ return path/: file ]
655
+ file: repend to file! name [#"-" system/build/os #"-" system/build/arch %.rebx ]
656
+ if exists? path/: file [ return path/: file ]
657
+ ]
658
+ none
659
+ ]
660
+
661
+ download-extension : function [
662
+ "Downloads extension from a given url and stores it in a current directory!"
663
+ name [word! ]
664
+ url [url! ]
665
+ ] [
666
+ either dir? url [
667
+ file: repend to file! name [#"-" system/build/os #"-" system/build/arch %.rebx ]
668
+ url: append copy url file
669
+ if system/platform <> 'Windows [append url %.gz ]
670
+ ][
671
+ file: select decode-url url 'target
672
+ ]
673
+ opt: system/options/log
674
+ try /except [
675
+ if exists? file [
676
+ ; we don't want to overwrite existing files!
677
+ log/error ["File already exists:^[ [m" file]
678
+ return file
679
+ ]
680
+ log/info 'REBOL ["Downloading:^[ [m" url]
681
+ system/options/log: make map! [http: 0 tls: 0 ]
682
+ bin: read url
683
+ if %.gz = suffix? url [bin: decompress bin 'gzip]
684
+ write file bin
685
+ file: to-real-file file ; makes it absolute
686
+ ][
687
+ log/error ["Failed to download:^[ [m" file]
688
+ file: none
689
+ ]
690
+ system/options/log: opt
691
+ file
692
+ ]
693
+
650
694
import : function [
651
695
"Imports a module; locate, load, make, and setup its bindings."
652
696
module [word! file! url! string! binary! module! block! ]
@@ -677,7 +721,23 @@ import: function [
677
721
path/: file version ver check sum no-share no-lib /import /as module
678
722
] [break]
679
723
]
724
+ unless name [
725
+ ; try to locate as an extension...
726
+ if file: any [
727
+ locate-extension module
728
+ all [
729
+ url? mod: select system/modules module
730
+ download-extension module mod
731
+ ]
732
+ ][
733
+ log/info 'REBOL ["Importing extension:^[ [m" file]
734
+ set [name: mod: ] apply :load-module [
735
+ file version ver check sum no-share no-lib /import /as module
736
+ ]
737
+ ]
738
+ ]
680
739
]
740
+
681
741
any [file? module url? module] [
682
742
cause-error 'access 'cannot-open reduce [module "not found or not valid" ]
683
743
]
0 commit comments