-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bundle command support into cli (#549)
- Loading branch information
Showing
35 changed files
with
1,060 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.crowdin.cli.client; | ||
|
||
import com.crowdin.client.bundles.model.Bundle; | ||
import com.crowdin.client.bundles.model.BundleExport; | ||
import com.crowdin.client.tasks.model.Status; | ||
import com.crowdin.client.translationmemory.model.TranslationMemoryExportRequest; | ||
import com.crowdin.client.translationmemory.model.TranslationMemoryExportStatus; | ||
|
||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ClientBundle extends Client { | ||
|
||
List<Bundle> listBundle(); | ||
|
||
Bundle addBundle(Bundle request); | ||
|
||
Optional<Bundle> getBundle(Long id); | ||
|
||
URL downloadBundle(Long id, String exportId); | ||
|
||
BundleExport startExportingBundle(Long id, Bundle bundle); | ||
|
||
BundleExport checkExportingBundle(Long tmId, String exportId); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/crowdin/cli/client/CrowdinClientBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.crowdin.cli.client; | ||
|
||
|
||
import com.crowdin.client.bundles.model.Bundle; | ||
import com.crowdin.client.bundles.model.BundleExport; | ||
import com.crowdin.client.glossaries.model.Glossary; | ||
|
||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class CrowdinClientBundle extends CrowdinClientCore implements ClientBundle { | ||
|
||
private final com.crowdin.client.Client client; | ||
private final String projectId; | ||
|
||
public CrowdinClientBundle(com.crowdin.client.Client client, String projectId) { | ||
this.client = client; | ||
this.projectId = projectId; | ||
} | ||
|
||
public List<Bundle> listBundle() { | ||
return executeRequestFullList((limit, offset) -> this.client.getBundlesApi() | ||
.listBundles(Long.valueOf(projectId))); | ||
} | ||
|
||
@Override | ||
public Bundle addBundle(Bundle bundleRequest) { | ||
return executeRequest(() -> this.client.getBundlesApi() | ||
.addBundle(Long.valueOf(projectId), bundleRequest) | ||
.getData()); | ||
} | ||
|
||
@Override | ||
public Optional<Bundle> getBundle(Long bundleId) { | ||
try { | ||
return Optional.of(executeRequest(() -> this.client.getBundlesApi() | ||
.getBundle(Long.valueOf(projectId), bundleId)) | ||
.getData()); | ||
} catch (Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public URL downloadBundle(Long id, String exportId) { | ||
return url(executeRequest(() -> this.client.getBundlesApi() | ||
.downloadBundle(Long.valueOf(projectId), id, exportId) | ||
.getData())); | ||
} | ||
|
||
@Override | ||
public BundleExport startExportingBundle(Long id, Bundle bundle) { | ||
return executeRequest(() -> this.client.getBundlesApi() | ||
.exportBundle(Long.valueOf(projectId), id, bundle) | ||
.getData()); | ||
} | ||
|
||
@Override | ||
public BundleExport checkExportingBundle(Long id, String exportId) { | ||
return executeRequest(() -> this.client.getBundlesApi() | ||
.checkBundleExportStatus(Long.valueOf(projectId), id, exportId) | ||
.getData()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/crowdin/cli/commands/actions/BundleAddAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ClientBundle; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.client.bundles.model.Bundle; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.OK; | ||
|
||
@AllArgsConstructor | ||
class BundleAddAction implements NewAction<ProjectProperties, ClientBundle> { | ||
|
||
private String name; | ||
|
||
private String format; | ||
|
||
private List<String> source; | ||
|
||
private List<String> ignore; | ||
|
||
private String translation; | ||
|
||
private List<Long> labels; | ||
|
||
private boolean plainView; | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ClientBundle client) { | ||
Bundle bundle; | ||
Bundle addBundleRequest = new Bundle(); | ||
Optional.ofNullable(name).ifPresent(addBundleRequest::setName); | ||
Optional.ofNullable(format).ifPresent(addBundleRequest::setFormat); | ||
Optional.ofNullable(source).ifPresent(addBundleRequest::setSourcePatterns); | ||
Optional.ofNullable(ignore).ifPresent(addBundleRequest::setIgnorePatterns); | ||
Optional.ofNullable(translation).ifPresent(addBundleRequest::setExportPattern); | ||
|
||
Optional.ofNullable(labels).ifPresent(addBundleRequest::setLabelIds); | ||
|
||
try { | ||
bundle = client.addBundle(addBundleRequest); | ||
} catch (Exception e) { | ||
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.bundle_is_not_added"), addBundleRequest), e); | ||
} | ||
|
||
if (!plainView) { | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.bundle.added"), bundle.getId(), bundle.getName()))); | ||
} else { | ||
out.println(String.valueOf(bundle.getId())); | ||
} | ||
|
||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/crowdin/cli/commands/actions/BundleListAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ClientBundle; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.client.bundles.model.Bundle; | ||
|
||
import java.util.List; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.*; | ||
|
||
class BundleListAction implements NewAction<ProjectProperties, ClientBundle> { | ||
|
||
private final boolean plainView; | ||
|
||
public BundleListAction(boolean plainView) { | ||
this.plainView = plainView; | ||
} | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ClientBundle client) { | ||
List<Bundle> bundles = client.listBundle(); | ||
for (Bundle bundle : bundles) { | ||
if (!plainView) { | ||
out.println(String.format(RESOURCE_BUNDLE.getString("message.bundle.list"), bundle.getId(), | ||
bundle.getName(), | ||
bundle.getFormat(), bundle.getExportPattern())); | ||
} else { | ||
out.println(bundle.getId() + " " + bundle.getName()); | ||
} | ||
} | ||
if (bundles.isEmpty()) { | ||
if (!plainView) { | ||
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.bundle.list_empty"))); | ||
} else { | ||
out.println(RESOURCE_BUNDLE.getString("message.bundle.list_empty")); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/main/java/com/crowdin/cli/commands/actions/DownloadBundleAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ClientBundle; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.commands.functionality.*; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.utils.Utils; | ||
import com.crowdin.cli.utils.console.ConsoleSpinner; | ||
import com.crowdin.client.bundles.model.Bundle; | ||
import com.crowdin.client.bundles.model.BundleExport; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
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; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.OK; | ||
|
||
public class DownloadBundleAction implements NewAction<ProjectProperties, ClientBundle> { | ||
|
||
private final Long id; | ||
private FilesInterface files; | ||
private boolean noProgress; | ||
private boolean plainView; | ||
private boolean keepArchive; | ||
private File to; | ||
|
||
private Outputter out; | ||
|
||
public DownloadBundleAction(Long id, FilesInterface files, boolean plainView, boolean keepArchive, boolean noProgress) { | ||
this.id = id; | ||
this.files = files; | ||
this.plainView = plainView; | ||
this.keepArchive = keepArchive; | ||
this.noProgress = noProgress; | ||
} | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ClientBundle client) { | ||
this.out = out; | ||
Bundle bundle = getBundle(client); | ||
BundleExport status = this.buildBundle(out, client, bundle.getId(), bundle); | ||
to = new File("bundle-" + status.getIdentifier() + ".zip"); | ||
downloadBundle(client, bundle.getId(), status.getIdentifier()); | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.bundle.download_success"), bundle.getId(), bundle.getName()))); | ||
|
||
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); | ||
for (File file: downloadedFiles) { | ||
String filePath = Utils.noSepAtStart(StringUtils.removeStart(file.getAbsolutePath(), baseTempDir.getAbsolutePath())); | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.extracted_file"), filePath))); | ||
} | ||
if (!keepArchive) { | ||
try { | ||
files.deleteFile(to); | ||
} catch (IOException e) { | ||
out.println(ERROR.withIcon(String.format(RESOURCE_BUNDLE.getString("error.deleting_archive"), to))); | ||
} | ||
} else { | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.archive"), to.getAbsolutePath()))); | ||
} | ||
} | ||
|
||
private Bundle getBundle(ClientBundle client) { | ||
return client.getBundle(id) | ||
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.bundle.not_found_by_id"))); | ||
} | ||
|
||
private BundleExport buildBundle(Outputter out, ClientBundle client, Long bundleId, Bundle request) { | ||
return ConsoleSpinner.execute( | ||
out, | ||
"message.spinner.building_bundle", | ||
"error.bundle.build_bundle", | ||
this.noProgress, | ||
false, | ||
() -> { | ||
BundleExport status = client.startExportingBundle(bundleId, request); | ||
|
||
while (!status.getStatus().equalsIgnoreCase("finished")) { | ||
ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.spinner.building_bundle_percents"), status.getProgress())); | ||
Thread.sleep(1000); | ||
|
||
status = client.checkExportingBundle(bundleId, status.getIdentifier()); | ||
|
||
if (status.getStatus().equalsIgnoreCase("failed")) { | ||
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.spinner.build_has_failed")); | ||
} | ||
} | ||
|
||
ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.spinner.building_bundle_percents"), 100)); | ||
|
||
return status; | ||
} | ||
); | ||
} | ||
|
||
private void downloadBundle(ClientBundle client, Long bundleId, String exportId) { | ||
URL url = client.downloadBundle(bundleId, exportId); | ||
try (InputStream data = url.openStream()) { | ||
files.writeToFile(to.toString(), data); | ||
} catch (IOException e) { | ||
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.write_file"), e); | ||
} | ||
} | ||
|
||
private List<File> extractArchive(java.io.File zipArchive, java.io.File dir) { | ||
return ConsoleSpinner.execute( | ||
out, | ||
"message.spinner.extracting_archive", | ||
"error.extracting_files", | ||
this.noProgress, | ||
this.plainView, | ||
() -> files.extractZipArchive(zipArchive, dir) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.