Skip to content

Commit

Permalink
feat: Option to exclude some languages from the build (#595)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Bodnar <andrii.bodnar@crowdin.com>
  • Loading branch information
yzerk and andrii-bodnar authored Jun 12, 2023
1 parent f8bf3b2 commit 325a014
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public interface Actions {

NewAction<PropertiesWithFiles, ProjectClient> download(
FilesInterface files, boolean noProgress, List<String> languageIds, boolean pseudo, String branchName,
FilesInterface files, boolean noProgress, List<String> languageIds, List<String> excludeLanguageIds, boolean pseudo, String branchName,
boolean ignoreMatch, boolean isVerbose, boolean plainView, boolean userServerSources, boolean keepArchive
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class CliActions implements Actions {

@Override
public NewAction<PropertiesWithFiles, ProjectClient> download(
FilesInterface files, boolean noProgress, List<String> languageIds, boolean pseudo, String branchName,
FilesInterface files, boolean noProgress, List<String> languageIds, List<String> excludeLanguageIds, boolean pseudo, String branchName,
boolean ignoreMatch, boolean isVerbose, boolean plainView, boolean useServerSources, boolean keepArchive
) {
return new DownloadAction(files, noProgress, languageIds, pseudo, branchName, ignoreMatch, isVerbose, plainView, useServerSources, keepArchive);
return new DownloadAction(files, noProgress, languageIds, excludeLanguageIds, pseudo, branchName, ignoreMatch, isVerbose, plainView, useServerSources, keepArchive);
}

@Override
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/crowdin/cli/commands/actions/DownloadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class DownloadAction implements NewAction<PropertiesWithFiles, ProjectClient> {
private FilesInterface files;
private boolean noProgress;
private List<String> languageIds;
private List<String> excludeLanguageIds;
private boolean pseudo;
private String branchName;
private boolean ignoreMatch;
Expand All @@ -69,12 +70,13 @@ class DownloadAction implements NewAction<PropertiesWithFiles, ProjectClient> {
private Outputter out;

public DownloadAction(
FilesInterface files, boolean noProgress, List<String> languageIds, boolean pseudo, String branchName,
FilesInterface files, boolean noProgress, List<String> languageIds, List<String> excludeLanguageIds, boolean pseudo, String branchName,
boolean ignoreMatch, boolean isVerbose, boolean plainView, boolean useServerSources, boolean keepArchive
) {
this.files = files;
this.noProgress = noProgress || plainView;
this.languageIds = languageIds;
this.excludeLanguageIds = excludeLanguageIds;
this.pseudo = pseudo;
this.branchName = branchName;
this.ignoreMatch = ignoreMatch;
Expand Down Expand Up @@ -115,6 +117,12 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
.orElseThrow(() -> new RuntimeException(
String.format(RESOURCE_BUNDLE.getString("error.language_not_exist"), lang))))
.collect(Collectors.toList());
List<Language> excludeLanguages = excludeLanguageIds == null ? new ArrayList<>() : excludeLanguageIds.stream()
.map(lang -> project.findLanguageById(lang, true)
.orElseThrow(() -> new RuntimeException(
String.format(RESOURCE_BUNDLE.getString("error.language_not_exist"), lang))))
.collect(Collectors.toList());

Optional<Branch> branch = Optional.ofNullable(project.getBranch());

Map<String, com.crowdin.client.sourcefiles.model.File> serverSources = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getFiles());
Expand Down Expand Up @@ -154,16 +162,24 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
}
tempDirs.put(downloadedFiles.getLeft(), downloadedFiles.getRight());
} else {
List<Language> forLanguages = languages != null ? languages : project.getProjectLanguages(true);
List<Language> forLanguages = languages != null ? languages :
project.getProjectLanguages(true).stream()
.filter(language -> !excludeLanguages.contains(language))
.collect(Collectors.toList());

if (!plainView) {
out.println((languageIds != null)
? OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.build_language_archive"), String.join(", ", languageIds)))
: OK.withIcon(RESOURCE_BUNDLE.getString("message.build_archive")));
}
CrowdinTranslationCreateProjectBuildForm templateRequest = new CrowdinTranslationCreateProjectBuildForm();

if (languages != null) {
templateRequest.setTargetLanguageIds(languages.stream().map(Language::getId).collect(Collectors.toList()));
} else if (!excludeLanguages.isEmpty()) {
templateRequest.setTargetLanguageIds(forLanguages.stream().map(Language::getId).collect(Collectors.toList()));
}

branch
.map(Branch::getId)
.ifPresent(templateRequest::setBranchId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import com.crowdin.cli.commands.functionality.FsFiles;
import com.crowdin.cli.properties.ParamsWithFiles;
import com.crowdin.cli.properties.PropertiesWithFiles;
import org.apache.commons.lang3.ArrayUtils;
import picocli.CommandLine;

import java.util.ArrayList;
import java.util.List;

@CommandLine.Command(
Expand All @@ -31,6 +33,9 @@ class DownloadSubcommand extends ActCommandWithFiles {
@CommandLine.Option(names = {"-l", "--language"}, paramLabel = "...", order = -2)
protected List<String> languageIds;

@CommandLine.Option(names = {"-el", "--exclude-language"}, paramLabel = "...", order = -2)
protected List<String> excludeLanguageIds;

@CommandLine.Option(names = {"--pseudo"}, descriptionKey = "crowdin.download.pseudo", order = -2)
protected boolean pseudo;

Expand Down Expand Up @@ -62,7 +67,7 @@ class DownloadSubcommand extends ActCommandWithFiles {
protected NewAction<PropertiesWithFiles, ProjectClient> getAction(Actions actions) {
return (dryrun)
? actions.listTranslations(noProgress, treeView, false, plainView, all, true)
: actions.download(new FsFiles(), noProgress, languageIds, pseudo, branchName, ignoreMatch, isVerbose, plainView, all, keepArchive);
: actions.download(new FsFiles(), noProgress, languageIds, excludeLanguageIds, pseudo, branchName, ignoreMatch, isVerbose, plainView, all, keepArchive);
}

@Override
Expand All @@ -74,4 +79,13 @@ protected boolean isAnsi() {
protected void updateParams(ParamsWithFiles params) {
params.setExportOptions(skipTranslatedOnly, skipUntranslatedFiles, exportApprovedOnly);
}

@Override
protected List<String> checkOptions() {
List<String> errors = new ArrayList<>();
if (languageIds != null && excludeLanguageIds != null) {
errors.add(RESOURCE_BUNDLE.getString("error.download.include_exclude_lang_conflict"));
}
return errors;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ tree.dryrun=List contents of directories in a tree-like format in dryrun mode
crowdin.download.usage.description=Download the latest translations from Crowdin to the specified place
crowdin.download.usage.customSynopsis=@|fg(green) crowdin |@(@|fg(green) download|@|@|fg(green) pull|@) [CONFIG OPTIONS] [OPTIONS]
crowdin.download.ignore-match=Ignore warning message about a configuration change
crowdin.download.language=Use this option to download translations for a single specified language. Default: all
crowdin.download.language=Download translations for the specified language. Can be specified multiple times
crowdin.download.exclude-language=Skip the language during download. Can be specified multiple times
crowdin.download.pseudo=Download pseudo-localized translation files
crowdin.download.all=Download files even if local sources are missing

Expand Down Expand Up @@ -341,6 +342,7 @@ error.language_not_exist=Language '%s' doesn't exist in the project. Try specify
error.languages_not_exist=Language(s) %s doesn't exist in the project. Try specifying another language code(s)
error.building_translation=Failed to build translation. Please contact our support team for help
error.downloading_file=Failed to download ZIP archive. Try to run the command once again
error.download.include_exclude_lang_conflict=The '--language' and '--exclude-language' options can't be used simultaneously
error.extracting_files=Failed to extract files. Try to run the application with admin permission
error.archive_not_exist=Archive '%s' wasn't found. Try to run the application with admin permission
error.creatingDirectory=Failed to extract files. Couldn't create a directory for files extraction. Try to run the application with admin permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CliActionsTest {

@Test
public void testDownload() {
assertNotNull(actions.download(new FsFiles(), false, null, false, null, false, false, false, false, false));
assertNotNull(actions.download(new FsFiles(), false, null, null, false, null, false, false, false, false, false));
}

@Test
Expand Down
Loading

0 comments on commit 325a014

Please sign in to comment.