Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #374

Merged
merged 4 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ private void populateProjectWithStructure(CrowdinProjectFull project) {
.listFiles(this.projectId, null, null, null, limit, offset)));
project.setDirectories(executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()
.listDirectories(this.projectId, null, null, null, limit, offset)));
project.setBranches(executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()
.listBranches(this.projectId, null, limit, offset)));
project.setBranches(this.listBranches());
}

private void populateProjectWithLangs(CrowdinProject project) {
Expand Down Expand Up @@ -106,13 +105,25 @@ public List<LanguageProgress> getProjectProgress(String languageId) {
.getProjectProgress(this.projectId, limit, offset, languageId));
}

@Override
public List<LanguageProgress> getBranchProgress(Long branchId) {
return executeRequestFullList((limit, offset) -> this.client.getTranslationStatusApi()
.getBranchProgress(this.projectId, branchId, limit, offset));
}

@Override
public Branch addBranch(AddBranchRequest request) {
return executeRequest(() -> this.client.getSourceFilesApi()
.addBranch(this.projectId, request)
.getData());
}

@Override
public List<Branch> listBranches() {
return executeRequestFullList((limit, offset) -> this.client.getSourceFilesApi()
.listBranches(this.projectId, null, limit, offset));
}

@Override
public Long uploadStorage(String fileName, InputStream content) {
Storage storage = executeRequest(() -> this.client.getStorageApi()
Expand All @@ -134,6 +145,15 @@ public Directory addDirectory(AddDirectoryRequest request) throws ResponseExcept
.getData());
}

@Override
public void deleteDirectory(Long directoryId) {
executeRequest(() -> {
this.client.getSourceFilesApi()
.deleteDirectory(this.projectId, directoryId);
return null;
});
}

