sourceafFandocViewer::Main.fan

using flux
using util

// afFandocViewer Announced at http://fantom.org/sidewalk/topic/2116

** Launch [Flux]`http://fantom.org/doc/flux/#overview` from the command line, passing in 
** a pod name, a uri, or a Windows file path. 
** 
** pre>
**   C:\> fan afFandocViewer fandoc://sys
**   C:\> fan afFandocViewer C:\Projects\FandocViewer\pod.fandoc
** <pre
class Main : AbstractMain {

    @Arg { help="pod name, file or url. e.g. afFandocViewer, pod.fandoc, http://www.fantomfactory.org/" }
    Str? file
    
    @Opt { help="Install Fandoc Viewer Flux Tools" } 
    Bool install
    
    @Opt { help="Print version"; aliases=["v"] } 
    Bool version
    
    override Int main(Str[] args := Env.cur.args) {
        argsOk := parseArgs(args)
        if (version)
            echo(Pod.of(this).name + "-" + Pod.of(this).version)
        if (install)
            return InstallScript().main
        if (helpOpt || file == null)
            return usage
        
        startUri := toUri(file)    
        f := FluxFrame()
        f.loadState
        f.hideNavBar
        f.load(startUri)
        f.open
        
        return 0
    }

    override Int run() { -1 }
    
    ** *Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.*
    ** 
    ** > Jamie Zawinski (1997)
    ** 
    ** See `http://regex.info/blog/2006-09-15/247`
    internal static Uri toUri(Str str) {
        if (str == "/" || str == "//")
            return `fandoc://`
        
        typeUri := toType(str, null)
        if (typeUri != null)
            return typeUri
        
        if (str.split('/').size == 2) {
            podName  := str.split('/')[0]
            typeName := str.split('/')[1]
            typeUri   = toType(podName, typeName)
            if (typeUri != null)
                return typeUri
        }

        if (str.split(':').size == 3) {
            podName  := str.split(':')[0]
            typeName := str.split(':')[2]
            typeUri   = toType(podName, typeName)
            if (typeUri != null)
                return typeUri
        }
        
        // check if str is too small to have a scheme
        if (str.size <= 3)
            return str.toUri

        if (str.chars[0].isAlpha && str.chars[1] == ':' && (str.chars[2] == '\\' || str.chars[2] == '/'))
            str = File.os(str).normalize.toStr
        return str.toUri
    }
    
    ** TODO: Move into FandocResource, so all uris benefit.
    private static Uri? toType(Str podName, Str? typeName) {
        // BugFix: Pod.list throws an Err if any pod is invalid (wrong dependencies etc) 
        // this way we don't even load the pod into memory!
        podNameQ := Env.cur().findAllPodNames.find { it.equalsIgnoreCase(podName) }
        if (podNameQ == null)
            return null
        
        if (typeName == null)
            return `fandoc://${podNameQ}`

        typeNameQ := Pod.find(podNameQ).types.find { it.name.equalsIgnoreCase(typeName) }
        if (typeNameQ == null)
            return null
        
        return `fandoc://${podNameQ}/${typeNameQ.name}`
    }
}