first commit

This commit is contained in:
Geert Bevin 2023-04-07 21:22:26 -04:00
commit 95412fbe7e
23 changed files with 946 additions and 0 deletions

26
.github/workflows/bld.yml vendored Normal file
View file

@ -0,0 +1,26 @@
name: bld-ci
on: [push, pull_request, workflow_dispatch]
jobs:
build-project:
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [ 17, 19, 20 ]
steps:
- name: Checkout source repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: ${{ matrix.java-version }}
- name: Run tests
run: ./bld download compile test

57
.github/workflows/pages.yml vendored Normal file
View file

@ -0,0 +1,57 @@
name: javadocs-pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow one concurrent deployment
concurrency:
group: "pages"
cancel-in-progress: true
jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout source repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 19
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 17
- name: Build Javadocs
run: ./bld download clean javadoc
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload generated Javadocs repository
path: 'build/javadoc/'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1

55
.gitignore vendored Normal file
View file

@ -0,0 +1,55 @@
.gradle
.DS_Store
build
lib/bld/**
lib/compile/**
lib/runtime/**
lib/standalone/**
lib/test/**
!bld-wrapper.jar
!bld-wrapper.properties
# IDEA ignores
# User-specific
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Editor-based Rest Client
.idea/httpRequests

8
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

29
.idea/app.iml generated Normal file
View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/build/main" />
<output-test url="file://$MODULE_DIR$/build/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/src/main/resources/templates" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="RUNTIME">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/src/main/resources/templates" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="compile" level="project" />
<orderEntry type="library" scope="RUNTIME" name="runtime" level="project" />
<orderEntry type="library" scope="TEST" name="test" level="project" />
</component>
</module>

14
.idea/bld.iml generated Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/build/bld" />
<output-test url="file://$MODULE_DIR$/build/bld" />
<exclude-output />
<content url="file://$MODULE_DIR$/src/bld">
<sourceFolder url="file://$MODULE_DIR$/src/bld/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="bld" level="project" />
</component>
</module>

17
.idea/libraries/bld.xml generated Normal file
View file

@ -0,0 +1,17 @@
<component name="libraryTable">
<library name="bld">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/bld" />
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.18-SNAPSHOT7.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$USER_HOME$/.rife2/dist/rife2-1.5.18-SNAPSHOT7-sources.jar!/" />
</SOURCES>
<excluded>
<root url="jar://$PROJECT_DIR$/lib/bld/bld-wrapper.jar!/" />
</excluded>
<jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/bld" recursive="false" type="SOURCES" />
</library>
</component>

13
.idea/libraries/compile.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="compile">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/compile" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/compile" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/compile" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/compile" recursive="false" type="SOURCES" />
</library>
</component>

13
.idea/libraries/runtime.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="runtime">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/runtime" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/runtime" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/runtime" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/runtime" recursive="false" type="SOURCES" />
</library>
</component>

13
.idea/libraries/test.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="test">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/test" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/test" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/test" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/test" recursive="false" type="SOURCES" />
</library>
</component>

9
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PDMPlugin">
<option name="skipTestSources" value="false" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build" />
</component>
</project>

9
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/app.iml" filepath="$PROJECT_DIR$/.idea/app.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/bld.iml" filepath="$PROJECT_DIR$/.idea/bld.iml" />
</modules>
</component>
</project>

9
.idea/runConfigurations/Run Main.xml generated Normal file
View file

@ -0,0 +1,9 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Main" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="rife.bld.extension.ArchiveMain" />
<module name="app" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

13
.idea/runConfigurations/Run Tests.xml generated Normal file
View file

@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Tests" type="JUnit" factoryName="JUnit">
<module name="app" />
<option name="PACKAGE_NAME" value="moog" />
<option name="MAIN_CLASS_NAME" value="rife.bld.extension" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="directory" />
<dir value="$PROJECT_DIR$/src/test/java" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

24
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Run Main",
"request": "launch",
"mainClass": "rife.bld.extension.ArchiveMain"
},
{
"type": "java",
"name": "Run Tests",
"request": "launch",
"mainClass": "org.junit.platform.console.ConsoleLauncher",
"args": [
"--details=verbose",
"--scan-classpath",
"--disable-banner",
"--disable-ansi-colors",
"--exclude-engine=junit-platform-suite",
"--exclude-engine=junit-vintage"]
}
]
}

15
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,15 @@
{
"java.project.sourcePaths": [
"src/main/java",
"src/main/resources",
"src/test/java",
"src/bld/java"
],
"java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [
"${HOME}/.rife2/dist/rife2-1.5.18.jar",
"lib/compile/*.jar",
"lib/runtime/*.jar",
"lib/test/*.jar"
]
}

2
bld Executable file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env sh
java -jar "$(dirname "$0")/lib/bld/bld-wrapper.jar" "$0" --build rife.bld.extension.ArchiveBuild "$@"

4
bld.bat Normal file
View file

@ -0,0 +1,4 @@
@echo off
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
java -jar "%DIRNAME%/lib/bld/bld-wrapper.jar" "%0" --build rife.bld.extension.ArchiveBuild %*

BIN
lib/bld/bld-wrapper.jar Normal file

Binary file not shown.

View file

@ -0,0 +1,6 @@
bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true
bld.extensions=
bld.repositories=MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
rife2.downloadLocation=https://uwyn.com
rife2.version=1.5.18-SNAPSHOT7

View file

@ -0,0 +1,64 @@
package rife.bld.extension;
import rife.bld.Project;
import rife.bld.publish.PublishDeveloper;
import rife.bld.publish.PublishLicense;
import rife.bld.publish.PublishScm;
import java.util.List;
import static rife.bld.dependencies.Repository.*;
import static rife.bld.dependencies.Scope.*;
import static rife.bld.operations.JavadocOptions.DocLinkOption.NO_MISSING;
public class ArchiveBuild extends Project {
public ArchiveBuild() {
pkg = "rife.bld.extension";
name = "Archive";
version = version(0,1,0,"SNAPSHOT");
archiveBaseName = "bld-archive";
javaRelease = 17;
downloadSources = true;
autoDownloadPurge = true;
repositories = List.of(MAVEN_CENTRAL, RIFE2_SNAPSHOTS, RIFE2_RELEASES);
scope(compile)
.include(dependency("com.uwyn.rife2", "rife2", version(1,5,18,"SNAPSHOT")))
.include(dependency("org.apache.commons", "commons-compress", version(1,23,0)));
scope(test)
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5,9,2)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1,9,2)));
javadocOperation()
.javadocOptions()
.docLint(NO_MISSING)
.link("https://rife2.github.io/rife2/");
publishOperation()
.repository(version.isSnapshot() ? repository("rife2-snapshots") : repository("rife2-releases"))
.info()
.groupId("com.uwyn.rife2")
.artifactId("bld-archive")
.description("bld extension for creating archives")
.url("https://github.com/rife2/bld-archive")
.developer(new PublishDeveloper()
.id("gbevin")
.name("Geert Bevin")
.email("gbevin@uwyn.com")
.url("https://github.com/gbevin"))
.license(new PublishLicense()
.name("The Apache License, Version 2.0")
.url("https://www.apache.org/licenses/LICENSE-2.0.txt"))
.scm(new PublishScm()
.connection("scm:git:https://github.com/rife2/bld-archive.git")
.developerConnection("scm:git:git@github.com:rife2/bld-archive.git")
.url("https://github.com/rife2/bld-archive"))
.signKey(property("sign.key"))
.signPassphrase(property("sign.passphrase"));
}
public static void main(String[] args) {
new ArchiveBuild().start(args);
}
}

View file

@ -0,0 +1,374 @@
/*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.bld.extension;
import org.apache.commons.compress.archivers.zip.UnixStat;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import rife.bld.NamedFile;
import rife.bld.operations.AbstractOperation;
import rife.tools.FileUtils;
import rife.tools.StringUtils;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.regex.Pattern;
/**
* Creates a zip archive of the provided sources and directories.
*
* @author Geert Bevin (gbevin[remove] at uwyn dot com)
* @since 1.0
*/
public class ZipOperation extends AbstractOperation<ZipOperation> {
private final List<File> sourceDirectories_ = new ArrayList<>();
private final List<NamedFile> sourceFiles_ = new ArrayList<>();
private File destinationDirectory_;
private String destinationFileName_;
private final List<Pattern> included_ = new ArrayList<>();
private final List<Pattern> excluded_ = new ArrayList<>();
/**
* Performs the zip operation.
*
* @throws IOException when an exception occurred during the zip creation process
* @since 1.0
*/
public void execute()
throws IOException {
executeCreateDestinationDirectory();
executeCreateZipArchive();
if (!silent()) {
System.out.println("The zip archive was created at '" + destinationFile() + "'");
}
}
/**
* Part of the {@link #execute} operation, create the destination directory.
*
* @since 1.0
*/
protected void executeCreateDestinationDirectory() {
destinationDirectory().mkdirs();
}
/**
* Part of the {@link #execute} operation, create the zip archive.
*
* @since 1.0
*/
protected void executeCreateZipArchive()
throws IOException {
var out_file = new File(destinationDirectory(), destinationFileName());
try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(out_file)) {
for (var source_dir : sourceDirectories()) {
for (var file_name : FileUtils.getFileList(source_dir)) {
var file = new File(source_dir, file_name);
if (StringUtils.filter(file.getAbsolutePath(), included(), excluded(), false)) {
executeAddFileToZip(zip, new NamedFile(file_name, file));
}
}
}
for (var source_file : sourceFiles()) {
if (StringUtils.filter(source_file.file().getAbsolutePath(), included(), excluded(), false)) {
executeAddFileToZip(zip, source_file);
}
}
zip.finish();
}
}
/**
* Part of the {@link #execute} operation, add a single file to the zip archive.
*
* @since 1.0
*/
protected void executeAddFileToZip(ZipArchiveOutputStream zip, NamedFile file)
throws IOException {
var entry = new ZipArchiveEntry(file.file(), file.name().replace('\\', '/'));
entry.setTime(file.file().lastModified());
int mode = 0;
if (file.file().isFile()) {
mode = UnixStat.FILE_FLAG;
} else if (file.file().isDirectory()) {
mode = UnixStat.DIR_FLAG;
}
if (mode != 0) {
var permissions = Files.getPosixFilePermissions(file.file().toPath());
var owner = 0;
var group = 0;
var others = 0;
for (var p : permissions) {
switch (p) {
case OWNER_READ -> owner += 4;
case OWNER_WRITE -> owner += 2;
case OWNER_EXECUTE -> owner += 1;
case GROUP_READ -> group += 4;
case GROUP_WRITE -> group += 2;
case GROUP_EXECUTE -> group += 1;
case OTHERS_READ -> others += 4;
case OTHERS_WRITE -> others += 2;
case OTHERS_EXECUTE -> others += 1;
}
}
mode = owner * 64 + group * 8 + others;
entry.setUnixMode(mode);
}
zip.putArchiveEntry(entry);
if (file.file().isFile()) {
try (InputStream i = Files.newInputStream(file.file().toPath())) {
IOUtils.copy(i, zip);
}
}
zip.closeArchiveEntry();
}
/**
* Provides source directories that will be used for the zip archive creation.
*
* @param directories source directories
* @return this operation instance
* @since 1.0
*/
public ZipOperation sourceDirectories(File... directories) {
sourceDirectories_.addAll(List.of(directories));
return this;
}
/**
* Provides a list of source directories that will be used for the zip archive creation.
* <p>
* A copy will be created to allow this list to be independently modifiable.
*
* @param directories a list of source directories
* @return this operation instance
* @since 1.0
*/
public ZipOperation sourceDirectories(List<File> directories) {
sourceDirectories_.addAll(directories);
return this;
}
/**
* Provides source files that will be used for the zip archive creation.
*
* @param files source files
* @return this operation instance
* @since 1.0
*/
public ZipOperation sourceFiles(NamedFile... files) {
sourceFiles_.addAll(List.of(files));
return this;
}
/**
* Provides a list of source files that will be used for the zip archive creation.
* <p>
* A copy will be created to allow this list to be independently modifiable.
*
* @param files a list of source files
* @return this operation instance
* @since 1.0
*/
public ZipOperation sourceFiles(List<NamedFile> files) {
sourceFiles_.addAll(files);
return this;
}
/**
* Provides the destination directory in which the zip archive will be created.
*
* @param directory the zip destination directory
* @return this operation instance
* @since 1.0
*/
public ZipOperation destinationDirectory(File directory) {
destinationDirectory_ = directory;
return this;
}
/**
* Provides the destination file name that will be used for the zip archive creation.
*
* @param name the zip archive destination file name
* @return this operation instance
* @since 1.0
*/
public ZipOperation destinationFileName(String name) {
destinationFileName_ = name;
return this;
}
/**
* Provides regex patterns that will be found to determine which files
* will be included in the javadoc generation.
*
* @param included inclusion patterns
* @return this operation instance
* @since 1.0
*/
public ZipOperation included(String... included) {
included_.addAll(Arrays.stream(included).map(Pattern::compile).toList());
return this;
}
/**
* Provides patterns that will be found to determine which files
* will be included in the zip archive.
*
* @param included inclusion patterns
* @return this operation instance
* @since 1.0
*/
public ZipOperation included(Pattern... included) {
included_.addAll(List.of(included));
return this;
}
/**
* Provides a list of patterns that will be found to determine which files
* will be included in the zip archive.
* <p>
* A copy will be created to allow this list to be independently modifiable.
*
* @param included a list of inclusion patterns
* @return this operation instance
* @since 1.0
*/
public ZipOperation included(List<Pattern> included) {
included_.addAll(included);
return this;
}
/**
* Provides regex patterns that will be found to determine which files
* will be excluded from the javadoc generation.
*
* @param excluded exclusion patterns
* @return this operation instance
* @since 1.0
*/
public ZipOperation excluded(String... excluded) {
excluded_.addAll(Arrays.stream(excluded).map(Pattern::compile).toList());
return this;
}
/**
* Provides patterns that will be found to determine which files
* will be excluded from the zip archive.
*
* @param excluded exclusion patterns
* @return this operation instance
* @since 1.0
*/
public ZipOperation excluded(Pattern... excluded) {
excluded_.addAll(List.of(excluded));
return this;
}
/**
* Provides a list of patterns that will be found to determine which files
* will be excluded from the zip archive.
* <p>
* A copy will be created to allow this list to be independently modifiable.
*
* @param excluded a list of exclusion patterns
* @return this operation instance
* @since 1.0
*/
public ZipOperation excluded(List<Pattern> excluded) {
excluded_.addAll(excluded);
return this;
}
/**
* Retrieves the list of source directories that will be used for the
* zip archive creation.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the zip archive's source directories
* @since 1.0
*/
public List<File> sourceDirectories() {
return sourceDirectories_;
}
/**
* Retrieves the list of source files that will be used for the
* zip archive creation.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the zip archive's source files
* @since 1.0
*/
public List<NamedFile> sourceFiles() {
return sourceFiles_;
}
/**
* Retrieves the destination directory in which the zip archive will
* be created.
*
* @return the zip archive's destination directory
* @since 1.0
*/
public File destinationDirectory() {
return destinationDirectory_;
}
/**
* Retrieves the destination file name that will be used for the zip
* archive creation.
*
* @return the zip archive's destination file name
* @since 1.0
*/
public String destinationFileName() {
return destinationFileName_;
}
/**
* Retrieves the destination file where the zip archive will be created.
*
* @return the zip archive's destination file
* @since 1.0
*/
public File destinationFile() {
return new File(destinationDirectory(), destinationFileName());
}
/**
* Retrieves the list of patterns that will be evaluated to determine which files
* will be included in the zip archive.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the zip's archive's inclusion patterns
* @since 1.0
*/
public List<Pattern> included() {
return included_;
}
/**
* Retrieves the list of patterns that will be evaluated to determine which files
* will be excluded the zip archive.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the zip's archive's exclusion patterns
* @since 1.0
*/
public List<Pattern> excluded() {
return excluded_;
}
}

