Skip to content

Commit

Permalink
refactor: move immutable classes to records
Browse files Browse the repository at this point in the history
  • Loading branch information
v1nc3n4 committed Jul 8, 2023
1 parent cd5aa64 commit 832fcf6
Show file tree
Hide file tree
Showing 28 changed files with 161 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ protected void configureTasks(final Project project, final String beanRegistryId
*/
protected void configureInstallNodeTask(final InstallNodeTask task, final String beanRegistryId,
final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Downloads and installs a Node.js distribution.");
Expand All @@ -319,7 +319,7 @@ protected void configureInstallNodeTask(final InstallNodeTask task, final String
.set(extension
.getNodeInstallDirectory()
.map(Directory::getAsFile)
.orElse(taskContext.getDefaultNodeInstallDirectoryPath().toFile()));
.orElse(taskContext.defaultNodeInstallDirectoryPath().toFile()));
task.getHttpProxyHost().set(extension.getHttpProxyHost());
task.getHttpProxyPort().set(extension.getHttpProxyPort());
task.getHttpProxyUsername().set(extension.getHttpProxyUsername());
Expand All @@ -333,7 +333,7 @@ protected void configureInstallNodeTask(final InstallNodeTask task, final String
.fileProvider(extension
.getNodeInstallDirectory()
.map(directory -> directory.getAsFile().toPath())
.orElse(taskContext.getDefaultNodeInstallDirectoryPath())
.orElse(taskContext.defaultNodeInstallDirectoryPath())
.map(nodeInstallDirectoryPath -> getBeanOrFail(beanRegistryId, ResolveGlobalNodeExecutablePath.class)
.execute(ResolveGlobalExecutablePathCommand
.builder()
Expand All @@ -354,7 +354,7 @@ protected void configureInstallNodeTask(final InstallNodeTask task, final String
*/
protected void configureResolvePackageManagerTask(final ResolvePackageManagerTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Resolves the package manager.");
Expand Down Expand Up @@ -383,7 +383,7 @@ protected void configureResolvePackageManagerTask(final ResolvePackageManagerTas
*/
protected void configureInstallPackageManagerTask(final InstallPackageManagerTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Installs the package manager.");
Expand Down Expand Up @@ -431,7 +431,7 @@ protected void configureInstallPackageManagerTask(final InstallPackageManagerTas
*/
protected void configureInstallFrontendTask(final InstallFrontendTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Installs frontend dependencies.");
Expand All @@ -458,7 +458,7 @@ protected void configureInstallFrontendTask(final InstallFrontendTask task, fina
*/
protected void configureCleanTask(final CleanTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Cleans frontend resources outside the build directory by running a specific script.");
Expand All @@ -480,7 +480,7 @@ protected void configureCleanTask(final CleanTask task, final String beanRegistr
*/
protected void configureCheckTask(final CheckTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Checks frontend by running a specific script.");
Expand All @@ -502,7 +502,7 @@ protected void configureCheckTask(final CheckTask task, final String beanRegistr
*/
protected void configureAssembleTask(final AssembleTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Assembles frontend artifacts by running a specific script.");
Expand All @@ -524,7 +524,7 @@ protected void configureAssembleTask(final AssembleTask task, final String beanR
*/
protected void configurePublishTask(final PublishTask task, final String beanRegistryId,
final TaskContainer taskContainer, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();

task.setGroup(TASK_GROUP);
task.setDescription("Publishes frontend artifacts by running a specific script.");
Expand All @@ -546,7 +546,7 @@ private Provider<ExecutableType> getExecutableType(final TaskContainer taskConta
return getBeanOrFail(beanRegistryId, ParsePackageManagerSpecification.class)
.execute(getBeanOrFail(beanRegistryId, FileManager.class).readString(f.toPath(),
StandardCharsets.UTF_8))
.getType()
.type()
.getExecutableType();
} catch (final IOException | MalformedPackageManagerSpecification |
UnsupportedPackageManagerException e) {
Expand Down Expand Up @@ -586,7 +586,7 @@ private <T extends Task, D extends Task> void configureDependency(final TaskCont
}

private Provider<File> resolveNodeInstallDirectory(final String beanRegistryId, final TaskContext taskContext) {
final FrontendExtension extension = taskContext.getExtension();
final FrontendExtension extension = taskContext.extension();
final ResolveNodeInstallDirectoryPath resolveNodeInstallDirectoryPath;
try {
resolveNodeInstallDirectoryPath = Beans.getBean(beanRegistryId, ResolveNodeInstallDirectoryPath.class);
Expand All @@ -600,8 +600,8 @@ private Provider<File> resolveNodeInstallDirectory(final String beanRegistryId,
.builder()
.nodeInstallDirectoryFromUser(extension.getNodeInstallDirectory().getAsFile().map(File::toPath))
.nodeDistributionProvided(extension.getNodeDistributionProvided())
.nodeInstallDirectoryFromEnvironment(taskContext.getNodeInstallDirectoryFromEnvironment())
.defaultPath(taskContext.getDefaultNodeInstallDirectoryPath())
.nodeInstallDirectoryFromEnvironment(taskContext.nodeInstallDirectoryFromEnvironment())
.defaultPath(taskContext.defaultNodeInstallDirectoryPath())
.build())
.map(Path::toFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,12 @@ public Path execute(final GetExecutablePathCommand command) {
.platform(command.getPlatform())
.nodeInstallDirectoryPath(command.getNodeInstallDirectoryPath())
.build();
final ExecutableType executableType = command.getExecutableType();
switch (executableType) {
case COREPACK:
return getCorepackExecutablePath.execute(resolveGlobalExecutablePathCommand);
case NODE:
return getNodeExecutablePath.execute(resolveGlobalExecutablePathCommand);
case NPM:
return getNpmExecutablePath.execute(resolveGlobalExecutablePathCommand);
case PNPM:
return getPnpmExecutablePath.execute(resolveGlobalExecutablePathCommand);
case YARN:
return getYarnExecutablePath.execute(resolveGlobalExecutablePathCommand);
default:
throw new IllegalArgumentException("Unsupported type of executable: " + executableType);
}
return switch (command.getExecutableType()) {
case COREPACK -> getCorepackExecutablePath.execute(resolveGlobalExecutablePathCommand);
case NODE -> getNodeExecutablePath.execute(resolveGlobalExecutablePathCommand);
case NPM -> getNpmExecutablePath.execute(resolveGlobalExecutablePathCommand);
case PNPM -> getPnpmExecutablePath.execute(resolveGlobalExecutablePathCommand);
case YARN -> getYarnExecutablePath.execute(resolveGlobalExecutablePathCommand);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.siouan.frontendgradleplugin.domain;

import lombok.Builder;
import lombok.Getter;

/**
* Model class providing the content of the
Expand All @@ -11,10 +10,4 @@
* @since 7.0.0
*/
@Builder
@Getter
public class PackageManager {

private final PackageManagerType type;

private final String version;
}
public record PackageManager(PackageManagerType type, String version) {}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public PackageManager execute(final Path packageJsonFilePath)
throw new InvalidJsonFileException(packageJsonFilePath);
}
final PackageManager packageManager = parsePackageManagerSpecification.execute(packageManagerSpecification);
logger.info("Package manager: {} v{}", packageManager.getType().getPackageManagerName(),
packageManager.getVersion());
logger.info("Package manager: {} v{}", packageManager.type().getPackageManagerName(), packageManager.version());
return packageManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ public ExecutionSettings execute(final ResolveExecutionSettingsCommand command)
.nodeInstallDirectoryPath(command.getNodeInstallDirectoryPath())
.platform(command.getPlatform())
.build());
final Path executablePath = getExecutablePath.execute(
new GetExecutablePathCommand(command.getExecutableType(), command.getNodeInstallDirectoryPath(),
command.getPlatform()));
final Path executablePath = getExecutablePath.execute(GetExecutablePathCommand
.builder()
.executableType(command.getExecutableType())
.nodeInstallDirectoryPath(command.getNodeInstallDirectoryPath())
.platform(command.getPlatform())
.build());

final Path executable;
final List<String> args = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public void execute(final ResolvePackageManagerCommand command)
throws InvalidJsonFileException, UnsupportedPackageManagerException, IOException,
MalformedPackageManagerSpecification {
final PackageManager packageManager = parsePackageManagerFromPackageJsonFile.execute(
command.getPackageJsonFilePath());
fileManager.writeString(command.getPackageManagerSpecificationFilePath(),
packageManager.getType().getPackageManagerName() + '@' + packageManager.getVersion(),
StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
fileManager.writeString(command.getPackageManagerExecutablePathFilePath(), getExecutablePath
command.packageJsonFilePath());
fileManager.writeString(command.packageManagerSpecificationFilePath(),
packageManager.type().getPackageManagerName() + '@' + packageManager.version(), StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
fileManager.writeString(command.packageManagerExecutablePathFilePath(), getExecutablePath
.execute(GetExecutablePathCommand
.builder()
.executableType(packageManager.getType().getExecutableType())
.nodeInstallDirectoryPath(command.getNodeInstallDirectoryPath())
.platform(command.getPlatform())
.executableType(packageManager.type().getExecutableType())
.nodeInstallDirectoryPath(command.nodeInstallDirectoryPath())
.platform(command.platform())
.build())
.toString(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,17 @@
import java.nio.file.Path;

import lombok.Builder;
import lombok.Getter;

/**
* Parameters to resolve the package manager in a project.
*
* @param packageJsonFilePath Path to the metadata file (i.e. {@code package.json} file).
* @param nodeInstallDirectoryPath Path to a Node.js install directory.
* @param platform Underlying platform.
* @param packageManagerSpecificationFilePath Path to the file providing the name of the package manager.
* @param packageManagerExecutablePathFilePath Path to the file providing the path to the package manager executable.
* @since 7.0.0
*/
@Builder
@Getter
public class ResolvePackageManagerCommand {

/**
* Path to the metadata file (i.e. {@code package.json} file).
*/
private final Path packageJsonFilePath;

/**
* Path to a Node.js install directory.
*/
private final Path nodeInstallDirectoryPath;

/**
* Underlying platform.
*/
private final Platform platform;

/**
* Path to the file providing the name of the package manager.
*/
private final Path packageManagerSpecificationFilePath;

/**
* Path to the file providing the path to the package manager executable.
*/
private final Path packageManagerExecutablePathFilePath;
}
public record ResolvePackageManagerCommand(Path packageJsonFilePath, Path nodeInstallDirectoryPath, Platform platform,
Path packageManagerSpecificationFilePath, Path packageManagerExecutablePathFilePath) {}
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,22 @@ public void execute(final InstallNodeDistributionCommand command)
throws IOException, InvalidNodeDistributionException, NodeDistributionShasumNotFoundException,
UnsupportedPlatformException, InvalidDistributionUrlException, ResourceDownloadException, ArchiverException,
UnsupportedDistributionArchiveException {
logger.info("Removing install directory '{}'", command.getInstallDirectoryPath());
fileManager.deleteFileTree(command.getInstallDirectoryPath(), true);
logger.info("Removing install directory '{}'", command.installDirectoryPath());
fileManager.deleteFileTree(command.installDirectoryPath(), true);

final GetDistributionCommand getDistributionCommand = new GetDistributionCommand(command.getPlatform(),
command.getVersion(), command.getDistributionUrlRoot(), command.getDistributionUrlPathPattern(),
command.getDistributionServerCredentials(), command.getProxySettings(),
command.getTemporaryDirectoryPath());
final GetDistributionCommand getDistributionCommand = new GetDistributionCommand(command.platform(),
command.version(), command.distributionUrlRoot(), command.distributionUrlPathPattern(),
command.distributionServerCredentials(), command.proxySettings(), command.temporaryDirectoryPath());
final Path distributionFilePath = getDistribution.execute(getDistributionCommand);

// Deploys the distribution
deployDistribution.execute(new DeployDistributionCommand(command.getPlatform(),
command.getTemporaryDirectoryPath().resolve(EXTRACT_DIRECTORY_NAME), command.getInstallDirectoryPath(),
deployDistribution.execute(new DeployDistributionCommand(command.platform(),
command.temporaryDirectoryPath().resolve(EXTRACT_DIRECTORY_NAME), command.installDirectoryPath(),
distributionFilePath));

logger.info("Removing distribution file '{}'", distributionFilePath);
fileManager.delete(distributionFilePath);

logger.info("Distribution installed in '{}'", command.getInstallDirectoryPath());
logger.info("Distribution installed in '{}'", command.installDirectoryPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,22 @@
import java.nio.file.Path;

import lombok.Builder;
import lombok.Getter;
import org.siouan.frontendgradleplugin.domain.Platform;

/**
* Settings to install a distribution.
*
* @param platform Underlying platform.
* @param version Version of the distribution.
* @param distributionUrlRoot URL root part to build the exact URL to download the distribution.
* @param distributionUrlPathPattern Trailing path pattern to build the exact URL to download the distribution.
* @param distributionServerCredentials Credentials to authenticate on the distribution server before download.
* @param proxySettings Proxy settings used for downloads.
* @param temporaryDirectoryPath Path to a temporary directory.
* @param installDirectoryPath Path to a directory where the distribution shall be installed.
* @since 1.1.2
*/
@Builder
@Getter
public class InstallNodeDistributionCommand {

/**
* Underlying platform.
*/
private final Platform platform;

/**
* Version of the distribution.
*/
private final String version;

/**
* URL root part to build the exact URL to download the distribution.
*/
private final String distributionUrlRoot;

/**
* Trailing path pattern to build the exact URL to download the distribution.
*/
private final String distributionUrlPathPattern;

/**
* Credentials to authenticate on the distribution server before download.
*/
private final Credentials distributionServerCredentials;

/**
* Proxy settings used for downloads.
*/
private final ProxySettings proxySettings;

/**
* Path to a temporary directory.
*/
private final Path temporaryDirectoryPath;

/**
* Path to a directory where the distribution shall be installed.
*/
private final Path installDirectoryPath;
}
public record InstallNodeDistributionCommand(Platform platform, String version, String distributionUrlRoot,
String distributionUrlPathPattern, Credentials distributionServerCredentials, ProxySettings proxySettings,
Path temporaryDirectoryPath, Path installDirectoryPath) {}
Loading

0 comments on commit 832fcf6

Please sign in to comment.