REBOL [ Title: "Yet Another Module Manager" Date: 2000-03-31 Version: 0.0.11 File: %yamm.r Author: "Ingo Hohmann" Rights: " (c) 1999 Ingo Hohmann " ; Needs: 2.2.0 Usage: { Put "do yamm.r" into your user.r file, and then load modules with "import [ module ], run them with "run %module } Purpose: { My little Rebol module system } History: [ [31-Mar-2000 "Ingo Hohmann" "I think I got trust right now"] [17-Sep-1999 "Ingo Hohmann" "initial Version"] ] Email: ingo@2b1.de Site: http://www.2b1.de/Rebol/ Category: 'utility Status: 'working ] do %error-handling.r ;use has a bug ... ;use [ module-list search-path be-word add-module ] [ ; ; helpers ; unset!: do [type?] be-word: func [w [file! word! string!] ] [ if word? w [return w] if file? w [ w: to-string w ] to-word w ] yamm: make object! [ ; ; Initialize ; inspect-error: true trust-all: false already-running: false module-list: copy [] search-paths: compose [ (rejoin [system/options/home "Work/"]) (rejoin [system/options/home "Work/Web/"]) (system/options/home) (rejoin [system/options/home "Skripte/"]) ] ; ; Internals ; message: func ["Prints value, if not quiet" value] [ if not system/options/quiet [ print value ] ] add-module: func ["internal" mod [word!]] [ if not found? find module-list mod [ append module-list mod ] ] ; ; UI functions ; module-path: func [ "Add path to module paths, or clear them" /init "initialize with block" paths [block!] /add "add a new path" path [file!] "path to add" /clear "clear paths" ] [ if clear [ search-paths: copy "" exit ] if add [ if dir? path [ append search-paths path ] ] ] module: func [ "Insert in the module list" mod [word!] /words "Block of global words (NOT USED)" wrds [block!] ] [ add-module mod ] ; ; if modules is a block, each module may be followed by a block ; containing the words, you are interested in. It isn'treally ; used up to now, but may be nice to know some day ; import: func [ "Module importer" modules [block! word! file!] /force "forces reload" /trust "allow redefinitions" /debug "helpful while debugging a script (trust + force)" /local mod path quiet exit redefined sub-process ] [ sub-process: already-running already-running: true if debug [ force: trust: true ] if trust-all [ unprotect-system ] exit: func ["cleans environment before exit"] [ system/options/quiet: quiet if not sub-process [ already-running: false if not trust-all [ protect-system ] ] ] quiet: system/options/quiet if not block? modules [ modules: load remold [ to-file modules ] ] foreach mod modules [ redefined: copy [] ; FIXME: problem with %../path/whereever if block? mod [ break ] ; it's not interesting, if it's a block mod: be-word mod if any [ not found? find module-list mod force ] [ catch [ foreach path search-paths [ file: to-file join path mod if exists? file [ message rejoin ["^/YAMM -- Found: " file] ;;;; if error? err: try [ do file true] [ if error? err: try [ locate-error file true] [ err: disarm err switch/default err/code [ 325 [ if any [ trust-all trust confirm rejoin [ {^/Script "} mod {" tries to redefine "} err/arg1 {"^/unprotect and retry? (y/N) }] ] [ unprotect err/arg1 append redefined err/arg1 system/options/quiet: true either trust [ import/force/trust mod ][ import/force mod ] throw ] exit ] ] [ either inspect-error [ print rejoin [ "Error in: " file ] if confirm "do you want to inspect? (y/N)" [ print mold err ] ] [ message rejoin [ "Error in: " file ] ] ] if 'View = system/product [unview/all] exit ] add-module be-word mod throw ] ] print rejoin ["^/YAMM -- " file " not found in search-path"] ; you always want an error ] ] ] exit ] ] ; yamm run: func [ "Run files, found in the path" 'file "file to run, given as a word" /trust "Allow overriding of words" ] [ either trust [ yamm/import/force/trust file ] [ yamm/import/force file ] ] yamm/module/words 'yamm.r [yamm run be-word unset!]