src/Pure/System/build.scala
author wenzelm
Wed, 18 Jul 2012 19:47:10 +0200
changeset 49355 6f4fc030882a
parent 49354 62570361e608
child 49356 752de4e10162
permissions -rw-r--r--
allow explicit specification of additional session directories;
     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 import scala.collection.mutable
    13 import scala.annotation.tailrec
    14 
    15 
    16 object Build
    17 {
    18   /* command line entry point */
    19 
    20   private object Bool
    21   {
    22     def unapply(s: String): Option[Boolean] =
    23       s match {
    24         case "true" => Some(true)
    25         case "false" => Some(false)
    26         case _ => None
    27       }
    28   }
    29 
    30   private object Chunks
    31   {
    32     private def chunks(list: List[String]): List[List[String]] =
    33       list.indexWhere(_ == "\n") match {
    34         case -1 => List(list)
    35         case i =>
    36           val (chunk, rest) = list.splitAt(i)
    37           chunk :: chunks(rest.tail)
    38       }
    39     def unapplySeq(list: List[String]): Option[List[List[String]]] = Some(chunks(list))
    40   }
    41 
    42   def main(args: Array[String])
    43   {
    44     val rc =
    45       try {
    46         args.toList match {
    47           case Bool(all_sessions) :: Bool(build_images) :: Bool(list_only) ::
    48             Chunks(more_dirs, options, sessions) =>
    49               build(all_sessions, build_images, list_only,
    50                 more_dirs.map(Path.explode), options, sessions)
    51           case _ => error("Bad arguments:\n" + cat_lines(args))
    52         }
    53       }
    54       catch {
    55         case exn: Throwable => java.lang.System.err.println(Exn.message(exn)); 2
    56       }
    57     sys.exit(rc)
    58   }
    59 
    60 
    61   /* build */
    62 
    63   def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
    64     more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
    65   {
    66     println("more_dirs = " + more_dirs.toString)
    67     println("options = " + options.toString)
    68     println("sessions = " + sessions.toString)
    69 
    70     find_sessions(more_dirs) foreach println
    71 
    72     0
    73   }
    74 
    75 
    76   /** session information **/
    77 
    78   type Options = List[(String, Option[String])]
    79 
    80   case class Session_Info(
    81     dir: Path,
    82     name: String,
    83     parent: String,
    84     description: String,
    85     options: Options,
    86     theories: List[(Options, String)],
    87     files: List[String])
    88 
    89   private val pure_info =
    90     Session_Info(Path.current, "Pure", "", "", Nil, List((Nil, "Pure")), Nil)
    91 
    92 
    93   /* parsing */
    94 
    95   val ROOT_NAME = "ROOT"
    96 
    97   private case class Session_Entry(
    98     name: String,
    99     reset: Boolean,
   100     path: Option[String],
   101     parent: String,
   102     description: String,
   103     options: Options,
   104     theories: List[(Options, List[String])],
   105     files: List[String])
   106 
   107   private object Parser extends Parse.Parser
   108   {
   109     val SESSION = "session"
   110     val IN = "in"
   111     val DESCRIPTION = "description"
   112     val OPTIONS = "options"
   113     val THEORIES = "theories"
   114     val FILES = "files"
   115 
   116     val syntax =
   117       Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
   118         SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
   119 
   120     val session_entry: Parser[Session_Entry] =
   121     {
   122       val session_name = atom("session name", _.is_name)
   123       val theory_name = atom("theory name", _.is_name)
   124 
   125       val option =
   126         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   127       val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
   128 
   129       val theories =
   130         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
   131           { case _ ~ (x ~ y) => (x, y) }
   132 
   133       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
   134         (keyword("!") ^^^ true | success(false)) ~
   135         (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
   136         (keyword("=") ~> session_name <~ keyword("+")) ~
   137         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
   138         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
   139         rep1(theories) ~
   140         (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
   141           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h => Session_Entry(a, b, c, d, e, f, g, h) }
   142     }
   143 
   144     def parse_entries(root: File): List[Session_Entry] =
   145     {
   146       val toks = syntax.scan(Standard_System.read_file(root))
   147       parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
   148         case Success(result, _) => result
   149         case bad => error(bad.toString)
   150       }
   151     }
   152   }
   153 
   154 
   155   /* find session */
   156 
   157   def find_sessions(more_dirs: List[Path]): List[Session_Info] =
   158   {
   159     val infos = new mutable.ListBuffer[Session_Info]
   160     infos += pure_info
   161 
   162     for {
   163       (dir, strict) <- Isabelle_System.components().map((_, false)) ++ more_dirs.map((_, true))
   164       root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
   165       _ = (strict && !root.isFile && error("Bad session root file: " + quote(root.toString)))
   166       if root.isFile
   167       entry <- Parser.parse_entries(root)
   168     }
   169     {
   170       try {
   171         val parent =
   172           infos.find(_.name == entry.parent) getOrElse
   173            error("Unknown parent session: " + quote(entry.parent))
   174         val full_name =
   175           if (entry.reset) entry.name
   176           else parent.name + "-" + entry.name
   177 
   178         if (entry.name == "") error("Bad session name")
   179         if (infos.exists(_.name == full_name))
   180           error("Duplicate session name: " + quote(full_name))
   181 
   182         val path =
   183           entry.path match {
   184             case Some(p) => Path.explode(p)
   185             case None => Path.basic(entry.name)
   186           }
   187         val thys = entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten
   188         val info =
   189           Session_Info(dir + path, full_name, entry.parent, entry.description,
   190             entry.options, thys, entry.files)
   191 
   192         infos += info
   193       }
   194       catch {
   195         case ERROR(msg) =>
   196           error(msg + "\nThe error(s) above occurred in session entry " +
   197             quote(entry.name) + " (file " + quote(root.toString) + ")")
   198       }
   199     }
   200     infos.toList
   201   }
   202 }
   203