1 /* Title: Pure/System/build.scala
4 Build and manage Isabelle sessions.
12 import scala.collection.mutable
13 import scala.annotation.tailrec
18 /* command line entry point */
22 def unapply(s: String): Option[Boolean] =
24 case "true" => Some(true)
25 case "false" => Some(false)
32 private def chunks(list: List[String]): List[List[String]] =
33 list.indexWhere(_ == "\n") match {
36 val (chunk, rest) = list.splitAt(i)
37 chunk :: chunks(rest.tail)
39 def unapplySeq(list: List[String]): Option[List[List[String]]] = Some(chunks(list))
42 def main(args: Array[String])
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))
55 case exn: Throwable => java.lang.System.err.println(Exn.message(exn)); 2
63 def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
64 more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
66 println("more_dirs = " + more_dirs.toString)
67 println("options = " + options.toString)
68 println("sessions = " + sessions.toString)
70 find_sessions(more_dirs) foreach println
76 /** session information **/
78 type Options = List[(String, Option[String])]
80 case class Session_Info(
86 theories: List[(Options, String)],
89 private val pure_info =
90 Session_Info(Path.current, "Pure", "", "", Nil, List((Nil, "Pure")), Nil)
95 val ROOT_NAME = "ROOT"
97 private case class Session_Entry(
100 path: Option[String],
104 theories: List[(Options, List[String])],
107 private object Parser extends Parse.Parser
109 val SESSION = "session"
111 val DESCRIPTION = "description"
112 val OPTIONS = "options"
113 val THEORIES = "theories"
117 Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
118 SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
120 val session_entry: Parser[Session_Entry] =
122 val session_name = atom("session name", _.is_name)
123 val theory_name = atom("theory name", _.is_name)
126 name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
127 val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
130 keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
131 { case _ ~ (x ~ y) => (x, y) }
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)) ~
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) }
144 def parse_entries(root: File): List[Session_Entry] =
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)
157 def find_sessions(more_dirs: List[Path]): List[Session_Info] =
159 val infos = new mutable.ListBuffer[Session_Info]
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)))
167 entry <- Parser.parse_entries(root)
172 infos.find(_.name == entry.parent) getOrElse
173 error("Unknown parent session: " + quote(entry.parent))
175 if (entry.reset) entry.name
176 else parent.name + "-" + entry.name
178 if (entry.name == "") error("Bad session name")
179 if (infos.exists(_.name == full_name))
180 error("Duplicate session name: " + quote(full_name))
184 case Some(p) => Path.explode(p)
185 case None => Path.basic(entry.name)
187 val thys = entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten
189 Session_Info(dir + path, full_name, entry.parent, entry.description,
190 entry.options, thys, entry.files)
196 error(msg + "\nThe error(s) above occurred in session entry " +
197 quote(entry.name) + " (file " + quote(root.toString) + ")")