View file

@ -0,0 +1,172 @@
/*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.bld.extension;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.junit.jupiter.api.Test;
import rife.bld.NamedFile;
import rife.tools.FileUtils;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import static java.nio.file.attribute.PosixFilePermission.*;
import static org.junit.jupiter.api.Assertions.*;
public class TestZipOperation {
@Test
void testInstantiation() {
var operation = new ZipOperation();
assertTrue(operation.sourceDirectories().isEmpty());
assertTrue(operation.sourceFiles().isEmpty());
assertNull(operation.destinationDirectory());
assertNull(operation.destinationFileName());
assertTrue(operation.included().isEmpty());
assertTrue(operation.excluded().isEmpty());
}
@Test
void testPopulation() {
var source_directory1 = new File("sourceDirectory1");
var source_directory2 = new File("sourceDirectory2");
var source_file1 = new NamedFile("sourceFile1", new File("sourceFile1"));
var source_file2 = new NamedFile("sourceFile2", new File("sourceFile2"));
var destination_directory = new File("destinationDirectory");
var destination_fileName = "destinationFileName";
var included1 = Pattern.compile("included1");
var included2 = Pattern.compile("included2");
var excluded1 = Pattern.compile("excluded1");
var excluded2 = Pattern.compile("excluded2");
var operation1 = new ZipOperation()
.sourceDirectories(List.of(source_directory1, source_directory2))
.sourceFiles(List.of(source_file1, source_file2))
.destinationDirectory(destination_directory)
.destinationFileName(destination_fileName)
.included(List.of(included1, included2))
.excluded(List.of(excluded1, excluded2));
assertTrue(operation1.sourceDirectories().contains(source_directory1));
assertTrue(operation1.sourceDirectories().contains(source_directory2));
assertTrue(operation1.sourceFiles().contains(source_file1));
assertTrue(operation1.sourceFiles().contains(source_file2));
assertEquals(destination_directory, operation1.destinationDirectory());
assertEquals(destination_fileName, operation1.destinationFileName());
assertEquals(new File(destination_directory, destination_fileName), operation1.destinationFile());
assertTrue(operation1.included().contains(included1));
assertTrue(operation1.included().contains(included2));
assertTrue(operation1.excluded().contains(excluded1));
assertTrue(operation1.excluded().contains(excluded2));
var operation2 = new ZipOperation()
.destinationDirectory(destination_directory)
.destinationFileName(destination_fileName);
operation2.sourceDirectories().add(source_directory1);
operation2.sourceDirectories().add(source_directory2);
operation2.sourceFiles().add(source_file1);
operation2.sourceFiles().add(source_file2);
operation2.included().add(included1);
operation2.included().add(included2);
operation2.excluded().add(excluded1);
operation2.excluded().add(excluded2);
assertTrue(operation2.sourceDirectories().contains(source_directory1));
assertTrue(operation2.sourceDirectories().contains(source_directory2));
assertTrue(operation2.sourceFiles().contains(source_file1));
assertTrue(operation2.sourceFiles().contains(source_file2));
assertEquals(destination_directory, operation2.destinationDirectory());
assertEquals(destination_fileName, operation2.destinationFileName());
assertTrue(operation2.included().contains(included1));
assertTrue(operation2.included().contains(included2));
assertTrue(operation2.excluded().contains(excluded1));
assertTrue(operation2.excluded().contains(excluded2));
var operation3 = new ZipOperation()
.sourceDirectories(source_directory1, source_directory2)
.sourceFiles(source_file1, source_file2)
.destinationDirectory(destination_directory)
.destinationFileName(destination_fileName)
.included(included1, included2)
.excluded(excluded1, excluded2);
assertTrue(operation3.sourceDirectories().contains(source_directory1));
assertTrue(operation3.sourceDirectories().contains(source_directory2));
assertTrue(operation3.sourceFiles().contains(source_file1));
assertTrue(operation3.sourceFiles().contains(source_file2));
assertEquals(destination_directory, operation3.destinationDirectory());
assertEquals(destination_fileName, operation3.destinationFileName());
assertTrue(operation3.included().contains(included1));
assertTrue(operation3.included().contains(included2));
assertTrue(operation3.excluded().contains(excluded1));
assertTrue(operation3.excluded().contains(excluded2));
}
@Test
void testExecute()
throws Exception {
var tmp = Files.createTempDirectory("test").toFile();
try {
var source_dir = new File(tmp, "source");
var destination_dir = new File(tmp, "destination");
var destination_name = "archive.zip";
source_dir.mkdirs();
var source1 = new File(source_dir, "source1.text");
var source2 = new File(source_dir, "source2.text");
var source3 = new File(source_dir, "source3.text");
var source4 = new File(source_dir, "source4.txt");
var source5 = new File(tmp, "source5.text");
var source6 = new File(tmp, "source6.text");
FileUtils.writeString("source1", source1);
FileUtils.writeString("source2", source2);
FileUtils.writeString("source3", source3);
FileUtils.writeString("source4", source4);
FileUtils.writeString("source5", source5);
FileUtils.writeString("source6", source6);
Files.setPosixFilePermissions(source1.toPath(), Set.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE));
Files.setPosixFilePermissions(source2.toPath(), Set.of(OWNER_READ, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE));
Files.setPosixFilePermissions(source3.toPath(), Set.of(OWNER_READ, OTHERS_READ, OTHERS_WRITE, OTHERS_EXECUTE));
new ZipOperation()
.sourceDirectories(List.of(source_dir))
.sourceFiles(List.of(
new NamedFile("src5.txt", source5),
new NamedFile("src6.txt", source6)))
.destinationDirectory(destination_dir)
.destinationFileName(destination_name)
.included("source.*\\.text")
.excluded("source5.*")
.execute();
var zip_archive = new File(destination_dir, destination_name);
assertTrue(zip_archive.exists());
var content = new StringBuilder();
try (var zip = new ZipFile(zip_archive)) {
var e = zip.getEntries();
while (e.hasMoreElements()) {
var zip_entry = e.nextElement();
content.append(zip_entry.getName());
content.append(" ");
content.append(zip_entry.getUnixMode());
content.append("\n");
}
}
assertEquals("""
source1.text 448
source2.text 312
source3.text 263
src6.txt 420
""", content.toString());
} finally {
FileUtils.deleteDirectory(tmp);
}
}
}