src/Pure/Tools/scala_project.scala
changeset 74056 fb8d5c0133c9
parent 74055 0ee44ed80290
child 74070 a69a13c4b049
equal deleted inserted replaced
74055:0ee44ed80290 74056:fb8d5c0133c9
     4 Manage Isabelle/Scala/Java project sources, with output to Gradle for
     4 Manage Isabelle/Scala/Java project sources, with output to Gradle for
     5 IntelliJ IDEA.
     5 IntelliJ IDEA.
     6 */
     6 */
     7 
     7 
     8 package isabelle
     8 package isabelle
     9 
       
    10 import scala.jdk.CollectionConverters._
       
    11 
     9 
    12 
    10 
    13 object Scala_Project
    11 object Scala_Project
    14 {
    12 {
    15   /* groovy syntax */
    13   /* groovy syntax */
    33   lazy val isabelle_files: (List[Path], List[Path]) =
    31   lazy val isabelle_files: (List[Path], List[Path]) =
    34   {
    32   {
    35     val contexts = Scala_Build.component_contexts() ::: plugin_contexts()
    33     val contexts = Scala_Build.component_contexts() ::: plugin_contexts()
    36 
    34 
    37     val jars1 = Path.split(Isabelle_System.getenv("ISABELLE_CLASSPATH"))
    35     val jars1 = Path.split(Isabelle_System.getenv("ISABELLE_CLASSPATH"))
    38     val jars2 =
    36     val jars2 = contexts.flatMap(_.requirements)
    39       (for {
       
    40         context <- contexts.iterator
       
    41         s <- context.requirements().asScala.iterator
       
    42         path <- context.requirement_paths(s).asScala.iterator
       
    43       } yield File.path(path.toFile)).toList
       
    44 
    37 
    45     val jar_files =
    38     val jar_files =
    46       Library.distinct(jars1 ::: jars2).filterNot(path =>
    39       Library.distinct(jars1 ::: jars2).filterNot(path => contexts.exists(_.is_module(path)))
    47         contexts.exists(context =>
       
    48         {
       
    49           val name: String = context.module_name()
       
    50           name.nonEmpty && File.eq(context.path(name).toFile, path.file)
       
    51         }))
       
    52 
    40 
    53     val source_files =
    41     val source_files =
    54       (for {
    42       (for {
    55         context <- contexts.iterator
    43         context <- contexts.iterator
    56         file <- context.sources.asScala.iterator
    44         path <- context.sources.iterator
    57         if file.endsWith(".scala") || file.endsWith(".java")
    45         if path.is_scala || path.is_java
    58       } yield File.path(context.path(file).toFile)).toList
    46       } yield path).toList
    59 
    47 
    60     (jar_files, source_files)
    48     (jar_files, source_files)
    61   }
    49   }
    62 
    50 
    63   lazy val isabelle_scala_files: Map[String, Path] =
    51   lazy val isabelle_scala_files: Map[String, Path] =
    64   {
    52   {
    65     val context = Scala_Build.context(Path.ISABELLE_HOME, component = true)
    53     val context = Scala_Build.context(Path.ISABELLE_HOME, component = true)
    66     context.sources().asScala.iterator.foldLeft(Map.empty[String, Path]) {
    54     context.sources.iterator.foldLeft(Map.empty[String, Path]) {
    67       case (map, name) =>
    55       case (map, path) =>
    68         if (name.endsWith(".scala")) {
    56         if (path.is_scala) {
    69         val path = File.path(context.path(name).toFile)
       
    70         val base = path.base.implode
    57         val base = path.base.implode
    71           map.get(base) match {
    58           map.get(base) match {
    72             case None => map + (base -> path)
    59             case None => map + (base -> path)
    73             case Some(path2) => error("Conflicting base names: " + path + " vs. " + path2)
    60             case Some(path2) => error("Conflicting base names: " + path + " vs. " + path2)
    74           }
    61           }
   105 
    92 
   106   /* scala project */
    93   /* scala project */
   107 
    94 
   108   def package_dir(source_file: Path): Option[Path] =
    95   def package_dir(source_file: Path): Option[Path] =
   109   {
    96   {
   110     val is_java = source_file.file_name.endsWith(".java")
       
   111     val lines = split_lines(File.read(source_file))
    97     val lines = split_lines(File.read(source_file))
   112     val Package = """\s*\bpackage\b\s*(?:object\b\s*)?((?:\w|\.)+)\b.*""".r
    98     val Package = """\s*\bpackage\b\s*(?:object\b\s*)?((?:\w|\.)+)\b.*""".r
   113     lines.collectFirst(
    99     lines.collectFirst(
   114       {
   100       {
   115         case Package(name) =>
   101         case Package(name) =>
   116           if (is_java) Path.explode(space_explode('.', name).mkString("/"))
   102           if (source_file.is_java) Path.explode(space_explode('.', name).mkString("/"))
   117           else Path.basic(name)
   103           else Path.basic(name)
   118       })
   104       })
   119   }
   105   }
   120 
   106 
   121   def the_package_dir(source_file: Path): Path =
   107   def the_package_dir(source_file: Path): Path =
   134 
   120 
   135     val (jar_files, source_files) = isabelle_files
   121     val (jar_files, source_files) = isabelle_files
   136     isabelle_scala_files
   122     isabelle_scala_files
   137 
   123 
   138     for (source <- source_files) {
   124     for (source <- source_files) {
   139       val dir = if (source.file_name.endsWith(".java")) java_src_dir else scala_src_dir
   125       val dir = if (source.is_java) java_src_dir else scala_src_dir
   140       val target = dir + the_package_dir(source)
   126       val target = dir + the_package_dir(source)
   141       Isabelle_System.make_directory(target)
   127       Isabelle_System.make_directory(target)
   142       if (symlinks) Isabelle_System.symlink(source, target)
   128       if (symlinks) Isabelle_System.symlink(source, target)
   143       else Isabelle_System.copy_file(source, target)
   129       else Isabelle_System.copy_file(source, target)
   144     }
   130     }