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

move

上级 9bd67ac8
......@@ -85,7 +85,7 @@
<dependency>
<groupId>com.ibizlab</groupId>
<artifactId>ibizlab-generator</artifactId>
<artifactId>ibizlab-generator-core</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
......
......@@ -56,7 +56,6 @@ public class DefaultGenerator implements Generator {
private final boolean dryRun;
protected CodegenConfig config;
protected ClientOptInput opts;
protected CodegenIgnoreProcessor ignoreProcessor;
private Boolean generateApis = null;
private Boolean generateModels = null;
private Boolean generateSupportingFiles = null;
......
......@@ -19,6 +19,7 @@ package com.ibizlab.codegen.config;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.github.mustachejava.reflect.ReflectionObjectHandler;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
......@@ -31,6 +32,7 @@ import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Getter
@Setter
......@@ -39,7 +41,12 @@ import java.util.List;
public class Context<TSpecDocument> {
private TSpecDocument specDocument;
private GeneratorSettings generatorSettings;
private static class MapMethodReflectionHandler extends ReflectionObjectHandler {
@Override
protected boolean areMethodsAccessible(Map<?, ?> map) {
return true;
}
}
public static void main(String[] args) throws IOException {
HashMap<String, Object> scopes = new HashMap<String, Object>();
......@@ -52,9 +59,16 @@ public class Context<TSpecDocument> {
list.add("b");
scopes.put("list",list);
Map map=new HashMap();
map.put("01","a01");
map.put("02","a02");
scopes.put("map1",map);
System.out.println(map.entrySet());
Writer writer = new OutputStreamWriter(System.out);
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader("{{name}}, {{#list}}{{.}}{{/list}}!"), "example");
DefaultMustacheFactory mf = new DefaultMustacheFactory();
mf.setObjectHandler(new MapMethodReflectionHandler());
Mustache mustache = mf.compile(new StringReader("{{name}}, {{map1.entrySet}}{{#map1.entrySet}}{{key}}{{/map1.entrySet}}!"), "example");
mustache.execute(writer, scopes);
writer.flush();
}
......
......@@ -60,43 +60,18 @@ public class GeneratorTemplateContentLocator implements TemplatePathLocator {
public String getFullTemplatePath(String relativeTemplateFile) {
CodegenConfig config = this.codegenConfig;
//check the supplied template library folder for the file
final String library = config.getLibrary();
if (StringUtils.isNotEmpty(library)) {
//look for the file in the library subfolder of the supplied template
final String libTemplateFile = buildLibraryFilePath(config.templateDir(), library, relativeTemplateFile);
// 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(libTemplateFile).exists() || classpathTemplateExists(libTemplateFile)) {
return libTemplateFile;
}
}
// check the supplied template main folder for the file
// File.separator is necessary here as the file load is OS-specific
final String template = config.templateDir() + File.separator + relativeTemplateFile;
final String template = config.getTemplateDir() + File.separator + relativeTemplateFile;
// 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() || classpathTemplateExists(template)) {
return template;
}
//try the embedded template library folder next
if (StringUtils.isNotEmpty(library)) {
final String embeddedLibTemplateFile = buildLibraryFilePath(config.embeddedTemplateDir(), library, relativeTemplateFile);
// *only* looks for those files in classpath as defined by embeddedTemplateDir
if (embeddedTemplateExists(embeddedLibTemplateFile)) {
// Fall back to the template file embedded/packaged in the JAR file library folder...
return embeddedLibTemplateFile;
}
}
// Fall back to the template file for generator root directory embedded/packaged in the JAR file...
String loc = config.embeddedTemplateDir() + File.separator + relativeTemplateFile;
// *only* looks for those files in classpath as defined by embeddedTemplateDir
if (embeddedTemplateExists(loc)) {
return loc;
}
return null;
}
......
......@@ -40,9 +40,7 @@ public class CaseFormatLambda implements Mustache.Lambda {
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = initialFormat.converterTo(targetFormat).convert(fragment.execute());
if (generator != null && generator.reservedWords().contains(text)) {
text = generator.escapeReservedWord(text);
}
writer.write(text);
}
}
......@@ -53,9 +53,7 @@ public class LowercaseLambda implements Mustache.Lambda {
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = fragment.execute().toLowerCase(Locale.ROOT);
if (generator != null && generator.reservedWords().contains(text)) {
text = generator.escapeReservedWord(text);
}
writer.write(text);
}
......
......@@ -249,7 +249,7 @@ public class StringAdvUtils {
public static String escape(final String name, final Map<String, String> replacementMap,
final List<String> charactersToAllow, final String appendToReplacement) {
EscapedNameOptions ns = new EscapedNameOptions(name, replacementMap.keySet(), charactersToAllow, appendToReplacement);
return escapedWordsCache.get(ns, wordToEscape -> {
return StringAdvUtils.escapedWordsCache.get(ns, wordToEscape -> {
String result = name.chars().mapToObj(c -> {
String character = String.valueOf((char) c);
if (charactersToAllow != null && charactersToAllow.contains(character)) {
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ibizlab-generator-project</artifactId>
<groupId>com.ibizlab</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibizlab</groupId>
<artifactId>ibizlab-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ibizlab-generator</name>
<dependencies>
<dependency>
<groupId>com.ibizlab</groupId>
<artifactId>ibizlab-generator-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibizlab.codegen;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
public class CodegenConfigLoader {
/**
* Tries to load config class with SPI first, then with class name directly from classpath
*
* @param name name of config, or full qualified class name in classpath
* @return config class
*/
public static CodegenConfig forName(String name) {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class, CodegenConfig.class.getClassLoader());
StringBuilder availableConfigs = new StringBuilder();
for (CodegenConfig config : loader) {
if (config.getName().equals(name)) {
return config;
}
availableConfigs.append(config.getName()).append("\n");
}
// else try to load directly
try {
return (CodegenConfig) Class.forName(name).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new GeneratorNotFoundException("Can't load config class with name '".concat(name) + "'\nAvailable:\n" + availableConfigs, e);
}
}
public static List<CodegenConfig> getAll() {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class, CodegenConfig.class.getClassLoader());
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
for (CodegenConfig aLoader : loader) {
output.add(aLoader);
}
return output;
}
}
......@@ -14,7 +14,6 @@
<modules>
<module>modules/ibizlab-generator-core</module>
<module>modules/ibizlab-generator</module>
<module>modules/ibizlab-generator-cli</module>
</modules>
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册