Skip to content

Commit

Permalink
#358 CopyMojo
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 5, 2021
1 parent 69e965c commit 6c2bfd3
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
110 changes: 110 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2021 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.io.InputOf;
import org.cactoos.text.TextOf;
import org.slf4j.impl.StaticLoggerBinder;

/**
* Make a file with a listing of all EO objects in this module.
*
* @since 0.11
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@Mojo(
name = "copy-sources",
defaultPhase = LifecyclePhase.GENERATE_SOURCES,
threadSafe = true
)
@SuppressWarnings("PMD.ImmutableField")
public final class CopyMojo extends AbstractMojo {

/**
* Replacer or version.
*/
private static final Pattern REPLACE = Pattern.compile(
"^(\\+rt .+):0\\.0\\.0(.*)$",
Pattern.MULTILINE
);

/**
* Directory in which .eo files are located.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
required = true,
defaultValue = "${project.basedir}/src/main/eo"
)
private File sourcesDir;

/**
* Target directory with resources to be packaged in JAR.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
required = true,
defaultValue = "${project.build.directory}/classes"
)
private File classesDir;

/**
* The version to use for 0.0.0 replacements.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(required = true, defaultValue = "${project.version}")
private String version;

@Override
public void execute() throws MojoFailureException {
StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
new Walk(this.sourcesDir.toPath()).forEach(
src -> {
try {
new Save(
CopyMojo.REPLACE.matcher(new TextOf(new InputOf(src)).asString()).replaceAll(
String.format("\1:%s\2", this.version)
),
this.classesDir.toPath().resolve("EO-SOURCES").resolve(
src.toAbsolutePath().toString().substring(
this.sourcesDir.toPath().toAbsolutePath().toString().length() + 1
)
)
).save();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@

/**
* Goes through all .class files and deletes those that
* were created from autogenerated sources.
* were created from autogenerated sources; also it deletes
* binary files, which were previously copied by "resolve" mojo.
*
* @since 0.1
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
Expand Down
69 changes: 69 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/CopyMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2021 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Test case for {@link CopyMojo}.
*
* @since 0.1
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class CopyMojoTest {

@Test
public void copiesSources(@TempDir final Path temp) throws Exception {
final Path src = temp.resolve("src");
final Path classes = temp.resolve("classes");
new Save(
"+rt foo:0.0.0\n\n[args] > main\n \"0.0.0\" > @\n",
src.resolve("foo/main.eo")
).save();
new Moja<>(CopyMojo.class)
.with("sourcesDir", src.toFile())
.with("classesDir", classes.toFile())
.with("version", "1.1.1")
.execute();
final Path out = classes.resolve("EO-SOURCES/foo/main.eo");
MatcherAssert.assertThat(
Files.exists(out),
Matchers.is(true)
);
MatcherAssert.assertThat(
new String(Files.readAllBytes(out), StandardCharsets.UTF_8),
Matchers.allOf(
Matchers.containsString("0.0.0"),
Matchers.containsString("1.1.1")
)
);
}

}

0 comments on commit 6c2bfd3

Please sign in to comment.