Skip to content

Commit 0a00e3f

Browse files
committed
Update code for new dependencies
1 parent 6ab1470 commit 0a00e3f

File tree

46 files changed

+535
-634
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+535
-634
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*.iml
1717
*.ipr
1818

19+
extraction.json
20+
simplelocalize.yml
21+
1922
test
2023

2124
### NetBeans ###

src/main/java/io/simplelocalize/cli/SimplelocalizeCliCommand.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,33 @@ public static void main(String[] args)
5353

5454
@Command(
5555
name = "extract",
56-
description = "Extract translation keys from project files. Use 'simplelocalize extract --help' to learn more about the parameters.")
56+
description = "Extract translation keys from your project or website. Use 'simplelocalize extract --help' to learn more about the parameters.")
5757
public void extract(
58-
@Option(names = {"--apiKey"}, description = "Project API Key") String apiKey,
59-
@Option(names = {"--projectType"}, description = "Project type tells CLI how to find i18n keys in your project files") String projectType,
60-
@Option(names = {"--searchDir"}, description = "(Optional) Search directory tells CLI where to look for project files which may contain translation keys. Default: ./") String searchDirectory,
61-
@Option(names = {"--baseUrl"}, description = "(Optional) Set custom server URL") String baseUrl
58+
@Option(names = {"--projectType"}, description = "Choose extraction type") String projectType,
59+
@Option(names = {"--searchDir"}, description = "(Optional) Choose where to search it can be local file path. Default: ./") String searchDir,
60+
@Option(names = {"--outputPath"}, description = "(Optional) Choose where to save results. Default: ./extraction.json") String outputPath
6261
)
6362
{
6463
try
6564
{
6665
ConfigurationLoader configurationLoader = new ConfigurationLoader();
6766
Configuration configuration = configurationLoader.loadOrGetDefault(configurationFilePath);
68-
if (StringUtils.isNotEmpty(baseUrl))
69-
{
70-
configuration.setBaseUrl(baseUrl);
71-
}
72-
if (StringUtils.isNotEmpty(apiKey))
73-
{
74-
configuration.setApiKey(apiKey);
75-
}
7667
if (StringUtils.isNotEmpty(projectType))
7768
{
7869
configuration.setProjectType(projectType);
7970
}
80-
if (StringUtils.isNotEmpty(searchDirectory))
71+
if (StringUtils.isNotEmpty(searchDir))
8172
{
82-
configuration.setSearchDir(searchDirectory);
73+
configuration.setSearchDir(searchDir);
8374
}
84-
if (StringUtils.isNotEmpty(searchDirectory))
75+
if (StringUtils.isNotEmpty(outputPath))
8576
{
86-
configuration.setSearchDir(searchDirectory);
77+
configuration.setOutputPath(outputPath);
8778
}
8879
this.effectiveCommandConfiguration = configuration;
8980
ConfigurationValidator configurationValidator = new ConfigurationValidator();
9081
configurationValidator.validateExtractConfiguration(configuration);
91-
SimpleLocalizeClient client = SimpleLocalizeClient.create(configuration.getBaseUrl(), configuration.getApiKey());
92-
ExtractCommand extractCommand = new ExtractCommand(client, configuration);
82+
ExtractCommand extractCommand = new ExtractCommand(configuration);
9383
extractCommand.invoke();
9484
} catch (Exception e)
9585
{

src/main/java/io/simplelocalize/cli/client/SimpleLocalizeClient.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.nio.file.StandardOpenOption;
23-
import java.util.Collection;
2423
import java.util.List;
2524
import java.util.Objects;
2625
import java.util.Optional;
@@ -55,15 +54,6 @@ public static SimpleLocalizeClient create(String baseUrl, String apiKey)
5554
return new SimpleLocalizeClient(baseUrl, apiKey);
5655
}
5756

58-
public Integer uploadKeys(Collection<String> keys) throws IOException, InterruptedException
59-
{
60-
URI uri = uriFactory.buildSendKeysURI();
61-
HttpRequest httpRequest = httpRequestFactory.createSendKeysRequest(uri, keys);
62-
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
63-
throwOnError(httpResponse);
64-
return JsonPath.read(httpResponse.body(), "$.data.uniqueKeysProcessed");
65-
}
66-
6757
public void uploadFile(UploadRequest uploadRequest) throws IOException, InterruptedException
6858
{
6959
URI uri = uriFactory.buildUploadUri(uploadRequest);

src/main/java/io/simplelocalize/cli/client/SimpleLocalizeHttpRequestFactory.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.simplelocalize.cli.client;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
43
import io.simplelocalize.cli.Version;
54
import io.simplelocalize.cli.client.dto.UploadRequest;
65

@@ -9,7 +8,6 @@
98
import java.net.http.HttpRequest;
109
import java.nio.file.Path;
1110
import java.security.SecureRandom;
12-
import java.util.Collection;
1311
import java.util.HashMap;
1412
import java.util.Map;
1513

@@ -28,14 +26,6 @@ public SimpleLocalizeHttpRequestFactory(String apiKey)
2826
this.random = new SecureRandom();
2927
}
3028

31-
HttpRequest createSendKeysRequest(URI uri, Collection<String> keys) throws JsonProcessingException
32-
{
33-
return createBaseRequest(uri)
34-
.POST(ClientBodyBuilders.ofKeysBody(keys))
35-
.header(CONTENT_TYPE_HEADER_NAME, "application/json")
36-
.build();
37-
}
38-
3929
HttpRequest createUploadFileRequest(URI uri, UploadRequest uploadRequest) throws IOException
4030
{
4131
int pseudoRandomNumber = (int) (random.nextDouble() * 1_000_000_000);

src/main/java/io/simplelocalize/cli/client/dto/proxy/Configuration.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ public class Configuration
2121
private String apiKey;
2222

2323
private String projectType;
24-
private String searchDir;
24+
25+
@Builder.Default
26+
private String searchDir = "./";
27+
28+
@Builder.Default
29+
private String outputPath = "extraction.json";
2530

2631
@Builder.Default
2732
private List<String> ignoreKeys = new ArrayList<>();
2833

34+
@Builder.Default
35+
private List<String> ignorePaths = new ArrayList<>();
36+
2937
private String uploadPath;
3038
private String uploadFormat;
3139

src/main/java/io/simplelocalize/cli/command/DownloadCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void invoke() throws IOException, InterruptedException
3232
String downloadFormat = configuration.getDownloadFormat();
3333
String languageKey = configuration.getLanguageKey();
3434
String customerId = configuration.getCustomerId();
35+
String namespace = configuration.getNamespace();
3536
String sort = configuration.getDownloadSort();
3637
List<String> downloadOptions = new ArrayList<>(configuration.getDownloadOptions());
3738

@@ -52,12 +53,17 @@ public void invoke() throws IOException, InterruptedException
5253
.withLanguageKey(languageKey)
5354
.withSort(sort)
5455
.build();
55-
log.info("Preparing files to download");
5656
log.info("File format: {}", downloadFormat);
5757
if (StringUtils.isNotEmpty(customerId))
5858
{
5959
log.info("Customer ID: {}", customerId);
6060
}
61+
62+
if (StringUtils.isNotEmpty(namespace))
63+
{
64+
log.info("Namespace: {}", namespace);
65+
}
66+
6167
if (StringUtils.isNotEmpty(languageKey))
6268
{
6369
log.info("Language key: {}", languageKey);
Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.simplelocalize.cli.command;
22

3-
import io.simplelocalize.cli.client.SimpleLocalizeClient;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.simplelocalize.cli.client.ObjectMapperSingleton;
45
import io.simplelocalize.cli.client.dto.proxy.Configuration;
5-
import io.simplelocalize.cli.extraction.ExtractionResult;
6-
import io.simplelocalize.cli.extraction.ProjectProcessorFactory;
6+
import io.simplelocalize.cli.extraction.*;
77
import io.simplelocalize.cli.extraction.processor.ExtractionProcessor;
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
@@ -13,38 +13,68 @@
1313
import java.nio.file.Paths;
1414
import java.util.HashSet;
1515
import java.util.List;
16+
import java.util.Map;
1617
import java.util.Set;
18+
import java.util.stream.Collectors;
1719

1820
public class ExtractCommand implements CliCommand
1921
{
2022
private static final Logger log = LoggerFactory.getLogger(ExtractCommand.class);
2123

22-
private final SimpleLocalizeClient client;
2324
private final Configuration configuration;
25+
private final ExtractionProcessorFactory extractionProcessorFactory = new ExtractionProcessorFactory();
26+
private final ExtractionResultMapper extractionResultMapper = new ExtractionResultMapper();
2427

25-
public ExtractCommand(SimpleLocalizeClient client, Configuration configuration)
28+
public ExtractCommand(Configuration configuration)
2629
{
2730
this.configuration = configuration;
28-
this.client = client;
2931
}
3032

3133
public void invoke() throws IOException, InterruptedException
3234
{
33-
String searchDir = configuration.getSearchDir();
34-
String projectType = configuration.getProjectType();
35+
final String projectType = configuration.getProjectType();
36+
log.info("Project type: {}", projectType);
3537

36-
ProjectProcessorFactory processorFactory = new ProjectProcessorFactory();
37-
ExtractionProcessor extractionProcessor = processorFactory.createForType(projectType);
38-
ExtractionResult result = extractionProcessor.process(Paths.get(searchDir));
38+
final String searchDir = configuration.getSearchDir();
39+
log.info("Search directory: {}", searchDir);
3940

40-
Set<String> keys = result.getKeys();
41-
List<Path> processedFiles = result.getProcessedFiles();
42-
log.info("Found {} unique keys in {} components", keys.size(), processedFiles.size());
41+
final String outputPath = configuration.getOutputPath();
42+
log.info("Output path: {}", outputPath);
4343

44-
Set<String> ignoredKeys = new HashSet<>(configuration.getIgnoreKeys());
45-
keys.removeAll(ignoredKeys);
44+
final List<String> ignorePaths = configuration.getIgnorePaths();
45+
log.info("Ignoring paths: {}", ignorePaths);
4646

47-
Integer processedKeys = client.uploadKeys(keys);
48-
log.info("Uploaded {} keys to SimpleLocalize", processedKeys);
47+
final List<String> ignoreKeys = configuration.getIgnoreKeys();
48+
Set<String> ignoredKeys = new HashSet<>(ignoreKeys);
49+
log.info("Ignoring keys: {}", ignoredKeys);
50+
51+
ExtractionProcessor extractionProcessor = extractionProcessorFactory.createForType(projectType);
52+
Set<ExtractionResult> results = new HashSet<>(extractionProcessor.process(Paths.get(searchDir), ignorePaths));
53+
54+
Set<String> uniqueKeys = results.stream()
55+
.map(ExtractionResult::getKey)
56+
.collect(Collectors.toSet());
57+
58+
Set<Path> uniqueFiles = results.stream()
59+
.map(ExtractionResult::getFilePath)
60+
.collect(Collectors.toSet());
61+
62+
log.info("Extracted {} unique keys from {} files", uniqueKeys.size(), uniqueFiles.size());
63+
64+
Set<ExtractionResult> filteredKeys = ExtractionUtils.filterOutIgnoredKeys(results, ignoredKeys);
65+
Map<String, SimpleLocalizeJsonMetadata> keysWithMetadata = extractionResultMapper.map(filteredKeys);
66+
67+
log.info("Saving results to: {}", outputPath);
68+
saveToFile(keysWithMetadata, outputPath);
69+
log.info("Done!");
70+
}
71+
72+
73+
private void saveToFile(Map<String, SimpleLocalizeJsonMetadata> data, String outputPath) throws IOException
74+
{
75+
ObjectMapper objectMapperSingleton = ObjectMapperSingleton.getInstance();
76+
objectMapperSingleton
77+
.writerWithDefaultPrettyPrinter()
78+
.writeValue(Paths.get(outputPath).toFile(), data);
4979
}
5080
}

src/main/java/io/simplelocalize/cli/command/UploadCommand.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
public class UploadCommand implements CliCommand
2020
{
2121
private static final Logger log = LoggerFactory.getLogger(UploadCommand.class);
22-
private static final List<String> MULTI_LANGUAGE_FORMATS = List.of("multi-language-json", "excel", "csv-translations");
2322
private final FileListReader fileListReader;
2423
private final SimpleLocalizeClient client;
2524
private final Configuration configuration;
@@ -63,9 +62,26 @@ public void invoke() throws IOException, InterruptedException
6362
{
6463
log.info("Customer ID: {}", customerId);
6564
}
65+
66+
String configurationNamespace = configuration.getNamespace();
67+
if (StringUtils.isNotBlank(configurationNamespace))
68+
{
69+
log.info("Namespace: {}", configurationNamespace);
70+
}
71+
6672
log.info("Upload options: {}", uploadOptions);
6773

68-
log.info("Found {} files to upload", filesToUpload.size());
74+
log.info("Found {} files matching upload path '{}'", filesToUpload.size(), uploadPath);
75+
76+
if (isDryRun)
77+
{
78+
log.info("Dry run mode enabled, no files will be uploaded");
79+
} else
80+
{
81+
log.info("Uploading files...");
82+
}
83+
84+
6985
for (FileToUpload fileToUpload : filesToUpload)
7086
{
7187
Path path = fileToUpload.path();
@@ -96,14 +112,17 @@ public void invoke() throws IOException, InterruptedException
96112
requestLanguageKey = configurationLanguageKey;
97113
}
98114

99-
boolean isMultiLanguageFormat = isMultiLanguageFormat(configuration.getUploadFormat());
100-
if (!hasFileLanguageKey && !hasConfigurationLanguageKey && !isMultiLanguageFormat)
115+
boolean isMultiLanguage = isMultiLanguage(configuration);
116+
if (!hasFileLanguageKey && !hasConfigurationLanguageKey && !isMultiLanguage)
101117
{
102118
log.info("Language key not present in '--uploadPath' nor '--languageKey' parameter, file: {}", path);
103119
}
104120

105-
106121
String namespace = fileToUpload.namespace();
122+
if (StringUtils.isBlank(namespace))
123+
{
124+
namespace = configuration.getNamespace();
125+
}
107126
UploadRequest uploadRequest = UploadRequest.builder()
108127
.withPath(path)
109128
.withLanguageKey(requestLanguageKey)
@@ -115,7 +134,7 @@ public void invoke() throws IOException, InterruptedException
115134

116135
if (isDryRun)
117136
{
118-
log.info("[Dry run] Found file to upload, language=[{}], namespace=[{}], file: {}", language, namespace, path);
137+
log.info("[Dry run] Found file to upload, language=[{}], namespace=[{}] = {}", language, namespace, path);
119138
} else
120139
{
121140
log.info("Uploading language=[{}] namespace=[{}] = {}", language, namespace, path);
@@ -125,19 +144,31 @@ public void invoke() throws IOException, InterruptedException
125144

126145
if (!isDryRun)
127146
{
128-
log.info("Uploaded {} files to SimpleLocalize", filesToUpload.size());
147+
log.info("Uploaded {} file(s) to SimpleLocalize", filesToUpload.size());
129148
}
130149
}
131150

132-
private boolean isMultiLanguageFormat(String inputUploadFormat)
151+
private boolean isMultiLanguage(Configuration configuration)
133152
{
134-
for (String uploadFormat : MULTI_LANGUAGE_FORMATS)
153+
List<String> multiLanguageFileFormats = List.of("multi-language-json", "excel", "csv-translations");
154+
for (String uploadFormat : multiLanguageFileFormats)
135155
{
136-
if (uploadFormat.equalsIgnoreCase(inputUploadFormat))
156+
if (uploadFormat.equalsIgnoreCase(configuration.getUploadFormat()))
137157
{
138158
return true;
139159
}
140160
}
161+
162+
List<String> uploadOptions = configuration.getUploadOptions();
163+
for (String uploadOption : uploadOptions)
164+
{
165+
if (uploadOption.equalsIgnoreCase("MULTI_LANGUAGE"))
166+
{
167+
return true;
168+
}
169+
}
170+
171+
141172
return false;
142173
}
143174
}

src/main/java/io/simplelocalize/cli/configuration/ConfigurationValidator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import io.simplelocalize.cli.client.dto.proxy.Configuration;
44
import io.simplelocalize.cli.exception.ConfigurationException;
55
import io.simplelocalize.cli.util.StringUtils;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
86

97
public final class ConfigurationValidator
108
{
119
public void validateExtractConfiguration(Configuration configuration)
1210
{
13-
validateIsNotEmptyOrNull(configuration.getApiKey(), "apiKey");
1411
validateIsNotEmptyOrNull(configuration.getProjectType(), "projectType");
1512
}
1613

src/main/java/io/simplelocalize/cli/exception/NoProcessorMatchException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)