src/Pure/Tools/update_header.scala
author wenzelm
Wed, 03 Dec 2014 14:04:38 +0100
changeset 59083 88b0b1f28adc
parent 58872 f0f623005324
child 61463 8e46cea6a45a
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58872
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/update_header.scala
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     3
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     4
Replace theory header command.
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     5
*/
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     6
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     7
package isabelle
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     8
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
     9
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    10
object Update_Header
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    11
{
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    12
  def update_header(section: String, path: Path)
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    13
  {
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    14
    val text0 = File.read(path)
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    15
    val text1 =
59083
88b0b1f28adc tuned signature;
wenzelm
parents: 58872
diff changeset
    16
      (for (tok <- Token.explode(Keyword.Keywords.empty, text0).iterator)
58872
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    17
        yield { if (tok.source == "header") section else tok.source }).mkString
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    18
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    19
    if (text0 != text1) {
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    20
      Output.writeln("changing " + path)
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    21
      File.write_backup2(path, text1)
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    22
    }
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    23
  }
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    24
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    25
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    26
  /* command line entry point */
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    27
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    28
  def main(args: Array[String])
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    29
  {
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    30
    Command_Line.tool0 {
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    31
      args.toList match {
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    32
        case section :: files =>
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    33
          if (!Set("chapter", "section", "subsection", "subsubsection").contains(section))
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    34
            error("Bad heading command: " + quote(section))
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    35
          files.foreach(file => update_header(section, Path.explode(file)))
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    36
        case _ => error("Bad arguments:\n" + cat_lines(args))
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    37
      }
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    38
    }
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    39
  }
f0f623005324 added update_header tool;
wenzelm
parents:
diff changeset
    40
}