Skip to content

Commit

Permalink
#26002 Initial commit for handling the sync process of languages
Browse files Browse the repository at this point in the history
  • Loading branch information
jgambarios committed Sep 20, 2023
1 parent f7eb10d commit a32d925
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.dotcms.cli.common.OutputOptionMixin;
import com.dotcms.cli.common.PushMixin;
import com.dotcms.common.WorkspaceManager;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import javax.enterprise.context.control.ActivateRequestContext;
Expand Down Expand Up @@ -100,26 +98,16 @@ CommandLine createCommandLine(DotPush command) {
/**
* Checks if the provided file is a valid workspace.
*
* @param fromFile the file representing the workspace directory. If null, the current directory
* is used.
* @param path represents the workspace directory.
* @throws IllegalArgumentException if no valid workspace is found at the specified path.
*/
void checkValidWorkspace(final File fromFile) {

String fromPath;
if (fromFile == null) {
// If the workspace is not specified, we use the current directory
fromPath = Paths.get("").toAbsolutePath().normalize().toString();
} else {
fromPath = fromFile.getAbsolutePath();
}
void checkValidWorkspace(final Path path) {

final Path path = Paths.get(fromPath);
final var workspace = workspaceManager.findWorkspace(path);

if (workspace.isEmpty()) {
throw new IllegalArgumentException(
String.format("No valid workspace found at path: [%s]", fromPath));
String.format("No valid workspace found at path: [%s]", path.toAbsolutePath()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
Expand Down Expand Up @@ -67,31 +66,21 @@ protected File getOrCreateWorkspaceFilesDirectory(final Path workspacePath) thro
}

/**
* Returns the directory where the workspace is.
*
* @param fromFile the file object representing a directory within the workspace, or null if not specified
* @param path represents a directory within the workspace
* @return the workspace files directory
* @throws IllegalArgumentException if a valid workspace is not found from the provided path
*/
protected File getWorkspaceDirectory(final File fromFile) {

String fromPath;
if (fromFile == null) {
// If the workspace is not specified, we use the current directory
fromPath = Paths.get("").toAbsolutePath().normalize().toString();
} else {
fromPath = fromFile.getAbsolutePath();
}
protected File getWorkspaceDirectory(final Path path) {

final Path path = Paths.get(fromPath);
final var workspace = workspaceManager.findWorkspace(path);

if (workspace.isPresent()) {
return workspace.get().root().toFile();
}

throw new IllegalArgumentException(
String.format("No valid workspace found at path: [%s]", fromPath));
String.format("No valid workspace found at path: [%s]", path.toAbsolutePath()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import com.dotcms.cli.command.DotCommand;
import com.dotcms.cli.command.DotPush;
import com.dotcms.cli.common.ConsoleLoadingAnimation;
import com.dotcms.cli.common.FilesPushMixin;
import com.dotcms.cli.common.OutputOptionMixin;
import com.dotcms.cli.common.PushMixin;
import com.dotcms.common.AssetsUtils;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -61,18 +59,14 @@ public Integer call() throws Exception {
}

// Getting the workspace
var workspace = getWorkspaceDirectory(pushMixin.path);

// If the source is not specified, we use the current directory
if (pushMixin.path == null) {
pushMixin.path = Paths.get("").toAbsolutePath().normalize().toFile();
}
var workspace = getWorkspaceDirectory(pushMixin.path());

CompletableFuture<List<Triple<List<Exception>, AssetsUtils.LocalPathStructure, TreeNode>>>
folderTraversalFuture = CompletableFuture.supplyAsync(
() -> {
// Service to handle the traversal of the folder
return pushService.traverseLocalFolders(output, workspace, pushMixin.path,
return pushService.traverseLocalFolders(output, workspace,
pushMixin.path().toFile(),
filesPushMixin.removeAssets, filesPushMixin.removeFolders,
true, true);
});
Expand All @@ -95,7 +89,8 @@ public Integer call() throws Exception {

if (result == null) {
output.error(String.format(
"Error occurred while pushing folder info: [%s].", pushMixin.path));
"Error occurred while pushing folder info: [%s].",
pushMixin.path().toAbsolutePath()));
return CommandLine.ExitCode.SOFTWARE;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dotcms.cli.common;
package com.dotcms.cli.command.files;

import picocli.CommandLine;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.cli.common;

import java.io.File;
import java.nio.file.Path;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -42,12 +43,15 @@ public class PushMixin {
public boolean noValidateUnmatchedArguments;

/**
* Returns the path of the file. (Useful for mocking)
* Returns the path of the file. If no path is provided, it will return current working directory.
*
* @return The path of the file.
*/
public File path() {
return path;
public Path path() {
if (null == path) {
return Path.of("");
}
return path.toPath();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void testAllPushCommandsAreCalled() throws Exception {
when(parseResult.expandedArgs()).
thenReturn(new ArrayList<>());

when(pushMixin.path()).thenReturn(tempFolder.toFile());
when(pushMixin.path()).thenReturn(tempFolder.toAbsolutePath());

pushCommand.workspaceManager = workspaceManager;
pushCommand.call();
Expand Down

0 comments on commit a32d925

Please sign in to comment.