Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Refactor templating management #6357

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
636c4dd
[core] Refactor templating management
jimschubert May 22, 2020
d67d3d4
Fix javadoc errs
jimschubert May 18, 2020
66f49b6
Tests for dry run file outputs
jimschubert May 19, 2020
6a745de
Update API usage in Meta, test TemplateManager
jimschubert May 22, 2020
a1b23d9
Fix temp file in overwrite test.
jimschubert May 19, 2020
f3ccf3c
Wait on lastModified, lookup by filename in SpringCodegenTest
jimschubert May 19, 2020
4c3c28e
Remove minimal update test from DefaultGenerator (dupe with TemplateM…
jimschubert May 19, 2020
bb804a4
Test DefaultGenerator + ignore file
jimschubert May 20, 2020
ecb985e
Move config.processOpenAPI in DefaultGenerator
jimschubert May 22, 2020
f970e7c
Clarify comment in GeneratorTemplateContentLocator
jimschubert May 22, 2020
8062b77
Do not overwrite existing test files
jimschubert May 22, 2020
048cc6e
Fix wrong use of libraries templateDirector (java)
jimschubert May 22, 2020
d5307f3
[samples] Regenerate
jimschubert May 22, 2020
e267159
Merge branch 'master' into cleanup-template-management
jimschubert May 22, 2020
edaa592
Fix handlebars extension usage, clean up Meta tasks
jimschubert May 23, 2020
ccbcf6f
Merge branch 'master' into cleanup-template-management
jimschubert May 23, 2020
ef4e835
Merge branch 'master' into cleanup-template-management
jimschubert May 23, 2020
00ebf20
Merge branch 'master' into cleanup-template-management
jimschubert May 23, 2020
5f6368c
Move FILES/VERSION metadata gen to private methods
jimschubert May 23, 2020
9b046c2
Update kotlin-multiplatform gradle wrapper
jimschubert May 24, 2020
8943146
Rename GraphQL .gitignore template
jimschubert May 24, 2020
d256cec
Log entire stacktrace in go sdk built by gradle in AppVeyor
jimschubert May 24, 2020
33f89fd
Rename PHP .gitignore to gitignore
jimschubert May 24, 2020
3ea7469
[samples] Regenerate
jimschubert May 24, 2020
bc86eea
[php] Rename .gitignore templates to gitignore
jimschubert May 24, 2020
e934bec
Use same classpath lookup in common locator
jimschubert May 25, 2020
062b4d2
Merge branch 'master' into cleanup-template-management
jimschubert May 27, 2020
8faa776
[samples] Regenerate
jimschubert May 27, 2020
e3dcb2f
Merge branch 'master' into cleanup-template-management
jimschubert May 28, 2020
ff91eec
Regenerate rust samples
jimschubert May 28, 2020
9a982d4
[rust] Properly escape empty triple-braces
jimschubert May 28, 2020
5166440
Merge branch 'master' into cleanup-template-management
jimschubert May 30, 2020
2033a90
[samples] Regenerate
jimschubert May 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.TemplateManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -146,9 +147,9 @@ public File convert(SupportingFile support) {
new File(new File(targetDir.getAbsolutePath()), support.folder);
File outputFile = new File(destinationFolder, support.destinationFilename);

String template =
generator.readTemplate(new File(TEMPLATE_DIR_CLASSPATH,
support.templateFile).getPath());
String template =((TemplateManager)generator.getTemplateProcessor()).readTemplate(new File(TEMPLATE_DIR_CLASSPATH,
support.templateFile).getPath());

String formatted = template;

if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) {
Expand Down Expand Up @@ -180,7 +181,7 @@ private static Mustache.TemplateLoader loader(final DefaultGenerator generator)
return new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) {
return generator.getTemplateReader(TEMPLATE_DIR_CLASSPATH + File.separator
return ((TemplateManager)generator.getTemplateProcessor()).getTemplateReader(TEMPLATE_DIR_CLASSPATH + File.separator
+ name.concat(MUSTACHE_EXTENSION));
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.openapitools.codegen.api;

/**
* Provides means for searching for "actual" template location based on relative template file.
*/
public interface TemplatePathLocator {
/**
* Get the full path to a relative template file.
*
* @param relativeTemplateFile Template file
* @return String Full template file path
*/
String getFullTemplatePath(String relativeTemplateFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.openapitools.codegen.api;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;

/**
* Interface for abstractions around writing templated data to a file.
*/
public interface TemplateProcessor {
/**
* Writes data to a compiled template
*
* @param data Input data
* @param template Input template location
* @param target The targeted file output location
*
* @return The actual file
*/
File write(Map<String, Object> data, String template, File target) throws IOException;

/**
* Write bytes to a file
*
* @param filename The name of file to write
* @param contents The contents bytes. Typically this is a UTF-8 formatted string.
* @return File representing the written file.
* @throws IOException If file cannot be written.
*/
File writeToFile(String filename, byte[] contents) throws IOException;

/**
* Allow a caller to mark a path as ignored with accompanying reason
*
* @param path The ignored path
* @param context The reason for ignoring this path
*/
void ignore(Path path, String context);

/**
* Allow a caller to mark a path as skipped with accompanying reason
*
* @param path The skipped path
* @param context The reason for skipping this path
*/
void skip(Path path, String context);

/**
* Allow a caller to mark a path having errored during processing with accompanying reason
*
* @param path The path which has caused an error
* @param context The reason for the error
*/
default void error(Path path, String context) { };
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ public interface TemplatingEngineAdapter {
*/
String getIdentifier();

/**
* During generation, if a supporting file has a file extension that is
* inside that array, then it is considered a templated supporting file
* and we use the templating engine adapter to generate it
*
* @return string array of the valid file extensions for this templating engine
*/
String[] getFileExtensions();

/**
* Compiles a template into a string
*
* @param generator From where we can fetch the templates content (e.g. an instance of DefaultGenerator)
* @param executor From where we can fetch the templates content (e.g. an instance of DefaultGenerator)
* @param bundle The map of values to pass to the template
* @param templateFile The name of the template (e.g. model.mustache )
* @return the processed template result
* @throws IOException an error ocurred in the template processing
*/
String compileTemplate(TemplatingGenerator generator, Map<String, Object> bundle,
String compileTemplate(TemplatingExecutor executor, Map<String, Object> bundle,
String templateFile) throws IOException;

/**
* During generation, if a supporting file has a file extension that is
* inside that array, then it is considered a templated supporting file
* and we use the templating engine adapter to generate it
*
* @return string array of the valid file extensions for this templating engine
*/
String[] getFileExtensions();

/**
* Determines whether the template file with supported extensions exists. This may be on the filesystem,
* external filesystem, or classpath (implementation is up to TemplatingGenerator).
Expand All @@ -65,7 +65,7 @@ String compileTemplate(TemplatingGenerator generator, Map<String, Object> bundle
* @param templateFile The original target filename
* @return True if the template is available in the template search path, false if it can not be found
*/
default boolean templateExists(TemplatingGenerator generator, String templateFile) {
default boolean templateExists(TemplatingExecutor generator, String templateFile) {
return Arrays.stream(getFileExtensions()).anyMatch(ext -> {
int idx = templateFile.lastIndexOf(".");
String baseName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openapitools.codegen.api;

import java.nio.file.Path;

/**
* interface to the full template content
* implementers might take into account the -t cli option,
* look in the resources for a generator specific template, etc
*/
public interface TemplatingExecutor {
/**
* returns the template content by name
*
* @param name the template name (e.g. model.mustache)
* @return the contents of that template
*/
String getFullTemplateContents(String name);

/**
* Returns the path of a template, allowing access to the template where consuming literal contents aren't desirable or possible.
*
* @param name the template name (e.g. model.mustache)
* @return The {@link Path} to the template
*/
Path getFullTemplatePath(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,16 @@

package org.openapitools.codegen.api;

import java.nio.file.Path;

// TODO: 6.0 Remove
/**
* interface to the full template content
* implementers might take into account the -t cli option,
* look in the resources for a language specific template, etc
*
* @deprecated as of 5.0, replaced by {@link TemplatingExecutor}
* @apiNote Deprecation is considered low-impact breaking change because tooling only supports internal implementations.
*/
public interface TemplatingGenerator {

/**
* returns the template content by name
*
* @param name the template name (e.g. model.mustache)
* @return the contents of that template
*/
String getFullTemplateContents(String name);
@Deprecated()
public interface TemplatingGenerator extends TemplatingExecutor {

/**
* Returns the path of a template, allowing access to the template where consuming literal contents aren't desirable or possible.
*
* @param name the template name (e.g. model.mustache)
* @return The {@link Path} to the template
*/
Path getFullTemplatePath(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import org.gradle.api.tasks.TaskAction
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutputFactory
import org.gradle.kotlin.dsl.property
import org.openapitools.codegen.CodegenConfig
import org.openapitools.codegen.CodegenConstants
import org.openapitools.codegen.DefaultGenerator
import org.openapitools.codegen.SupportingFile
import org.openapitools.codegen.*
import java.io.File
import java.io.IOException
import java.nio.charset.Charset
Expand Down Expand Up @@ -90,9 +87,17 @@ open class MetaTask : DefaultTask() {
destinationFolder.mkdirs()
val outputFile = File(destinationFolder, it.destinationFilename)

val template = generator.readTemplate(File("codegen", it.templateFile).path)
val templateProcessor = generator.templateProcessor as TemplateManager

val template = templateProcessor.getFullTemplateContents(File("codegen", it.templateFile).path)
var formatted = template

val loader: (DefaultGenerator) -> Mustache.TemplateLoader = { g ->
Mustache.TemplateLoader { name ->
templateProcessor.getTemplateReader("codegen${File.separator}$name.mustache")
}
}

if (it.templateFile.endsWith(".mustache")) {
formatted = Mustache.compiler()
.withLoader(loader(generator))
Expand All @@ -115,12 +120,6 @@ open class MetaTask : DefaultTask() {
out.formatln("Created generator %s", klass)
}

private fun loader(generator: DefaultGenerator): Mustache.TemplateLoader {
return Mustache.TemplateLoader { name ->
generator.getTemplateReader("codegen${File.separator}$name.mustache")
}
}

private fun String.titleCasedTextOnly(): String =
this.split(Regex("[^a-zA-Z0-9]")).joinToString(separator = "", transform = String::capitalize)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ public interface CodegenConfig {

String getDocExtension();

String getCommonTemplateDir();

void setIgnoreFilePathOverride(String ignoreFileOverride);

String getIgnoreFilePathOverride();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
protected Map<String, String> reservedWordsMappings = new HashMap<String, String>();
protected String templateDir;
protected String embeddedTemplateDir;
protected String commonTemplateDir = "_common";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now handled by template loading functionality, as that is not the responsibility of DefaultCodegen.

protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
protected Map<String, String> serverVariables = new HashMap<String, String>();
protected Map<String, Object> vendorExtensions = new HashMap<String, Object>();
Expand Down Expand Up @@ -946,14 +945,6 @@ public String embeddedTemplateDir() {
}
}

public String getCommonTemplateDir() {
return this.commonTemplateDir;
}

public void setCommonTemplateDir(String commonTemplateDir) {
this.commonTemplateDir = commonTemplateDir;
}

public Map<String, String> apiDocTemplateFiles() {
return apiDocTemplateFiles;
}
Expand Down Expand Up @@ -1426,12 +1417,6 @@ public DefaultCodegen() {
importMapping.put("LocalDate", "org.joda.time.*");
importMapping.put("LocalTime", "org.joda.time.*");

// we've used the .openapi-generator-ignore approach as
// suppportingFiles can be cleared by code generator that extends
// the default codegen, leaving the commented code below for
// future reference
//supportingFiles.add(new GlobalSupportingFile("LICENSE", "LICENSE"));

cliOptions.add(CliOption.newBoolean(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG,
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC).defaultValue(Boolean.TRUE.toString()));
cliOptions.add(CliOption.newBoolean(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG,
Expand Down
Loading