src/Pure/Tools/scala_build.scala
changeset 74061 203dfa8bc0fc
parent 74060 8cd746a5c291
child 74062 4dbac13d89a5
equal deleted inserted replaced
74060:8cd746a5c291 74061:203dfa8bc0fc
     6 
     6 
     7 package isabelle
     7 package isabelle
     8 
     8 
     9 
     9 
    10 import java.util.{Properties => JProperties}
    10 import java.util.{Properties => JProperties}
       
    11 import java.io.{ByteArrayOutputStream, PrintStream}
    11 import java.nio.file.Files
    12 import java.nio.file.Files
    12 
    13 
    13 import scala.jdk.CollectionConverters._
    14 import scala.jdk.CollectionConverters._
    14 
    15 
    15 
    16 
    32       (for {
    33       (for {
    33         s <- java_context.requirements().asScala.iterator
    34         s <- java_context.requirements().asScala.iterator
    34         p <- java_context.requirement_paths(s).asScala.iterator
    35         p <- java_context.requirement_paths(s).asScala.iterator
    35       } yield (File.path(p.toFile))).toList
    36       } yield (File.path(p.toFile))).toList
    36 
    37 
    37     def build(fresh: Boolean = false): Unit =
    38     def build(fresh: Boolean = false): String =
    38       isabelle.setup.Build.build(java_context, fresh)
    39     {
       
    40       val output0 = new ByteArrayOutputStream
       
    41       val output = new PrintStream(output0)
       
    42       def get_output(): String =
       
    43       {
       
    44         output.flush()
       
    45         Library.trim_line(output0.toString(UTF8.charset))
       
    46       }
       
    47       try {
       
    48         Console.withOut(output) {
       
    49           Console.withErr(output) {
       
    50             isabelle.setup.Build.build(output, java_context, fresh)
       
    51           }
       
    52         }
       
    53         get_output()
       
    54       }
       
    55       catch { case ERROR(msg) => cat_error(get_output(), msg) }
       
    56     }
    39   }
    57   }
    40 
    58 
    41   def context(dir: Path,
    59   def context(dir: Path,
    42     component: Boolean = false,
    60     component: Boolean = false,
    43     no_title: Boolean = false,
    61     no_title: Boolean = false,
    61   def build(dir: Path,
    79   def build(dir: Path,
    62     fresh: Boolean = false,
    80     fresh: Boolean = false,
    63     component: Boolean = false,
    81     component: Boolean = false,
    64     no_title: Boolean = false,
    82     no_title: Boolean = false,
    65     do_build: Boolean = false,
    83     do_build: Boolean = false,
    66     module: Option[Path] = None): Unit =
    84     module: Option[Path] = None): String =
    67   {
    85   {
    68     context(dir, component = component, no_title = no_title, do_build = do_build, module = module)
    86     context(dir, component = component, no_title = no_title, do_build = do_build, module = module)
    69       .build(fresh = fresh)
    87       .build(fresh = fresh)
    70   }
    88   }
    71 
    89 
    72   def build_result(dir: Path, component: Boolean = false): Bytes =
    90   sealed case class Result(output: String, jar: Bytes)
       
    91 
       
    92   def build_result(dir: Path, component: Boolean = false): Result =
    73   {
    93   {
    74     Isabelle_System.with_tmp_file("result", "jar")(tmp_file =>
    94     Isabelle_System.with_tmp_file("result", "jar")(tmp_file =>
    75     {
    95     {
    76       build(dir, component = component, no_title = true, do_build = true, module = Some(tmp_file))
    96       val output =
    77       Bytes.read(tmp_file)
    97         build(dir, component = component, no_title = true, do_build = true, module = Some(tmp_file))
       
    98       val jar = Bytes.read(tmp_file)
       
    99       Result(output, jar)
    78     })
   100     })
    79   }
   101   }
    80 
   102 
    81   def component_contexts(): List[Context] =
   103   def component_contexts(): List[Context] =
    82     isabelle.setup.Build.component_contexts().asScala.toList.map(new Context(_))
   104     isabelle.setup.Build.component_contexts().asScala.toList.map(new Context(_))