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

129

上级 a881b53d
......@@ -26,6 +26,7 @@ public class IbizLabGenerator {
Generate.class,
HelpCommand.class,
Version.class,
ListCommand.class,
CompletionCommand.class
);
......
......@@ -45,6 +45,9 @@ public class Generate extends IbizLabGeneratorCommand {
"(or multiple options, each with -t /templateA -t /tmp/templateB)")
private List<String> templateDirs;
@Option(name = {"--template-name"}, title = "template name",
description = "special template name, e.g. r7 or r8 or ibiz-boot or doc, sets template name properties that can be load inner template from class-path/resources ")
private String templateName;
@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 " +
......@@ -169,6 +172,10 @@ public class Generate extends IbizLabGeneratorCommand {
configurator.setTemplateDirs(templateDirs);
}
if(!StringUtils.isEmpty(templateName)) {
configurator.setTemplateName(templateName);
}
if (!ObjectUtils.isEmpty(templatePaths)) {
configurator.setTemplatePaths(templatePaths);
}
......
package cn.ibizlab.codegen.cmd;
import cn.ibizlab.codegen.templating.TemplateManager;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@SuppressWarnings({"unused", "java:S106"})
@Command(name = "ls", description = "Show list information used in tooling")
public class ListCommand extends IbizLabGeneratorCommand {
@Option(name = {"-n","--template-name"}, description = "sepc template list")
private String template;
@Option(name = {"-a","--all"}, description = "template list")
private Boolean all;
@Override
public void execute() {
List<String> retList = new ArrayList<>();
if (!StringUtils.isEmpty(template)) {
getList("templ/"+template,"",retList,true);
} else {
getList("templ","",retList,false);
}
retList.forEach(item->System.out.println(item));
}
private ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
public void getList(String rootDir,String templateDir, List<String> list,boolean recu) {
try
{
if(templateDir.endsWith("//"))
return;
URL url = this.getClass().getClassLoader().getResource(rootDir+"/"+templateDir);
if (url != null) {
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(rootDir+"/"+templateDir);
try(Scanner scanner = new Scanner(stream)) {
while (scanner.hasNextLine()) {
String str = scanner.nextLine();
if(!StringUtils.isEmpty(str))
{
String newPath=str;
if(!StringUtils.isEmpty(templateDir))
newPath=templateDir+"/"+str;
if(recu&&(!str.endsWith(".hbs"))&&(!str.endsWith(".ibizlab-generator-ignore"))&&(str.indexOf(".")<0))
getList(rootDir,newPath,list,recu);
else
list.add(newPath);
}
}
} catch (Exception e) {
}
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
......@@ -18,6 +18,7 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.io.File;
import java.net.URL;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
......@@ -32,6 +33,7 @@ public class CodegenConfig {
private String inputSpec;
private String outputDir;
private List<String> filters;
......@@ -39,6 +41,9 @@ public class CodegenConfig {
private CliFilter cliFilter;
private List<String> templateDirs;
private String templateName;
private String auth;
private List<String> templatePaths;
......@@ -161,6 +166,23 @@ public class CodegenConfig {
private TemplateManager templateProcessor;
private CommonTemplateContentLocator commonTemplateContentLocator;
public CommonTemplateContentLocator getCommonTemplateContentLocator() {
if((!StringUtils.isEmpty(templateName))&&commonTemplateContentLocator==null) {
String loc = TemplateManager.getCPResourcePath(Paths.get("templ" , templateName).toString());
URL url = this.getClass().getClassLoader().getResource(loc);
if (url != null) {
commonTemplateContentLocator = new CommonTemplateContentLocator(loc);
LOGGER.info("Loaded embed template from resources ({}).",loc);
}
else {
LOGGER.error("Failed to Load embed template from resources ({}), not found.",loc);
}
}
return commonTemplateContentLocator;
}
public TemplateManager getTemplateProcessor()
{
if(templateProcessor==null)
......@@ -171,7 +193,9 @@ public class CodegenConfig {
this.getTemplateDirs().forEach(templateDir->{
list.add(new GeneratorTemplateContentLocator(templateDir));
});
list.add(new CommonTemplateContentLocator());
if(getCommonTemplateContentLocator()!=null) {
list.add(commonTemplateContentLocator);
}
this.templateProcessor = new TemplateManager(
new TemplateManagerOptions(this.isEnableMinimalUpdate(),this.isSkipOverwrite()),
templatingEngine,
......@@ -233,6 +257,7 @@ public class CodegenConfig {
}
scanEmbedTemplate(templates);
}
return templates.values();
......@@ -293,6 +318,35 @@ public class CodegenConfig {
}
}
private void scanEmbedTemplate(Map<String,TemplateDefinition> templateDefinitions)
{
if(this.getCommonTemplateContentLocator()!=null)
{
this.getCommonTemplateContentLocator().getTemplatePaths().forEach(item->{
String path=item;
if(!path.startsWith("/"))
path="/"+path;
if(!ObjectUtils.isEmpty(this.templateFilters))
{
boolean matched=false;
for(String filter:this.templateFilters)
{
if(path.matches(filter))
{
matched=true;
break;
}
}
if(!matched)
return;
}
if(!templateDefinitions.containsKey(path)){
TemplateDefinition templateDefinition=new TemplateDefinition(path,this.getCommonTemplateContentLocator().getResourceLocation());
templateDefinitions.put(path,templateDefinition);
}
});
}
}
@SuppressWarnings("static-method")
......
......@@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
......@@ -40,6 +41,7 @@ public final class CodegenConfigurator {
private String outputDir;
private List<String> filters;
private List<String> templateDirs;
private String templateName;
private List<String> templatePaths;
private List<String> templateFilters;
private String auth;
......@@ -202,19 +204,21 @@ public final class CodegenConfigurator {
return this;
}
public CodegenConfigurator setOutputDir(String outputDir) {
this.outputDir = outputDir;
return this;
}
public CodegenConfigurator setTemplateDirs(List<String> templateDirs) {
this.templateDirs = templateDirs;
return this;
}
public CodegenConfigurator setTemplateName(String templateName) {
this.templateName = templateName;
return this;
}
public CodegenConfigurator setTemplatePaths(List<String> templatePaths) {
this.templatePaths = templatePaths;
return this;
......@@ -260,6 +264,10 @@ public final class CodegenConfigurator {
config.setTemplateDirs(this.templateDirs);
}
if(!StringUtils.isEmpty(templateName)) {
config.setTemplateName(this.templateName);
}
if(!ObjectUtils.isEmpty(templatePaths)) {
config.setTemplatePaths(this.templatePaths);
}
......@@ -274,5 +282,4 @@ public final class CodegenConfigurator {
return config;
}
}
......@@ -3,7 +3,12 @@ package cn.ibizlab.codegen.templating;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Locates generator-agnostic templates from a common built-in location.
......@@ -27,6 +32,10 @@ public class CommonTemplateContentLocator implements TemplatePathLocator {
this.resourceLocation = resourceLocation;
}
public String getResourceLocation() {
return resourceLocation;
}
/**
* Get the full path to a relative template file.
*
......@@ -36,7 +45,7 @@ public class CommonTemplateContentLocator implements TemplatePathLocator {
@Override
public String getFullTemplatePath(String relativeTemplateFile) {
if (StringUtils.isNotEmpty(relativeTemplateFile)) {
String loc = this.resourceLocation + File.separator + relativeTemplateFile;
String loc = Paths.get(this.resourceLocation , relativeTemplateFile).toString();
URL url = this.getClass().getClassLoader().getResource(TemplateManager.getCPResourcePath(loc));
if (url != null) {
......@@ -45,4 +54,45 @@ public class CommonTemplateContentLocator implements TemplatePathLocator {
}
return null;
}
public List<String> getTemplatePaths() {
List<String> list = new ArrayList<>();
getList(this.resourceLocation,"",list,true);
return list;
}
private void getList(String rootDir, String templateDir, List<String> list, boolean recu) {
try
{
if(templateDir.endsWith("//"))
return;
URL url = this.getClass().getClassLoader().getResource(rootDir+"/"+templateDir);
if (url != null) {
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(rootDir+"/"+templateDir);
try(Scanner scanner = new Scanner(stream)) {
while (scanner.hasNextLine()) {
String str = scanner.nextLine();
if(!org.springframework.util.StringUtils.isEmpty(str))
{
String newPath=str;
if(!org.springframework.util.StringUtils.isEmpty(templateDir))
newPath=templateDir+"/"+str;
if(recu&&(!str.endsWith(".hbs"))&&(!str.endsWith(".ibizlab-generator-ignore"))&&(str.indexOf(".")<0))
getList(rootDir,newPath,list,recu);
else
list.add(newPath);
}
}
} catch (Exception e) {
}
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
......@@ -3,6 +3,7 @@ package cn.ibizlab.codegen.templating;
import cn.ibizlab.codegen.CodegenConfig;
import java.io.File;
import java.nio.file.Paths;
/**
* Locates templates according to {@link CodegenConfig} settings.
......@@ -21,7 +22,7 @@ public class GeneratorTemplateContentLocator implements TemplatePathLocator {
// check the supplied template main folder for the file
// File.separator is necessary here as the file load is OS-specific
final String template = templateDir + File.separator + relativeTemplateFile;
final String template = Paths.get(templateDir , relativeTemplateFile).toString();
// looks for user-defined file or classpath
// supports template dir which refers to local file system or custom path in classpath as defined by templateDir
if (new File(template).exists()) {
......
......@@ -147,17 +147,24 @@ public class TemplateManager implements TemplatingExecutor, TemplateProcessor {
*/
@Override
public File write(Map<String, Object> data, String template, File target) throws IOException {
String templateContent="";
if(!this.engineAdapter.handlesFile(template))
{
File dir=target.getParentFile();
if(!dir.exists())
dir.mkdirs();
Files.copy(this.getFullTemplatePath(template),Paths.get(target.getPath()), StandardCopyOption.REPLACE_EXISTING);
return target;
Path srcPath=this.getFullTemplatePath(template);
if(srcPath.toFile().exists()) {
Files.copy(srcPath, Paths.get(target.getPath()), StandardCopyOption.REPLACE_EXISTING);
return target;
}
else
{
templateContent=getFullTemplateContents(template);
}
}
String templateContent = this.engineAdapter.compileTemplate(this, data, template);
else
templateContent = this.engineAdapter.compileTemplate(this, data, template);
if(StringUtils.isEmpty(templateContent))
return null;
return writeToFile(target.getPath(), templateContent);
......
......@@ -16,16 +16,12 @@
<version>2.4.0</version>
</parent>
<modules>
<module>{{projectName}}-pub</module>
<module>{{projectName}}-core</module>
<!-- boot -->
<module>{{projectName}}-boot</module>
</modules>
<dependencies>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-boot-starter</artifactId>
<version>2.4.0.129</version>
</dependency>
<!--达梦数据库-->
<dependency>
<groupId>com.dameng</groupId>
......
......@@ -10,13 +10,13 @@
</parent>
<artifactId>{{projectName}}-boot</artifactId>
<name>{{projectDesc}} Dev Monolithic Boot</name>
<name>{{projectDesc}} Boot [{{projectName}}-boot]</name>
<description>{{projectDesc}} Boot</description>
<dependencies>
<dependency>
<groupId>{{packageName}}</groupId>
<artifactId>{{projectName}}-pub</artifactId>
<artifactId>{{projectName}}-core</artifactId>
<version>1.0.0.0</version>
</dependency>
</dependencies>
......@@ -70,8 +70,11 @@
${basedir}/src/main/resources/model/com/sa/jcsc
</inputSpec>
<templateDirs>
/Users/mac/workshop/ibizlab-generator/modules/ibizlab-generator-core/src/main/resources/templ/ibiz-boot
${basedir}/src/main/resources/templ/ibiz-boot
</templateDirs>
<templateName>
ibiz-boot
</templateName>
<packageName>
${project.groupId}
</packageName>
......
......@@ -9,8 +9,15 @@
<version>1.0.0.0</version>
</parent>
<artifactId>{{projectName}}-pub</artifactId>
<name>{{projectDesc}} Generator Code</name>
<description>{{projectDesc}} 模板生成代码,不要直接修改,修改请在{{projectName}}-boot项目中重写</description>
<artifactId>{{projectName}}-core</artifactId>
<name>{{projectDesc}} Generator Code [{{projectName}}-core]</name>
<description>{{projectDesc}} 模板生成代码,再次发布会覆盖,建议不要直接修改,修改请在{{projectName}}-boot项目中重写</description>
<dependencies>
<dependency>
<groupId>net.ibizsys.plugin</groupId>
<artifactId>ibiz-boot-starter</artifactId>
<version>2.4.0.132</version>
</dependency>
</dependencies>
</project>
......@@ -91,9 +91,7 @@
<version>1.0-SNAPSHOT</version>
<configuration>
<template>
<!-- <controller>/template/controller.java.vm</controller> -->
</template>
</configuration>
</plugin>
......
......@@ -3,6 +3,7 @@ package cn.ibizlab.codegen;
import cn.ibizlab.codegen.config.CodegenConfigurator;
import cn.ibizlab.codegen.config.CodegenConfiguratorUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
......@@ -40,6 +41,8 @@ public class GenerateMojo extends AbstractMojo {
@Parameter
private String[] templateDirs;
@Parameter
private String templateName;
@Parameter
private String[] templatePaths;
......@@ -133,6 +136,10 @@ public class GenerateMojo extends AbstractMojo {
configurator.setTemplateDirs(Arrays.asList(templateDirs));
}
if(!StringUtils.isEmpty(templateName)) {
configurator.setTemplateName(this.templateName);
}
if (!ObjectUtils.isEmpty(templatePaths)) {
configurator.setTemplatePaths(Arrays.asList(templatePaths));
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册