Skip to content

Commit

Permalink
Merge branch 'crowdin:main' into feature-lables-pre-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
debanjanc01 authored Oct 5, 2023
2 parents 09690d5 + 201eb58 commit 2fb9803
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.ERROR;
Expand All @@ -28,16 +29,18 @@ public class DownloadBundleAction implements NewAction<ProjectProperties, Client
private final boolean noProgress;
private final boolean plainView;
private final boolean keepArchive;
private final boolean dryrun;
private File to;

private Outputter out;

public DownloadBundleAction(Long id, FilesInterface files, boolean plainView, boolean keepArchive, boolean noProgress) {
public DownloadBundleAction(Long id, FilesInterface files, boolean plainView, boolean keepArchive, boolean noProgress, boolean dryrun) {
this.id = id;
this.files = files;
this.plainView = plainView;
this.keepArchive = keepArchive;
this.noProgress = noProgress;
this.dryrun = dryrun;
}

@Override
Expand All @@ -49,12 +52,18 @@ public void act(Outputter out, ProjectProperties pb, ClientBundle client) {
downloadBundle(client, bundle.getId(), status.getIdentifier());
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.bundle.download_success"), bundle.getId(), bundle.getName())));

List<String> extractedPaths;
String baseTemp = StringUtils.removeEnd(pb.getBasePath(), Utils.PATH_SEPARATOR) + Utils.PATH_SEPARATOR;
java.io.File baseTempDir = new java.io.File(baseTemp + Utils.PATH_SEPARATOR);
List<java.io.File> downloadedFiles = extractArchive(to, baseTempDir);
File baseTempDir = new File(baseTemp + Utils.PATH_SEPARATOR);
if (dryrun) {
extractedPaths = files.zipArchiveContent(to);
} else {
List<File> downloadedFiles = extractArchive(to, baseTempDir);
extractedPaths = downloadedFiles.stream().map(File::getAbsolutePath).collect(Collectors.toList());
}

for (File file: downloadedFiles) {
String filePath = Utils.noSepAtStart(StringUtils.removeStart(file.getAbsolutePath(), baseTempDir.getAbsolutePath()));
for (String file: extractedPaths) {
String filePath = Utils.noSepAtStart(StringUtils.removeStart(file, baseTempDir.getAbsolutePath()));
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.extracted_file"), filePath)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface FilesInterface {

List<File> extractZipArchive(File zipArchive, File dir);

List<String> zipArchiveContent(File zipArchive);

void deleteFile(File file) throws IOException;

void deleteDirectory(File dir) throws IOException;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/crowdin/cli/commands/functionality/FsFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ public List<File> extractZipArchive(File zipArchive, File dir) {
return extractedFiles;
}

@Override
public List<String> zipArchiveContent(File zipArchive) {
List<String> archiveFiles = new ArrayList<>();
ZipFile zipFile;
try {
zipFile = new ZipFile(zipArchive);
} catch (IllegalArgumentException e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.archive_not_exist"), zipArchive.getAbsolutePath()));
}
try {
List<FileHeader> fileHeaders = zipFile.getFileHeaders();
for (FileHeader fileHeader : fileHeaders) {
archiveFiles.add(fileHeader.getFileName());
}
} catch (net.lingala.zip4j.exception.ZipException e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.extract_archive"), zipArchive.getAbsolutePath()));
}
return archiveFiles;
}

@Override
public void deleteFile(File file) throws IOException {
java.nio.file.Files.delete(file.toPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public class DownloadBundleSubcommand extends ActCommandBundle {
@CommandLine.Option(names = {"--keep-archive"}, descriptionKey = "params.keepArchive")
protected boolean keepArchive;

@CommandLine.Option(names = {"--dryrun"})
protected boolean dryrun;

@Override
protected NewAction<ProjectProperties, ClientBundle> getAction(Actions actions) {
return new DownloadBundleAction(id, new FsFiles(), plainView, keepArchive, noProgress);
return new DownloadBundleAction(id, new FsFiles(), plainView, keepArchive, noProgress, dryrun);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testDownloadBundle() {
.build();

Bundle bundle = new Bundle();
bundle.setId(1l);
bundle.setId(1L);

BundleExport export = new BundleExport();
export.setStatus("finished");
Expand All @@ -63,12 +63,51 @@ public void testDownloadBundle() {
FilesInterface files = mock(FilesInterface.class);

NewAction<ProjectProperties, ClientBundle> action =
new DownloadBundleAction(bundle.getId(), files, false, false, false);
new DownloadBundleAction(bundle.getId(), files, false, false, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).getBundle(bundle.getId());
verify(client).startExportingBundle(bundle.getId());
verify(client).downloadBundle(bundle.getId(), null);
verifyNoMoreInteractions(client);
}

@Test
public void testDryRun() {
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean(
"/values/strings.xml", "/values-%two_letters_code%/%original_file_name%",
null, "/common/%original_file_name%")
.setBasePath(project.getBasePath())
.build();

Bundle bundle = new Bundle();
bundle.setId(1L);

BundleExport export = new BundleExport();
export.setStatus("finished");
ClientBundle client = mock(ClientBundle.class);

URL urlMock = MockitoUtils.getMockUrl(getClass());

when(client.downloadBundle(bundle.getId(), null))
.thenReturn(urlMock);
when(client.getBundle(bundle.getId()))
.thenReturn(Optional.of(bundle));

when(client.startExportingBundle(bundle.getId()))
.thenReturn(export);

FilesInterface files = mock(FilesInterface.class);

NewAction<ProjectProperties, ClientBundle> action =
new DownloadBundleAction(bundle.getId(), files, false, false, false, true);
action.act(Outputter.getDefault(), pb, client);

verify(client).getBundle(bundle.getId());
verify(client).startExportingBundle(bundle.getId());
verify(client).downloadBundle(bundle.getId(), null);

verifyNoMoreInteractions(client);
}
}

0 comments on commit 2fb9803

Please sign in to comment.