@Override
public void updateSource(Long sourceId, UpdateFileRequest request) {
executeRequestWithPossibleRetry(
Expand All @@ -160,6 +180,15 @@ public void editSource(Long fileId, List<PatchRequest> request) {
.editFile(this.projectId, fileId, request));
}

@Override
public void deleteSource(Long fileId) {
executeRequest(() -> {
this.client.getSourceFilesApi()
.deleteFile(this.projectId, fileId);
return null;
});
}

@Override
public void uploadTranslations(String languageId, UploadTranslationsRequest request) throws ResponseException {
Map<BiPredicate<String, String>, ResponseException> errorhandlers = new LinkedHashMap<BiPredicate<String, String>, ResponseException>() {{
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectFull.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -55,6 +56,13 @@ public Map<Long, Directory> getDirectories() {
.collect(Collectors.toMap(Directory::getId, Function.identity()));
}

public Map<Long, Directory> getDirectories(Long branchId) {
return directories
.stream()
.filter(dir -> Objects.equals(dir.getBranchId(), branchId))
.collect(Collectors.toMap(Directory::getId, Function.identity()));
}

/**
* returns list of files. Should be checked with isManagerAccess. Otherwise use getFileInfos()
* @return list of files
Expand All @@ -72,7 +80,27 @@ public List<File> getFiles() {
}
}

public List<File> getFiles(Long branchId) {
List<File> result = new ArrayList<>();
for (File file : this.getFiles()) {
if (Objects.equals(file.getBranchId(), branchId)) {
result.add(file);
}
}
return result;
}

public List<FileInfo> getFileInfos() {
return (List<FileInfo>) files;
}

public List<FileInfo> getFileInfos(Long branchId) {
List<FileInfo> result = new ArrayList<>();
for (FileInfo file : this.getFileInfos()) {
if (Objects.equals(file.getBranchId(), branchId)) {
result.add(file);
}
}
return result;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ public interface ProjectClient extends Client {

Branch addBranch(AddBranchRequest request);

List<Branch> listBranches();

Long uploadStorage(String fileName, InputStream content);

Directory addDirectory(AddDirectoryRequest request) throws ResponseException;

void deleteDirectory(Long directoryId);

void updateSource(Long sourceId, UpdateFileRequest request);

void addSource(AddFileRequest request) throws ResponseException;

void editSource(Long fileId, List<PatchRequest> request);

void deleteSource(Long fileId);

void uploadTranslations(String languageId, UploadTranslationsRequest request) throws ResponseException;

ProjectBuild startBuildingTranslation(BuildProjectTranslationRequest request);
Expand All @@ -53,6 +59,8 @@ public interface ProjectClient extends Client {

List<LanguageProgress> getProjectProgress(String languageId);

List<LanguageProgress> getBranchProgress(Long branchId);

SourceString addSourceString(AddSourceStringRequest request);

List<SourceString> listSourceString(Long fileId, String labelIds, String filter);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ NewAction<PropertiesWithFiles, ProjectClient> listTranslations(
NewAction<PropertiesWithFiles, ProjectClient> listLanguages(BaseCli.LanguageCode code, boolean noProgress, boolean plainView);

NewAction<PropertiesWithFiles, ProjectClient> status(
boolean noProgress, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved);
boolean noProgress, String branchName, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved);

NewAction<PropertiesWithFiles, ProjectClient> stringAdd(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, Boolean hidden);
Expand All @@ -58,7 +58,7 @@ NewAction<PropertiesWithFiles, ProjectClient> stringList(
boolean noProgress, boolean isVerbose, String file, String filter);

NewAction<PropertiesWithFiles, ProjectClient> uploadSources(
String branchName, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView);
String branchName, boolean deleteObsolete, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView);

NewAction<PropertiesWithFiles, ProjectClient> uploadTranslations(
boolean noProgress, String languageId, String branchName, boolean importEqSuggestions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public NewAction<PropertiesWithFiles, ProjectClient> listLanguages(BaseCli.Langu

@Override
public NewAction<PropertiesWithFiles, ProjectClient> status(
boolean noProgress, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved
boolean noProgress, String branchName, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved
) {
return new StatusAction(noProgress, languageId, isVerbose, showTranslated, showApproved);
return new StatusAction(noProgress, branchName, languageId, isVerbose, showTranslated, showApproved);
}

@Override
Expand Down Expand Up @@ -105,9 +105,9 @@ public NewAction<PropertiesWithFiles, ProjectClient> stringList(

@Override
public NewAction<PropertiesWithFiles, ProjectClient> uploadSources(
String branchName, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView
String branchName, boolean deleteObsolete, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView
) {
return new UploadSourcesAction(branchName, noProgress, autoUpdate, debug, plainView);
return new UploadSourcesAction(branchName, deleteObsolete, noProgress, autoUpdate, debug, plainView);
}

@Override
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/crowdin/cli/commands/actions/StatusAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.translationstatus.model.LanguageProgress;

import java.util.List;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;

class StatusAction implements NewAction<PropertiesWithFiles, ProjectClient> {

private boolean noProgress;
private String branchName;
private String languageId;
private boolean isVerbose;
private boolean showTranslated;
private boolean showApproved;

public StatusAction(boolean noProgress, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved) {
public StatusAction(boolean noProgress, String branchName, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved) {
this.noProgress = noProgress;
this.branchName = branchName;
this.languageId = languageId;
this.isVerbose = isVerbose;
this.showTranslated = showTranslated;
Expand All @@ -37,8 +41,24 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
project.findLanguageById(languageId, true)
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.not_found_language"), languageId)));
}
List<Branch> branches = client.listBranches();
Long branchId = (branchName == null) ? null : branches.stream()
.filter(branch -> branchName.equals(branch.getName()))
.findFirst()
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch")))
.getId();

List<LanguageProgress> progresses = client.getProjectProgress(languageId);
List<LanguageProgress> progresses;
if (branchId == null) {
progresses = client.getProjectProgress(languageId);
} else {
progresses = client.getBranchProgress(branchId);
if (languageId != null) {
progresses = progresses.stream()
.filter(langProgress -> languageId.equals(langProgress.getLanguageId()))
.collect(Collectors.toList());
}
}

if (isVerbose) {
progresses.forEach(pr -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.actions.subactions.DeleteObsoleteProjectFilesSubAction;
import com.crowdin.cli.commands.functionality.ProjectFilesUtils;
import com.crowdin.cli.commands.functionality.ProjectUtils;
import com.crowdin.cli.commands.functionality.PropertiesBeanUtils;
Expand Down Expand Up @@ -55,13 +56,15 @@
class UploadSourcesAction implements NewAction<PropertiesWithFiles, ProjectClient> {

private String branchName;
private boolean deleteObsolete;
private boolean noProgress;
private boolean autoUpdate;
private boolean debug;
private boolean plainView;

public UploadSourcesAction(String branchName, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView) {
public UploadSourcesAction(String branchName, boolean deleteObsolete, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView) {
this.branchName = branchName;
this.deleteObsolete = deleteObsolete;
this.noProgress = noProgress || plainView;
this.autoUpdate = autoUpdate;
this.debug = debug;
Expand All @@ -75,23 +78,32 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {

boolean containsExcludedLanguages = pb.getFiles().stream()
.map(FileBean::getExcludedTargetLanguages).filter(Objects::nonNull).anyMatch(l -> !l.isEmpty());
if (!project.isManagerAccess() && containsExcludedLanguages) {
if (!project.isManagerAccess() && (containsExcludedLanguages || deleteObsolete)) {
if (!plainView) {
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.no_manager_access_for_excluded_languages")));
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.no_manager_access_in_upload_sources")));
return;
} else {
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.no_manager_access_for_excluded_languages"));
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.no_manager_access_in_upload_sources"));
}
}

PlaceholderUtil placeholderUtil = new PlaceholderUtil(project.getSupportedLanguages(), project.getProjectLanguages(false), pb.getBasePath());

Branch branchId = (branchName != null) ? this.getOrCreateBranch(out, branchName, client, project) : null;
Branch branch = (branchName != null) ? this.getOrCreateBranch(out, branchName, client, project) : null;
Long branchId = (branch != null) ? branch.getId() : null;

Map<String, Long> directoryPaths = ProjectFilesUtils.buildDirectoryPaths(project.getDirectories(), project.getBranches())
.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Map<String, FileInfo> paths = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos());

DeleteObsoleteProjectFilesSubAction deleteObsoleteProjectFilesSubAction = new DeleteObsoleteProjectFilesSubAction(out, client);
if (deleteObsolete) {
Map<String, Long> directories = ProjectFilesUtils.buildDirectoryPaths(project.getDirectories(branchId))
.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Map<String, com.crowdin.client.sourcefiles.model.File> projectFiles = ProjectFilesUtils.buildFilePaths(project.getDirectories(branchId), project.getFiles(branchId));
deleteObsoleteProjectFilesSubAction.setData(projectFiles, directories, pb.getPreserveHierarchy(), this.plainView);
}

List<String> uploadedSources = new ArrayList<>();

Map<String, Long> labels = client.listLabels().stream()
Expand Down Expand Up @@ -128,6 +140,17 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
}
String commonPath =
(pb.getPreserveHierarchy()) ? "" : SourcesUtils.getCommonPath(sources, pb.getBasePath());
if (deleteObsolete) {
List<String> filesToUpdate = sources.stream().map(source -> (file.getDest() != null)
? PropertiesBeanUtils.prepareDest(file.getDest(), StringUtils.removeStart(source, pb.getBasePath()), placeholderUtil)
: StringUtils.removeStart(source, pb.getBasePath() + commonPath))
.collect(Collectors.toList());
if (file.getDest() != null) {
deleteObsoleteProjectFilesSubAction.act(file.getDest(), file.getTranslation(), filesToUpdate);
} else {
deleteObsoleteProjectFilesSubAction.act(file.getSource(), file.getIgnore(), file.getTranslation(), filesToUpdate);
}
}
List<Runnable> taskss = sources.stream()
.map(source -> {
final File sourceFile = new File(source);
Expand Down Expand Up @@ -210,16 +233,16 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
return (Runnable) () -> {
Long directoryId = null;
try {
directoryId = ProjectUtils.createPath(out, client, directoryPaths, filePath, branchId, plainView);
directoryId = ProjectUtils.createPath(out, client, directoryPaths, filePath, branch, plainView);
} catch (Exception e) {
errorsPresented.set(true);
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.creating_directories"), e);
}

if (directoryId != null) {
request.setDirectoryId(directoryId);
} else if (branchId != null) {
request.setBranchId(branchId.getId());
} else if (branch != null) {
request.setBranchId(branch.getId());
}

try (InputStream fileStream = new FileInputStream(sourceFile)) {
Expand Down
Loading