src/Pure/Tools/imports.scala
author wenzelm
Fri, 16 Mar 2018 15:43:56 +0100
changeset 67878 15027fb50a0c
parent 67026 687c822ee5e3
child 68204 a554da2811f2
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
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    33
  /* report imports */
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    34
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    35
  sealed case class Report(
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    36
    info: Sessions.Info,
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    37
    declared_imports: Set[String],
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    38
    potential_imports: Option[List[String]],
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    39
    actual_imports: Option[List[String]])
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    40
  {
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    41
    def print(keywords: Keyword.Keywords, result: List[String]): String =
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    42
    {
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    43
      def print_name(name: String): String = Token.quote_name(keywords, name)
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    44
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    45
      "  session " + print_name(info.name) + ": " + result.map(print_name(_)).mkString(" ")
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    46
    }
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    47
  }
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    48
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
    49
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
    50
  /* update imports */
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
    51
65526
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    52
  sealed case class Update(range: Text.Range, old_text: String, new_text: String)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    53
  {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    54
    def edits: List[Text.Edit] =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    55
      Text.Edit.replace(range.start, old_text, new_text)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    56
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    57
    override def toString: String =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    58
      range.toString + ": " + old_text + " -> " + new_text
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
  def update_name(keywords: Keyword.Keywords, pos: Position.T, update: String => String)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    62
    : Option[(JFile, Update)] =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    63
  {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    64
    val file =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    65
      pos match {
65833
95fd3b9888e6 tuned signature;
wenzelm
parents: 65830
diff changeset
    66
        case Position.File(file) => Path.explode(file).canonical_file
65526
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    67
        case _ => error("Missing file in position" + Position.here(pos))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    68
      }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    69
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    70
    val text = File.read(file)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    71
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    72
    val range =
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    73
      pos match {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    74
        case Position.Range(symbol_range) => Symbol.Text_Chunk(text).decode(symbol_range)
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    75
        case _ => error("Missing range in position" + Position.here(pos))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    76
      }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    77
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    78
    Token.read_name(keywords, range.substring(text)) match {
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    79
      case Some(tok) =>
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    80
        val s1 = tok.source
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    81
        val s2 = Token.quote_name(keywords, update(tok.content))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    82
        if (s1 == s2) None else Some((file, Update(range, s1, s2)))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    83
      case None => error("Name token expected" + Position.here(pos))
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    84
    }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    85
  }
41dda3a292e6 actual update_imports operations;
wenzelm
parents: 65518
diff changeset
    86
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    87
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    88
  /* collective operations */
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
    89
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    90
  def imports(
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    91
    options: Options,
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
    92
    operation_imports: Boolean = false,
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
    93
    operation_repository_files: Boolean = false,
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    94
    operation_update: Boolean = false,
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
    95
    update_message: String = "",
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    96
    progress: Progress = No_Progress,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    97
    selection: Sessions.Selection = Sessions.Selection.empty,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    98
    dirs: List[Path] = Nil,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
    99
    select_dirs: List[Path] = Nil,
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   100
    verbose: Boolean = false) =
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   101
  {
67026
687c822ee5e3 tuned signature;
wenzelm
parents: 67025
diff changeset
   102
    val full_sessions = Sessions.load_structure(options, dirs = dirs, select_dirs = select_dirs)
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   103
    val selected_sessions = full_sessions.selection(selection)
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   104
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   105
    val deps =
66962
e1bde71bace6 clarified signature: global_theories is always required;
wenzelm
parents: 66961
diff changeset
   106
      Sessions.deps(selected_sessions, full_sessions.global_theories,
e1bde71bace6 clarified signature: global_theories is always required;
wenzelm
parents: 66961
diff changeset
   107
        progress = progress, verbose = verbose).check_errors
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   108
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   109
    val root_keywords = Sessions.root_syntax.keywords
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   110
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   111
    if (operation_imports) {
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   112
      val report_imports: List[Report] =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   113
        selected_sessions.build_topological_order.map((session_name: String) =>
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   114
          {
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   115
            val info = selected_sessions(session_name)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   116
            val session_base = deps(session_name)
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   117
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   118
            val declared_imports =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   119
              selected_sessions.imports_requirements(List(session_name)).toSet
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   120
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   121
            val extra_imports =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   122
              (for {
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   123
                (_, a) <- session_base.known.theories.iterator
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   124
                if session_base.theory_qualifier(a) == info.name
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   125
                b <- deps.all_known.get_file(a.path.file)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   126
                qualifier = session_base.theory_qualifier(b)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   127
                if !declared_imports.contains(qualifier)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   128
              } yield qualifier).toSet
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   129
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   130
            val loaded_imports =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   131
              selected_sessions.imports_requirements(
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   132
                session_base.loaded_theories.keys.map(a =>
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   133
                  session_base.theory_qualifier(session_base.known.theories(a))))
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   134
              .toSet - session_name
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   135
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   136
            val minimal_imports =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   137
              loaded_imports.filter(s1 =>
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   138
                !loaded_imports.exists(s2 => selected_sessions.imports_graph.is_edge(s1, s2)))
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   139
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   140
            def make_result(set: Set[String]): Option[List[String]] =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   141
              if (set.isEmpty) None
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   142
              else Some(selected_sessions.imports_topological_order.filter(set))
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   143
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   144
            Report(info, declared_imports, make_result(extra_imports),
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   145
              if (loaded_imports == declared_imports - session_name) None
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   146
              else make_result(minimal_imports))
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   147
          })
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   148
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   149
      progress.echo("\nPotential session imports:")
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   150
      for {
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   151
        report <- report_imports.sortBy(_.declared_imports.size)
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   152
        potential_imports <- report.potential_imports
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   153
      } progress.echo(report.print(root_keywords, potential_imports))
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   154
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   155
      progress.echo("\nActual session imports:")
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   156
      for {
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   157
        report <- report_imports
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   158
        actual_imports <- report.actual_imports
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   159
      } progress.echo(report.print(root_keywords, actual_imports))
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   160
    }
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   161
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   162
    if (operation_repository_files) {
66671
41b64e53b6a1 more documentation;
wenzelm
parents: 66575
diff changeset
   163
      progress.echo("\nMercurial repository check:")
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   164
      val unused_files =
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   165
        for {
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   166
          (_, dir) <- Sessions.directories(dirs, select_dirs)
65830
064925cb656f prefer explicit progress channel;
wenzelm
parents: 65824
diff changeset
   167
          file <- repository_files(progress, dir, file => file.getName.endsWith(".thy"))
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   168
          if deps.all_known.get_file(file).isEmpty
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   169
        } yield file
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   170
      unused_files.foreach(file => progress.echo("unused file " + quote(file.toString)))
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   171
    }
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   172
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   173
    if (operation_update) {
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   174
      progress.echo("\nUpdate theory imports" + update_message + ":")
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   175
      val updates =
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   176
        selected_sessions.build_topological_order.flatMap(session_name =>
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   177
          {
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   178
            val info = selected_sessions(session_name)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   179
            val session_base = deps(session_name)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   180
            val session_resources = new Resources(session_base)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   181
            val imports_base = session_base.get_imports
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   182
            val imports_resources = new Resources(imports_base)
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   183
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   184
            def standard_import(qualifier: String, dir: String, s: String): String =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   185
              imports_resources.standard_import(session_base, qualifier, dir, s)
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   186
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   187
            val updates_root =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   188
              for {
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   189
                (_, pos) <- info.theories.flatMap(_._2)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   190
                upd <- update_name(root_keywords, pos, standard_import(info.name, info.dir.implode, _))
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   191
              } yield upd
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   192
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   193
            val updates_theories =
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   194
              for {
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   195
                (_, name) <- session_base.known.theories_local.toList
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   196
                if session_base.theory_qualifier(name) == info.name
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   197
                (_, pos) <- session_resources.check_thy(name, Token.Pos.file(name.node)).imports
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   198
                upd <- update_name(session_base.overall_syntax.keywords, pos,
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   199
                  standard_import(session_base.theory_qualifier(name), name.master_dir, _))
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   200
              } yield upd
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   201
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   202
            updates_root ::: updates_theories
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   203
          })
65564
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   204
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   205
      val file_updates = (Multi_Map.empty[JFile, Update] /: updates)(_ + _)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   206
      val conflicts =
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   207
        file_updates.iterator_list.flatMap({ case (file, upds) =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   208
          Library.duplicates(upds.sortBy(_.range.start),
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   209
            (x: Update, y: Update) => x.range overlaps y.range) match
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   210
          {
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   211
            case Nil => None
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   212
            case bad => Some((file, bad))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   213
          }
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   214
        })
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   215
      if (conflicts.nonEmpty)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   216
        error(cat_lines(
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   217
          conflicts.map({ case (file, bad) =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   218
            "Conflicting updates for file " + file + bad.mkString("\n  ", "\n  ", "") })))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   219
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   220
      for ((file, upds) <- file_updates.iterator_list.toList.sortBy(p => p._1.toString)) {
65564
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   221
        progress.echo("file " + quote(file.toString))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   222
        val edits =
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   223
          upds.sortBy(upd => - upd.range.start).flatMap(upd =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   224
            Text.Edit.replace(upd.range.start, upd.old_text, upd.new_text))
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   225
        val new_text =
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   226
          (File.read(file) /: edits)({ case (text, edit) =>
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   227
            edit.edit(text, 0) match {
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   228
              case (None, text1) => text1
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   229
              case (Some(_), _) => error("Failed to apply edit " + edit + " to file " + file)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   230
            }
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   231
          })
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   232
        File.write_backup2(Path.explode(File.standard_path(file)), new_text)
5b3cae328a94 tuned messages;
wenzelm
parents: 65561
diff changeset
   233
      }
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   234
    }
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   235
  }
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   236
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   237
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   238
  /* Isabelle tool wrapper */
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   239
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   240
  val isabelle_tool =
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
   241
    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
   242
    {
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   243
      var base_sessions: List[String] = Nil
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   244
      var select_dirs: List[Path] = Nil
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   245
      var operation_imports = false
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   246
      var operation_repository_files = false
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   247
      var requirements = false
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   248
      var operation_update = false
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   249
      var exclude_session_groups: List[String] = Nil
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   250
      var all_sessions = false
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   251
      var dirs: List[Path] = Nil
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   252
      var session_groups: List[String] = Nil
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   253
      var incremental_update = false
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   254
      var options = Options.init()
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   255
      var verbose = false
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   256
      var exclude_sessions: List[String] = Nil
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   257
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   258
      val getopts = Getopts("""
65557
29c69a599743 clarified tool name -- more official status;
wenzelm
parents: 65542
diff changeset
   259
Usage: isabelle imports [OPTIONS] [SESSIONS ...]
65518
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
  Options are:
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   262
    -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
   263
    -D DIR       include session directory and select its sessions
66851
c75769065548 more informative Imports.Report with actual session imports (minimized);
wenzelm
parents: 66848
diff changeset
   264
    -I           operation: report session imports
66671
41b64e53b6a1 more documentation;
wenzelm
parents: 66575
diff changeset
   265
    -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
   266
    -R           operate on requirements of selected sessions
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   267
    -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
   268
    -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
   269
    -a           select all sessions
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   270
    -d DIR       include session directory
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   271
    -g NAME      select session group NAME
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   272
    -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
   273
    -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
   274
    -v           verbose
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   275
    -x NAME      exclude session NAME and all descendants
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   276
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   277
  Maintain theory imports wrt. session structure. At least one operation
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   278
  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
   279
""",
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   280
      "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
   281
      "D:" -> (arg => select_dirs = select_dirs ::: List(Path.explode(arg))),
65566
94c514ea2846 support for potential session imports;
wenzelm
parents: 65564
diff changeset
   282
      "I" -> (_ => operation_imports = true),
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   283
      "M" -> (_ => operation_repository_files = true),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   284
      "R" -> (_ => requirements = true),
65558
479406635409 support for multiple operations via options;
wenzelm
parents: 65557
diff changeset
   285
      "U" -> (_ => operation_update = true),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   286
      "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
   287
      "a" -> (_ => all_sessions = true),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   288
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   289
      "g:" -> (arg => session_groups = session_groups ::: List(arg)),
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   290
      "i" -> (_ => incremental_update = true),
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   291
      "o:" -> (arg => options = options + arg),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   292
      "v" -> (_ => verbose = true),
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   293
      "x:" -> (arg => exclude_sessions = exclude_sessions ::: List(arg)))
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   294
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   295
      val sessions = getopts(args)
65824
4ff79bd2b265 clarified: repository files before commit;
wenzelm
parents: 65566
diff changeset
   296
      if (args.isEmpty || !(operation_imports || operation_repository_files || operation_update))
65561
741b1d3930c0 support for Mercurial manifest check;
wenzelm
parents: 65558
diff changeset
   297
        getopts.usage()
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   298
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   299
      val selection =
66737
2edc0c42c883 option -B for "isabelle build" and "isabelle imports";
wenzelm
parents: 66720
diff changeset
   300
        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
   301
          exclude_sessions, session_groups, sessions)
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   302
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   303
      val progress = new Console_Progress(verbose = verbose)
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   304
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   305
      if (operation_imports || operation_repository_files ||
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   306
          operation_update && !incremental_update)
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   307
      {
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   308
        imports(options, operation_imports = operation_imports,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   309
          operation_repository_files = operation_repository_files,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   310
          operation_update = operation_update,
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   311
          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
   312
          verbose = verbose)
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   313
      }
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   314
      else if (operation_update && incremental_update) {
67026
687c822ee5e3 tuned signature;
wenzelm
parents: 67025
diff changeset
   315
        Sessions.load_structure(options, dirs = dirs, select_dirs = select_dirs).
687c822ee5e3 tuned signature;
wenzelm
parents: 67025
diff changeset
   316
          selection(selection).imports_topological_order.foreach(name =>
67025
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   317
            {
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   318
              imports(options, operation_update = operation_update, progress = progress,
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   319
                update_message = " for session " + quote(name),
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   320
                selection = Sessions.Selection(sessions = List(name)),
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   321
                dirs = dirs ::: select_dirs, verbose = verbose)
961285f581e6 clarifified selection: always wrt. build_graph structure;
wenzelm
parents: 67023
diff changeset
   322
            })
66449
1be102db1598 support for incremental update according to session graph structure;
wenzelm
parents: 66234
diff changeset
   323
      }
65518
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   324
    })
bc8fa59211b7 wrapper for "isabelle update_imports" with selection options like "isabelle build";
wenzelm
parents:
diff changeset
   325
}