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

feat: config params for generate command #670

Merged
merged 5 commits into from
Nov 1, 2023
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
3 changes: 2 additions & 1 deletion src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ NewAction<PropertiesWithFiles, ProjectClient> download(
boolean ignoreMatch, boolean isVerbose, boolean plainView, boolean userServerSources, boolean keepArchive
);

NewAction<NoProperties, NoClient> generate(FilesInterface files, Path destinationPath, boolean skipGenerateDescription);
NewAction<NoProperties, NoClient> generate(FilesInterface files, String token, String baseUrl, String basePath,
String projectId, String source, String translation, Boolean preserveHierarchy, Path destinationPath, boolean skipGenerateDescription);

NewAction<ProjectProperties, ProjectClient> listBranches(boolean noProgress, boolean plainView);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public NewAction<PropertiesWithFiles, ProjectClient> download(
}

@Override
public NewAction<NoProperties, NoClient> generate(FilesInterface files, Path destinationPath, boolean skipGenerateDescription) {
return new GenerateAction(files, destinationPath, skipGenerateDescription);
public NewAction<NoProperties, NoClient> generate(FilesInterface files, String token, String baseUrl, String basePath,
String projectId, String source, String translation, Boolean preserveHierarchy, Path destinationPath, boolean skipGenerateDescription
) {
return new GenerateAction(files, token, baseUrl, basePath, projectId, source, translation, preserveHierarchy, destinationPath, skipGenerateDescription);
}

@Override
Expand Down
106 changes: 64 additions & 42 deletions src/main/java/com/crowdin/cli/commands/actions/GenerateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.cli.utils.console.ExecutionStatus;
import com.crowdin.cli.utils.http.OAuthUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.*;

import static com.crowdin.cli.BaseCli.OAUTH_CLIENT_ID;
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.properties.PropertiesBuilder.API_TOKEN;
import static com.crowdin.cli.properties.PropertiesBuilder.BASE_PATH;
import static com.crowdin.cli.properties.PropertiesBuilder.BASE_URL;
import static com.crowdin.cli.properties.PropertiesBuilder.PROJECT_ID;
import static com.crowdin.cli.properties.PropertiesBuilder.*;
import static com.crowdin.cli.utils.console.ExecutionStatus.ERROR;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;
import static com.crowdin.cli.utils.console.ExecutionStatus.WARNING;
import static java.lang.Boolean.TRUE;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

@RequiredArgsConstructor
class GenerateAction implements NewAction<NoProperties, NoClient> {

public static final String BASE_PATH_DEFAULT = ".";
Expand All @@ -46,15 +45,16 @@
public static final String LINK = "https://support.crowdin.com/configuration-file/";
public static final String ENTERPRISE_LINK = "https://support.crowdin.com/enterprise/configuration-file/";

private FilesInterface files;
private Path destinationPath;
private boolean skipGenerateDescription;

public GenerateAction(FilesInterface files, Path destinationPath, boolean skipGenerateDescription) {
this.files = files;
this.destinationPath = destinationPath;
this.skipGenerateDescription = skipGenerateDescription;
}
private final FilesInterface files;
private final String token;
private final String baseUrl;
private final String basePath;
private final String projectId;
private final String source;
private final String translation;
private final Boolean preserveHierarchy;
private final Path destinationPath;
private final boolean skipGenerateDescription;

@Override
public void act(Outputter out, NoProperties noProperties, NoClient noClient) {
Expand Down Expand Up @@ -87,8 +87,9 @@

private void updateWithUserInputs(Outputter out, Asking asking, List<String> fileLines) {
Map<String, String> values = new HashMap<>();
setGivenParams(values);

withBrowser = !StringUtils.startsWithAny(asking.ask(
withBrowser = isNull(token) && !StringUtils.startsWithAny(asking.ask(
RESOURCE_BUNDLE.getString("message.ask_auth_via_browser") + ": (Y/n) "), "n", "N", "-");
if (withBrowser) {
String token;
Expand All @@ -108,38 +109,45 @@
values.put(BASE_URL, BASE_URL_DEFAULT);
}
} else {
this.isEnterprise = StringUtils.startsWithAny(asking.ask(
if (isNull(baseUrl)) {
this.isEnterprise = StringUtils.startsWithAny(asking.ask(
RESOURCE_BUNDLE.getString("message.ask_is_enterprise") + ": (N/y) "), "y", "Y", "+");
if (this.isEnterprise) {
String organizationName = asking.ask(RESOURCE_BUNDLE.getString("message.ask_organization_name") + ": ");
if (StringUtils.isNotEmpty(organizationName)) {
if (PropertiesBeanUtils.isUrlValid(organizationName)) {
String realOrganizationName = PropertiesBeanUtils.getOrganization(organizationName);
System.out.println(String.format(RESOURCE_BUNDLE.getString("message.extracted_organization_name"), realOrganizationName));
values.put(BASE_URL, String.format(BASE_ENTERPRISE_URL_DEFAULT, realOrganizationName));
if (this.isEnterprise) {
String organizationName = asking.ask(RESOURCE_BUNDLE.getString("message.ask_organization_name") + ": ");
if (StringUtils.isNotEmpty(organizationName)) {
if (PropertiesBeanUtils.isUrlValid(organizationName)) {
String realOrganizationName = PropertiesBeanUtils.getOrganization(organizationName);
System.out.println(String.format(RESOURCE_BUNDLE.getString("message.extracted_organization_name"), realOrganizationName));
values.put(BASE_URL, String.format(BASE_ENTERPRISE_URL_DEFAULT, realOrganizationName));
} else {
values.put(BASE_URL, String.format(BASE_ENTERPRISE_URL_DEFAULT, PropertiesBeanUtils.getOrganization(organizationName)));
}
} else {
values.put(BASE_URL, String.format(BASE_ENTERPRISE_URL_DEFAULT, PropertiesBeanUtils.getOrganization(organizationName)));
this.isEnterprise = false;
values.put(BASE_URL, BASE_URL_DEFAULT);
}
} else {
this.isEnterprise = false;
values.put(BASE_URL, BASE_URL_DEFAULT);
}
} else {
values.put(BASE_URL, BASE_URL_DEFAULT);
}
String apiToken = asking.askParam(API_TOKEN);
if (!apiToken.isEmpty()) {
values.put(API_TOKEN, apiToken);
if (isNull(token)) {
String apiToken = asking.askParam(API_TOKEN);
if (!apiToken.isEmpty()) {
values.put(API_TOKEN, apiToken);
}
}
}
boolean projectIdSpecified = nonNull(projectId);
while (true) {
String projectId = asking.askParam(PROJECT_ID);
if (projectId.isEmpty()) {
String projectIdToSet = projectIdSpecified ? projectId : asking.askParam(PROJECT_ID);
if (projectIdToSet.isEmpty()) {
break;
} else if (StringUtils.isNumeric(projectId)) {
values.put(PROJECT_ID, projectId);
} else if (StringUtils.isNumeric(projectIdToSet)) {
values.put(PROJECT_ID, projectIdToSet);
break;
} else {
projectIdSpecified = false;
values.remove(PROJECT_ID);

Check warning on line 150 in src/main/java/com/crowdin/cli/commands/actions/GenerateAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/GenerateAction.java#L149-L150

Added lines #L149 - L150 were not covered by tests
System.out.println(String.format(RESOURCE_BUNDLE.getString("error.init.project_id_is_not_number"), projectId));
}
}
Expand All @@ -148,23 +156,37 @@
} else {
System.out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("error.init.skip_project_validation")));
}
String basePath = asking.askWithDefault(RESOURCE_BUNDLE.getString("message.ask_project_directory"), BASE_PATH_DEFAULT);
java.io.File basePathFile = Paths.get(basePath).normalize().toAbsolutePath().toFile();
String basePathToSet = nonNull(basePath) ? basePath : asking.askWithDefault(RESOURCE_BUNDLE.getString("message.ask_project_directory"), BASE_PATH_DEFAULT);
java.io.File basePathFile = Paths.get(basePathToSet).normalize().toAbsolutePath().toFile();
if (!basePathFile.exists()) {
System.out.println(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("error.init.path_not_exist"), basePathFile)));
}
values.put(BASE_PATH, basePath);
values.put(BASE_PATH, basePathToSet);

for (Map.Entry<String, String> entry : values.entrySet()) {
for (int i = 0; i < fileLines.size(); i++) {
if (fileLines.get(i).contains(entry.getKey())) {
fileLines.set(i, fileLines.get(i).replaceFirst(": \"*\"", String.format(": \"%s\"", Utils.regexPath(entry.getValue()))));
String keyToSearch = String.format("\"%s\"", entry.getKey());
if (fileLines.get(i).contains(keyToSearch)) {
String updatedLine = PRESERVE_HIERARCHY.equals(entry.getKey()) ?
fileLines.get(i).replace(String.valueOf(TRUE), entry.getValue())
: fileLines.get(i).replaceFirst(": \"*\"", String.format(": \"%s\"", Utils.regexPath(entry.getValue())));
fileLines.set(i, updatedLine);
break;
}
}
}
}

private void setGivenParams(Map<String, String> values) {
Optional.ofNullable(token).ifPresent(v -> values.put(API_TOKEN, token));
Optional.ofNullable(baseUrl).ifPresent(v -> values.put(BASE_URL, baseUrl));
Optional.ofNullable(basePath).ifPresent(v -> values.put(BASE_PATH, basePath));
Optional.ofNullable(projectId).ifPresent(v -> values.put(PROJECT_ID, projectId));
Optional.ofNullable(source).ifPresent(v -> values.put(SOURCE, source));
Optional.ofNullable(translation).ifPresent(v -> values.put(TRANSLATION, translation));
Optional.ofNullable(preserveHierarchy).ifPresent(v -> values.put(PRESERVE_HIERARCHY, String.valueOf(preserveHierarchy)));
}

public static class Asking {

private Outputter out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,40 @@

@CommandLine.Command(
name = CommandNames.GENERATE,
aliases = CommandNames.ALIAS_GENERATE)
aliases = CommandNames.ALIAS_GENERATE,
sortOptions = false
)
public class GenerateSubcommand extends GenericActCommand<NoProperties, NoClient> {

@CommandLine.Option(names = {"-d", "--destination"}, paramLabel = "...", defaultValue = "crowdin.yml")
@CommandLine.Option(names = {"-d", "--destination"}, paramLabel = "...", descriptionKey = "crowdin.generate.destination", defaultValue = "crowdin.yml", order = -2)
private Path destinationPath;

@CommandLine.Option(names = {"-T", "--token"}, paramLabel = "...", descriptionKey = "params.token", order = -2)
private String token;

@CommandLine.Option(names = {"-i", "--project-id"}, paramLabel = "...", descriptionKey = "params.project-id", order = -2)
private String projectId;

@CommandLine.Option(names = {"--base-path"}, paramLabel = "...", descriptionKey = "params.base-path", order = -2)
private String basePath;

@CommandLine.Option(names = {"--base-url"}, paramLabel = "...", descriptionKey = "params.base-url", order = -2)
private String baseUrl;

@CommandLine.Option(names = {"-s", "--source"}, paramLabel = "...", descriptionKey = "params.source", order = -2)
private String source;

@CommandLine.Option(names = {"-t", "--translation"}, paramLabel = "...", descriptionKey = "params.translation", order = -2)
private String translation;

@CommandLine.Option(names = {"--preserve-hierarchy"}, negatable = true, paramLabel = "...", descriptionKey = "params.preserve-hierarchy", order = -2)
private Boolean preserveHierarchy;

@CommandLine.Option(names = "--skip-generate-description", hidden = true)
private boolean skipGenerateDescription;

protected NewAction<NoProperties, NoClient> getAction(Actions actions) {
return actions.generate(new FsFiles(), destinationPath, skipGenerateDescription);
return actions.generate(new FsFiles(), token, baseUrl, basePath, projectId, source, translation, preserveHierarchy, destinationPath, skipGenerateDescription);
}

protected NoProperties getProperties(PropertiesBuilders propertiesBuilders, Outputter out) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testDownload() {

@Test
public void testGenerate() {
assertNotNull(actions.generate(new FsFiles(), null, false));
assertNotNull(actions.generate(new FsFiles(), null, null, null, null, null, null, null, null, false));
}

@Test
Expand Down
Loading