Search in sources :

Example 1 with TemplateConfig

use of com.spawpaw.mybatis.generator.gui.entity.TemplateConfig in project mybatis-generator-gui-extension by spawpaw.

the class SCVXGeneratorPlugin method contextGenerateAdditionalJavaFiles.

@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
    log.info(">>>> generating extra files...");
    // : 2018/3/22 将 introspectedTable 转化为对象
    Map<String, String> map = new HashMap<>();
    for (Object o : properties.keySet()) {
        map.put(o.toString(), properties.getProperty(o.toString()));
    }
    Table table = new Table(context, introspectedTable, map);
    // : 2018/3/22 初始化context
    VelocityContext templateContext = new VelocityContext();
    templateContext.put("table", table);
    templateContext.put("projectDir.ss", projectDir);
    // 输出测试数据
    String content = renderTemplateAsString("test.vm", templateContext);
    log.info("hierarchical table structure: {}", content);
    // : 2018/3/22 保存到指定目录
    List<TemplateConfig> configs = new Yaml().loadAs(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.yml"), ConfigWrapper.class).getTemplateConfig();
    for (TemplateConfig config : configs) {
        String template = config.getTemplate();
        String destDir = config.getDestDir().replaceAll("\\.", "/");
        String destPackage = config.getDestPackage();
        String destFileName = config.getDestFileName();
        String absPath = (projectDir == null || projectDir.isEmpty() ? "" : projectDir) + destDir + "/" + destPackage.replace(".", "/") + "/" + destFileName;
        absPath = absPath.replace("//", "/");
        absPath = absPath.replace("${entityName}", table.getEntityName());
        absPath = absPath.replace("${basePackage}", basePackage.replace(".", "/"));
        log.info("generate file `{}` from template `{}`", absPath, template);
        content = renderTemplateAsString(config.getTemplate(), templateContext);
        // 写入文件
        try {
            FileUtil.writeStringToFile(absPath, content);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        log.info("content: {}", content);
    }
    log.info("<<< generated extra files.");
    return null;
}
Also used : IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) Table(com.spawpaw.mybatis.generator.gui.entity.Table) ConfigWrapper(com.spawpaw.mybatis.generator.gui.entity.ConfigWrapper) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) TemplateConfig(com.spawpaw.mybatis.generator.gui.entity.TemplateConfig) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

ConfigWrapper (com.spawpaw.mybatis.generator.gui.entity.ConfigWrapper)1 Table (com.spawpaw.mybatis.generator.gui.entity.Table)1 TemplateConfig (com.spawpaw.mybatis.generator.gui.entity.TemplateConfig)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 VelocityContext (org.apache.velocity.VelocityContext)1 IntrospectedTable (org.mybatis.generator.api.IntrospectedTable)1 Yaml (org.yaml.snakeyaml.Yaml)1