src/Pure/Tools/update_then.scala
author wenzelm
Mon, 27 Feb 2023 15:09:59 +0100
changeset 77398 19e9cafaafc5
parent 75906 2167b9e3157a
permissions -rw-r--r--
clarified signature: works for general Build_Job;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61216
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/update_then.scala
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     3
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     4
Expand old Isar command conflations 'hence' and 'thus'.
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     5
*/
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     6
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     7
package isabelle
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     8
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    10
object Update_Then {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    11
  def update_then(path: Path): Unit = {
61216
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    12
    val text0 = File.read(path)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    13
    val text1 =
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    14
      (for (tok <- Token.explode(Keyword.Keywords.empty, text0).iterator)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    15
        yield {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    16
          tok.source match {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    17
            case "hence" => "then have"
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    18
            case "thus" => "then show"
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    19
            case s => s
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    20
        } }).mkString
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    21
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    22
    if (text0 != text1) {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    23
      Output.writeln("changing " + path)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    24
      File.write_backup2(path, text1)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    25
    }
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    26
  }
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    27
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    28
62836
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    29
  /* Isabelle tool wrapper */
61216
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    30
62836
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    31
  val isabelle_tool =
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 68994
diff changeset
    32
    Isabelle_Tool("update_then", "expand old Isar command conflations 'hence' and 'thus'",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    33
      Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    34
      { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    35
        val getopts = Getopts("""
62449
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    36
Usage: isabelle update_then [FILES|DIRS...]
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    37
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    38
  Recursively find .thy files and expand old Isar command conflations:
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    39
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    40
    hence  ~>  then have
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    41
    thus   ~>  then show
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    42
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    43
  Old versions of files are preserved by appending "~~".
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    44
""")
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    45
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    46
        val specs = getopts(args)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    47
        if (specs.isEmpty) getopts.usage()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    48
  
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    49
        for {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    50
          spec <- specs
75906
2167b9e3157a clarified signature: support for adhoc file types;
wenzelm
parents: 75394
diff changeset
    51
          file <- File.find_files(Path.explode(spec).file, file => File.is_thy(file.getName))
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    52
        } update_then(File.path(file))
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    53
      })
61216
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    54
}