logo

class

docCompiler::Main

sys::Obj
  docCompiler::Main
   1  //
   2  // Copyright (c) 2007, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   5 May 06  Brian Frank  Creation
   7  //
   8  
   9  using compiler
  10  
  11  **
  12  ** Main is the main entry point for the Fan documentation compiler.
  13  **
  14  class Main
  15  {
  16  
  17  //////////////////////////////////////////////////////////////////////////
  18  // Run
  19  //////////////////////////////////////////////////////////////////////////
  20  
  21    **
  22    ** Main entry point for compiler.
  23    **
  24    Int run(Str[] args)
  25    {
  26      t1 := Duration.now
  27      success := true
  28  
  29      // process args
  30      if (!parseArgs(args)) return 0
  31  
  32      // process each directory specified
  33      try
  34      {
  35        pipeline.call([compiler])
  36      }
  37      catch (CompilerErr err)
  38      {
  39        // all errors should already be logged by Compiler
  40        success = false
  41      }
  42      catch (Err err)
  43      {
  44        compiler.log.compilerErr(CompilerErr.make("Internal compiler error", null, err))
  45        err.trace
  46        success = false
  47      }
  48  
  49      t2 := Duration.now
  50      if (success)
  51      {
  52        println("SUCCESS (" + (t2-t1).toMillis + "ms)")
  53        return 0
  54      }
  55      else
  56      {
  57        println("FAILED (" + (t2-t1).toMillis + "ms)")
  58        return -1
  59      }
  60    }
  61  
  62    **
  63    ** Process command line args and return false if we should exit.
  64    **
  65    Bool parseArgs(Str[] args)
  66    {
  67      if (args.isEmpty)
  68      {
  69        help
  70        return false
  71      }
  72  
  73      enoughArgs := false
  74      for (i:=0; i<args.size; ++i)
  75      {
  76        a := args[i]
  77        if (a.isEmpty) continue
  78        if (a == "-help" || a == "-h" || a == "-?")
  79        {
  80          help
  81          return false
  82        }
  83        else if (a == "-version")
  84        {
  85          version
  86          return false
  87        }
  88        else if (a == "-d")
  89        {
  90          if (i+1 >= args.size)
  91          {
  92            println("ERROR: must specified dir with -d option")
  93            return false
  94          }
  95          compiler.outDir = File.make(args[++i].toUri).normalize
  96        }
  97        else if (a == "-v")
  98        {
  99          compiler.log.level = LogLevel.debug
 100        }
 101        else if (a == "-silent")
 102        {
 103          compiler.log.level = LogLevel.silent
 104        }
 105        else if (a == "-topindex")
 106        {
 107          pipeline = &DocCompiler.compileTopIndexToHtml
 108          enoughArgs = true
 109        }
 110        else if (a[0] == '-')
 111        {
 112          println("WARNING: Unknown option " + a)
 113        }
 114        else
 115        {
 116          compiler.pod = Pod.find(a, false)
 117          if (compiler.pod == null)
 118          {
 119            println("ERROR: Pod not found: $a")
 120            return false
 121          }
 122          enoughArgs = true
 123        }
 124      }
 125  
 126      // if no dirs were specified, assume current dir
 127      if (!enoughArgs)
 128      {
 129        println("ERROR: not enough arguments")
 130        help
 131        return false
 132      }
 133  
 134      return true
 135    }
 136  
 137    **
 138    ** Dump help usage.
 139    **
 140    Void help()
 141    {
 142      println("Fan Doc Compiler")
 143      println("Usage:")
 144      println("  docCompiler [options] <podName>")
 145      println("Options:")
 146      println("  -help, -h, -?  print usage help")
 147      println("  -version       print version information")
 148      println("  -d <dir>       output directory for pod file")
 149      println("  -v             verbose mode (more logging)")
 150      println("  -silent        silent mode (no logging)")
 151      println("  -topindex      compile top index")
 152    }
 153  
 154    **
 155    ** Dump version.
 156    **
 157    Void version()
 158    {
 159      println("Fan Doc Compiler")
 160      println("Copyright (c) 2007, Brian Frank and Andy Frank")
 161      println("Licensed under the Academic Free License version 3.0")
 162    }
 163  
 164  //////////////////////////////////////////////////////////////////////////
 165  // Utils
 166  //////////////////////////////////////////////////////////////////////////
 167  
 168    Void println(Obj s)
 169    {
 170      compiler.log.printLine(s)
 171    }
 172  
 173  //////////////////////////////////////////////////////////////////////////
 174  // Main
 175  //////////////////////////////////////////////////////////////////////////
 176  
 177    static Void main()
 178    {
 179      Sys.exit(make.run(Sys.args))
 180    }
 181  
 182  //////////////////////////////////////////////////////////////////////////
 183  // Fields
 184  //////////////////////////////////////////////////////////////////////////
 185  
 186    DocCompiler compiler := DocCompiler.make;
 187    |DocCompiler c| pipeline := &DocCompiler.compilePodToHtml
 188  
 189  }