Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WINDUP-3721 Add '--exportZipReport' input option (#1629)
- Loading branch information
Showing
7 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
config/api/src/main/java/org/jboss/windup/config/ExportZipOutputOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.jboss.windup.config; | ||
|
||
import org.jboss.windup.graph.model.WindupConfigurationModel; | ||
import org.jboss.windup.util.ThemeProvider; | ||
|
||
/** | ||
* Indicates output path will be compressed in a ZIP file | ||
* | ||
* @author Marco Rizzi | ||
*/ | ||
public class ExportZipOutputOption extends AbstractConfigurationOption { | ||
public static final String NAME = WindupConfigurationModel.EXPORT_ZIP_REPORT; | ||
|
||
@Override | ||
public String getDescription() { | ||
return "If set, " + ThemeProvider.getInstance().getTheme().getBrandNameAcronym() + " will create a ZIP file containing the files from the output path."; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public String getLabel() { | ||
return "Should " + ThemeProvider.getInstance().getTheme().getBrandNameAcronym() + " create a ZIP file with the file in the output path?"; | ||
} | ||
|
||
@Override | ||
public Class<?> getType() { | ||
return Boolean.class; | ||
} | ||
|
||
@Override | ||
public InputType getUIType() { | ||
return InputType.SINGLE; | ||
} | ||
|
||
@Override | ||
public boolean isRequired() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public ValidationResult validate(Object value) { | ||
return ValidationResult.SUCCESS; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ing/impl/src/main/java/org/jboss/windup/reporting/export/ExportZipReportRuleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.jboss.windup.reporting.export; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.jboss.windup.config.AbstractRuleProvider; | ||
import org.jboss.windup.config.GraphRewrite; | ||
import org.jboss.windup.config.loader.RuleLoaderContext; | ||
import org.jboss.windup.config.metadata.RuleMetadata; | ||
import org.jboss.windup.config.operation.GraphOperation; | ||
import org.jboss.windup.config.phase.PostFinalizePhase; | ||
import org.jboss.windup.config.query.WindupConfigurationQuery; | ||
import org.jboss.windup.graph.GraphContextFactory; | ||
import org.jboss.windup.graph.model.WindupConfigurationModel; | ||
import org.jboss.windup.graph.model.resource.FileModel; | ||
import org.jboss.windup.graph.service.WindupConfigurationService; | ||
import org.jboss.windup.util.ZipUtil; | ||
import org.jboss.windup.util.exception.WindupException; | ||
import org.ocpsoft.rewrite.config.Configuration; | ||
import org.ocpsoft.rewrite.config.ConfigurationBuilder; | ||
import org.ocpsoft.rewrite.context.EvaluationContext; | ||
|
||
/** | ||
* Creates a ZIP file containing all the files in the output path | ||
* | ||
* @author Marco Rizzi | ||
*/ | ||
@RuleMetadata( | ||
afterIDs = "DeleteWorkDirsAtTheEndRuleProvider", | ||
description = "Creates a ZIP file containing all the files in the output path." | ||
+ " Use --" + WindupConfigurationModel.EXPORT_ZIP_REPORT + " to enable it.", | ||
phase = PostFinalizePhase.class | ||
) | ||
public class ExportZipReportRuleProvider extends AbstractRuleProvider { | ||
public static final String ZIP_REPORTS_NAME = "reports.zip"; | ||
@Override | ||
public Configuration getConfiguration(RuleLoaderContext ruleLoaderContext) { | ||
|
||
return ConfigurationBuilder.begin() | ||
.addRule() | ||
.when(WindupConfigurationQuery.hasOption(WindupConfigurationModel.EXPORT_ZIP_REPORT, true).as("discard")) | ||
.perform( | ||
new GraphOperation() { | ||
public void perform(GraphRewrite event, EvaluationContext context) { | ||
final WindupConfigurationModel windupConfiguration = WindupConfigurationService.getConfigurationModel(event.getGraphContext()); | ||
try { | ||
final FileModel outputFolderToZip = windupConfiguration.getOutputPath(); | ||
ZipUtil.zipFolder(outputFolderToZip.asFile().toPath(), outputFolderToZip.getFilePath(), ZIP_REPORTS_NAME, List.of(GraphContextFactory.DEFAULT_GRAPH_SUBDIRECTORY)); | ||
} catch (IOException e) { | ||
throw new WindupException(e); | ||
} | ||
} | ||
|
||
public String toString() { | ||
return "Create a ZIP file to collect the reports"; | ||
} | ||
} | ||
); | ||
} | ||
|
||
} |
135 changes: 135 additions & 0 deletions
135
reporting/tests/src/test/java/org/jboss/windup/reporting/ZipExportingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package org.jboss.windup.reporting; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.arquillian.AddonDependencies; | ||
import org.jboss.forge.arquillian.AddonDependency; | ||
import org.jboss.forge.arquillian.archive.AddonArchive; | ||
import org.jboss.forge.furnace.util.Predicate; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.windup.config.LegacyReportsRenderingOption; | ||
import org.jboss.windup.config.RuleProvider; | ||
import org.jboss.windup.config.SkipReportsRenderingOption; | ||
import org.jboss.windup.engine.predicates.RuleProviderWithDependenciesPredicate; | ||
import org.jboss.windup.exec.WindupProcessor; | ||
import org.jboss.windup.exec.configuration.WindupConfiguration; | ||
import org.jboss.windup.graph.GraphContext; | ||
import org.jboss.windup.graph.GraphContextFactory; | ||
import org.jboss.windup.reporting.export.ExportZipReportRuleProvider; | ||
import org.jboss.windup.rules.apps.java.config.ScanPackagesOption; | ||
import org.jboss.windup.rules.apps.java.config.SourceModeOption; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(Arquillian.class) | ||
public class ZipExportingTest { | ||
|
||
@Deployment | ||
@AddonDependencies({ | ||
@AddonDependency(name = "org.jboss.windup.config:windup-config"), | ||
@AddonDependency(name = "org.jboss.windup.graph:windup-graph"), | ||
@AddonDependency(name = "org.jboss.windup.reporting:windup-reporting"), | ||
@AddonDependency(name = "org.jboss.windup.exec:windup-exec"), | ||
@AddonDependency(name = "org.jboss.windup.rules.apps:windup-rules-java"), | ||
@AddonDependency(name = "org.jboss.windup.utils:windup-utils"), | ||
@AddonDependency(name = "org.jboss.forge.furnace.container:cdi") | ||
}) | ||
public static AddonArchive getDeployment() { | ||
return ShrinkWrap.create(AddonArchive.class).addBeansXML(); | ||
} | ||
|
||
@Inject | ||
private GraphContextFactory factory; | ||
|
||
@Inject | ||
private WindupProcessor processor; | ||
|
||
@Test | ||
public void testExportZipReport() throws Exception { | ||
zipReportTest(true, false, false); | ||
} | ||
|
||
@Test | ||
public void testExportZipReportLegacyReports() throws Exception { | ||
zipReportTest(true, true, false); | ||
} | ||
|
||
@Test | ||
public void testExportZipReportSkipReports() throws Exception { | ||
zipReportTest(true, false, true); | ||
} | ||
|
||
@Test | ||
public void testNotExportZipReport() throws Exception { | ||
zipReportTest(false, false, false); | ||
} | ||
|
||
private void zipReportTest(boolean exportZipReports, boolean legacyReports, boolean skipReports) throws Exception { | ||
final Path outputPath = Paths.get(FileUtils.getTempDirectory().toString(), | ||
"windup_" + RandomStringUtils.randomAlphanumeric(6)); | ||
|
||
outputPath.toFile().mkdirs(); | ||
try (GraphContext context = factory.create(true)) { | ||
String inputPath = "src/test/resources"; | ||
Predicate<RuleProvider> predicate = new RuleProviderWithDependenciesPredicate(ExportZipReportRuleProvider.class); | ||
WindupConfiguration configuration = new WindupConfiguration() | ||
.setGraphContext(context) | ||
.setRuleProviderFilter(predicate) | ||
.addInputPath(Paths.get(inputPath)) | ||
.setOutputDirectory(outputPath) | ||
.setOptionValue(ScanPackagesOption.NAME, Collections.singletonList("")) | ||
.setOptionValue(SourceModeOption.NAME, true); | ||
if (exportZipReports) { | ||
configuration.setExportingZipReport(true); | ||
configuration.setOptionValue(LegacyReportsRenderingOption.NAME, legacyReports); | ||
if (skipReports) configuration.setOptionValue(SkipReportsRenderingOption.NAME, true); | ||
} | ||
processor.execute(configuration); | ||
final File reportsZipFile = new File(outputPath + "/reports.zip"); | ||
Assert.assertEquals(exportZipReports, reportsZipFile.exists()); | ||
if (exportZipReports) { | ||
try (ZipFile reportsZip = new ZipFile(reportsZipFile); | ||
Stream<Path> outputFiles = Files.walk(outputPath.toAbsolutePath())){ | ||
final Set<String> zipContent = reportsZip.stream() | ||
.map(ZipEntry::getName) | ||
.sorted() | ||
.collect(Collectors.toCollection(LinkedHashSet::new)); | ||
Assert.assertTrue( | ||
"Zip file content is different from the output folder content", | ||
outputFiles | ||
// reports.zip file itself and root folder are not part of the ZIP file | ||
.filter(file -> !file.endsWith("reports.zip") && !outputPath.equals(file)) | ||
.map(path -> { | ||
final Path relativePath = outputPath.relativize(path); | ||
// in the ZipFile, the directories end with "/" but they don't in the Files.walk | ||
if (Files.isDirectory(path)) { | ||
return relativePath + File.separator; | ||
} | ||
else { | ||
return relativePath.toString(); | ||
} | ||
}) | ||
// check every file in the output path is contained in the ZIP file | ||
.allMatch(zipContent::contains)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters