src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxRpmBundler.java
branchJDK-8200758-branch
changeset 58791 fca9cb5f4953
parent 58647 2c43b89b1679
child 58886 45bb0bebd36f
equal deleted inserted replaced
58764:015949faea55 58791:fca9cb5f4953
    30 import java.text.MessageFormat;
    30 import java.text.MessageFormat;
    31 import java.util.*;
    31 import java.util.*;
    32 import java.util.regex.Matcher;
    32 import java.util.regex.Matcher;
    33 import java.util.regex.Pattern;
    33 import java.util.regex.Pattern;
    34 import java.util.stream.Collectors;
    34 import java.util.stream.Collectors;
       
    35 import java.util.stream.Stream;
    35 
    36 
    36 import static jdk.jpackage.internal.StandardBundlerParam.*;
    37 import static jdk.jpackage.internal.StandardBundlerParam.*;
    37 import static jdk.jpackage.internal.LinuxAppBundler.LINUX_INSTALL_DIR;
    38 import static jdk.jpackage.internal.LinuxAppBundler.LINUX_INSTALL_DIR;
    38 import static jdk.jpackage.internal.OverridableResource.createResource;
    39 import static jdk.jpackage.internal.OverridableResource.createResource;
    39 
    40 
   149         createResource(DEFAULT_SPEC_TEMPLATE, params)
   150         createResource(DEFAULT_SPEC_TEMPLATE, params)
   150                 .setCategory(I18N.getString("resource.rpm-spec-file"))
   151                 .setCategory(I18N.getString("resource.rpm-spec-file"))
   151                 .setSubstitutionData(replacementData)
   152                 .setSubstitutionData(replacementData)
   152                 .saveToFile(specFile);
   153                 .saveToFile(specFile);
   153 
   154 
   154         return buildRPM(params, outputParentDir);
   155         return buildRPM(params, outputParentDir.toPath()).toFile();
   155     }
   156     }
   156 
   157 
   157     @Override
   158     @Override
   158     protected Map<String, String> createReplacementData(
   159     protected Map<String, String> createReplacementData(
   159             Map<String, ? super Object> params) throws IOException {
   160             Map<String, ? super Object> params) throws IOException {
   184                 "-q", "--whatprovides", file.toString())
   185                 "-q", "--whatprovides", file.toString())
   185                 .saveOutput(true).executeExpectSuccess().getOutput().stream();
   186                 .saveOutput(true).executeExpectSuccess().getOutput().stream();
   186         });
   187         });
   187     }
   188     }
   188 
   189 
       
   190     @Override
       
   191     protected List<ConfigException> verifyOutputBundle(
       
   192             Map<String, ? super Object> params, Path packageBundle) {
       
   193         List<ConfigException> errors = new ArrayList<>();
       
   194 
       
   195         String specFileName = specFile(params).getFileName().toString();
       
   196 
       
   197         try {
       
   198             List<PackageProperty> properties = List.of(
       
   199                     new PackageProperty("Name", PACKAGE_NAME.fetchFrom(params),
       
   200                             "APPLICATION_PACKAGE", specFileName),
       
   201                     new PackageProperty("Version", VERSION.fetchFrom(params),
       
   202                             "APPLICATION_VERSION", specFileName),
       
   203                     new PackageProperty("Release", RELEASE.fetchFrom(params),
       
   204                             "APPLICATION_RELEASE", specFileName),
       
   205                     new PackageProperty("Arch", rpmArch(), null, specFileName));
       
   206 
       
   207             List<String> actualValues = Executor.of(TOOL_RPM, "-qp", "--queryformat",
       
   208                     properties.stream().map(entry -> String.format("%%{%s}",
       
   209                     entry.name)).collect(Collectors.joining("\\n")),
       
   210                     packageBundle.toString()).saveOutput(true).executeExpectSuccess().getOutput();
       
   211 
       
   212             Iterator<String> actualValuesIt = actualValues.iterator();
       
   213             properties.forEach(property -> errors.add(property.verifyValue(
       
   214                     actualValuesIt.next())));
       
   215         } catch (IOException ex) {
       
   216             // Ignore error as it is not critical. Just report it.
       
   217             Log.verbose(ex);
       
   218         }
       
   219 
       
   220         return errors;
       
   221     }
       
   222 
       
   223     private String rpmArch() throws IOException {
       
   224         if (rpmArch == null) {
       
   225             rpmArch = Executor.of(TOOL_RPMBUILD, "--eval=%{_target_cpu}").saveOutput(
       
   226                     true).executeExpectSuccess().getOutput().get(0);
       
   227         }
       
   228         return rpmArch;
       
   229     }
       
   230 
   189     private Path specFile(Map<String, ? super Object> params) {
   231     private Path specFile(Map<String, ? super Object> params) {
   190         return TEMP_ROOT.fetchFrom(params).toPath().resolve(Path.of("SPECS",
   232         return TEMP_ROOT.fetchFrom(params).toPath().resolve(Path.of("SPECS",
   191                 PACKAGE_NAME.fetchFrom(params) + ".spec"));
   233                 PACKAGE_NAME.fetchFrom(params) + ".spec"));
   192     }
   234     }
   193 
   235 
   194     private File buildRPM(Map<String, ? super Object> params,
   236     private Path buildRPM(Map<String, ? super Object> params,
   195             File outdir) throws IOException {
   237             Path outdir) throws IOException {
       
   238 
       
   239         Path rpmFile = outdir.toAbsolutePath().resolve(String.format(
       
   240                 "%s-%s-%s.%s.rpm", PACKAGE_NAME.fetchFrom(params),
       
   241                 VERSION.fetchFrom(params), RELEASE.fetchFrom(params), rpmArch()));
       
   242 
   196         Log.verbose(MessageFormat.format(I18N.getString(
   243         Log.verbose(MessageFormat.format(I18N.getString(
   197                 "message.outputting-bundle-location"),
   244                 "message.outputting-bundle-location"),
   198                 outdir.getAbsolutePath()));
   245                 rpmFile.getParent()));
   199 
   246 
   200         PlatformPackage thePackage = createMetaPackage(params);
   247         PlatformPackage thePackage = createMetaPackage(params);
   201 
   248 
   202         //run rpmbuild
   249         //run rpmbuild
   203         Executor.of(
   250         Executor.of(
   204                 TOOL_RPMBUILD,
   251                 TOOL_RPMBUILD,
   205                 "-bb", specFile(params).toAbsolutePath().toString(),
   252                 "-bb", specFile(params).toAbsolutePath().toString(),
   206                 "--define", String.format("%%_sourcedir %s",
   253                 "--define", String.format("%%_sourcedir %s",
   207                         thePackage.sourceRoot()),
   254                         thePackage.sourceRoot()),
   208                 // save result to output dir
   255                 // save result to output dir
   209                 "--define", String.format("%%_rpmdir %s",
   256                 "--define", String.format("%%_rpmdir %s", rpmFile.getParent()),
   210                         outdir.getAbsolutePath()),
       
   211                 // do not use other system directories to build as current user
   257                 // do not use other system directories to build as current user
   212                 "--define", String.format("%%_topdir %s",
   258                 "--define", String.format("%%_topdir %s",
   213                         TEMP_ROOT.fetchFrom(params).toPath().toAbsolutePath())
   259                         TEMP_ROOT.fetchFrom(params).toPath().toAbsolutePath()),
       
   260                 "--define", String.format("%%_rpmfilename %s", rpmFile.getFileName())
   214         ).executeExpectSuccess();
   261         ).executeExpectSuccess();
   215 
   262 
   216         Log.verbose(MessageFormat.format(
   263         Log.verbose(MessageFormat.format(
   217                 I18N.getString("message.output-bundle-location"),
   264                 I18N.getString("message.output-bundle-location"),
   218                 outdir.getAbsolutePath()));
   265                 rpmFile.getParent()));
   219 
   266 
   220         // presume the result is the ".rpm" file with the newest modified time
   267         return rpmFile;
   221         // not the best solution, but it is the most reliable
       
   222         File result = null;
       
   223         long lastModified = 0;
       
   224         File[] list = outdir.listFiles();
       
   225         if (list != null) {
       
   226             for (File f : list) {
       
   227                 if (f.getName().endsWith(".rpm") &&
       
   228                         f.lastModified() > lastModified) {
       
   229                     result = f;
       
   230                     lastModified = f.lastModified();
       
   231                 }
       
   232             }
       
   233         }
       
   234 
       
   235         return result;
       
   236     }
   268     }
   237 
   269 
   238     @Override
   270     @Override
   239     public String getName() {
   271     public String getName() {
   240         return I18N.getString("rpm.bundler.name");
   272         return I18N.getString("rpm.bundler.name");
   253     @Override
   285     @Override
   254     public boolean isDefault() {
   286     public boolean isDefault() {
   255         return !LinuxDebBundler.isDebian();
   287         return !LinuxDebBundler.isDebian();
   256     }
   288     }
   257 
   289 
       
   290     private String rpmArch;
   258 }
   291 }