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

整改cmd

上级 ac294684
package cn.ibizlab.codegen.cmd;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class ExecuteResult {
private int exitCode;
private String executeOut;
private String errorOut;
public ExecuteResult(int exitCode, String executeOut) {
this.exitCode = exitCode;
this.executeOut = executeOut;
}
}
\ No newline at end of file
......@@ -7,23 +7,32 @@ import cn.ibizlab.codegen.GeneratorNotFoundException;
import cn.ibizlab.codegen.config.CodegenConfigurator;
import cn.ibizlab.codegen.config.CodegenConfiguratorUtils;
import cn.ibizlab.codegen.*;
import cn.ibizlab.codegen.config.EmbedTemplate;
import cn.ibizlab.codegen.config.Volume;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
@SuppressWarnings({"java:S106"})
@Command(name = "generate", description = "Generate code with the specified generator.")
public class Generate extends IbizLabGeneratorCommand {
CodegenConfigurator configurator;
Generator generator;
......@@ -46,8 +55,8 @@ public class Generate extends IbizLabGeneratorCommand {
private List<String> templateDirs;
@Option(name = {"--embed-template"}, title = "embed template name",
description = "special embed template name, e.g. r7 or r8 or ibizboot or doc, sets template name properties that can be load inner template from class-path/resources ")
private String embedTemplate;
description = "special embed template name, e.g. ibizlab-template-ibizedge:1.0-SNAPSHOT or ibizlab-template-ibizboot:1.0-SNAPSHOT , sets template name properties that can be load inner template from maven repo ")
private List<String> embedTemplates;
@Option(name = {"--template-path"}, title = "template files relative path",
description = "special template file's relative path, multiple paths are supported, the format of /folderA/README.md.hbs,/folderB/sub/file.json.hbs " +
......@@ -60,6 +69,11 @@ public class Generate extends IbizLabGeneratorCommand {
"(or multiple options, each with --template-filter /folderA --template-filter /folderB/.*/{{entities}}.java.hbs)")
private List<String> templateFilters;
@Option(name = {"-v","--volume"}, title = "template file's filter regex",
description = "volume dir's or file's , multiple regex are supported, the format of /folderA,/folderB/file1.java " +
"(or multiple options, each with -v /folderA -v /folderB/file1.java)")
private List<String> volumes;
@Option(
name = {"-a", "--auth"},
title = "authorization",
......@@ -93,6 +107,7 @@ public class Generate extends IbizLabGeneratorCommand {
@Option(
name = {"-p", "--additional-properties"},
title = "additional properties",
......@@ -103,142 +118,159 @@ public class Generate extends IbizLabGeneratorCommand {
@Option(name = {"--git-host"}, title = "git host",
description = CodegenConstants.GIT_HOST_DESC)
private String gitHost;
@Option(name = {"--git-user-id"}, title = "git user id",
description = CodegenConstants.GIT_USER_ID_DESC)
private String gitUserId;
@Override
public void execute() {
@Option(name = {"--git-repo-id"}, title = "git repo id",
description = CodegenConstants.GIT_REPO_ID_DESC)
private String gitRepoId;
List<CodegenConfigurator> configs=null;
if (configFile != null && configFile.length() > 0) {
// attempt to load from configFile
configs = CodegenConfigurator.fromFile(configFile);
}
if(configs==null)
{
configs=new ArrayList<>();
configs.add(new CodegenConfigurator());
}
@Option(name = {"--release-note"}, title = "release note",
description = CodegenConstants.RELEASE_NOTE_DESC)
private String releaseNote;
configs.forEach(configurator->{
@Option(name = {"--http-user-agent"}, title = "http user agent",
description = CodegenConstants.HTTP_USER_AGENT_DESC)
private String httpUserAgent;
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);
}
@Override
public void execute() {
// this initial check allows for field-level package private injection (for unit testing)
if (configurator == null) {
if (configFile != null && configFile.length() > 0) {
// attempt to load from configFile
configurator = CodegenConfigurator.fromFile(configFile);
if (isEmpty(configurator.getOutputDir())&&isNotEmpty(output)) {
configurator.setOutputDir(output);
}
// else if (StringUtils.isEmpty(spec)) {
// // if user doesn't pass configFile and does not pass spec, we can fail immediately because one of these two is required to run.
// System.err.println("[error] Required option '-i' is missing");
// System.exit(1);
// }
// if a config file wasn't specified, or we were unable to read it
if (configurator == null) {
// create a fresh configurator
configurator = new CodegenConfigurator();
if (isEmpty(configurator.getAuth())&&isNotEmpty(auth)) {
configurator.setAuth(auth);
}
}
if (ObjectUtils.isEmpty(configurator.getTemplateDirs())&&(!ObjectUtils.isEmpty(templateDirs))) {
configurator.setTemplateDirs(templateDirs);
}
if(ObjectUtils.isEmpty(configurator.getEmbedTemplates())&&(!ObjectUtils.isEmpty(embedTemplates))) {
List<EmbedTemplate> list=new ArrayList<>();
embedTemplates.forEach(item->{
EmbedTemplate embedTemplate=EmbedTemplate.from(item);
if(embedTemplate!=null)
list.add(embedTemplate);
});
configurator.setEmbedTemplates(list);
}
if (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);
if(ObjectUtils.isEmpty(configurator.getVolumes())&&(!ObjectUtils.isEmpty(volumes))) {
List<Volume> list=new ArrayList<>();
volumes.forEach(item->{
Volume volume=Volume.from(item);
if(volume!=null)
list.add(volume);
});
configurator.setVolumes(list);
}
configurator.setInputSpec(spec);
}
if (ObjectUtils.isEmpty(configurator.getTemplatePaths())&&(!ObjectUtils.isEmpty(templatePaths))) {
configurator.setTemplatePaths(templatePaths);
}
if (ObjectUtils.isEmpty(configurator.getTemplateFilters())&&(!ObjectUtils.isEmpty(templateFilters))) {
configurator.setTemplateFilters(templateFilters);
}
if (isNotEmpty(output)) {
configurator.setOutputDir(output);
}
if (ObjectUtils.isEmpty(configurator.getFilters())&&(!ObjectUtils.isEmpty(filters))) {
configurator.setFilters(filters);
}
if (isNotEmpty(auth)) {
configurator.setAuth(auth);
}
if (ObjectUtils.isEmpty(configurator.getAdditionalProperties().get(CodegenConstants.PACKAGE_NAME))&&isNotEmpty(packageName)) {
configurator.setPackageName(packageName);
}
if (!ObjectUtils.isEmpty(templateDirs)) {
configurator.setTemplateDirs(templateDirs);
}
if (ObjectUtils.isEmpty(configurator.getAdditionalProperties().get(CodegenConstants.PROJECT_NAME))&&isNotEmpty(name)) {
configurator.setProjectName(name);
}
if(!StringUtils.isEmpty(embedTemplate)) {
configurator.setEmbedTemplate(embedTemplate);
}
if (!ObjectUtils.isEmpty(templatePaths)) {
configurator.setTemplatePaths(templatePaths);
}
if (!ObjectUtils.isEmpty(templateFilters)) {
configurator.setTemplateFilters(templateFilters);
}
if (!ObjectUtils.isEmpty(filters)) {
configurator.setFilters(filters);
}
if (globalProperties != null && !globalProperties.isEmpty()) {
CodegenConfiguratorUtils.applyGlobalPropertiesKvpList(globalProperties, configurator);
}
if (isNotEmpty(packageName)) {
configurator.setPackageName(packageName);
}
CodegenConfiguratorUtils.applyAdditionalPropertiesKvpList(additionalProperties, configurator);
if (isNotEmpty(name)) {
configurator.setProjectName(name);
}
try {
if (isNotEmpty(gitHost)) {
configurator.setGitHost(gitHost);
}
File tempDir= Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()).toFile();
if(!ObjectUtils.isEmpty(configurator.getVolumes()))
{
if (isNotEmpty(gitUserId)) {
configurator.setGitUserId(gitUserId);
}
tempDir.mkdirs();
try{
InputStream ignoreFile = this.getClass().getResourceAsStream("/templ/.ibizlab-generator-ignore"); //权限资源
if (!ObjectUtils.isEmpty(ignoreFile)) {
Files.copy(ignoreFile,Paths.get(tempDir.getPath(),".ibizlab-generator-ignore"),StandardCopyOption.REPLACE_EXISTING);
}
}catch (Exception ex){}
if (isNotEmpty(gitRepoId)) {
configurator.setGitRepoId(gitRepoId);
}
configurator.getVolumes().forEach(volume -> {
File source=volume.getSourcePath().toFile();
if(source.exists())
{
java.nio.file.Path targetPath=Paths.get(tempDir.getPath(),volume.getTarget());
File target=targetPath.toFile();
if(source.getAbsolutePath().equalsIgnoreCase(target.getAbsolutePath()))
return;
if (isNotEmpty(releaseNote)) {
configurator.setReleaseNote(releaseNote);
}
target.getParentFile().mkdirs();
try {
Files.createSymbolicLink(targetPath,Paths.get(source.getAbsolutePath()));
System.out.println("volume ln "+source.getAbsolutePath()+" -> "+target.getAbsolutePath());
if (isNotEmpty(httpUserAgent)) {
configurator.setHttpUserAgent(httpUserAgent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
if(configurator.getTemplateDirs()==null)
configurator.setTemplateDirs(new ArrayList<>());
configurator.getTemplateDirs().add(tempDir.getAbsolutePath());
}
// this null check allows us to inject for unit testing.
Generator generator = new DefaultGenerator();
if (globalProperties != null && !globalProperties.isEmpty()) {
CodegenConfiguratorUtils.applyGlobalPropertiesKvpList(globalProperties, configurator);
}
generator.opts(configurator.toClientOptInput());
generator.generate();
CodegenConfiguratorUtils.applyAdditionalPropertiesKvpList(additionalProperties, configurator);
try {
if(!ObjectUtils.isEmpty(configurator.getEmbedTemplates()))
{
LocalCommandExecutor.getInstance().executeCommand("mvn ibizlabcli:code -f "+ Paths.get(this.output,"generator.xml").toString(),120000);
}
// this null check allows us to inject for unit testing.
if (generator == null) {
generator = new DefaultGenerator();
if(tempDir.exists())
tempDir.delete();
} catch (GeneratorNotFoundException e) {
System.err.println(e.getMessage());
System.err.println("[error] Check the spelling of the generator's name and try again.");
System.exit(1);
}
});
generator.opts(configurator.toClientOptInput());
generator.generate();
} catch (GeneratorNotFoundException e) {
System.err.println(e.getMessage());
System.err.println("[error] Check the spelling of the generator's name and try again.");
System.exit(1);
}
}
}
package cn.ibizlab.codegen.cmd;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.concurrent.*;
public class LocalCommandExecutor {
static final Logger logger = LoggerFactory.getLogger(LocalCommandExecutor.class);
static ExecutorService pool = new ThreadPoolExecutor(0, 50, 3L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
private static LocalCommandExecutor instance;
static {
instance = new LocalCommandExecutor();
}
public static LocalCommandExecutor getInstance() {
return instance;
}
private void closeQuietly(Closeable c) {
try {
if (c != null) {
c.close();
}
} catch (IOException e) {
logger.error("exception", e);
}
}
public ExecuteResult executeCommand(String command, long timeout) {
Future<ExecuteResult> executeFuture = null;
try {
logger.info(command);
// create a Callable for the command's Process which can be called by an Executor
Callable<ExecuteResult> call = new Callable<ExecuteResult>() {
public ExecuteResult call() throws Exception {
ProcessBuilder processBuilder = null;
Process process = null;
InputStream pIn = null;
StreamGobbler outputGobbler = null;
try
{
processBuilder=new ProcessBuilder(new String[]{"sh","-c",command});
processBuilder.redirectErrorStream(true);
process=processBuilder.start();
logger.debug(command+ " started()");
// close process's output stream.
process.getOutputStream().close();
pIn = process.getInputStream();
outputGobbler = new StreamGobbler(pIn, "OUTPUT");
outputGobbler.start();
System.out.println(Thread.currentThread().getName() + " is running");
process.waitFor();
return new ExecuteResult(process.exitValue(), outputGobbler.getContent());
}
catch (Exception ex)
{
String errorMessage = "The command [" + command + "] execute failed.";
logger.error(errorMessage, ex);
return new ExecuteResult(-1, "");
}
finally {
if (pIn != null) {
closeQuietly(pIn);
if (outputGobbler != null && !outputGobbler.isInterrupted()) {
outputGobbler.interrupt();
}
}
if ( process != null) {
process.destroy();
}
}
}
};
// submit the command's call and get the result from a
executeFuture = pool.submit(call);
return executeFuture.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
String errorMessage = "The command [" + command + "] timed out.";
logger.error(errorMessage, ex);
return new ExecuteResult(-1, "");
} catch (ExecutionException ex) {
String errorMessage = "The command [" + command + "] did not complete due to an execution error.";
logger.error(errorMessage, ex);
return new ExecuteResult(-1, "");
} catch (InterruptedException ex) {
String errorMessage = "The command [" + command + "] did not complete due to an interrupted error.";
logger.error(errorMessage, ex);
return new ExecuteResult(-1, "");
} finally {
if (executeFuture != null) {
try {
executeFuture.cancel(true);
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
}
}
\ No newline at end of file
package cn.ibizlab.codegen.cmd;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StreamGobbler extends Thread {
private static Logger logger = LoggerFactory.getLogger(StreamGobbler.class);
private InputStream inputStream;
private String streamType;
private StringBuilder buf;
private volatile boolean isStopped = false;
/**
* @param inputStream the InputStream to be consumed
* @param streamType the stream type (should be OUTPUT or ERROR)
*/
public StreamGobbler(final InputStream inputStream, final String streamType) {
this.inputStream = inputStream;
this.streamType = streamType;
this.buf = new StringBuilder();
this.isStopped = false;
}
/**
* Consumes the output from the input stream and displays the lines consumed
* if configured to do so.
*/
@Override
public void run() {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
this.buf.append(line + "\n");
System.out.println(line);
}
} catch (IOException ex) {
logger.trace("Failed to successfully consume and display the input stream of type " + streamType + ".", ex);
} finally {
this.isStopped = true;
synchronized (this) {
notify();
}
}
}
public String getContent() {
if (!this.isStopped) {
synchronized (this) {
try {
wait();
} catch (InterruptedException ignore) {
ignore.printStackTrace();
}
}
}
return this.buf.toString();
}
public String getText() {
return this.buf.toString();
}
}
\ No newline at end of file
package cn.ibizlab.codegen;
import cn.ibizlab.codegen.config.EmbedTemplate;
import cn.ibizlab.codegen.config.GlobalSettings;
import cn.ibizlab.codegen.model.CliFilter;
import cn.ibizlab.codegen.templating.*;
......@@ -42,7 +43,7 @@ public class CodegenConfig {
private List<String> templateDirs;
private String embedTemplate;
private List<EmbedTemplate> embedTemplates;
private String auth;
......@@ -172,8 +173,7 @@ public class CodegenConfig {
public CommonTemplateContentLocator getCommonTemplateContentLocator() {
if(commonTemplateContentLocator==null) {
String path="templ";
if(!StringUtils.isEmpty(embedTemplate))
path=Paths.get("templ" , embedTemplate).toString();
String loc = TemplateManager.getCPResourcePath(path);
URL url = this.getClass().getClassLoader().getResource(loc);
if (url != null) {
......@@ -197,15 +197,16 @@ public class CodegenConfig {
this.getTemplateDirs().forEach(templateDir->{
list.add(new GeneratorTemplateContentLocator(templateDir));
});
if(getCommonTemplateContentLocator()!=null) {
list.add(commonTemplateContentLocator);
}
this.templateProcessor = new TemplateManager(
new TemplateManagerOptions(this.isEnableMinimalUpdate(),this.isSkipOverwrite()),
templatingEngine,
list
);
}
if(getCommonTemplateContentLocator()!=null) {
list.add(commonTemplateContentLocator);
}
this.templateProcessor = new TemplateManager(
new TemplateManagerOptions(this.isEnableMinimalUpdate(),this.isSkipOverwrite()),
templatingEngine,
list
);
}
return templateProcessor;
......
......@@ -41,7 +41,8 @@ public final class CodegenConfigurator {
private String outputDir;
private List<String> filters;
private List<String> templateDirs;
private String embedTemplate;
private List<EmbedTemplate> embedTemplates;
private List<Volume> volumes;
private List<String> templatePaths;
private List<String> templateFilters;
private String auth;
......@@ -52,7 +53,6 @@ public final class CodegenConfigurator {
private Map<String, Object> additionalProperties = new HashMap<>();
private GeneratorSettings generatorSettings=new GeneratorSettings();
public CodegenConfigurator() {
......@@ -60,24 +60,36 @@ public final class CodegenConfigurator {
}
@SuppressWarnings("DuplicatedCode")
public static CodegenConfigurator fromFile(String configFile, Module... modules) {
public static List<CodegenConfigurator> fromFile(String configFile, Module... modules) {
if (isNotEmpty(configFile)) {
DynamicSettings settings = readDynamicSettings(configFile, modules);
CodegenConfigurator configurator = new CodegenConfigurator();
GeneratorSettings generatorSettings = settings.getGeneratorSettings();
if(generatorSettings.getAdditionalProperties() != null) {
configurator.additionalProperties.putAll(generatorSettings.getAdditionalProperties());
DynamicSettings dynamicSettings = readDynamicSettings(configFile, modules);
if(!ObjectUtils.isEmpty(dynamicSettings))
{
List<CodegenConfigurator> list = new ArrayList<>();
dynamicSettings.values().forEach(settings->{
CodegenConfigurator configurator = new CodegenConfigurator();
configurator.setAuth(settings.getAuth());
configurator.setOutputDir(settings.getOutput());
configurator.setInputSpec(settings.getInputSpec());
configurator.setFilters(settings.getInputSpecFilters());
configurator.setTemplateDirs(settings.getTemplateDirs());
configurator.setTemplatePaths(settings.getTemplatePaths());
configurator.setTemplateFilters(settings.getInputSpecFilters());
configurator.setEmbedTemplates(settings.getEmbedTemplates());
configurator.setPackageName(settings.getPackageName());
configurator.setVolumes(settings.getVolumes());
configurator.setProjectName(settings.getProjectName());
if(settings.getAdditionalProperties() != null) {
configurator.additionalProperties.putAll(settings.getAdditionalProperties());
}
list.add(configurator);
});
return list;
}
return configurator;
}
return null;
}
......@@ -108,7 +120,6 @@ public final class CodegenConfigurator {
public CodegenConfigurator addAdditionalProperty(String key, Object value) {
this.additionalProperties.put(key, value);
generatorSettings.getAdditionalProperties().put(key, value);
return this;
}
......@@ -121,7 +132,6 @@ public final class CodegenConfigurator {
public CodegenConfigurator setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
generatorSettings.setAdditionalProperties(additionalProperties);
return this;
}
......@@ -137,7 +147,6 @@ public final class CodegenConfigurator {
if (StringUtils.isNotEmpty(projectName)) {
addAdditionalProperty(CodegenConstants.PROJECT_NAME, projectName);
}
generatorSettings.setProjectName(projectName);
return this;
}
......@@ -146,62 +155,55 @@ public final class CodegenConfigurator {
if (StringUtils.isNotEmpty(packageName)) {
addAdditionalProperty(CodegenConstants.PACKAGE_NAME, packageName);
}
generatorSettings.setPackageName(packageName);
return this;
}
public CodegenConfigurator setGitRepoId(String gitRepoId) {
if (StringUtils.isNotEmpty(gitRepoId)) {
addAdditionalProperty(CodegenConstants.GIT_REPO_ID, gitRepoId);
public CodegenConfigurator setAuth(String auth) {
// do not cache this in additional properties.
this.auth = auth;
if (StringUtils.isNotEmpty(auth)) {
addAdditionalProperty("auth", auth);
}
generatorSettings.setGitRepoId(gitRepoId);
return this;
}
public CodegenConfigurator setGitHost(String gitHost) {
if (StringUtils.isNotEmpty(gitHost)) {
addAdditionalProperty(CodegenConstants.GIT_HOST, gitHost);
}
generatorSettings.setGitHost(gitHost);
public CodegenConfigurator setInputSpec(String inputSpec) {
this.inputSpec = inputSpec;
return this;
}
public CodegenConfigurator setGitUserId(String gitUserId) {
if (StringUtils.isNotEmpty(gitUserId)) {
addAdditionalProperty(CodegenConstants.GIT_USER_ID, gitUserId);
}
generatorSettings.setGitUserId(gitUserId);
return this;
public String getInputSpec() {
return this.inputSpec;
}
public String getOutputDir() {
return outputDir;
}
public CodegenConfigurator setReleaseNote(String releaseNote) {
if (StringUtils.isNotEmpty(releaseNote)) {
addAdditionalProperty(CodegenConstants.RELEASE_NOTE, releaseNote);
}
generatorSettings.setReleaseNote(releaseNote);
return this;
public List<String> getFilters() {
return filters;
}
public CodegenConfigurator setHttpUserAgent(String httpUserAgent) {
if (StringUtils.isNotEmpty(httpUserAgent)) {
addAdditionalProperty(CodegenConstants.HTTP_USER_AGENT, httpUserAgent);
}
generatorSettings.setHttpUserAgent(httpUserAgent);
return this;
public List<String> getTemplateDirs() {
return templateDirs;
}
public List<String> getTemplatePaths() {
return templatePaths;
}
public List<String> getTemplateFilters() {
return templateFilters;
}
public CodegenConfigurator setAuth(String auth) {
// do not cache this in additional properties.
this.auth = auth;
return this;
public String getAuth() {
return auth;
}
public CodegenConfigurator setInputSpec(String inputSpec) {
this.inputSpec = inputSpec;
return this;
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}
public CodegenConfigurator setOutputDir(String outputDir) {
......@@ -214,11 +216,26 @@ public final class CodegenConfigurator {
return this;
}
public CodegenConfigurator setEmbedTemplate(String embedTemplate) {
this.embedTemplate = embedTemplate;
public CodegenConfigurator setEmbedTemplates(List<EmbedTemplate> embedTemplates) {
this.embedTemplates = embedTemplates;
return this;
}
public List<EmbedTemplate> getEmbedTemplates() {
return embedTemplates;
}
public CodegenConfigurator setVolumes(List<Volume> volumes) {
this.volumes = volumes;
return this;
}
public List<Volume> getVolumes() {
return volumes;
}
public CodegenConfigurator setTemplatePaths(List<String> templatePaths) {
this.templatePaths = templatePaths;
return this;
......@@ -264,8 +281,8 @@ public final class CodegenConfigurator {
config.setTemplateDirs(this.templateDirs);
}
if(!StringUtils.isEmpty(embedTemplate)) {
config.setEmbedTemplate(this.embedTemplate);
if(!ObjectUtils.isEmpty(embedTemplates)) {
config.setEmbedTemplates(this.embedTemplates);
}
if(!ObjectUtils.isEmpty(templatePaths)) {
......
......@@ -35,35 +35,4 @@ public class Context<TSpecDocument> {
}
}
public static void main(String[] args) throws IOException {
Path path= Paths.get("/122/33/abc.java");
System.out.println(path.getParent().getFileName());
System.out.println(TemplateFileType.entity.toString());
System.out.println(TemplateFileType.entity.value());
HashMap<String, Object> scopes = new HashMap<String, Object>();
scopes.put("name", "Mustache");
GeneratorSettings generatorSettings=new GeneratorSettings().setGitHost("host");
generatorSettings.getAdditionalProperties().put("sub",new GeneratorSettings().setGitHost("son"));
scopes.put("feature",generatorSettings );
List<String> list=new ArrayList<>();
list.add("a");
list.add("b");
scopes.put("list",list);
Map map=new HashMap();
map.put("01","a01");
map.put("02","a02");
scopes.put("map1",map);
scopes.put("opt",new PojoOption().set("system_name","ab").set("persistent",false));
scopes.put("type",TemplateFileType.entity);
Writer writer = new OutputStreamWriter(System.out);
DefaultMustacheFactory mf = new DefaultMustacheFactory();
mf.setObjectHandler(new MapMethodReflectionHandler());
Mustache mustache = mf.compile(new StringReader("{{type}},{{type.value}},{{name}},{{opt.system}},{{opt.physicalField}}, {{map1.entrySet}}{{#map1.entrySet}}{{key}}{{/map1.entrySet}}!"), "example");
mustache.execute(writer, scopes);
writer.flush();
}
}
package cn.ibizlab.codegen.config;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.lang.reflect.Field;
import java.util.*;
public class DynamicSettings {
@JsonAnySetter
private Map<String, Object> dynamicProperties = new HashMap<>();
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DynamicSettings extends LinkedHashMap<String,GeneratorSettings>{
@JsonUnwrapped
@JsonDeserialize(builder = GeneratorSettings.Builder.class)
private GeneratorSettings generatorSettings;
public GeneratorSettings getGeneratorSettings() {
excludeSettingsFromDynamicProperties();
GeneratorSettings.Builder builder = GeneratorSettings.newBuilder(generatorSettings);
// This allows us to put any unknown top-level properties into additionalProperties of the generator object.
for (Map.Entry<String, Object> entry : dynamicProperties.entrySet()) {
builder.withAdditionalProperty(entry.getKey(), entry.getValue());
}
return builder.build();
}
/**
* <p>Constructor for DynamicSettings.</p>
*/
@JsonCreator
public DynamicSettings() { }
/**
* Gets all "custom" properties included in the config object.
*
* @return All user-specified custom properties.
*/
public Map<String, Object> getDynamicProperties() {
return dynamicProperties;
}
private void excludeSettingsFromDynamicProperties(){
Set<String> fieldNames = new HashSet<>();
for (Field field : GeneratorSettings.class.getDeclaredFields()) {
fieldNames.add(field.getName());
}
dynamicProperties.keySet().removeAll(fieldNames);
}
}
package cn.ibizlab.codegen.config;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class EmbedTemplate {
private String name;
private String version;
@JsonCreator
public static EmbedTemplate from(String tag) {
if(ObjectUtils.isEmpty(tag))
return null;
EmbedTemplate template=new EmbedTemplate();
String tags[]=tag.split(":");
template.setName(tags[0]);
if(tags.length>1)
template.setVersion(tags[1]);
else
template.setVersion("1.0-SNAPSHOT");
return template;
}
}
package cn.ibizlab.codegen.config;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
......@@ -14,221 +16,48 @@ import java.util.*;
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeneratorSettings implements Serializable {
private static final Logger LOGGER = LoggerFactory.getLogger(GeneratorSettings.class);
private static final String DEFAULT_GIT_HOST = "github.com";
private static final String DEFAULT_GIT_USER_ID = "GIT_USER_ID";
private static final String DEFAULT_GIT_REPO_ID = "GIT_REPO_ID";
private static final String DEFAULT_RELEASE_NOTE = "Minor update";
private String projectName;
private String packageName;
@JsonIgnore
private Map<String, Object> additionalProperties = new LinkedHashMap<>();
private String gitHost;
private String gitUserId;
private String gitRepoId;
private String releaseNote;
private String httpUserAgent;
private String output ;
private Map<String, String> typeMappings;
private String inputSpec;
public String getHost()
{
return gitHost+"abc";
}
private List<String> inputSpecFilters;
private List<String> templateDirs;
private GeneratorSettings(Builder builder) {
projectName = builder.projectName;
packageName = builder.packageName;
typeMappings = Collections.unmodifiableMap(builder.typeMappings);
gitHost = builder.gitHost;
gitUserId = builder.gitUserId;
gitRepoId = builder.gitRepoId;
releaseNote = builder.releaseNote;
httpUserAgent = builder.httpUserAgent;
Map<String, Object> additional = new HashMap<>(builder.additionalProperties);
additionalProperties = Collections.unmodifiableMap(additional);
}
private List<String> templatePaths;
private List<String> templateFilters;
private List<Volume> volumes;
private String auth;
private List<EmbedTemplate> embedTemplates;
public static Builder newBuilder(GeneratorSettings copy) {
Builder builder = new Builder();
builder.projectName = copy.projectName;
builder.packageName = copy.packageName;
builder.typeMappings = Collections.unmodifiableMap(copy.typeMappings);
builder.gitHost = copy.gitHost;
builder.gitUserId = copy.gitUserId;
builder.gitRepoId = copy.gitRepoId;
builder.releaseNote = copy.releaseNote;
builder.httpUserAgent = copy.httpUserAgent;
Map<String, Object> additional = new HashMap<>(copy.additionalProperties);
builder.additionalProperties = Collections.unmodifiableMap(additional);
return builder;
@JsonAnyGetter
public Map<String , Object> any() {
return additionalProperties;
}
public static final class Builder {
private String projectName;
private String packageName;
private String gitHost;
private String gitUserId;
private String gitRepoId;
private String releaseNote;
private String httpUserAgent;
private Map<String, String> typeMappings;
private Map<String, Object> additionalProperties;
/**
* Instantiates a new Builder.
*/
public Builder() {
typeMappings = new HashMap<>();
additionalProperties = new HashMap<>();
gitHost = DEFAULT_GIT_HOST;
gitUserId = DEFAULT_GIT_USER_ID;
gitRepoId = DEFAULT_GIT_REPO_ID;
releaseNote = DEFAULT_RELEASE_NOTE;
}
public Builder withProjectName(String projectName) {
this.projectName = projectName;
return this;
}
/**
* Sets the {@code packageName} and returns a reference to this Builder so that the methods can be chained together.
*
* @param packageName the {@code packageName} to set
* @return a reference to this Builder
*/
public Builder withPackageName(String packageName) {
this.packageName = packageName;
return this;
}
/**
* Sets the {@code typeMappings} and returns a reference to this Builder so that the methods can be chained together.
*
* @param typeMappings the {@code typeMappings} to set
* @return a reference to this Builder
*/
public Builder withTypeMappings(Map<String, String> typeMappings) {
this.typeMappings = typeMappings;
return this;
}
public Builder withTypeMapping(String key, String value) {
if (this.typeMappings == null) {
this.typeMappings = new HashMap<>();
}
this.typeMappings.put(key, value);
return this;
}
/**
* Sets the {@code additionalProperties} and returns a reference to this Builder so that the methods can be chained together.
*
* @param additionalProperties the {@code additionalProperties} to set
* @return a reference to this Builder
*/
public Builder withAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
return this;
}
/**
* Sets the {@code additionalProperties} and returns a reference to this Builder so that the methods can be chained together.
*
* @param key A key for some additional property
* @param value The value of some additional property
* @return a reference to this Builder
*/
public Builder withAdditionalProperty(String key, Object value) {
if (this.additionalProperties == null) {
this.additionalProperties = new HashMap<>();
}
this.additionalProperties.put(key, value);
return this;
}
/**
* Sets the {@code gitHost} and returns a reference to this Builder so that the methods can be chained together.
*
* @param gitHost the {@code gitHost} to set
* @return a reference to this Builder
*/
public Builder withGitHost(String gitHost) {
this.gitHost = gitHost;
return this;
}
/**
* Sets the {@code gitUserId} and returns a reference to this Builder so that the methods can be chained together.
*
* @param gitUserId the {@code gitUserId} to set
* @return a reference to this Builder
*/
public Builder withGitUserId(String gitUserId) {
this.gitUserId = gitUserId;
return this;
}
/**
* Sets the {@code gitRepoId} and returns a reference to this Builder so that the methods can be chained together.
*
* @param gitRepoId the {@code gitRepoId} to set
* @return a reference to this Builder
*/
public Builder withGitRepoId(String gitRepoId) {
this.gitRepoId = gitRepoId;
return this;
}
/**
* Sets the {@code releaseNote} and returns a reference to this Builder so that the methods can be chained together.
*
* @param releaseNote the {@code releaseNote} to set
* @return a reference to this Builder
*/
public Builder withReleaseNote(String releaseNote) {
this.releaseNote = releaseNote;
return this;
}
/**
* Sets the {@code httpUserAgent} and returns a reference to this Builder so that the methods can be chained together.
*
* @param httpUserAgent the {@code httpUserAgent} to set
* @return a reference to this Builder
*/
public Builder withHttpUserAgent(String httpUserAgent) {
this.httpUserAgent = httpUserAgent;
return this;
}
/**
* Returns a {@code GeneratorSettings} built from the parameters previously set.
*
* @return a {@code GeneratorSettings} built with parameters of this {@code GeneratorSettings.Builder}
*/
public GeneratorSettings build() {
GeneratorSettings instance = new GeneratorSettings(this);
//noinspection PlaceholderCountMatchesArgumentCount
LOGGER.debug("GeneratorSettings#build: %s", instance.toString());
return instance;
}
@JsonAnySetter
public void set(String field, Object value) {
this.additionalProperties.put(field ,value);
}
}
package cn.ibizlab.codegen.config;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Volume {
private String source;
private String target;
public void setSource(String source) {
this.source = source.replace("\\", "/");
}
public void setTarget(String target) {
this.target = target.replace("\\", "/");
}
public Path getSourcePath() {
return Paths.get(source);
}
@JsonCreator
public static Volume from(String tag) {
if(ObjectUtils.isEmpty(tag))
return null;
Volume volume=new Volume();
String tags[]=tag.split(":");
volume.setSource(tags[0]);
if(tags.length>1)
volume.setTarget(tags[1]);
else
volume.setTarget(tags[0]);
return volume;
}
}
......@@ -9,6 +9,7 @@ import cn.ibizlab.codegen.utils.StringAdvUtils;
import net.ibizsys.model.IPSSystem;
import net.ibizsys.model.PSModelServiceImpl;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.nio.file.Files;
......@@ -86,6 +87,8 @@ public class ModelStorage {
opt.setProjectDesc(systemModel.getProjectDesc());
opt.setPackageName(systemModel.getPackageName());
opt.set("system", systemModel);
if(!ObjectUtils.isEmpty(config.getEmbedTemplates()))
opt.set("embedTemplates",config.getEmbedTemplates());
return opt;
}
......
*volumes
*target
.settings
*node_modules
*bin
*.project
*.classpath
*.factorypath
.history
.vscode
.idea
**.iml
*.jar
*.log
.DS_Store
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
......@@ -103,6 +103,17 @@
${project.artifactId}
</name>
</configuration>
{{#if embedTemplates}}
<dependencies>
{{#each embedTemplates}}
<dependency>
<groupId>cn.ibizlab</groupId>
<artifactId>{{name}}</artifactId>
<version>{{version}}</version>
</dependency>
{{/each}}
</dependencies>
{{/if}}
</plugin>
</plugins>
</build>
......
......@@ -2,6 +2,7 @@ package cn.ibizlab.codegen;
import cn.ibizlab.codegen.config.CodegenConfigurator;
import cn.ibizlab.codegen.config.CodegenConfiguratorUtils;
import cn.ibizlab.codegen.config.EmbedTemplate;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
......@@ -41,8 +42,6 @@ public class GenerateMojo extends AbstractMojo {
@Parameter
private String[] templateDirs;
@Parameter
private String embedTemplate;
@Parameter
private String[] templatePaths;
......@@ -57,8 +56,6 @@ public class GenerateMojo extends AbstractMojo {
@Parameter
private String[] globalProperties ;
@Parameter
private String configFile;
@Parameter
private String name;
......@@ -74,20 +71,6 @@ public class GenerateMojo extends AbstractMojo {
@Parameter
private String gitHost;
@Parameter
private String gitUserId;
@Parameter
private String gitRepoId;
@Parameter
private String releaseNote;
@Parameter
private String httpUserAgent;
/**
......@@ -136,10 +119,6 @@ public class GenerateMojo extends AbstractMojo {
configurator.setTemplateDirs(Arrays.asList(templateDirs));
}
if(!StringUtils.isEmpty(embedTemplate)) {
configurator.setEmbedTemplate(this.embedTemplate);
}
if (!ObjectUtils.isEmpty(templatePaths)) {
configurator.setTemplatePaths(Arrays.asList(templatePaths));
}
......@@ -161,26 +140,6 @@ public class GenerateMojo extends AbstractMojo {
}
if (isNotEmpty(gitHost)) {
configurator.setGitHost(gitHost);
}
if (isNotEmpty(gitUserId)) {
configurator.setGitUserId(gitUserId);
}
if (isNotEmpty(gitRepoId)) {
configurator.setGitRepoId(gitRepoId);
}
if (isNotEmpty(releaseNote)) {
configurator.setReleaseNote(releaseNote);
}
if (isNotEmpty(httpUserAgent)) {
configurator.setHttpUserAgent(httpUserAgent);
}
if (!ObjectUtils.isEmpty(globalProperties)) {
......
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
**.ibizlab-generator-ignore
**.DS_Store
**/node_modules/**
**/target/**
**@macro/**
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册