Skip to content

Commit

Permalink
Merge pull request #406 from crowdin/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
andrii-bodnar authored Nov 19, 2021
2 parents 2a1d5cf + 143f0c1 commit ac91d43
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/cli/BaseCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ public class BaseCli {
public enum LanguageCode {
two_letters_code, three_letters_code, locale, android_code, osx_code, osx_locale
}

public static final Integer CHECK_WAITING_TIME_FIRST = 1000;
public static final Integer CHECK_WAITING_TIME_INCREMENT = 500;
public static final Integer CHECK_WAITING_TIME_MAX = 5000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.CHECK_WAITING_TIME_FIRST;
import static com.crowdin.cli.BaseCli.CHECK_WAITING_TIME_INCREMENT;
import static com.crowdin.cli.BaseCli.CHECK_WAITING_TIME_MAX;
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.ERROR;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;
Expand Down Expand Up @@ -200,6 +205,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
Map<String, List<String>> totalOmittedFiles = null;
List<List<String>> omittedFilesNoSources = new ArrayList<>();

AtomicBoolean anyFileDownloaded = new AtomicBoolean(false);
for (File tempDir : fileBeansWithDownloadedFilesNoRepetitions.keySet()) {
Set<Pair<String, String>> filesWithMapping = fileBeansWithDownloadedFilesNoRepetitions.get(tempDir);
List<String> downloadedFiles = tempDirs.get(tempDir);
Expand All @@ -208,6 +214,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
sortFiles(downloadedFiles, filesWithMapping, pb.getBasePath(), tempDir.getAbsolutePath() + Utils.PATH_SEPARATOR);
new TreeMap<>(result.getLeft()).forEach((fromFile, toFile) -> { //files to extract
files.copyFile(fromFile, toFile);
anyFileDownloaded.set(true);
if (!plainView) {
out.println(OK.withIcon(
String.format(
Expand Down Expand Up @@ -242,10 +249,15 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
omittedFilesNoSources.add(allOmittedFilesNoSources);
}

if (!anyFileDownloaded.get()) {
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.warning.no_file_to_download")));
}

if (!ignoreMatch && !plainView) {
totalOmittedFiles = totalOmittedFiles.entrySet().stream()
.filter(entry -> !entry.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

if (!totalOmittedFiles.isEmpty()) {
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.downloaded_files_omitted")));
totalOmittedFiles.forEach((file, translations) -> {
Expand Down Expand Up @@ -348,6 +360,7 @@ private String replaceFileName(String filePath, String newName) {
}

private ProjectBuild buildTranslation(ProjectClient client, BuildProjectTranslationRequest request) {
AtomicInteger sleepTime = new AtomicInteger(CHECK_WAITING_TIME_FIRST);
return ConsoleSpinner.execute(out, "message.spinner.fetching_project_info",
"error.collect_project_info", this.noProgress, this.plainView, () -> {
ProjectBuild build = client.startBuildingTranslation(request);
Expand All @@ -356,7 +369,7 @@ private ProjectBuild buildTranslation(ProjectClient client, BuildProjectTranslat
ConsoleSpinner.update(
String.format(RESOURCE_BUNDLE.getString("message.building_translation"),
Math.toIntExact(build.getProgress())));
Thread.sleep(100);
Thread.sleep(sleepTime.getAndUpdate(val -> val < CHECK_WAITING_TIME_MAX ? val + CHECK_WAITING_TIME_INCREMENT : CHECK_WAITING_TIME_MAX));
build = client.checkBuildingTranslation(build.getId());
}
ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.building_translation"), 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -48,10 +49,16 @@ public List<File> extractZipArchive(File zipArchive, File dir) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.creatingDirectory"));
}
}
Integer filesQnt = null;
try {
filesQnt = zipFile.getFileHeaders().size();
zipFile.extractAll(dir.getAbsolutePath());
} catch (net.lingala.zip4j.exception.ZipException e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.extract_archive"), zipArchive.getAbsolutePath()));
if (filesQnt != null && filesQnt.equals(1)) {
return new ArrayList<>();
} else {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.extract_archive"), zipArchive.getAbsolutePath()));
}
}
try {
return java.nio.file.Files.walk(dir.toPath())
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public String replaceFileDependentPlaceholders(String toFormat, File file) {
String fileNameWithoutExt = FilenameUtils.removeExtension(fileName);
String fileExt = FilenameUtils.getExtension(fileName);
String tempBasePath = basePath;
String fileParent = StringUtils.removeStart((file.getParent() != null ? file.getParent() + Utils.PATH_SEPARATOR : ""), tempBasePath);
String fileParent = Utils.noSepAtEnd(StringUtils.removeStart((file.getParent() != null ? file.getParent() + Utils.PATH_SEPARATOR : ""), tempBasePath));

toFormat = toFormat.contains(PLACEHOLDER_ORIGINAL_FILE_NAME) ? toFormat.replace(PLACEHOLDER_ORIGINAL_FILE_NAME, fileName) : toFormat;
toFormat = toFormat.contains(PLACEHOLDER_FILE_NAME) ? toFormat.replace(PLACEHOLDER_FILE_NAME, fileNameWithoutExt) : toFormat;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ message.warning.not_yml=File @|bold '%s'|@ is not a YAML or YML file
message.warning.browser_not_found=\nError opening a web browser. Please open the following link manually:\n@|bold %s|@
message.warning.file_not_uploaded_cause_of_language=Translation file @|yellow,bold '%s'|@ @|yellow hasn't been uploaded|@ since @|bold %s|@ is not enabled as a target language for the source file in your Crowdin project
message.warning.auto_approve_option_with_mt='--auto-approve-option' is used only for the TM Pre-Translation method
message.warning.no_file_to_download=Couldn't find any file to download

message.spinner.fetching_project_info=Fetching project info
message.spinner.building_translation=Building translations
Expand Down

0 comments on commit ac91d43

Please sign in to comment.