logo

class

build::FanScript

sys::Obj
  build::Task
    build::FanScript
  1  //
  2  // Copyright (c) 2006, Brian Frank and Andy Frank
  3  // Licensed under the Academic Free License version 3.0
  4  //
  5  // History:
  6  //   4 Nov 06  Brian Frank  Creation
  7  //
  8  
  9  using compiler
 10  
 11  **
 12  ** FanScript is used to compiler a Fan script into
 13  ** memory and run it via reflection.
 14  **
 15  class FanScript : Task
 16  {
 17    new make(BuildScript script, File file, Obj[] args := null)
 18      : super(script)
 19    {
 20      this.file = file
 21      this.args = args
 22    }
 23  
 24    Pod compile()
 25    {
 26      try
 27      {
 28        // TODO - this is a temp hack to compile a script
 29        podName := file.uri.toStr.replace("/", "_").replace(":", "").replace(".", "_")
 30        input := CompilerInput.make
 31        input.podName    = podName
 32        input.log.level  = LogLevel.error // TODO - use my own log
 33        input.isScript   = true
 34        input.mode       = CompilerInputMode.str
 35        input.srcStr     = file.readAllStr
 36        input.srcStrLocation = Location.makeFile(file)
 37        input.output     = CompilerOutputMode.transientPod
 38        return Compiler.make(input).compile.transientPod
 39      }
 40      catch (CompilerErr err)
 41      {
 42        // all errors should already be logged by Compiler
 43        throw FatalBuildErr.make
 44      }
 45      catch (Err err)
 46      {
 47        err.trace
 48        throw fatal("Cannot load script [$file]")
 49      }
 50    }
 51  
 52    override Void run()
 53    {
 54      // run main on first type with specified args
 55      t := compile.types.first
 56      main := t.method("main")
 57      if (main.isStatic)
 58        main.call(args)
 59      else
 60        main.callOn(t.make, args)
 61    }
 62  
 63    File file
 64    Obj[] args
 65  }

More Info