Skip to content

Commit

Permalink
feat: Introduce hawkeye-maven-plugin (#84)
Browse files Browse the repository at this point in the history
Signed-off-by: spencercjh <[email protected]>
  • Loading branch information
spencercjh authored Aug 30, 2023
1 parent a519041 commit fbcdd4e
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public boolean isMatchForText(Document d, HeaderDefinition headerDefinition, Cha
.limit(headerContentLines.length + 10)
.collect(Collectors.joining("\n", "", "\n\n"));
final String fileHeader = firstLines.replaceAll(" *\r?\n", "\n");
final String expectedHeader = d.mergeProperties(buildForDefinition(headerDefinition));
final String expectedHeader =
d.mergeProperties(buildForDefinition(headerDefinition)).replaceAll(" *\r?\n", "\n");
return fileHeader.contains(expectedHeader);
}
}
58 changes: 58 additions & 0 deletions hawkeye-maven-plugin/pom.xml
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>
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;
}
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");
}
}
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()));
}
}
}
}
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));
}
}
}
}
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();
}
}
Loading

0 comments on commit fbcdd4e

Please sign in to comment.