Skip to content

Commit

Permalink
refactor: ♻️ Unify common methods in AbstractLockFileMojo (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jul 14, 2023
1 parent dd598b9 commit c81d4f7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package io.github.chains_project.maven_lockfile;

import io.github.chains_project.maven_lockfile.checksum.AbstractChecksumCalculator;
import io.github.chains_project.maven_lockfile.checksum.FileSystemChecksumCalculator;
import io.github.chains_project.maven_lockfile.checksum.RemoteChecksumCalculator;
import io.github.chains_project.maven_lockfile.data.Metadata;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilder;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;

Expand Down Expand Up @@ -45,4 +52,23 @@ public abstract class AbstractLockfileMojo extends AbstractMojo {

@Parameter(defaultValue = "false", property = "reduced")
protected String reduced;

@Parameter(defaultValue = "false", property = "skip")
protected String skip;

protected Metadata generateMetaInformation() {
String osName = System.getProperty("os.name");
return new Metadata(osName, mavenVersion, javaVersion);
}

protected AbstractChecksumCalculator getChecksumCalculator() throws MojoExecutionException {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
if (checksumMode.equals("maven_local")) {
return new FileSystemChecksumCalculator(dependencyResolver, buildingRequest, checksumAlgorithm);
} else if (checksumMode.equals("maven_central")) {
return new RemoteChecksumCalculator(checksumAlgorithm);
} else {
throw new MojoExecutionException("Invalid checksum mode: " + checksumMode);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.chains_project.maven_lockfile;

import io.github.chains_project.maven_lockfile.checksum.AbstractChecksumCalculator;
import io.github.chains_project.maven_lockfile.checksum.FileSystemChecksumCalculator;
import io.github.chains_project.maven_lockfile.checksum.RemoteChecksumCalculator;
import io.github.chains_project.maven_lockfile.data.LockFile;
import io.github.chains_project.maven_lockfile.data.Metadata;
import java.io.IOException;
Expand All @@ -12,8 +10,6 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;

/**
* This plugin generates a lock file for a project. The lock file contains the checksums of all
Expand All @@ -33,20 +29,12 @@ public class GenerateLockFileMojo extends AbstractLockfileMojo {
* @throws MojoExecutionException if the lock file could not be written or the generation failed.
*/
public void execute() throws MojoExecutionException {
if (Boolean.parseBoolean(skip)) {
getLog().info("Skipping maven-lockfile");
}
try {
String osName = System.getProperty("os.name");
Metadata metadata = new Metadata(osName, mavenVersion, javaVersion);
AbstractChecksumCalculator checksumCalculator;
ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
if (checksumMode.equals("maven_local")) {
checksumCalculator =
new FileSystemChecksumCalculator(dependencyResolver, buildingRequest, checksumAlgorithm);
} else if (checksumMode.equals("maven_central")) {
checksumCalculator = new RemoteChecksumCalculator(checksumAlgorithm);
} else {
throw new MojoExecutionException("Invalid checksum mode: " + checksumMode);
}
Metadata metadata = generateMetaInformation();
AbstractChecksumCalculator checksumCalculator = getChecksumCalculator();
LockFile lockFile = LockFileFacade.generateLockFileFromProject(
session,
project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static io.github.chains_project.maven_lockfile.LockFileFacade.getLockFilePath;

import io.github.chains_project.maven_lockfile.checksum.AbstractChecksumCalculator;
import io.github.chains_project.maven_lockfile.checksum.FileSystemChecksumCalculator;
import io.github.chains_project.maven_lockfile.checksum.RemoteChecksumCalculator;
import io.github.chains_project.maven_lockfile.data.LockFile;
import io.github.chains_project.maven_lockfile.data.Metadata;
import io.github.chains_project.maven_lockfile.reporting.LockFileDifference;
Expand All @@ -14,8 +12,6 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;

/**
* Plugin goal that validates the checksums of the dependencies of a project against a lock file.
Expand All @@ -34,21 +30,12 @@ public class ValidateChecksumMojo extends AbstractLockfileMojo {
*/
public void execute() throws MojoExecutionException {
getLog().info("Validating lock file ...");
if (Boolean.parseBoolean(skip)) {
getLog().info("Skipping maven-lockfile");
}
try {

String osName = System.getProperty("os.name");
Metadata metadata = new Metadata(osName, mavenVersion, javaVersion);
AbstractChecksumCalculator checksumCalculator;
ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
if (checksumMode.equals("maven_local")) {
checksumCalculator =
new FileSystemChecksumCalculator(dependencyResolver, buildingRequest, checksumAlgorithm);
} else if (checksumMode.equals("maven_central")) {
checksumCalculator = new RemoteChecksumCalculator(checksumAlgorithm);
} else {
throw new MojoExecutionException("Invalid checksum mode: " + checksumMode);
}
Metadata metadata = generateMetaInformation();
AbstractChecksumCalculator checksumCalculator = getChecksumCalculator();
LockFile lockFileFromFile = LockFile.readLockFile(getLockFilePath(project));
LockFile lockFileFromProject = LockFileFacade.generateLockFileFromProject(
session,
Expand Down

0 comments on commit c81d4f7

Please sign in to comment.