src/Pure/Tools/imports.scala
author wenzelm
Thu, 12 Oct 2017 11:25:06 +0200
changeset 66848 982baed14542
parent 66780 bf54ca580bf2
child 66851 c75769065548
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
     1
/*  Title:      Pure/Tools/imports.scala
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     3
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
     4
Maintain theory imports wrt. session structure.
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     5
*/
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     6
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     7
package isabelle
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     8
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
     9
65526
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    10
import java.io.{File => JFile}
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    11
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    12
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
    13
object Imports
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
    14
{
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
    15
  /* repository files */
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    16
65830
064925cb656f prefer explicit progress channel;
wenzelm
parents: 65824
diff changeset
    17
  def repository_files(progress: Progress, start: Path, pred: JFile => Boolean = _ => true)
064925cb656f prefer explicit progress channel;
wenzelm
parents: 65824
diff changeset
    18
      : List[JFile] =
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    19
    Mercurial.find_repository(start) match {
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    20
      case None =>
65830
064925cb656f prefer explicit progress channel;
wenzelm
parents: 65824
diff changeset
    21
        progress.echo_warning("Ignoring directory " + start + " (no Mercurial repository)")
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    22
        Nil
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    23
      case Some(hg) =>
65833
95fd3b9888e6 tuned signature;
wenzelm
parents: 65830
diff changeset
    24
        val start_path = start.canonical_file.toPath
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    25
        for {
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
    26
          name <- hg.known_files()
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    27
          file = (hg.root + Path.explode(name)).file
66234
836898197296 clarified platform file operations;
wenzelm
parents: 66033
diff changeset
    28
          if pred(file) && File.canonical(file).toPath.startsWith(start_path)
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    29
        } yield file
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    30
    }
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    31
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    32
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
    33
  /* update imports */
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
    34
65526
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    35
  sealed case class Update(range: Text.Range, old_text: String, new_text: String)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    36
  {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    37
    def edits: List[Text.Edit] =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    38
      Text.Edit.replace(range.start, old_text, new_text)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    39
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    40
    override def toString: String =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    41
      range.toString + ": " + old_text + " -> " + new_text
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    42
  }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    43
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    44
  def update_name(keywords: Keyword.Keywords, pos: Position.T, update: String => String)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    45
    : Option[(JFile, Update)] =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    46
  {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    47
    val file =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    48
      pos match {
65833
95fd3b9888e6 tuned signature;
wenzelm
parents: 65830
diff changeset
    49
        case Position.File(file) => Path.explode(file).canonical_file
65526
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    50
        case _ => error("Missing file in position" + Position.here(pos))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    51
      }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    52
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    53
    val text = File.read(file)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    54
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    55
    val range =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    56
      pos match {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    57
        case Position.Range(symbol_range) => Symbol.Text_Chunk(text).decode(symbol_range)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    58
        case _ => error("Missing range in position" + Position.here(pos))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    59
      }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    60
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    61
    Token.read_name(keywords, range.substring(text)) match {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    62
      case Some(tok) =>
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    63
        val s1 = tok.source
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    64
        val s2 = Token.quote_name(keywords, update(tok.content))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    65
        if (s1 == s2) None else Some((file, Update(range, s1, s2)))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    66
      case None => error("Name token expected" + Position.here(pos))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    67
    }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    68
  }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    69
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    70
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    71
  /* collective operations */
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    72
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    73
  def imports(
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    74
    options: Options,
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    75
    operation_imports: Boolean = false,
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
    76
    operation_repository_files: Boolean = false,
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    77
    operation_update: Boolean = false,
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
    78
    update_message: String = "",
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    79
    progress: Progress = No_Progress,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    80
    selection: Sessions.Selection = Sessions.Selection.empty,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    81
    dirs: List[Path] = Nil,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    82
    select_dirs: List[Path] = Nil,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    83
    verbose: Boolean = false) =
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    84
  {
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    85
    val full_sessions = Sessions.load(options, dirs, select_dirs)
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    86
    val (selected, selected_sessions) = full_sessions.selection(selection)
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    87
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    88
    val deps =
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    89
      Sessions.deps(selected_sessions, progress = progress, verbose = verbose,
66575
191048506504 clarified signature: provide all_known information uniformly (it is subject to Sessions.T selection);
wenzelm
parents: 66571
diff changeset
    90
        global_theories = full_sessions.global_theories).check_errors
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    91
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    92
    val root_keywords = Sessions.root_syntax.keywords
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    93
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    94
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    95
    if (operation_imports) {
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    96
      progress.echo("\nPotential session imports:")
66033
e4a8e1e20d45 clarified output;
wenzelm
parents: 66030
diff changeset
    97
      selected.flatMap(session_name =>
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    98
      {
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    99
        val info = full_sessions(session_name)
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   100
        val session_resources = new Resources(deps(session_name))
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   101
66848
982baed14542 clarified signature;
wenzelm
parents: 66780
diff changeset
   102
        val declared_imports = full_sessions.imports_requirements(List(session_name)).toSet
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   103
        val extra_imports =
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   104
          (for {
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   105
            (_, a) <- session_resources.session_base.known.theories.iterator
66780
bf54ca580bf2 theory qualifier is always session name (see also 31e8a86971a8);
wenzelm
parents: 66767
diff changeset
   106
            if session_resources.theory_qualifier(a) == info.name
66716
8737b866bd1c tuned signature;
wenzelm
parents: 66671
diff changeset
   107
            b <- deps.all_known.get_file(a.path.file)
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   108
            qualifier = session_resources.theory_qualifier(b)
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   109
            if !declared_imports.contains(qualifier)
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   110
          } yield qualifier).toSet
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   111
66033
e4a8e1e20d45 clarified output;
wenzelm
parents: 66030
diff changeset
   112
        if (extra_imports.isEmpty) None
e4a8e1e20d45 clarified output;
wenzelm
parents: 66030
diff changeset
   113
        else Some((session_name, extra_imports.toList.sorted, declared_imports.size))
e4a8e1e20d45 clarified output;
wenzelm
parents: 66030
diff changeset
   114
      }).sortBy(_._3).foreach({ case (session_name, extra_imports, _) =>
e4a8e1e20d45 clarified output;
wenzelm
parents: 66030
diff changeset
   115
        progress.echo("session " + Token.quote_name(root_keywords, session_name) + ": " +
e4a8e1e20d45 clarified output;
wenzelm
parents: 66030
diff changeset
   116
          extra_imports.map(Token.quote_name(root_keywords, _)).mkString(" "))
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   117
      })
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   118
    }
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   119
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   120
    if (operation_repository_files) {
66671
41b64e53b6a1 more documentation;
wenzelm
parents: 66575
diff changeset
   121
      progress.echo("\nMercurial repository check:")
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   122
      val unused_files =
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   123
        for {
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   124
          (_, dir) <- Sessions.directories(dirs, select_dirs)
65830
064925cb656f prefer explicit progress channel;
wenzelm
parents: 65824
diff changeset
   125
          file <- repository_files(progress, dir, file => file.getName.endsWith(".thy"))
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   126
          if deps.all_known.get_file(file).isEmpty
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   127
        } yield file
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   128
      unused_files.foreach(file => progress.echo("unused file " + quote(file.toString)))
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   129
    }
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   130
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   131
    if (operation_update) {
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   132
      progress.echo("\nUpdate theory imports" + update_message + ":")
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   133
      val updates =
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   134
        selected.flatMap(session_name =>
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   135
        {
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   136
          val info = full_sessions(session_name)
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   137
          val session_base = deps(session_name)
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   138
          val session_resources = new Resources(session_base)
66028
wenzelm
parents: 66025
diff changeset
   139
          val imports_base = session_base.get_imports
wenzelm
parents: 66025
diff changeset
   140
          val imports_resources = new Resources(imports_base)
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   141
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   142
          def standard_import(qualifier: String, dir: String, s: String): String =
66767
294c2e9a689e clarified modules;
wenzelm
parents: 66737
diff changeset
   143
            imports_resources.standard_import(session_resources, qualifier, dir, s)
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   144
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   145
          val updates_root =
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   146
            for {
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   147
              (_, pos) <- info.theories.flatMap(_._2)
66780
bf54ca580bf2 theory qualifier is always session name (see also 31e8a86971a8);
wenzelm
parents: 66767
diff changeset
   148
              upd <- update_name(root_keywords, pos, standard_import(info.name, info.dir.implode, _))
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   149
            } yield upd
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   150
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   151
          val updates_theories =
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   152
            for {
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   153
              (_, name) <- session_base.known.theories_local.toList
66780
bf54ca580bf2 theory qualifier is always session name (see also 31e8a86971a8);
wenzelm
parents: 66767
diff changeset
   154
              if session_resources.theory_qualifier(name) == info.name
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   155
              (_, pos) <- session_resources.check_thy(name, Token.Pos.file(name.node)).imports
66720
b07192253605 tuned signature;
wenzelm
parents: 66716
diff changeset
   156
              upd <- update_name(session_base.overall_syntax.keywords, pos,
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   157
                standard_import(session_resources.theory_qualifier(name), name.master_dir, _))
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   158
            } yield upd
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   159
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   160
          updates_root ::: updates_theories
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   161
        })
65564
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   162
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   163
      val file_updates = (Multi_Map.empty[JFile, Update] /: updates)(_ + _)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   164
      val conflicts =
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   165
        file_updates.iterator_list.flatMap({ case (file, upds) =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   166
          Library.duplicates(upds.sortBy(_.range.start),
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   167
            (x: Update, y: Update) => x.range overlaps y.range) match
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   168
          {
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   169
            case Nil => None
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   170
            case bad => Some((file, bad))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   171
          }
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   172
        })
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   173
      if (conflicts.nonEmpty)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   174
        error(cat_lines(
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   175
          conflicts.map({ case (file, bad) =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   176
            "Conflicting updates for file " + file + bad.mkString("\n  ", "\n  ", "") })))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   177
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   178
      for ((file, upds) <- file_updates.iterator_list.toList.sortBy(p => p._1.toString)) {
65564
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   179
        progress.echo("file " + quote(file.toString))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   180
        val edits =
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   181
          upds.sortBy(upd => - upd.range.start).flatMap(upd =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   182
            Text.Edit.replace(upd.range.start, upd.old_text, upd.new_text))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   183
        val new_text =
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   184
          (File.read(file) /: edits)({ case (text, edit) =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   185
            edit.edit(text, 0) match {
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   186
              case (None, text1) => text1
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   187
              case (Some(_), _) => error("Failed to apply edit " + edit + " to file " + file)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   188
            }
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   189
          })
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   190
        File.write_backup2(Path.explode(File.standard_path(file)), new_text)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   191
      }
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   192
    }
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   193
  }
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   194
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   195
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   196
  /* Isabelle tool wrapper */
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   197
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   198
  val isabelle_tool =
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
   199
    Isabelle_Tool("imports", "maintain theory imports wrt. session structure", args =>
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   200
    {
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   201
      var base_sessions: List[String] = Nil
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   202
      var select_dirs: List[Path] = Nil
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   203
      var operation_imports = false
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   204
      var operation_repository_files = false
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   205
      var requirements = false
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   206
      var operation_update = false
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   207
      var exclude_session_groups: List[String] = Nil
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   208
      var all_sessions = false
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   209
      var dirs: List[Path] = Nil
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   210
      var session_groups: List[String] = Nil
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   211
      var incremental_update = false
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   212
      var options = Options.init()
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   213
      var verbose = false
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   214
      var exclude_sessions: List[String] = Nil
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   215
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   216
      val getopts = Getopts("""
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
   217
Usage: isabelle imports [OPTIONS] [SESSIONS ...]
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   218
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   219
  Options are:
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   220
    -B NAME      include session NAME and all descendants
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   221
    -D DIR       include session directory and select its sessions
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   222
    -I           operation: report potential session imports
66671
41b64e53b6a1 more documentation;
wenzelm
parents: 66575
diff changeset
   223
    -M           operation: Mercurial repository check for theory files
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   224
    -R           operate on requirements of selected sessions
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   225
    -U           operation: update theory imports to use session qualifiers
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   226
    -X NAME      exclude sessions from group NAME and all descendants
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   227
    -a           select all sessions
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   228
    -d DIR       include session directory
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   229
    -g NAME      select session group NAME
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   230
    -i           incremental update according to session graph structure
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   231
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   232
    -v           verbose
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   233
    -x NAME      exclude session NAME and all descendants
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   234
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   235
  Maintain theory imports wrt. session structure. At least one operation
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   236
  needs to be specified (see options -I -M -U).
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   237
""",
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   238
      "B:" -> (arg => base_sessions = base_sessions ::: List(arg)),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   239
      "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   240
      "I" -> (_ => operation_imports = true),
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   241
      "M" -> (_ => operation_repository_files = true),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   242
      "R" -> (_ => requirements = true),
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   243
      "U" -> (_ => operation_update = true),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   244
      "X:" -> (arg => exclude_session_groups = exclude_session_groups ::: List(arg)),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   245
      "a" -> (_ => all_sessions = true),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   246
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   247
      "g:" -> (arg => session_groups = session_groups ::: List(arg)),
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   248
      "i" -> (_ => incremental_update = true),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   249
      "o:" -> (arg => options = options + arg),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   250
      "v" -> (_ => verbose = true),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   251
      "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   252
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   253
      val sessions = getopts(args)
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   254
      if (args.isEmpty || !(operation_imports || operation_repository_files || operation_update))
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   255
        getopts.usage()
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   256
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   257
      val selection =
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   258
        Sessions.Selection(requirements, all_sessions, base_sessions, exclude_session_groups,
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   259
          exclude_sessions, session_groups, sessions)
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   260
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   261
      val progress = new Console_Progress(verbose = verbose)
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   262
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   263
      if (operation_imports || operation_repository_files ||
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   264
          operation_update && !incremental_update)
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   265
      {
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   266
        imports(options, operation_imports = operation_imports,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   267
          operation_repository_files = operation_repository_files,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   268
          operation_update = operation_update,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   269
          progress = progress, selection = selection, dirs = dirs, select_dirs = select_dirs,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   270
          verbose = verbose)
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   271
      }
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   272
      else if (operation_update && incremental_update) {
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   273
        val (selected, selected_sessions) =
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   274
          Sessions.load(options, dirs, select_dirs).selection(selection)
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   275
        selected_sessions.imports_topological_order.foreach(info =>
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   276
        {
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   277
          imports(options, operation_update = operation_update, progress = progress,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   278
            update_message = " for session " + quote(info.name),
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   279
            selection = Sessions.Selection(sessions = List(info.name)),
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   280
            dirs = dirs ::: select_dirs, verbose = verbose)
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   281
        })
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   282
      }
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   283
    })
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   284
}