Skip to content

Commit ab80acc

Browse files
committed
テンプレートでユーザー定義の変数を使用できるように修正。
--configオプションで指定するJSONファイルで以下のように定義する。 { ..., "additionalProperties": { "name1": "value1", "name2": "value2", ... } } additionalProperties オブジェクトの下に定義する。 上記の例ではstring型だけだが、それ以外にも integer, number, boolean, object, array型の値を指定可能。
1 parent f93d490 commit ab80acc

8 files changed

Lines changed: 193 additions & 2 deletions

File tree

.idea/markdown-exported-files.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown-navigator.xml

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown-navigator/profiles_settings.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/deviceconnect-codegen/src/main/java/org/deviceconnect/codegen/DConnectCodegen.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99

1010
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.core.type.TypeReference;
1112
import com.fasterxml.jackson.databind.JsonNode;
1213
import com.fasterxml.jackson.databind.ObjectMapper;
1314
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1415
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
15-
import config.Config;
16-
import config.ConfigParser;
16+
import com.google.common.collect.ImmutableMap;
1717
import io.swagger.codegen.*;
1818
import io.swagger.models.*;
1919
import io.swagger.parser.SwaggerParser;
@@ -154,6 +154,11 @@ public static void main(String[] args) {
154154
config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt()));
155155
}
156156
}
157+
config.additionalProperties().putAll(genConfig.getAdditionalProperties());
158+
159+
for (Map.Entry<String, Object> prop : config.additionalProperties().entrySet()) {
160+
LOGGER.info("Parsed additionProperties: " + prop.getKey() + "=" + prop.getValue());
161+
}
157162
}
158163
}
159164
if (cmd.hasOption("t")) {
@@ -693,4 +698,66 @@ static void usage(Options options) {
693698
formatter.printHelp("DConnectCodegen", options);
694699
}
695700

701+
private static class Config {
702+
703+
private final Map<String, String> options = new HashMap<>();
704+
private final Map<String, Object> additionalProperties = new HashMap<>();
705+
706+
Map<String, String> getOptions() {
707+
return ImmutableMap.copyOf(options);
708+
}
709+
710+
boolean hasOption(String opt) {
711+
return options.containsKey(opt);
712+
}
713+
714+
String getOption(String opt) {
715+
return options.get(opt);
716+
}
717+
718+
void setOption(String opt, String value) {
719+
options.put(opt, value);
720+
}
721+
722+
Map<String, Object> getAdditionalProperties() {
723+
return ImmutableMap.copyOf(additionalProperties);
724+
}
725+
726+
void addAdditionalProperties(final Map<String, Object> properties) {
727+
additionalProperties.putAll(properties);
728+
}
729+
}
730+
731+
private static class ConfigParser {
732+
733+
private static final Logger LOGGER = LoggerFactory.getLogger(config.ConfigParser.class);
734+
735+
public static Config read(String location) {
736+
ObjectMapper mapper = new ObjectMapper();
737+
738+
Config config = new Config();
739+
740+
try {
741+
JsonNode rootNode = mapper.readTree(new File(location));
742+
Iterator<Map.Entry<String, JsonNode>> optionNodes = rootNode.fields();
743+
744+
while (optionNodes.hasNext()) {
745+
Map.Entry<String, JsonNode> optionNode = optionNodes.next();
746+
if (optionNode.getValue().isValueNode()) {
747+
config.setOption(optionNode.getKey(), optionNode.getValue().asText());
748+
} else if (optionNode.getValue().isObject() && "additionalProperties".equals(optionNode.getKey())) {
749+
Map<String, Object> map = mapper.convertValue(optionNode.getValue(), new TypeReference<Map<String, Object>>() {});
750+
config.addAdditionalProperties(map);
751+
} else {
752+
LOGGER.warn("omitting non-value node " + optionNode.getKey());
753+
}
754+
}
755+
} catch (Exception e) {
756+
LOGGER.error(e.getMessage());
757+
return null;
758+
}
759+
760+
return config;
761+
}
762+
}
696763
}

samples/templates/deviceConnectAndroidPlugin/README.md.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ Android Studio 2.2.1以上
1919

2020
## 関連ページ
2121
- [Device Connect プラグイン開発マニュアル](https://github.com/DeviceConnect/DeviceConnect-Android/wiki/DevicePlugin-Manual-for-Android-Studio-110)
22+
23+
## テスト
24+
以下は config オプションのテストです。
25+
26+
testString: {{testString}}
27+
testInt: {{testInt}}
28+
testNumber: {{testNumber}}
29+
testBoolean: {{testBoolean}}
30+
testObject: {{#testObject}}{{objKey}}{{/testObject}}
31+
testArray: {{#testArray}}{{.}} {{/testArray}}

tests/good/all.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ rm -rf ./output/
99
./good-android-plugin-005-option-names.sh
1010
./good-android-plugin-006-option-connection-type.sh
1111
./good-android-plugin-007-option-templates.sh
12+
./good-android-plugin-008-event-without-interval.sh
13+
./good-android-plugin-009-same-name-object-props.sh
14+
./good-android-plugin-010-config-additional-properties.sh
1215
./good-html-app-001-input-spec.sh
1316
./good-html-app-002-option.sh
1417
./good-html-docs-001-input-spec.sh
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compileSdkVersion": "28",
3+
"minSdkVersion": "19",
4+
"targetSdkVersion": "28",
5+
"deviceConnectPluginSdkVersion": "2.7.2",
6+
7+
"additionalProperties": {
8+
"testString": "testString",
9+
"testInt": 1234567890,
10+
"testNumber": 1234567890.1234567890,
11+
"testBoolean": true,
12+
"testObject": {
13+
"objKey": "testObjectChild"
14+
},
15+
"testArray": ["e1", "e2", "e3"]
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh -x
2+
3+
java -jar ../../bin/deviceconnect-codegen.jar \
4+
--lang deviceConnectAndroidPlugin \
5+
--template-dir templates/deviceConnectAndroidPlugin \
6+
--package-name com.mydomain.testplugin010 \
7+
--display-name Test009 \
8+
--input-spec profile-specs/swagger-files-android-plugin-009/ \
9+
--output output/android-plugin-010 \
10+
--gradle-plugin-version 3.3.2 \
11+
--config configs/android-plugin.json

0 commit comments

Comments
 (0)