Skip to content

Commit

Permalink
return old destination and remove dest param
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 committed Oct 19, 2023
1 parent 2419162 commit aca365a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ NewAction<PropertiesWithFiles, ProjectClient> download(
);

NewAction<NoProperties, NoClient> generate(FilesInterface files, String token, String baseUrl, String basePath,
String projectId, String source, String translation, String dest, Boolean preserveHierarchy, Path configDestPath, boolean skipGenerateDescription);
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 @@ -35,9 +35,9 @@ public NewAction<PropertiesWithFiles, ProjectClient> download(

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

@Override
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/com/crowdin/cli/commands/actions/GenerateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ class GenerateAction implements NewAction<NoProperties, NoClient> {
private final String projectId;
private final String source;
private final String translation;
private final String dest;
private final Boolean preserveHierarchy;
private final Path configDestPath;
private final Path destinationPath;
private final boolean skipGenerateDescription;

@Override
Expand All @@ -64,10 +63,10 @@ public void act(Outputter out, NoProperties noProperties, NoClient noClient) {
try {
out.println(String.format(
RESOURCE_BUNDLE.getString("message.command_generate_description"),
configDestPath.toAbsolutePath()));
if (Files.exists(configDestPath)) {
destinationPath.toAbsolutePath()));
if (Files.exists(destinationPath)) {
out.println(ExecutionStatus.SKIPPED.getIcon() + String.format(
RESOURCE_BUNDLE.getString("message.already_exists"), configDestPath.toAbsolutePath()));
RESOURCE_BUNDLE.getString("message.already_exists"), destinationPath.toAbsolutePath()));
return;
}

Expand All @@ -76,7 +75,7 @@ public void act(Outputter out, NoProperties noProperties, NoClient noClient) {
this.updateWithUserInputs(out, asking, fileLines);
}
files.writeToFile(
configDestPath.toString(), new ByteArrayInputStream(StringUtils.join(fileLines, "\n").getBytes(StandardCharsets.UTF_8)));
destinationPath.toString(), new ByteArrayInputStream(StringUtils.join(fileLines, "\n").getBytes(StandardCharsets.UTF_8)));
out.println(String.format(
RESOURCE_BUNDLE.getString("message.generate_successful"),
this.isEnterprise ? ENTERPRISE_LINK : LINK));
Expand Down Expand Up @@ -168,13 +167,9 @@ private void updateWithUserInputs(Outputter out, Asking asking, List<String> fil
for (int i = 0; i < fileLines.size(); i++) {
String keyToSearch = String.format("\"%s\"", entry.getKey());
if (fileLines.get(i).contains(keyToSearch)) {
String updatedLine;
if (PRESERVE_HIERARCHY.equals(entry.getKey())) {
updatedLine = fileLines.get(i).replace(String.valueOf(TRUE), entry.getValue());
} else {
updatedLine = fileLines.get(i).replaceFirst(": \"*\"", String.format(": \"%s\"", Utils.regexPath(entry.getValue())));
}
updatedLine = updatedLine.replace(" #", "");
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;
}
Expand All @@ -189,7 +184,6 @@ private void setGivenParams(Map<String, String> values) {
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(dest).ifPresent(v -> values.put(DEST, dest));
Optional.ofNullable(preserveHierarchy).ifPresent(v -> values.put(PRESERVE_HIERARCHY, String.valueOf(preserveHierarchy)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,35 @@
aliases = CommandNames.ALIAS_GENERATE)
public class GenerateSubcommand extends GenericActCommand<NoProperties, NoClient> {

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

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

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

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

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

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

@CommandLine.Option(names = {"--dest"}, paramLabel = "...", descriptionKey = "params.dest")
private String dest;

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

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

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

protected NewAction<NoProperties, NoClient> getAction(Actions actions) {
return actions.generate(new FsFiles(), token, baseUrl, basePath, projectId, source, translation, dest, preserveHierarchy, configDestPath, 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, null, null, null, null, null, null, null, null, false));
assertNotNull(actions.generate(new FsFiles(), null, null, null, null, null, null, null, null, false));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void simpleTest() throws IOException {
InputStream responsesIS = setResponses(false, false, "apiToken", "42", ".");
System.setIn(responsesIS);

action = new GenerateAction(files, null, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class));

verify(files).writeToFile(anyString(), any());
Expand All @@ -55,7 +55,7 @@ public void simpleTest() throws IOException {
public void userInputTest() throws IOException {
FilesInterface files = mock(FilesInterface.class);

action = new GenerateAction(files, "token", "", ".", "42", "file.json", "translation.json", "dest", true, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, "token", "", ".", "42", "file.json", "translation.json", true, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class));

verify(files).writeToFile(anyString(), any());
Expand All @@ -69,7 +69,7 @@ public void writeToFileThrowsTest() throws IOException {
InputStream responsesIS = setResponses(false, false, "apiToken", "42", ".");
System.setIn(responsesIS);

action = new GenerateAction(files, null, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class)));

verify(files).writeToFile(anyString(), any());
Expand All @@ -83,7 +83,7 @@ public void enterprisetest() throws IOException {
InputStream responsesIS = setResponses(false, true, "undefined", "apiToken", "42", ".");
System.setIn(responsesIS);

action = new GenerateAction(files, null, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class)));

verify(files).writeToFile(anyString(), any());
Expand All @@ -97,7 +97,7 @@ public void enterpriseUrlTest() throws IOException {
InputStream responsesIS = setResponses(false, true, "https://undefined.crowdin.com", "apiToken", "42", ".");
System.setIn(responsesIS);

action = new GenerateAction(files, null, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class)));

verify(files).writeToFile(anyString(), any());
Expand All @@ -111,7 +111,7 @@ public void enterpriseNoNametest() throws IOException {
InputStream responsesIS = setResponses(false, true, "", "apiToken", "42", ".");
System.setIn(responsesIS);

action = new GenerateAction(files, null, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class)));

verify(files).writeToFile(anyString(), any());
Expand All @@ -126,7 +126,7 @@ public void fileExists() throws IOException {
InputStream responsesIS = setResponses(false, true, "https://undefined.crowdin.com", "apiToken", "42", ".");
System.setIn(responsesIS);

action = new GenerateAction(files, null, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action = new GenerateAction(files, null, null, null, null, null, null, null, Paths.get(project.getBasePath() + "/crowdin.yml"), false);
action.act(Outputter.getDefault(), new NoProperties(), mock(NoClient.class));

verifyNoMoreInteractions(files);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GenerateSubcommandTest extends PicocliTestUtils {
public void testGenerate() {
this.execute(CommandNames.GENERATE);
verify(actionsMock)
.generate(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), anyBoolean());
.generate(any(), any(), any(), any(), any(), any(), any(), any(), any(), anyBoolean());
this.check(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void mockActions() {

when(actionsMock.download(any(), anyBoolean(), any(), any(), anyBoolean(), any(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.generate(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), anyBoolean()))
when(actionsMock.generate(any(), any(), any(), any(), any(), any(), any(), any(), any(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.listBranches(anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
Expand Down

0 comments on commit aca365a

Please sign in to comment.