src/Pure/Tools/update_then.scala
author wenzelm
Sun, 04 Sep 2016 15:44:20 +0200
changeset 63778 e06e899b78d0
parent 62836 98dbed6cfa44
child 68994 d961e11e0e87
permissions -rw-r--r--
clarified modules; tuned signature;
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
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    10
object Update_Then
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    11
{
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    12
  def update_then(path: Path)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    13
  {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    14
    val text0 = File.read(path)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    15
    val text1 =
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    16
      (for (tok <- Token.explode(Keyword.Keywords.empty, text0).iterator)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    17
        yield {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    18
          tok.source match {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    19
            case "hence" => "then have"
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    20
            case "thus" => "then show"
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    21
            case s => s
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    22
        } }).mkString
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    23
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    24
    if (text0 != text1) {
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    25
      Output.writeln("changing " + path)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    26
      File.write_backup2(path, text1)
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    27
    }
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    28
  }
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    29
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    30
62836
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    31
  /* Isabelle tool wrapper */
61216
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    32
62836
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    33
  val isabelle_tool =
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    34
    Isabelle_Tool("update_then", "expand old Isar command conflations 'hence' and 'thus'", args =>
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    35
    {
62454
38c89353b349 tuned signature;
wenzelm
parents: 62449
diff changeset
    36
      val getopts = Getopts("""
62449
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    37
Usage: isabelle update_then [FILES|DIRS...]
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    38
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    39
  Recursively find .thy files and expand old Isar command conflations:
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    40
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    41
    hence  ~>  then have
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    42
    thus   ~>  then show
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    43
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    44
  Old versions of files are preserved by appending "~~".
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    45
""")
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    46
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    47
      val specs = getopts(args)
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    48
      if (specs.isEmpty) getopts.usage()
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    49
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    50
      for {
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    51
        spec <- specs
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    52
        file <- File.find_files(Path.explode(spec).file, file => file.getName.endsWith(".thy"))
1785cbadd226 moved getopts to Scala;
wenzelm
parents: 61216
diff changeset
    53
      } update_then(Path.explode(File.standard_path(file)))
62836
98dbed6cfa44 prefer internal tool;
wenzelm
parents: 62454
diff changeset
    54
    })
61216
4ca490f09ec6 added isabelle update_then;
wenzelm
parents:
diff changeset
    55
}