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