src/Pure/Tools/update.scala
author wenzelm
Mon, 14 Oct 2019 19:58:38 +0200
changeset 70863 d1299774543d
parent 70856 545229df2f82
child 70864 e94fec16bf50
permissions -rw-r--r--
clarified "isabelle update" options -- more like "isabelle dump";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/update.scala
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     3
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     4
Update theory sources based on PIDE markup.
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     5
*/
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     6
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     7
package isabelle
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     8
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
     9
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    10
object Update
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    11
{
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    12
  def update(options: Options, logic: String,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    13
    progress: Progress = No_Progress,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    14
    log: Logger = No_Logger,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    15
    dirs: List[Path] = Nil,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    16
    select_dirs: List[Path] = Nil,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    17
    selection: Sessions.Selection = Sessions.Selection.empty)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    18
  {
70856
545229df2f82 clarified signature: static Dump.Context vs. dynamic Dump.Session;
wenzelm
parents: 70640
diff changeset
    19
    val context =
545229df2f82 clarified signature: static Dump.Context vs. dynamic Dump.Session;
wenzelm
parents: 70640
diff changeset
    20
      Dump.Context(options, progress = progress, dirs = dirs, select_dirs = select_dirs,
545229df2f82 clarified signature: static Dump.Context vs. dynamic Dump.Session;
wenzelm
parents: 70640
diff changeset
    21
        selection = selection)
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    22
70856
545229df2f82 clarified signature: static Dump.Context vs. dynamic Dump.Session;
wenzelm
parents: 70640
diff changeset
    23
    val path_cartouches = context.session_options.bool("update_path_cartouches")
69603
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    24
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    25
    def update_xml(xml: XML.Body): XML.Body =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    26
      xml flatMap {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    27
        case XML.Wrapped_Elem(markup, body1, body2) =>
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    28
          if (markup.name == Markup.UPDATE) update_xml(body1) else update_xml(body2)
69603
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    29
        case XML.Elem(Markup.Language(Markup.Language.PATH, _, _, _), body)
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    30
        if path_cartouches =>
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    31
          Token.read_embedded(Keyword.Keywords.empty, XML.content(body)) match {
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    32
            case Some(tok) => List(XML.Text(Symbol.cartouche(tok.content)))
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    33
            case None => update_xml(body)
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    34
          }
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    35
        case XML.Elem(_, body) => update_xml(body)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    36
        case t => List(t)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    37
      }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    38
70856
545229df2f82 clarified signature: static Dump.Context vs. dynamic Dump.Session;
wenzelm
parents: 70640
diff changeset
    39
    context.session(logic, log = log).process((args: Dump.Args) =>
70640
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    40
      {
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    41
        progress.echo("Processing theory " + args.print_node + " ...")
69571
676182f2e375 tuned messages;
wenzelm
parents: 69557
diff changeset
    42
70640
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    43
        val snapshot = args.snapshot
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    44
        for ((node_name, node) <- snapshot.nodes) {
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    45
          val xml =
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    46
            snapshot.state.markup_to_XML(snapshot.version, node_name,
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    47
              Text.Range.full, Markup.Elements(Markup.UPDATE, Markup.LANGUAGE))
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    48
70640
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    49
          val source1 = Symbol.encode(XML.content(update_xml(xml)))
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    50
          if (source1 != Symbol.encode(node.source)) {
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    51
            progress.echo("Updating " + node_name.path)
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    52
            File.write(node_name.path, source1)
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    53
          }
70640
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    54
        }
5f4b8a505090 more explicit type Dump.Session, with context information;
wenzelm
parents: 69896
diff changeset
    55
      })
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    56
  }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    57
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    58
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    59
  /* Isabelle tool wrapper */
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    60
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    61
  val isabelle_tool =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    62
    Isabelle_Tool("update", "update theory sources based on PIDE markup", args =>
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    63
    {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    64
      var base_sessions: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    65
      var select_dirs: List[Path] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    66
      var requirements = false
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    67
      var exclude_session_groups: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    68
      var all_sessions = false
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    69
      var dirs: List[Path] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    70
      var session_groups: List[String] = Nil
70863
d1299774543d clarified "isabelle update" options -- more like "isabelle dump";
wenzelm
parents: 70856
diff changeset
    71
      var logic = Dump.default_logic
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    72
      var options = Options.init()
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    73
      var verbose = false
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    74
      var exclude_sessions: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    75
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    76
      val getopts = Getopts("""
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    77
Usage: isabelle update [OPTIONS] [SESSIONS ...]
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    78
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    79
  Options are:
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    80
    -B NAME      include session NAME and all descendants
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    81
    -D DIR       include session directory and select its sessions
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    82
    -R           operate on requirements of selected sessions
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    83
    -X NAME      exclude sessions from group NAME and all descendants
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    84
    -a           select all sessions
70863
d1299774543d clarified "isabelle update" options -- more like "isabelle dump";
wenzelm
parents: 70856
diff changeset
    85
    -b NAME      base logic image (default """ + isabelle.quote(Dump.default_logic) + """)
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    86
    -d DIR       include session directory
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    87
    -g NAME      select session group NAME
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    88
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    89
    -u OPT       overide update option: shortcut for "-o update_OPT"
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    90
    -v           verbose
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    91
    -x NAME      exclude session NAME and all descendants
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    92
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    93
  Update theory sources based on PIDE markup.
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    94
""",
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    95
      "B:" -> (arg => base_sessions = base_sessions ::: List(arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    96
      "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    97
      "R" -> (_ => requirements = true),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    98
      "X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    99
      "a" -> (_ => all_sessions = true),
70863
d1299774543d clarified "isabelle update" options -- more like "isabelle dump";
wenzelm
parents: 70856
diff changeset
   100
      "b:" -> (arg => logic = arg),
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   101
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   102
      "g:" -> (arg => session_groups = session_groups ::: List(arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   103
      "o:" -> (arg => options = options + arg),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   104
      "u:" -> (arg => options = options + ("update_" + arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   105
      "v" -> (_ => verbose = true),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   106
      "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   107
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   108
      val sessions = getopts(args)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   109
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   110
      val progress = new Console_Progress(verbose = verbose)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   111
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   112
      progress.interrupt_handler {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   113
        update(options, logic,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   114
          progress = progress,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   115
          dirs = dirs,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   116
          select_dirs = select_dirs,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   117
          selection = Sessions.Selection(
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   118
            requirements = requirements,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   119
            all_sessions = all_sessions,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   120
            base_sessions = base_sessions,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   121
            exclude_session_groups = exclude_session_groups,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   122
            exclude_sessions = exclude_sessions,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   123
            session_groups = session_groups,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   124
            sessions = sessions))
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   125
      }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   126
    })
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   127
}