src/Pure/Tools/update.scala
author wenzelm
Mon, 11 Mar 2019 16:23:30 +0100
changeset 69896 be7243b97c41
parent 69856 bb41977edb7e
child 70640 5f4b8a505090
permissions -rw-r--r--
tuned signature;
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
  {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    19
    Build.build_logic(options, logic, build_heap = true, progress = progress,
69854
cc0b3e177b49 system option "system_heaps" supersedes various command-line options for "system build mode";
wenzelm
parents: 69603
diff changeset
    20
      dirs = dirs ::: select_dirs, strict = true)
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    21
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    22
    val dump_options = Dump.make_options(options)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    23
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    24
    val deps =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    25
      Dump.dependencies(dump_options, progress = progress,
69856
bb41977edb7e tuned signature;
wenzelm
parents: 69854
diff changeset
    26
        dirs = dirs, select_dirs = select_dirs, selection = selection)
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    27
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    28
    val resources =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    29
      Headless.Resources.make(dump_options, logic, progress = progress, log = log,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    30
        session_dirs = dirs ::: select_dirs,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    31
        include_sessions = deps.sessions_structure.imports_topological_order)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    32
69603
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    33
    val path_cartouches = dump_options.bool("update_path_cartouches")
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
    def update_xml(xml: XML.Body): XML.Body =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    36
      xml flatMap {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    37
        case XML.Wrapped_Elem(markup, body1, body2) =>
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    38
          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
    39
        case XML.Elem(Markup.Language(Markup.Language.PATH, _, _, _), body)
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    40
        if path_cartouches =>
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    41
          Token.read_embedded(Keyword.Keywords.empty, XML.content(body)) match {
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    42
            case Some(tok) => List(XML.Text(Symbol.cartouche(tok.content)))
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    43
            case None => update_xml(body)
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    44
          }
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    45
        case XML.Elem(_, body) => update_xml(body)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    46
        case t => List(t)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    47
      }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    48
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    49
    Dump.session(deps, resources, progress = progress,
69896
be7243b97c41 tuned signature;
wenzelm
parents: 69856
diff changeset
    50
      process_theory = (args: Dump.Args) =>
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    51
        {
69896
be7243b97c41 tuned signature;
wenzelm
parents: 69856
diff changeset
    52
          progress.echo("Processing theory " + args.print_node + " ...")
69571
676182f2e375 tuned messages;
wenzelm
parents: 69557
diff changeset
    53
69896
be7243b97c41 tuned signature;
wenzelm
parents: 69856
diff changeset
    54
          val snapshot = args.snapshot
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    55
          for ((node_name, node) <- snapshot.nodes) {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    56
            val xml =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    57
              snapshot.state.markup_to_XML(snapshot.version, node_name,
69603
67ae2e164c0f support for isabelle update -u path_cartouches;
wenzelm
parents: 69571
diff changeset
    58
                Text.Range.full, Markup.Elements(Markup.UPDATE, Markup.LANGUAGE))
69557
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    59
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    60
            val source1 = Symbol.encode(XML.content(update_xml(xml)))
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    61
            if (source1 != Symbol.encode(node.source)) {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    62
              progress.echo("Updating " + node_name.path)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    63
              File.write(node_name.path, source1)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    64
            }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    65
          }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    66
        })
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    67
  }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    68
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    69
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    70
  /* Isabelle tool wrapper */
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    71
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    72
  val isabelle_tool =
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    73
    Isabelle_Tool("update", "update theory sources based on PIDE markup", args =>
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    74
    {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    75
      var base_sessions: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    76
      var select_dirs: List[Path] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    77
      var requirements = false
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    78
      var exclude_session_groups: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    79
      var all_sessions = false
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    80
      var dirs: List[Path] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    81
      var session_groups: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    82
      var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    83
      var options = Options.init()
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    84
      var verbose = false
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    85
      var exclude_sessions: List[String] = Nil
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    86
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    87
      val getopts = Getopts("""
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    88
Usage: isabelle update [OPTIONS] [SESSIONS ...]
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    89
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    90
  Options are:
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    91
    -B NAME      include session NAME and all descendants
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    92
    -D DIR       include session directory and select its sessions
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    93
    -R           operate on requirements of selected sessions
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    94
    -X NAME      exclude sessions from group NAME and all descendants
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    95
    -a           select all sessions
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    96
    -d DIR       include session directory
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    97
    -g NAME      select session group NAME
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    98
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
    99
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   100
    -u OPT       overide update option: shortcut for "-o update_OPT"
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   101
    -v           verbose
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   102
    -x NAME      exclude session NAME and all descendants
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   103
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   104
  Update theory sources based on PIDE markup.
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   105
""",
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   106
      "B:" -> (arg => base_sessions = base_sessions ::: List(arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   107
      "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   108
      "R" -> (_ => requirements = true),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   109
      "X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   110
      "a" -> (_ => all_sessions = true),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   111
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   112
      "g:" -> (arg => session_groups = session_groups ::: List(arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   113
      "l:" -> (arg => logic = arg),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   114
      "o:" -> (arg => options = options + arg),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   115
      "u:" -> (arg => options = options + ("update_" + arg)),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   116
      "v" -> (_ => verbose = true),
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   117
      "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   118
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   119
      val sessions = getopts(args)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   120
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   121
      val progress = new Console_Progress(verbose = verbose)
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   122
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   123
      progress.interrupt_handler {
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   124
        update(options, logic,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   125
          progress = progress,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   126
          dirs = dirs,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   127
          select_dirs = select_dirs,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   128
          selection = Sessions.Selection(
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   129
            requirements = requirements,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   130
            all_sessions = all_sessions,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   131
            base_sessions = base_sessions,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   132
            exclude_session_groups = exclude_session_groups,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   133
            exclude_sessions = exclude_sessions,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   134
            session_groups = session_groups,
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   135
            sessions = sessions))
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   136
      }
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   137
    })
e72360fef69a update theory sources based on PIDE markup;
wenzelm
parents:
diff changeset
   138
}