提交 ce7f195c 编写于 作者: sq3536's avatar sq3536

支持环境变量

上级 8afb82ad
......@@ -135,15 +135,22 @@ public class Generate extends IbizLabGeneratorCommand {
configs.forEach(configurator->{
if (isEmpty(configurator.getInputSpec())&&isNotEmpty(spec)) {
if (!spec.matches("^http(s)?://.*") && !new File(spec).exists()) {
System.err.println("[error] The spec file is not found: " + spec);
System.err.println("[error] Check the path of the ibizlab-Model spec and try again.");
System.exit(1);
}
configurator.setInputSpec(spec);
}
if (isEmpty(configurator.getInputSpec())) {
System.err.println("[error] The spec file is not found: " + spec);
System.err.println("[error] Check the path of the ibizlab-Model spec and try again.");
System.exit(1);
}
File specFile=new File(configurator.getInputSpec());
if (!configurator.getInputSpec().matches("^http(s)?://.*") && !specFile.exists()) {
System.err.println("[error] The spec file is not found: " + spec);
System.err.println("[error] Check the path of the ibizlab-Model spec and try again.");
System.exit(1);
}
configurator.addAdditionalProperty("inputSpecPath",specFile.getAbsolutePath());
......
......@@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
......@@ -62,7 +63,7 @@ public final class CodegenConfigurator {
@SuppressWarnings("DuplicatedCode")
public static List<CodegenConfigurator> fromFile(String configFile, Module... modules) {
if (isNotEmpty(configFile)) {
if (isNotEmpty(configFile)&& Files.exists(Paths.get(configFile))) {
DynamicSettings dynamicSettings = readDynamicSettings(configFile, modules);
if(!ObjectUtils.isEmpty(dynamicSettings))
{
......
package cn.ibizlab.codegen.config;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DynamicParamUtils {
private static final String DYNAMIC_PARAM_PREFIX = "${";
private static final String DYNAMIC_PARAM_SUFFIX = "}";
public static Object processObject(Object content) {
if(ObjectUtils.isEmpty(content))
return content;
else if(content instanceof String)
return process(content.toString());
else if(content instanceof List && ((List) content).get(0) instanceof String) {
return process((List)content);
}
return content;
}
public static String process(String content) {
return processDynamicParams(content,System.getenv());
}
public static List<String> process(List<String> content) {
if(content==null)
return null;
List<String> rt=new ArrayList<>();
content.forEach(item->{
rt.add(process(item));
});
return rt;
}
// 多变量,data 为 key - value 形式
public static String processDynamicParams(String content, Map data) {
if(content==null)
return null;
int begin = content.indexOf(DYNAMIC_PARAM_PREFIX);
int end = content.indexOf(DYNAMIC_PARAM_SUFFIX);
if (begin == -1 || end == -1 || begin > end) {
return content;
}
String startContent = content.substring(0, begin);
String substring = content.substring(begin, end + 1);
if (StringUtils.isNotEmpty(substring)) {
String key = substring.replace("${", "").replace("}", "");
String[] pairs=key.split(":");
String defaultValue=null;
if(pairs.length>1)
{
key=pairs[0];
defaultValue=pairs[1];
}
Object value = data.get(key);
if(ObjectUtils.isEmpty(value))
value=data.getOrDefault(key.toUpperCase(),defaultValue);
if (value != null) {
startContent = startContent + value.toString();
} else {
startContent += substring;
}
}
int length = content.length();
if ( length == end + 1) {
return startContent;
} else {
return startContent + processDynamicParams(content.substring(end + 1, length), data);
}
}
}
......@@ -23,6 +23,7 @@ public class EmbedTemplate {
public static EmbedTemplate from(String tag) {
if(ObjectUtils.isEmpty(tag))
return null;
tag=DynamicParamUtils.process(tag);
EmbedTemplate template=new EmbedTemplate();
String tags[]=tag.split(":");
template.setName(tags[0]);
......
......@@ -57,7 +57,46 @@ public class GeneratorSettings implements Serializable {
@JsonAnySetter
public void set(String field, Object value) {
this.additionalProperties.put(field ,value);
this.additionalProperties.put(field ,DynamicParamUtils.processObject(value));
}
public void setProjectName(String projectName) {
this.projectName = DynamicParamUtils.process(projectName);
}
public void setPackageName(String packageName) {
this.packageName = DynamicParamUtils.process(packageName);
}
public void setOutput(String output) {
this.output = DynamicParamUtils.process(output);
}
public void setInputSpec(String inputSpec) {
this.inputSpec = DynamicParamUtils.process(inputSpec);
}
public void setInputSpecFilters(List<String> inputSpecFilters) {
this.inputSpecFilters = DynamicParamUtils.process(inputSpecFilters);
}
public void setTemplateDirs(List<String> templateDirs) {
this.templateDirs = DynamicParamUtils.process(templateDirs);
}
public void setTemplatePaths(List<String> templatePaths) {
this.templatePaths = DynamicParamUtils.process(templatePaths);
}
public void setTemplateFilters(List<String> templateFilters) {
this.templateFilters = DynamicParamUtils.process(templateFilters);
}
public void setAuth(String auth) {
this.auth = DynamicParamUtils.process(auth);
}
}
......@@ -38,6 +38,7 @@ public class Volume {
public static Volume from(String tag) {
if(ObjectUtils.isEmpty(tag))
return null;
tag=DynamicParamUtils.process(tag);
Volume volume=new Volume();
String tags[]=tag.split(":");
volume.setSource(tags[0]);
......@@ -48,4 +49,5 @@ public class Volume {
return volume;
}
}
......@@ -91,7 +91,15 @@
${basedir}/
</output>
<inputSpec>
{{#if embedTemplates}}
{{#if inputSpecPath}}
{{inputSpecPath}}
{{else}}
${basedir}/{{system.modelFolder}}
{{/if}}
{{else}}
${basedir}/{{system.modelFolder}}
{{/if}}
</inputSpec>
<templateDirs>
${basedir}/src/main/resources/templ
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册