-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce hawkeye-maven-plugin (#84)
Signed-off-by: spencercjh <[email protected]>
- Loading branch information
1 parent
a519041
commit fbcdd4e
Showing
12 changed files
with
543 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!-- | ||
Copyright 2023 Korandoru Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.korandoru.hawkeye</groupId> | ||
<artifactId>hawkeye</artifactId> | ||
<version>3.2.1-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>hawkeye-maven-plugin</artifactId> | ||
<packaging>maven-plugin</packaging> | ||
<name>Hawkeve :: Maven Plugin</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.korandoru.hawkeye</groupId> | ||
<artifactId>hawkeye-core</artifactId> | ||
<version>3.2.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-plugin-api</artifactId> | ||
<version>${maven-plugin-api.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-tools</groupId> | ||
<artifactId>maven-plugin-annotations</artifactId> | ||
<version>${maven-plugin-annotation.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-plugin-plugin</artifactId> | ||
<version>${maven-plugin-plugin.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
28 changes: 28 additions & 0 deletions
28
hawkeye-maven-plugin/src/main/java/io/korandoru/hawkeye/AbstractMojo.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,28 @@ | ||
/* | ||
* Copyright 2023 Korandoru Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.korandoru.hawkeye; | ||
|
||
import java.io.File; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
abstract class AbstractMojo extends org.apache.maven.plugin.AbstractMojo { | ||
@Parameter(name = "config", alias = "cfg", defaultValue = "${project.basedir}/licenserc.toml") | ||
public File config; | ||
|
||
@Parameter(name = "dryRun", defaultValue = "false") | ||
public boolean dryRun; | ||
} |
62 changes: 62 additions & 0 deletions
62
hawkeye-maven-plugin/src/main/java/io/korandoru/hawkeye/CheckMojo.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,62 @@ | ||
/* | ||
* Copyright 2023 Korandoru Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.korandoru.hawkeye; | ||
|
||
import io.korandoru.hawkeye.core.LicenseChecker; | ||
import io.korandoru.hawkeye.core.config.HawkEyeConfig; | ||
import io.korandoru.hawkeye.core.report.Report; | ||
import io.korandoru.hawkeye.core.report.ReportConstants; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
|
||
@Mojo(name = "check") | ||
public class CheckMojo extends AbstractMojo { | ||
@Override | ||
public void execute() throws MojoFailureException { | ||
final Log log = getLog(); | ||
log.info("Checking license headers... with cfg: %s".formatted(config)); | ||
final HawkEyeConfig heConfig = HawkEyeConfig.of(config).build(); | ||
final LicenseChecker checker = new LicenseChecker(heConfig); | ||
final Report report = checker.call(); | ||
|
||
final List<String> unknownHeaderFiles = report.getResults().entrySet().stream() | ||
.filter(e -> ReportConstants.RESULT_UNKNOWN.equals(e.getValue())) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
for (String unknownHeaderFile : unknownHeaderFiles) { | ||
log.warn("Processing unknown file: %s".formatted(unknownHeaderFile)); | ||
} | ||
|
||
final List<String> missingHeaderFiles = report.getResults().entrySet().stream() | ||
.filter(e -> ReportConstants.RESULT_MISSING.equals(e.getValue())) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
if (missingHeaderFiles.isEmpty()) { | ||
log.info("No missing header file has been found."); | ||
return; | ||
} | ||
for (String missingHeaderFile : missingHeaderFiles) { | ||
log.error("Found missing header file: %s".formatted(missingHeaderFile)); | ||
} | ||
throw new MojoFailureException("Missing header files found"); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
hawkeye-maven-plugin/src/main/java/io/korandoru/hawkeye/FormatMojo.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,64 @@ | ||
/* | ||
* Copyright 2023 Korandoru Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.korandoru.hawkeye; | ||
|
||
import io.korandoru.hawkeye.core.LicenseFormatter; | ||
import io.korandoru.hawkeye.core.config.HawkEyeConfig; | ||
import io.korandoru.hawkeye.core.report.Report; | ||
import io.korandoru.hawkeye.core.report.ReportConstants; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
|
||
@Mojo(name = "format") | ||
public class FormatMojo extends AbstractMojo { | ||
@Override | ||
public void execute() { | ||
final Log log = getLog(); | ||
log.info("Formatting license headers... with cfg: %s, dryRun: %s".formatted(config, dryRun)); | ||
|
||
final HawkEyeConfig heConfig = HawkEyeConfig.of(config).dryRun(dryRun).build(); | ||
final LicenseFormatter checker = new LicenseFormatter(heConfig); | ||
final Report report = checker.call(); | ||
|
||
final List<String> unknownHeaderFiles = report.getResults().entrySet().stream() | ||
.filter(e -> ReportConstants.RESULT_UNKNOWN.equals(e.getValue())) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
for (String unknownHeaderFile : unknownHeaderFiles) { | ||
log.warn("Processing unknown file: %s".formatted(unknownHeaderFile)); | ||
} | ||
|
||
final List<Map.Entry<String, String>> updatedHeaderFiles = report.getResults().entrySet().stream() | ||
.filter(e -> !ReportConstants.RESULT_UNKNOWN.equals(e.getValue())) | ||
.filter(e -> !ReportConstants.RESULT_NOOP.equals(e.getValue())) | ||
.toList(); | ||
|
||
if (updatedHeaderFiles.isEmpty()) { | ||
log.info("All files have proper header."); | ||
return; | ||
} | ||
|
||
if (!dryRun) { | ||
for (Map.Entry<String, String> updatedHeaderFile : updatedHeaderFiles) { | ||
log.info("Updated header for file: %s".formatted(updatedHeaderFile.getKey())); | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
hawkeye-maven-plugin/src/main/java/io/korandoru/hawkeye/RemoveMojo.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,63 @@ | ||
/* | ||
* Copyright 2023 Korandoru Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.korandoru.hawkeye; | ||
|
||
import io.korandoru.hawkeye.core.LicenseRemover; | ||
import io.korandoru.hawkeye.core.config.HawkEyeConfig; | ||
import io.korandoru.hawkeye.core.report.Report; | ||
import io.korandoru.hawkeye.core.report.ReportConstants; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
|
||
@Mojo(name = "remove") | ||
public class RemoveMojo extends AbstractMojo { | ||
@Override | ||
public void execute() { | ||
final Log log = getLog(); | ||
log.info("Removing license headers... with cfg: %s, dryRun: %s".formatted(config, dryRun)); | ||
|
||
final HawkEyeConfig heConfig = HawkEyeConfig.of(config).dryRun(dryRun).build(); | ||
final LicenseRemover remover = new LicenseRemover(heConfig); | ||
final Report report = remover.call(); | ||
|
||
final List<String> unknownHeaderFiles = report.getResults().entrySet().stream() | ||
.filter(e -> ReportConstants.RESULT_UNKNOWN.equals(e.getValue())) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
for (String unknownHeaderFile : unknownHeaderFiles) { | ||
log.warn("Processing unknown file: %s".formatted(unknownHeaderFile)); | ||
} | ||
|
||
final List<String> removedHeaderFiles = report.getResults().entrySet().stream() | ||
.filter(e -> ReportConstants.RESULT_REMOVED.equals(e.getValue())) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
if (removedHeaderFiles.isEmpty()) { | ||
log.info("No file has been removed header."); | ||
return; | ||
} | ||
if (!dryRun) { | ||
for (String removedHeaderFile : removedHeaderFiles) { | ||
log.info("Removed header for file: %s".formatted(removedHeaderFile)); | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
hawkeye-maven-plugin/src/test/java/io/korandoru/hawkeye/CheckMojoTest.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,48 @@ | ||
/* | ||
* Copyright 2023 Korandoru Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.korandoru.hawkeye; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CheckMojoTest { | ||
|
||
CheckMojo checkMojo; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
checkMojo = new CheckMojo(); | ||
checkMojo.config = new File("src/test/resources/t1.toml"); | ||
} | ||
|
||
@Test | ||
void execute() { | ||
assertDoesNotThrow(() -> checkMojo.execute()); | ||
} | ||
|
||
@Test | ||
void executeFailure() throws IOException { | ||
final File tempFile = File.createTempFile("test", ".yaml", new File("src/test/resources")); | ||
assertTrue(tempFile.setWritable(true)); | ||
assertThrows(MojoFailureException.class, () -> checkMojo.execute(), "Missing header files found"); | ||
tempFile.deleteOnExit(); | ||
} | ||
} |
Oops, something went wrong.