src/Pure/System/build.scala
author wenzelm
Wed, 18 Jul 2012 16:24:16 +0200
changeset 49351 3c55bfad22eb
parent 49350 2f923e994056
child 49352 9c7f8e5805b4
permissions -rw-r--r--
more tight treatment of reset_name;
     1 /*  Title:      Pure/System/build.scala
     2     Author:     Makarius
     3 
     4 Build and manage Isabelle sessions.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.io.File
    11 
    12 
    13 object Build
    14 {
    15   /* command line entry point */
    16 
    17   private object Bool
    18   {
    19     def unapply(s: String): Option[Boolean] =
    20       s match {
    21         case "true" => Some(true)
    22         case "false" => Some(false)
    23         case _ => None
    24       }
    25   }
    26 
    27   def main(args: Array[String])
    28   {
    29     def bad_args(): Nothing = error("Bad arguments: " + args.toString)
    30 
    31     val rc =
    32       try {
    33         args.toList match {
    34           case Bool(all_sessions) :: Bool(build_images) :: Bool(list_only) :: rest =>
    35             rest.indexWhere(_ == "\n") match {
    36               case -1 => bad_args()
    37               case i =>
    38                 val (options, rest1) = rest.splitAt(i)
    39                 val sessions = rest1.tail
    40                 build(all_sessions, build_images, list_only, options, sessions)
    41             }
    42           case _ => bad_args()
    43         }
    44       }
    45       catch {
    46         case exn: Throwable => java.lang.System.err.println(Exn.message(exn)); 2
    47       }
    48 
    49     sys.exit(rc)
    50   }
    51 
    52 
    53   /* build */
    54 
    55   def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
    56     options: List[String], sessions: List[String]): Int =
    57   {
    58     println("options = " + options.toString)
    59     println("sessions = " + sessions.toString)
    60 
    61     find_sessions() foreach println
    62 
    63     0
    64   }
    65 
    66 
    67   /* session information */
    68 
    69   val ROOT_NAME = "ROOT"
    70 
    71   type Options = List[(String, Option[String])]
    72 
    73   case class Session_Info(
    74     dir: Path,
    75     name: String,
    76     reset_name: Boolean,
    77     parent: String,
    78     description: String,
    79     options: Options,
    80     theories: List[(Options, String)],
    81     files: List[String])
    82 
    83   private object Parser extends Parse.Parser
    84   {
    85     val SESSION = "session"
    86     val IN = "in"
    87     val DESCRIPTION = "description"
    88     val OPTIONS = "options"
    89     val THEORIES = "theories"
    90     val FILES = "files"
    91 
    92     val syntax =
    93       Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
    94         SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
    95 
    96     def session_info(dir: Path): Parser[Session_Info] =
    97     {
    98       val session_name = atom("session name", _.is_name)
    99       val theory_name = atom("theory name", _.is_name)
   100 
   101       val option =
   102         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   103       val options: Parser[Options] =
   104         keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
   105 
   106       val theories =
   107         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
   108           { case _ ~ (x ~ y) => (x, y) }
   109 
   110       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
   111         (keyword("!") ^^^ true | success(false)) ~
   112         (opt(keyword(IN) ~! string ^^ { case _ ~ x => Path.explode(x) })) ~
   113         (keyword("=") ~> session_name <~ keyword("+")) ~
   114         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
   115         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
   116         rep(theories) ~
   117         (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
   118           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h =>
   119               val dir1 = dir + c.getOrElse(Path.basic(a))
   120               val thys = g.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten
   121               Session_Info(dir1, a, b, d, e, f, thys, h) }
   122     }
   123 
   124     def parse_entries(dir: Path, root: File): List[Session_Info] =
   125     {
   126       val toks = syntax.scan(Standard_System.read_file(root))
   127       parse_all(rep(session_info(dir)), Token.reader(toks, root.toString)) match {
   128         case Success(result, _) => result
   129         case bad => error(bad.toString)
   130       }
   131     }
   132   }
   133 
   134   def find_sessions(): List[Session_Info] =
   135   {
   136     for {
   137       dir <- Isabelle_System.components()
   138       root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
   139       if root.isFile
   140       entry <- Parser.parse_entries(dir, root)
   141     } yield entry
   142   }
   143 }
   144