src/Tools/Setup/isabelle/setup/Build_Scala.java
changeset 73922 3556303bd385
parent 73921 1f31ed84c467
child 73930 17c09d1b3588
equal deleted inserted replaced
73921:1f31ed84c467 73922:3556303bd385
     5 */
     5 */
     6 
     6 
     7 package isabelle.setup;
     7 package isabelle.setup;
     8 
     8 
     9 
     9 
       
    10 import java.io.BufferedOutputStream;
    10 import java.io.File;
    11 import java.io.File;
    11 import java.io.IOException;
    12 import java.io.IOException;
       
    13 import java.io.OutputStream;
    12 import java.math.BigInteger;
    14 import java.math.BigInteger;
    13 import java.nio.file.Files;
    15 import java.nio.file.Files;
    14 import java.nio.file.Path;
    16 import java.nio.file.Path;
    15 import java.nio.file.StandardCopyOption;
    17 import java.nio.file.StandardCopyOption;
    16 import java.security.MessageDigest;
    18 import java.security.MessageDigest;
    20 import java.util.LinkedList;
    22 import java.util.LinkedList;
    21 import java.util.List;
    23 import java.util.List;
    22 import java.util.Locale;
    24 import java.util.Locale;
    23 import java.util.Map;
    25 import java.util.Map;
    24 import java.util.Properties;
    26 import java.util.Properties;
       
    27 import java.util.jar.Attributes;
       
    28 import java.util.jar.JarEntry;
       
    29 import java.util.jar.JarOutputStream;
       
    30 import java.util.jar.Manifest;
    25 import java.util.stream.Stream;
    31 import java.util.stream.Stream;
    26 
    32 
    27 
    33 
    28 public class Build_Scala
    34 public class Build_Scala
    29 {
    35 {
       
    36     /** component directory context **/
       
    37 
    30     public static class Context
    38     public static class Context
    31     {
    39     {
    32         private final Path component_dir;
    40         private final Path component_dir;
    33         private Properties props;
    41         private Properties props;
    34 
    42 
    81                 return digest + " *" + file + "\n";
    89                 return digest + " *" + file + "\n";
    82             }
    90             }
    83             else { return ""; }
    91             else { return ""; }
    84         }
    92         }
    85     }
    93     }
       
    94 
       
    95 
       
    96 
       
    97     /** create jar **/
       
    98 
       
    99     public static void create_jar(Path dir, String main, Path jar)
       
   100         throws IOException
       
   101     {
       
   102         Files.deleteIfExists(jar);
       
   103 
       
   104         Manifest manifest = new Manifest();
       
   105         Attributes attributes = manifest.getMainAttributes();
       
   106         attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
       
   107         attributes.put(new Attributes.Name("Created-By"),
       
   108             System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")");
       
   109         if (!main.isEmpty()) { attributes.put(Attributes.Name.MAIN_CLASS, main); }
       
   110 
       
   111         try (JarOutputStream out =
       
   112             new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jar)), manifest))
       
   113         {
       
   114             for (Path path : Files.walk(dir).sorted().toArray(Path[]::new)) {
       
   115                 boolean is_dir = Files.isDirectory(path);
       
   116                 boolean is_file = Files.isRegularFile(path);
       
   117                 if (is_dir || is_file) {
       
   118                     String name = Environment.slashes(dir.relativize(path).toString());
       
   119                     JarEntry entry = new JarEntry(is_dir ? name + "/" : name);
       
   120                     entry.setTime(path.toFile().lastModified());
       
   121                     out.putNextEntry(entry);
       
   122                     if (is_file) { out.write(Files.readAllBytes(path)); }
       
   123                     out.closeEntry();
       
   124                 }
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129 
       
   130 
       
   131     /** build scala **/
    86 
   132 
    87     public static void build_scala(Context context, boolean fresh)
   133     public static void build_scala(Context context, boolean fresh)
    88         throws IOException, InterruptedException, NoSuchAlgorithmException
   134         throws IOException, InterruptedException, NoSuchAlgorithmException
    89     {
   135     {
    90         String jar_name = context.jar_name();
   136         String jar_name = context.jar_name();
   176                         Files.copy(context.path(name), target_file,
   222                         Files.copy(context.path(name), target_file,
   177                             StandardCopyOption.COPY_ATTRIBUTES);
   223                             StandardCopyOption.COPY_ATTRIBUTES);
   178                     }
   224                     }
   179 
   225 
   180 
   226 
   181                     /* jar */
   227                     /* packaging */
   182 
   228 
   183                     cmd.clear();
   229                     create_jar(build_dir, context.main(), context.path(jar_name));
   184                     cmd.add(Environment.platform_path(java_home + "/bin/jar"));
       
   185                     cmd.add("-c");
       
   186                     cmd.add("-f");
       
   187                     cmd.add(context.path(jar_name).toString());
       
   188                     if (!context.main().isEmpty()) {
       
   189                         cmd.add("-e");
       
   190                         cmd.add(context.main());
       
   191                     }
       
   192                     cmd.add(".");
       
   193 
       
   194                     res = Environment.exec_process(cmd, build_dir.toFile(), env, false);
       
   195                     if (!res.ok()) throw new RuntimeException(res.err());
       
   196 
       
   197 
       
   198                     /* shasum */
       
   199 
   230 
   200                     String shasum = context.shasum(jar_name) + shasum_sources;
   231                     String shasum = context.shasum(jar_name) + shasum_sources;
   201                     Files.writeString(context.path(shasum_name), shasum);
   232                     Files.writeString(context.path(shasum_name), shasum);
   202                 }
   233                 }
   203                 finally {
   234                 finally {