-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New command: distribution (#593)
Co-authored-by: Andrii Bodnar <[email protected]>
- Loading branch information
1 parent
e6c1b08
commit 9312fe1
Showing
31 changed files
with
983 additions
and
3 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/com/crowdin/cli/client/ClientDistribution.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,18 @@ | ||
package com.crowdin.cli.client; | ||
|
||
import com.crowdin.client.distributions.model.AddDistributionRequest; | ||
import com.crowdin.client.distributions.model.Distribution; | ||
import com.crowdin.client.distributions.model.DistributionRelease; | ||
|
||
import java.util.List; | ||
|
||
public interface ClientDistribution extends Client { | ||
|
||
List<Distribution> listDistribution(); | ||
|
||
Distribution addDistribution(AddDistributionRequest request); | ||
|
||
DistributionRelease release(String hash); | ||
|
||
DistributionRelease getDistributionRelease(String hash); | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/crowdin/cli/client/CrowdinClientDistribution.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,46 @@ | ||
package com.crowdin.cli.client; | ||
|
||
import com.crowdin.client.distributions.model.AddDistributionRequest; | ||
import com.crowdin.client.distributions.model.Distribution; | ||
import com.crowdin.client.distributions.model.DistributionRelease; | ||
|
||
import java.util.List; | ||
|
||
public class CrowdinClientDistribution extends CrowdinClientCore implements ClientDistribution { | ||
|
||
private final com.crowdin.client.Client client; | ||
private final String projectId; | ||
|
||
public CrowdinClientDistribution(com.crowdin.client.Client client, String projectId) { | ||
this.client = client; | ||
this.projectId = projectId; | ||
} | ||
|
||
@Override | ||
public List<Distribution> listDistribution() { | ||
return executeRequestFullList((limit, offset) -> this.client.getDistributionsApi() | ||
.listDistributions(Long.valueOf(projectId), limit, offset)); | ||
} | ||
|
||
@Override | ||
public Distribution addDistribution(AddDistributionRequest distributionRequest) { | ||
return executeRequest(() -> this.client.getDistributionsApi() | ||
.addDistribution(Long.valueOf(projectId), distributionRequest) | ||
.getData()); | ||
} | ||
|
||
@Override | ||
public DistributionRelease release(String hash) { | ||
return executeRequest(() -> this.client.getDistributionsApi() | ||
.createDistributionRelease(Long.valueOf(projectId), hash) | ||
.getData()); | ||
} | ||
|
||
@Override | ||
public DistributionRelease getDistributionRelease(String hash) { | ||
return executeRequest(() -> this.client.getDistributionsApi() | ||
.getDistributionRelease(Long.valueOf(projectId), hash) | ||
.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
98 changes: 98 additions & 0 deletions
98
src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.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,98 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ClientDistribution; | ||
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.RequestBuilder; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.utils.Utils; | ||
import com.crowdin.cli.utils.console.ConsoleSpinner; | ||
import com.crowdin.client.distributions.model.AddDistributionRequest; | ||
import com.crowdin.client.distributions.model.Distribution; | ||
import com.crowdin.client.distributions.model.ExportMode; | ||
import com.crowdin.client.sourcefiles.model.Branch; | ||
import com.crowdin.client.sourcefiles.model.FileInfo; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.OK; | ||
|
||
@AllArgsConstructor | ||
class DistributionAddAction implements NewAction<ProjectProperties, ClientDistribution> { | ||
|
||
private boolean noProgress; | ||
private boolean plainView; | ||
private String name; | ||
private ExportMode exportMode; | ||
private List<String> files; | ||
private List<Integer> bundleIds; | ||
private String branch; | ||
|
||
private ProjectClient projectClient; | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) { | ||
CrowdinProjectFull project = ConsoleSpinner.execute( | ||
out, | ||
"message.spinner.fetching_project_info", "error.collect_project_info", | ||
this.noProgress, | ||
this.plainView, | ||
() -> this.projectClient.downloadFullProject(this.branch) | ||
); | ||
List<Long> fileIds = null; | ||
if (files != null) { | ||
Map<String, Long> projectBranches = project.getBranches().values().stream() | ||
.collect(Collectors.toMap(Branch::getName, Branch::getId)); | ||
List<String> projectFiles = project.getFiles().stream() | ||
.filter(file -> branch == null || file.getBranchId().equals(projectBranches.get(branch))) | ||
.map(FileInfo::getPath) | ||
.collect(Collectors.toList()); | ||
List<String> notExistingFiles = files.stream() | ||
.map(file -> branch == null ? file : Paths.get(branch, file).toString()) | ||
.map(Utils::sepAtStart) | ||
.filter(file -> !projectFiles.contains(file)) | ||
.collect(Collectors.toList()); | ||
if (!notExistingFiles.isEmpty()) { | ||
throw new RuntimeException(notExistingFiles.stream().map(Utils::noSepAtStart) | ||
.map(file -> String.format(RESOURCE_BUNDLE.getString("error.file_not_found"), file)) | ||
.collect(Collectors.joining("\n\u274C "))); | ||
} | ||
files = branch != null ? files.stream().map(file -> Paths.get(branch, file).toString()) | ||
.collect(Collectors.toList()) : files; | ||
files = files.stream().map(Utils::sepAtStart).collect(Collectors.toList()); | ||
fileIds = project | ||
.getFiles() | ||
.stream() | ||
.filter(file -> files.contains(file.getPath())) | ||
.map(FileInfo::getId) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
Distribution distribution; | ||
AddDistributionRequest addDistributionRequest = RequestBuilder.addDistribution(name, exportMode, fileIds, bundleIds); | ||
Optional.ofNullable(name).ifPresent(addDistributionRequest::setName); | ||
Optional.ofNullable(exportMode).ifPresent(addDistributionRequest::setExportMode); | ||
Optional.ofNullable(fileIds).ifPresent(addDistributionRequest::setFileIds); | ||
Optional.ofNullable(bundleIds).ifPresent(addDistributionRequest::setBundleIds); | ||
|
||
try { | ||
distribution = client.addDistribution(addDistributionRequest); | ||
} catch (Exception e) { | ||
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.distribution_is_not_added"), addDistributionRequest), e); | ||
} | ||
|
||
if (!plainView) { | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.added"), distribution.getName()))); | ||
} else { | ||
out.println(String.valueOf(distribution.getName())); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/crowdin/cli/commands/actions/DistributionListAction.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,44 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ClientDistribution; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.client.distributions.model.Distribution; | ||
|
||
import java.util.List; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.OK; | ||
|
||
class DistributionListAction implements NewAction<ProjectProperties, ClientDistribution> { | ||
|
||
private final boolean plainView; | ||
|
||
public DistributionListAction(boolean plainView) { | ||
this.plainView = plainView; | ||
} | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) { | ||
List<Distribution> distributions = client.listDistribution(); | ||
|
||
for (Distribution distribution : distributions) { | ||
if (!plainView) { | ||
out.println(String.format(RESOURCE_BUNDLE.getString("message.distribution.list"), distribution.getHash(), | ||
distribution.getName(), | ||
distribution.getExportMode())); | ||
} else { | ||
out.println(distribution.getHash() + " " + distribution.getName()); | ||
} | ||
} | ||
|
||
if (distributions.isEmpty()) { | ||
if (!plainView) { | ||
out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.distribution.list_empty"))); | ||
} else { | ||
out.println(RESOURCE_BUNDLE.getString("message.distribution.list_empty")); | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.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,56 @@ | ||
package com.crowdin.cli.commands.actions; | ||
|
||
import com.crowdin.cli.client.ClientDistribution; | ||
import com.crowdin.cli.commands.NewAction; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.utils.console.ConsoleSpinner; | ||
import com.crowdin.client.distributions.model.DistributionRelease; | ||
import lombok.AllArgsConstructor; | ||
|
||
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; | ||
import static com.crowdin.cli.utils.console.ExecutionStatus.OK; | ||
|
||
@AllArgsConstructor | ||
class DistributionReleaseAction implements NewAction<ProjectProperties, ClientDistribution> { | ||
|
||
private boolean noProgress; | ||
private boolean plainView; | ||
private String hash; | ||
|
||
@Override | ||
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) { | ||
this.releaseDistribution(out, client); | ||
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.released"), hash))); | ||
} | ||
|
||
private DistributionRelease releaseDistribution(Outputter out, ClientDistribution client) { | ||
return ConsoleSpinner.execute( | ||
out, | ||
"message.spinner.releasing_distribution", | ||
"error.distribution_is_not_released", | ||
this.noProgress, | ||
false, | ||
() -> { | ||
DistributionRelease release = client.release(hash); | ||
|
||
while (!"success".equalsIgnoreCase(release.getStatus())) { | ||
ConsoleSpinner.update( | ||
String.format(RESOURCE_BUNDLE.getString("message.spinner.releasing_distribution_percents"), | ||
release.getProgress())); | ||
Thread.sleep(1000); | ||
|
||
release = client.getDistributionRelease(hash); | ||
|
||
if ("failed".equalsIgnoreCase(release.getStatus())) { | ||
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.spinner.build_has_failed")); | ||
} | ||
} | ||
|
||
ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.spinner.releasing_distribution_percents"), 100)); | ||
|
||
return release; | ||
} | ||
); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.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,33 @@ | ||
package com.crowdin.cli.commands.picocli; | ||
|
||
import com.crowdin.cli.client.ClientDistribution; | ||
import com.crowdin.cli.client.Clients; | ||
import com.crowdin.cli.client.ProjectClient; | ||
import com.crowdin.cli.commands.Outputter; | ||
import com.crowdin.cli.properties.ProjectParams; | ||
import com.crowdin.cli.properties.ProjectProperties; | ||
import com.crowdin.cli.properties.PropertiesBuilders; | ||
import picocli.CommandLine; | ||
|
||
public abstract class ActCommandDistribution extends GenericActCommand<ProjectProperties, ClientDistribution> { | ||
|
||
@CommandLine.Mixin | ||
private ConfigurationFilesProperties properties; | ||
|
||
@CommandLine.ArgGroup(exclusive = false, headingKey = "params.heading") | ||
private ProjectParams params; | ||
|
||
@Override | ||
protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, Outputter out) { | ||
return propertiesBuilders.buildProjectProperties(out, properties.getConfigFile(), properties.getIdentityFile(), params); | ||
} | ||
|
||
@Override | ||
protected ClientDistribution getClient(ProjectProperties properties) { | ||
return Clients.getClientDistribution(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); | ||
} | ||
|
||
protected ProjectClient getProjectClient(ProjectProperties properties) { | ||
return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId())); | ||
} | ||
} |
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.