Zdravim,

Maybe you need something like C/C++ 'include in REBOL. Maybe you've used
'include, 'yamm, 'require, 'evoke... But nothing is perfect. I think that
'require is most advanced example but it misses one VERY CRITICAL FEATURE -
paths to look in for script. So I've implemented it to 'require instead of
writing my own function. It is compatible with the old one except if you
'require script without path it looks for it in known paths. You can add to
them but you cannot save them - you have to edit the file manually. But I've
wrote it in one and half hour so you can call it QUICK-HACK.

I hope you'll find some bugs otherwise I'm perfect and there's no room to
improve myself :-)

Louci se
-- 
---------------------------------------------------------------
Boleslav Brezovsky, NOT know as Kryptoplex, maybe on [EMAIL PROTECTED]
                member of R.U.R (means RobotenUndRebol this week)
www.volny.cz/weirddream               MUSIC&SOUND               listen.to/sintezar   
---------------------------------------------------------------

REBOL [
         Title: "Script Requirements Manager"
         Date: 12-Aug-2000
         File: %require.r  ; Used internally, see source for details.
         Author: ["Brian Hawley"  "REBolek"]
         Email: [[EMAIL PROTECTED] [EMAIL PROTECTED]]
         Version: 0.1.1
         Purpose: "Manages script dependencies and current versions"
         Comment: trim/auto {
                Require manages script dependencies in a way similar to
                C/C++ #include but limiting inclusion to only a single time,
                or optionally when the script has been modified. The list of
                included scripts will be carried over if you do or require
                %require.r again.
                This script demonstrates a technique for implementing static
                local variables, initialized using compose, and some other
                fun tricks.
                See http://www.bigfoot.com/~brian.hawley/rebol/ for updates.
                ---
                Found it useful bud missed paths to read from. So I've added them.
         }
        History: [
                0.1.1 "REBolek" {My first try to implement paths. They're static->
                        you cannot save them (real 'path can do it!) but you can
                        add to them. At least.}
        ]
        Future: {It doesn't check for version for scripts searched in paths.
                It reads first found script so I'll have to meake some fix or what.}
        Category: [script advanced]
]

unprotect 'require

require: func [
        "Runs a script or scripts if not included yet, or perhaps if new."
        [catch]
        scripts [file! url! block!] "The script(s) required."
        /yet? "Check when a script has been included (none if never)."
        /new "Update if script has changed."
        /list-scripts "Return a copy of the list of included scripts."
        /add-path   "Adds new path to searched paths"
                new-path    "New path to add"
        /local paths script-list stime iref itime e
] compose [
        comment "List of paths to search in"
        paths: [
                system/options/home
                path/daemon ;yes, you need 'path initialized for this.
        ]
        comment "The list of included scripts, modified in place."
        script-list: (
                ; Carry over existing list of included scripts, if any
                if not all [
                        ; Check for an existing list
                        block? require: try [require/list-scripts []]
                        ; Test the integrity of the list
                        even? length? require: head require
                        foreach [f d] require [
                                either all [
                                        any [file? :f url? :f] date? :d
                                ] [true] [break/value false]
                        ]
                ] [require: []]
                ; Add the current script to the list
                use [f d] [
                        f: clean-path system/script/header/file
                        if date? d: try [modified? f] [
                                change any [find/last require f tail require] reduce 
[f d]
                        ]
                ]
                ; Return the list
                reduce [require]
        )
        ; Handle list query operations, if any
        if yet? [
                return either block? scripts [none] [
                        pick any [
                                find/last script-list clean-path scripts tail 
script-list
                        ] 2
                ]
        ]
        ; Deal with the single case
        if not block? scripts [scripts: reduce [scripts]]
        ;I have to find the script in given paths
        foreach script scripts [
                if all [
                        file? script
                        none? find script #"/"      ;pokud je zad�no pouze jm�no 
skriptu (ne cesta)
                ] [
                        foreach path paths [         ;je skript hled�n ve v�ech cest�ch
                                if exists? join path script [                    ; a 
kdy� existuje
                                        replace scripts script join path script      ; 
je nahrazen za skute�nou lokaci
                                        break                                        ; 
po prvn�m n�lezu je konec
                                ]                                         ; cht�lo by 
to naj�t nejnov�j�� verzi.
                        ]
                ]
        ]
        ; Include file/url list
        foreach script scripts [
                if all [
                        if any [file? :script url? :script] [script: clean-path script]
                        date? stime: try [modified? script]
                        any [
                                itime: none
                                none? iref: find/last script-list script
                                not date? itime: pick iref 2
                                all [new stime > itime]
                        ]
                ] [
                        change any [iref tail script-list] reduce [copy script stime]
                        if error? set/any 'e try [do script] [
                                while [iref: find script-list script] [remove/part 
iref 2]
                                if date? itime [append script-list reduce [script 
itime]]
                                throw e
                        ]
                ]
        ]
        if list-scripts [return copy/deep script-list]
        either add-path [append paths new-path] [exit]
]

protect 'require

Reply via email to