Skip to content

Commit

Permalink
feat: crowdin file list, remove generate alias (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Mar 25, 2024
1 parent 1cb1d93 commit 1c54a70
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ NewAction<NoProperties, NoClient> generate(FilesInterface files, String token, S

NewAction<ProjectProperties, ProjectClient> listBranches(boolean noProgress, boolean plainView);

NewAction<ProjectProperties, ProjectClient> listProject(
boolean noProgress, String branchName, boolean treeView, boolean plainView);
NewAction<ProjectProperties, ProjectClient> listFiles(
boolean noProgress, String branchName, boolean treeView, boolean plainView, boolean isVerbose);

NewAction<PropertiesWithFiles, ProjectClient> listSources(
boolean deleteObsolete, String branchName, boolean noProgress, boolean treeView, boolean plainView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public NewAction<ProjectProperties, ProjectClient> listBranches(boolean noProgre
}

@Override
public NewAction<ProjectProperties, ProjectClient> listProject(
boolean noProgress, String branchName, boolean treeView, boolean plainView
public NewAction<ProjectProperties, ProjectClient> listFiles(
boolean noProgress, String branchName, boolean treeView, boolean plainView, boolean isVerbose
) {
return new ListProjectAction(noProgress, branchName, treeView, plainView);
return new FileListAction(noProgress, branchName, treeView, plainView, isVerbose);
}

@Override
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/com/crowdin/cli/commands/actions/FileListAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.functionality.ProjectFilesUtils;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.cli.utils.tree.DrawTree;
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.sourcefiles.model.File;
import com.crowdin.client.sourcefiles.model.FileInfo;

import java.util.*;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

class FileListAction implements NewAction<ProjectProperties, ProjectClient> {

private final boolean noProgress;
private final String branchName;
private final boolean treeView;
private final boolean plainView;
private final boolean isVerbose;

public FileListAction(boolean noProgress, String branchName, boolean treeView, boolean plainView, boolean isVerbose) {
this.noProgress = noProgress || plainView;
this.branchName = branchName;
this.treeView = treeView;
this.plainView = plainView;
this.isVerbose = isVerbose;
}

@Override
public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
CrowdinProjectFull project = ConsoleSpinner
.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, () -> client.downloadFullProject(this.branchName));

Long branchId = Optional.ofNullable(project.getBranch()).map(Branch::getId).orElse(null);

List<Map.Entry<String, FileInfo>> list = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos())
.entrySet()
.stream()
.filter(entry -> Objects.equals(entry.getValue().getBranchId(), branchId))
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey().replaceAll("^[/\\\\]+", ""), e.getValue()))
.collect(Collectors.toList());

if (treeView) {
List<String> tree = list.stream().map(Map.Entry::getKey).collect(Collectors.toList());
DrawTree.draw(tree).forEach(out::println);
return;
}

for (Map.Entry<String, FileInfo> entry : list) {
if (plainView) {
if (isVerbose) {
if (entry.getValue() instanceof File) {
File file = (File) entry.getValue();
String str = entry.getValue().getId() +
" " +
entry.getKey() +
" " +
file.getType() +
" " +
file.getParserVersion() +
" " +
file.getRevisionId();
out.println(str);
} else {
out.println(entry.getValue().getId() + " " + entry.getKey() + " " + entry.getValue().getType());
}
} else {
out.println(entry.getValue().getId() + " " + entry.getKey());
}
} else {
if (isVerbose) {
if (entry.getValue() instanceof File) {
File file = (File) entry.getValue();
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list_verbose_full"), entry.getValue().getId(), entry.getKey(), entry.getValue().getType(), file.getParserVersion(), file.getRevisionId())));
} else {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list_verbose"), entry.getValue().getId(), entry.getKey(), entry.getValue().getType())));
}
} else {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.file.list"), entry.getValue().getId(), entry.getKey())));
}
}
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ public final class CommandNames {

public static final String DOWNLOAD_TARGETS = "targets";

public static final String GENERATE = "generate";

public static final String ALIAS_GENERATE = "init";
public static final String INIT = "init";

public static final String LINT = "lint";

public static final String LIST = "list";
public static final String LIST_PROJECT = "project";
public static final String LIST_SOURCES = "sources";
public static final String LIST_TRANSLATIONS = "translations";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import picocli.CommandLine;

@CommandLine.Command(
name = CommandNames.LIST_PROJECT,
name = CommandNames.LIST,
sortOptions = false
)
class ListProjectSubcommand extends ActCommandProject {
class FileListSubcommand extends ActCommandProject {

@CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...", order = -2)
protected String branch;
Expand All @@ -23,7 +23,7 @@ class ListProjectSubcommand extends ActCommandProject {

@Override
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) {
return actions.listProject(this.noProgress, this.branch, this.treeView, this.plainView);
return actions.listFiles(this.noProgress, this.branch, this.treeView, this.plainView, this.isVerbose);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@Command(
name = CommandNames.FILE,
subcommands = {
FileListSubcommand.class,
FileUploadSubcommand.class,
FileDownloadSubcommand.class,
FileDeleteSubcommand.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import java.nio.file.Path;

@CommandLine.Command(
name = CommandNames.GENERATE,
aliases = CommandNames.ALIAS_GENERATE,
name = CommandNames.INIT,
sortOptions = false
)
public class GenerateSubcommand extends GenericActCommand<NoProperties, NoClient> {
public class InitSubcommand extends GenericActCommand<NoProperties, NoClient> {

@CommandLine.Option(names = {"-d", "--destination"}, paramLabel = "...", descriptionKey = "crowdin.generate.destination", defaultValue = "crowdin.yml", order = -2)
private Path destinationPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
@CommandLine.Command(
name = CommandNames.LIST,
subcommands = {
ListProjectSubcommand.class,
ListSourcesSubcommand.class,
ListTranslationsSubcommand.class
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
DownloadSubcommand.class,
ListSubcommand.class,
LintSubcommand.class,
GenerateSubcommand.class,
InitSubcommand.class,
StatusSubcommand.class,
StringSubcommand.class,
GlossarySubcommand.class,
Expand Down
23 changes: 15 additions & 8 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ crowdin.download.sources.usage.customSynopsis=@|fg(green) crowdin |@(@|fg(green)
crowdin.download.bundle.usage.description=Download bundle from Crowdin
crowdin.download.bundle.usage.customSynopsis=@|fg(green) crowdin download bundle <id>|@

# CROWDIN GENERATE COMMAND
crowdin.generate.usage.description=Generate Crowdin CLI configuration skeleton
crowdin.generate.usage.customSynopsis=@|fg(green) crowdin |@(@|fg(green) generate|@|@|fg(green) init|@) [CONFIG OPTIONS] [OPTIONS]
crowdin.generate.destination=Place where the configuration skeleton should be saved. Default: ${DEFAULT-VALUE}
# CROWDIN INIT COMMAND
crowdin.init.usage.description=Generate Crowdin CLI configuration skeleton
crowdin.init.usage.customSynopsis=@|fg(green) crowdin init|@ [CONFIG OPTIONS] [OPTIONS]
crowdin.init.destination=Place where the configuration skeleton should be saved. Default: ${DEFAULT-VALUE}

# CROWDIN LINT COMMAND
crowdin.lint.usage.description=Analyze your configuration file for possible errors
Expand All @@ -72,10 +72,6 @@ crowdin.lint.usage.customSynopsis=@|fg(green) crowdin lint|@ [CONFIG OPTIONS] [O
# CROWDIN LIST COMMAND
crowdin.list.usage.plain=Provide plain, processable output

# CROWDIN LIST PROJECT COMMAND
crowdin.list.project.usage.description=Show a list of source files in the current project
crowdin.list.project.usage.customSynopsis=@|fg(green) crowdin list project|@ [CONFIG OPTIONS] [OPTIONS]

# CROWDIN LIST SOURCES COMMAND
crowdin.list.sources.usage.description=List information about the source files that match the wild-card pattern contained in the current project
crowdin.list.sources.usage.customSynopsis=@|fg(green) crowdin list sources|@ [CONFIG OPTIONS] [OPTIONS]
Expand Down Expand Up @@ -127,6 +123,12 @@ crowdin.file.usage.description=Manage source files and translations in a Crowdin
crowdin.file.usage.customSynopsis=@|fg(green) crowdin file|@ [SUBCOMMAND] [CONFIG OPTIONS] [OPTIONS]
crowdin.file.language=Target language identifier

# CROWDIN FILE LIST
crowdin.file.list.usage.description=Show a list of source files in the current project
crowdin.file.list.usage.customSynopsis=@|fg(green) crowdin file list|@ [CONFIG OPTIONS] [OPTIONS]
crowdin.file.list.branch=Specify branch name. Default: none
crowdin.file.list.tree=List contents of directories in a tree-like format

# CROWDIN FILE UPLOAD COMMAND
crowdin.file.upload.usage.description=Upload a file to a Crowdin project
crowdin.file.upload.usage.customSynopsis=@|fg(green) crowdin file upload|@ <file> [CONFIG OPTIONS] [OPTIONS]
Expand Down Expand Up @@ -671,8 +673,13 @@ message.download_sources.preserve_hierarchy_warning=Because the @|bold 'preserve
message.download_translations.preserve_hierarchy_warning=Because the @|bold 'preserve_hierarchy'|@ parameter is set to 'false' CLI might download some unexpected files that match the pattern

message.language.list=@|yellow %s|@ @|green %s|@

message.branch.list=@|yellow #%d|@ @|green %s|@

message.file.list=@|yellow #%d|@ @|green %s|@
message.file.list_verbose=@|yellow #%d|@ @|green %s|@ %s
message.file.list_verbose_full=@|yellow #%d|@ @|green %s|@ %s parser:%d revision:%d

message.source_string_uploaded=Source string was added successfully
message.source_string_for_file_uploaded=Source string was added successfully to the file @|bold '%s'|@
message.source_string_deleted=Source string @|bold "%s"|@ (id: %d; file: %s) was deleted successfully
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testListBranches() {

@Test
public void testListProject() {
assertNotNull(actions.listProject(false, null, false, false));
assertNotNull(actions.listFiles(false, null, false, false, false));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class ListProjectActionTest {
public class FileListActionTest {

TempProject project;

Expand Down Expand Up @@ -48,7 +48,7 @@ public void testForServerInteraction() throws ResponseException {
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addFile("first.po", "gettext", 101L, null, null).build());

action = new ListProjectAction(false, null, true, false);
action = new FileListAction(false, null, true, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject(null);
Expand All @@ -66,7 +66,7 @@ public void testForExistentBranch() throws ResponseException {
.addFile("first.po", "gettext", 101L, null, null)
.addBranches(1L, "existentBranch").build());

action = new ListProjectAction(false, "existentBranch", false, false);
action = new FileListAction(false, "existentBranch", false, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject("existentBranch");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.verify;

public class ListProjectSubcommandTest extends PicocliTestUtils {
public class FileListSubcommandTest extends PicocliTestUtils {

@Test
public void testListProject() {
this.execute(CommandNames.LIST, CommandNames.LIST_PROJECT);
this.execute(CommandNames.FILE, CommandNames.LIST);
verify(actionsMock)
.listProject(anyBoolean(), any(), anyBoolean(), anyBoolean());
.listFiles(anyBoolean(), any(), anyBoolean(), anyBoolean(), anyBoolean());
this.check(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.verify;

public class GenerateSubcommandTest extends PicocliTestUtils {
public class InitSubcommandTest extends PicocliTestUtils {

@Test
public void testGenerate() {
this.execute(CommandNames.GENERATE);
this.execute(CommandNames.INIT);
verify(actionsMock)
.generate(any(), any(), any(), any(), any(), any(), any(), any(), any(), anyBoolean());
this.check(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void mockActions() {
.thenReturn(actionMock);
when(actionsMock.listBranches(anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.listProject(anyBoolean(), any(), anyBoolean(), anyBoolean()))
when(actionsMock.listFiles(anyBoolean(), any(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.listSources(anyBoolean(), any(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
Expand Down
Loading

0 comments on commit 1c54a70

Please sign in to comment.