diff --git a/src/Pure/General/path.scala b/src/Pure/General/path.scala
--- a/src/Pure/General/path.scala
+++ b/src/Pure/General/path.scala
@@ -1,317 +1,325 @@
/* Title: Pure/General/path.scala
Author: Makarius
Algebra of file-system paths: basic POSIX notation, extended by named
roots (e.g. //foo) and variables (e.g. $BAR).
*/
package isabelle
import java.util.{Map => JMap}
import java.io.{File => JFile}
import java.nio.file.{Path => JPath}
import scala.util.matching.Regex
object Path
{
/* path elements */
sealed abstract class Elem
private case class Root(name: String) extends Elem
private case class Basic(name: String) extends Elem
private case class Variable(name: String) extends Elem
private case object Parent extends Elem
private def err_elem(msg: String, s: String): Nothing =
error(msg + " path element " + quote(s))
private val illegal_elem = Set("", "~", "~~", ".", "..")
private val illegal_char = "/\\$:\"'<>|?*"
private def check_elem(s: String): String =
if (illegal_elem.contains(s)) err_elem("Illegal", s)
else {
for (c <- s) {
if (c.toInt < 32)
err_elem("Illegal control character " + c.toInt + " in", s)
if (illegal_char.contains(c))
err_elem("Illegal character " + quote(c.toString) + " in", s)
}
s
}
private def root_elem(s: String): Elem = Root(check_elem(s))
private def basic_elem(s: String): Elem = Basic(check_elem(s))
private def variable_elem(s: String): Elem = Variable(check_elem(s))
private def apply_elem(y: Elem, xs: List[Elem]): List[Elem] =
(y, xs) match {
case (Root(_), _) => List(y)
case (Parent, Root(_) :: _) => xs
case (Parent, Basic(_) :: rest) => rest
case _ => y :: xs
}
private def norm_elems(elems: List[Elem]): List[Elem] =
elems.foldRight(List.empty[Elem])(apply_elem)
private def implode_elem(elem: Elem, short: Boolean): String =
elem match {
case Root("") => ""
case Root(s) => "//" + s
case Basic(s) => s
case Variable("USER_HOME") if short => "~"
case Variable("ISABELLE_HOME") if short => "~~"
case Variable(s) => "$" + s
case Parent => ".."
}
private def squash_elem(elem: Elem): String =
elem match {
case Root("") => "ROOT"
case Root(s) => "SERVER_" + s
case Basic(s) => s
case Variable(s) => s
case Parent => "PARENT"
}
/* path constructors */
val current: Path = new Path(Nil)
val root: Path = new Path(List(Root("")))
def named_root(s: String): Path = new Path(List(root_elem(s)))
def make(elems: List[String]): Path = new Path(elems.reverse.map(basic_elem))
def basic(s: String): Path = new Path(List(basic_elem(s)))
def variable(s: String): Path = new Path(List(variable_elem(s)))
val parent: Path = new Path(List(Parent))
val USER_HOME: Path = variable("USER_HOME")
val ISABELLE_HOME: Path = variable("ISABELLE_HOME")
/* explode */
def explode(str: String): Path =
{
def explode_elem(s: String): Elem =
try {
if (s == "..") Parent
else if (s == "~") Variable("USER_HOME")
else if (s == "~~") Variable("ISABELLE_HOME")
else if (s.startsWith("$")) variable_elem(s.substring(1))
else basic_elem(s)
}
catch { case ERROR(msg) => cat_error(msg, "The error(s) above occurred in " + quote(str)) }
val ss = space_explode('/', str)
val r = ss.takeWhile(_.isEmpty).length
val es = ss.dropWhile(_.isEmpty)
val (roots, raw_elems) =
if (r == 0) (Nil, es)
else if (r == 1) (List(Root("")), es)
else if (es.isEmpty) (List(Root("")), Nil)
else (List(root_elem(es.head)), es.tail)
val elems = raw_elems.filterNot(s => s.isEmpty || s == ".").map(explode_elem)
new Path(norm_elems(elems reverse_::: roots))
}
def is_wellformed(str: String): Boolean =
try { explode(str); true } catch { case ERROR(_) => false }
def is_valid(str: String): Boolean =
try { explode(str).expand; true } catch { case ERROR(_) => false }
def split(str: String): List[Path] =
space_explode(':', str).filterNot(_.isEmpty).map(explode)
/* encode */
val encode: XML.Encode.T[Path] = (path => XML.Encode.string(path.implode))
/* reserved names */
private val reserved_windows: Set[String] =
Set("CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9")
def is_reserved(name: String): Boolean =
Long_Name.explode(name).exists(a => reserved_windows.contains(Word.uppercase(a)))
/* case-insensitive names */
def check_case_insensitive(paths: List[Path]): Unit =
{
val table =
paths.foldLeft(Multi_Map.empty[String, String]) { case (tab, path) =>
val name = path.expand.implode
tab.insert(Word.lowercase(name), name)
}
val collisions =
(for { (_, coll) <- table.iterator_list if coll.length > 1 } yield coll).toList.flatten
if (collisions.nonEmpty) {
error(("Collision of file names due case-insensitivity:" :: collisions).mkString("\n "))
}
}
}
final class Path private(protected val elems: List[Path.Elem]) // reversed elements
{
override def hashCode: Int = elems.hashCode
override def equals(that: Any): Boolean =
that match {
case other: Path => elems == other.elems
case _ => false
}
def is_current: Boolean = elems.isEmpty
def is_absolute: Boolean = elems.nonEmpty && elems.last.isInstanceOf[Path.Root]
def is_root: Boolean = elems match { case List(Path.Root(_)) => true case _ => false }
def is_basic: Boolean = elems match { case List(Path.Basic(_)) => true case _ => false }
def starts_basic: Boolean = elems.nonEmpty && elems.last.isInstanceOf[Path.Basic]
def +(other: Path): Path = new Path(other.elems.foldRight(elems)(Path.apply_elem))
/* implode */
private def gen_implode(short: Boolean): String =
elems match {
case Nil => "."
case List(Path.Root("")) => "/"
case _ => elems.map(Path.implode_elem(_, short)).reverse.mkString("/")
}
def implode: String = gen_implode(false)
def implode_short: String = gen_implode(true)
override def toString: String = quote(implode)
/* base element */
private def split_path: (Path, String) =
elems match {
case Path.Basic(s) :: xs => (new Path(xs), s)
case _ => error("Cannot split path into dir/base: " + toString)
}
def dir: Path = split_path._1
def base: Path = new Path(List(Path.Basic(split_path._2)))
+ def ends_with(a: String): Boolean =
+ elems match {
+ case Path.Basic(b) :: _ => b.endsWith(a)
+ case _ => false
+ }
+ def is_java: Boolean = ends_with(".java")
+ def is_scala: Boolean = ends_with(".scala")
+
def ext(e: String): Path =
if (e == "") this
else {
val (prfx, s) = split_path
prfx + Path.basic(s + "." + e)
}
def xz: Path = ext("xz")
def xml: Path = ext("xml")
def html: Path = ext("html")
def tex: Path = ext("tex")
def pdf: Path = ext("pdf")
def thy: Path = ext("thy")
def tar: Path = ext("tar")
def gz: Path = ext("gz")
def log: Path = ext("log")
def backup: Path =
{
val (prfx, s) = split_path
prfx + Path.basic(s + "~")
}
def backup2: Path =
{
val (prfx, s) = split_path
prfx + Path.basic(s + "~~")
}
def platform_exe: Path =
if (Platform.is_windows) ext("exe") else this
private val Ext = new Regex("(.*)\\.([^.]*)")
def split_ext: (Path, String) =
{
val (prefix, base) = split_path
base match {
case Ext(b, e) => (prefix + Path.basic(b), e)
case _ => (prefix + Path.basic(base), "")
}
}
def drop_ext: Path = split_ext._1
def get_ext: String = split_ext._2
def squash: Path = new Path(elems.map(elem => Path.Basic(Path.squash_elem(elem))))
/* expand */
def expand_env(env: JMap[String, String]): Path =
{
def eval(elem: Path.Elem): List[Path.Elem] =
elem match {
case Path.Variable(s) =>
val path = Path.explode(Isabelle_System.getenv_strict(s, env))
if (path.elems.exists(_.isInstanceOf[Path.Variable]))
error("Illegal path variable nesting: " + Properties.Eq(s, path.toString))
else path.elems
case x => List(x)
}
new Path(Path.norm_elems(elems.flatMap(eval)))
}
def expand: Path = expand_env(Isabelle_System.settings())
def file_name: String = expand.base.implode
/* implode wrt. given directories */
def implode_symbolic: String =
{
val directories =
Library.space_explode(':', Isabelle_System.getenv("ISABELLE_DIRECTORIES")).reverse
val full_name = expand.implode
directories.view.flatMap(a =>
try {
val b = Path.explode(a).expand.implode
if (full_name == b) Some(a)
else {
Library.try_unprefix(b + "/", full_name) match {
case Some(name) => Some(a + "/" + name)
case None => None
}
}
} catch { case ERROR(_) => None }).headOption.getOrElse(implode)
}
def position: Position.T = Position.File(implode_symbolic)
/* platform files */
def file: JFile = File.platform_file(this)
def is_file: Boolean = file.isFile
def is_dir: Boolean = file.isDirectory
def java_path: JPath = file.toPath
def absolute_file: JFile = File.absolute(file)
def canonical_file: JFile = File.canonical(file)
def absolute: Path = File.path(absolute_file)
def canonical: Path = File.path(canonical_file)
}
diff --git a/src/Pure/Tools/scala_build.scala b/src/Pure/Tools/scala_build.scala
--- a/src/Pure/Tools/scala_build.scala
+++ b/src/Pure/Tools/scala_build.scala
@@ -1,47 +1,65 @@
/* Title: Pure/Tools/scala_build.scala
Author: Makarius
Manage and build Isabelle/Scala/Java components.
*/
package isabelle
import java.util.{Properties => JProperties}
import java.nio.file.Files
import scala.jdk.CollectionConverters._
object Scala_Build
{
- type Context = isabelle.setup.Build.Context
+ class Context private[Scala_Build](java_context: isabelle.setup.Build.Context)
+ {
+ def is_module(path: Path): Boolean =
+ {
+ val module_name: String = java_context.module_name()
+ module_name.nonEmpty && File.eq(java_context.path(module_name).toFile, path.file)
+ }
+
+ def sources: List[Path] =
+ java_context.sources().asScala.toList.map(s => File.path(java_context.path(s).toFile))
+
+ def requirements: List[Path] =
+ (for {
+ s <- java_context.requirements().asScala.iterator
+ p <- java_context.requirement_paths(s).asScala.iterator
+ } yield (File.path(p.toFile))).toList
+
+ def build(fresh: Boolean = false): Unit =
+ isabelle.setup.Build.build(java_context, fresh)
+ }
def context(dir: Path,
component: Boolean = false,
more_props: Properties.T = Nil): Context =
{
val props_name =
if (component) isabelle.setup.Build.COMPONENT_BUILD_PROPS
else isabelle.setup.Build.BUILD_PROPS
val props_path = dir + Path.explode(props_name)
val props = new JProperties
props.load(Files.newBufferedReader(props_path.java_path))
for ((a, b) <- more_props) props.put(a, b)
- new isabelle.setup.Build.Context(dir.java_path, props, props_path.implode)
+ new Context(new isabelle.setup.Build.Context(dir.java_path, props, props_path.implode))
}
def build(dir: Path,
fresh: Boolean = false,
component: Boolean = false,
more_props: Properties.T = Nil): Unit =
{
- isabelle.setup.Build.build(
- context(dir, component = component, more_props = more_props), fresh)
+ context(dir, component = component, more_props = more_props).build(fresh = fresh)
}
def component_contexts(): List[Context] =
- isabelle.setup.Build.component_contexts().asScala.toList
+ isabelle.setup.Build.component_contexts().asScala.toList.map(new Context(_))
}
diff --git a/src/Pure/Tools/scala_project.scala b/src/Pure/Tools/scala_project.scala
--- a/src/Pure/Tools/scala_project.scala
+++ b/src/Pure/Tools/scala_project.scala
@@ -1,195 +1,181 @@
/* Title: Pure/Tools/scala_project.scala
Author: Makarius
Manage Isabelle/Scala/Java project sources, with output to Gradle for
IntelliJ IDEA.
*/
package isabelle
-import scala.jdk.CollectionConverters._
-
object Scala_Project
{
/* groovy syntax */
def groovy_string(s: String): String =
{
s.map(c =>
c match {
case '\t' | '\b' | '\n' | '\r' | '\f' | '\\' | '\'' | '"' => "\\" + c
case _ => c.toString
}).mkString("'", "", "'")
}
/* file and directories */
def plugin_contexts(): List[Scala_Build.Context] =
for (plugin <- List("jedit_base", "jedit_main"))
yield Scala_Build.context(Path.explode("$ISABELLE_HOME/src/Tools/jEdit") + Path.basic(plugin))
lazy val isabelle_files: (List[Path], List[Path]) =
{
val contexts = Scala_Build.component_contexts() ::: plugin_contexts()
val jars1 = Path.split(Isabelle_System.getenv("ISABELLE_CLASSPATH"))
- val jars2 =
- (for {
- context <- contexts.iterator
- s <- context.requirements().asScala.iterator
- path <- context.requirement_paths(s).asScala.iterator
- } yield File.path(path.toFile)).toList
+ val jars2 = contexts.flatMap(_.requirements)
val jar_files =
- Library.distinct(jars1 ::: jars2).filterNot(path =>
- contexts.exists(context =>
- {
- val name: String = context.module_name()
- name.nonEmpty && File.eq(context.path(name).toFile, path.file)
- }))
+ Library.distinct(jars1 ::: jars2).filterNot(path => contexts.exists(_.is_module(path)))
val source_files =
(for {
context <- contexts.iterator
- file <- context.sources.asScala.iterator
- if file.endsWith(".scala") || file.endsWith(".java")
- } yield File.path(context.path(file).toFile)).toList
+ path <- context.sources.iterator
+ if path.is_scala || path.is_java
+ } yield path).toList
(jar_files, source_files)
}
lazy val isabelle_scala_files: Map[String, Path] =
{
val context = Scala_Build.context(Path.ISABELLE_HOME, component = true)
- context.sources().asScala.iterator.foldLeft(Map.empty[String, Path]) {
- case (map, name) =>
- if (name.endsWith(".scala")) {
- val path = File.path(context.path(name).toFile)
+ context.sources.iterator.foldLeft(Map.empty[String, Path]) {
+ case (map, path) =>
+ if (path.is_scala) {
val base = path.base.implode
map.get(base) match {
case None => map + (base -> path)
case Some(path2) => error("Conflicting base names: " + path + " vs. " + path2)
}
}
else map
}
}
/* compile-time position */
def here: Here =
{
val exn = new Exception
exn.getStackTrace.toList match {
case _ :: caller :: _ =>
val name = proper_string(caller.getFileName).getOrElse("")
val line = caller.getLineNumber
new Here(name, line)
case _ => new Here("", 0)
}
}
class Here private[Scala_Project](name: String, line: Int)
{
override def toString: String = name + ":" + line
def position: Position.T =
isabelle_scala_files.get(name) match {
case Some(path) => Position.Line_File(line, path.implode)
case None => Position.none
}
}
/* scala project */
def package_dir(source_file: Path): Option[Path] =
{
- val is_java = source_file.file_name.endsWith(".java")
val lines = split_lines(File.read(source_file))
val Package = """\s*\bpackage\b\s*(?:object\b\s*)?((?:\w|\.)+)\b.*""".r
lines.collectFirst(
{
case Package(name) =>
- if (is_java) Path.explode(space_explode('.', name).mkString("/"))
+ if (source_file.is_java) Path.explode(space_explode('.', name).mkString("/"))
else Path.basic(name)
})
}
def the_package_dir(source_file: Path): Path =
package_dir(source_file) getOrElse error("Failed to guess package from " + source_file)
def scala_project(project_dir: Path, symlinks: Boolean = false): Unit =
{
if (symlinks && Platform.is_windows)
error("Cannot create symlinks on Windows")
if (project_dir.is_file || project_dir.is_dir)
error("Project directory already exists: " + project_dir)
val java_src_dir = Isabelle_System.make_directory(project_dir + Path.explode("src/main/java"))
val scala_src_dir = Isabelle_System.make_directory(project_dir + Path.explode("src/main/scala"))
val (jar_files, source_files) = isabelle_files
isabelle_scala_files
for (source <- source_files) {
- val dir = if (source.file_name.endsWith(".java")) java_src_dir else scala_src_dir
+ val dir = if (source.is_java) java_src_dir else scala_src_dir
val target = dir + the_package_dir(source)
Isabelle_System.make_directory(target)
if (symlinks) Isabelle_System.symlink(source, target)
else Isabelle_System.copy_file(source, target)
}
File.write(project_dir + Path.explode("settings.gradle"), "rootProject.name = 'Isabelle'\n")
File.write(project_dir + Path.explode("build.gradle"),
"""plugins {
id 'scala'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.scala-lang:scala-library:""" + scala.util.Properties.versionNumberString + """'
compile files(
""" + jar_files.map(jar => groovy_string(File.platform_path(jar))).mkString("", ",\n ", ")") +
"""
}
""")
}
/* Isabelle tool wrapper */
val isabelle_tool =
Isabelle_Tool("scala_project", "setup Gradle project for Isabelle/Scala/jEdit",
Scala_Project.here, args =>
{
var symlinks = false
val getopts = Getopts("""
Usage: isabelle scala_project [OPTIONS] PROJECT_DIR
Options are:
-L make symlinks to original scala files
Setup Gradle project for Isabelle/Scala/jEdit --- to support Scala IDEs
such as IntelliJ IDEA.
""",
"L" -> (_ => symlinks = true))
val more_args = getopts(args)
val project_dir =
more_args match {
case List(dir) => Path.explode(dir)
case _ => getopts.usage()
}
scala_project(project_dir, symlinks = symlinks)
})
}
diff --git a/src/Tools/jEdit/src/main.scala b/src/Tools/jEdit/src/main.scala
--- a/src/Tools/jEdit/src/main.scala
+++ b/src/Tools/jEdit/src/main.scala
@@ -1,142 +1,142 @@
/* Title: src/Tools/jEdit/src/main.scala
Author: Makarius
Main Isabelle application entry point.
*/
package isabelle.jedit
import isabelle._
import org.gjt.sp.jedit.{MiscUtilities, jEdit}
object Main
{
/* main entry point */
def main(args: Array[String]): Unit =
{
if (args.nonEmpty && args(0) == "-init") {
Isabelle_System.init()
}
else {
val start =
{
try {
Isabelle_System.init()
Isabelle_Fonts.init()
GUI.init_lafs()
/* ROOTS template */
{
val roots = Path.explode("$ISABELLE_HOME_USER/ROOTS")
if (!roots.is_file) File.write(roots, """# Additional session root directories
#
# * each line contains one directory entry in Isabelle path notation, e.g.
#
# $ISABELLE_HOME/../AFP/thys
#
# for a copy of AFP put side-by-side to the Isabelle distribution
#
# * Isabelle/jEdit provides formal markup for C-hover-click and completion
#
# * lines starting with "#" are stripped
#
# * changes require restart of the Isabelle application
#
#:mode=text:encoding=UTF-8:
#$ISABELLE_HOME/../AFP/thys
""")
}
/* settings directory */
val settings_dir = Path.explode("$JEDIT_SETTINGS")
val properties = settings_dir + Path.explode("properties")
if (properties.is_file) {
val props1 = split_lines(File.read(properties))
val props2 = props1.filterNot(_.startsWith("plugin-blacklist.isabelle_jedit"))
if (props1 != props2) File.write(properties, cat_lines(props2))
}
Isabelle_System.make_directory(settings_dir + Path.explode("DockableWindowManager"))
if (!(settings_dir + Path.explode("perspective.xml")).is_file) {
File.write(settings_dir + Path.explode("DockableWindowManager/perspective-view0.xml"),
"""""")
File.write(settings_dir + Path.explode("perspective.xml"),
XML.header +
"""
""")
}
- Scala_Project.plugin_contexts().foreach(isabelle.setup.Build.build(_, false))
+ Scala_Project.plugin_contexts().foreach(_.build())
/* args */
val jedit_settings =
"-settings=" + File.platform_path(Path.explode("$JEDIT_SETTINGS"))
val jedit_server =
System.getProperty("isabelle.jedit_server") match {
case null | "" => "-noserver"
case name => "-server=" + name
}
val jedit_options =
Isabelle_System.getenv_strict("JEDIT_OPTIONS").split(" +")
val more_args =
{
args.toList.dropWhile(arg => arg.startsWith("-") && arg != "--") match {
case Nil | List("--") =>
args ++ Array(File.platform_path(Path.explode("$USER_HOME/Scratch.thy")))
case List(":") => args.slice(0, args.length - 1)
case _ => args
}
}
/* environment */
for (name <- List("ISABELLE_HOME", "ISABELLE_HOME_USER", "JEDIT_HOME", "JEDIT_SETTINGS")) {
MiscUtilities.putenv(name, File.platform_path(Isabelle_System.getenv(name)))
}
MiscUtilities.putenv("ISABELLE_ROOT", null)
/* properties */
System.setProperty("jedit.home", File.platform_path(Path.explode("$JEDIT_HOME")))
System.setProperty("scala.home", File.platform_path(Path.explode("$SCALA_HOME")))
System.setProperty("scala.color", "false")
/* main startup */
() => jEdit.main(Array(jedit_settings, jedit_server) ++ jedit_options ++ more_args)
}
catch {
case exn: Throwable =>
GUI.init_laf()
GUI.dialog(null, "Isabelle", GUI.scrollable_text(Exn.message(exn)))
sys.exit(2)
}
}
start()
}
}
}