org.gradle.buildinit.plugins.internal.TemplateOperationFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2013 the original author or authors.
*
* 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
*
* http://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 org.gradle.buildinit.plugins.internal;
import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.util.GradleVersion;
import java.io.File;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class TemplateOperationFactory {
private final String templatepackage;
private final DocumentationRegistry documentationRegistry;
private final Map defaultBindings;
public TemplateOperationFactory(String templatepackage, DocumentationRegistry documentationRegistry) {
this.documentationRegistry = documentationRegistry;
this.templatepackage = templatepackage;
this.defaultBindings = loadDefaultBindings();
}
private Map loadDefaultBindings() {
String now = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date());
Map map = new LinkedHashMap<>(3);
map.put("genDate", now);
map.put("genUser", System.getProperty("user.name"));
map.put("genGradleVersion", GradleVersion.current().toString());
return map;
}
public TemplateOperationBuilder newTemplateOperation() {
return new TemplateOperationBuilder(defaultBindings);
}
public class TemplateOperationBuilder {
private File target;
final private Map bindings = new HashMap<>();
private URL templateUrl;
public TemplateOperationBuilder(Map defaultBindings) {
this.bindings.putAll(defaultBindings);
}
public TemplateOperationBuilder withTemplate(final String relativeTemplatePath) {
this.templateUrl = getClass().getResource(templatepackage + "/" + relativeTemplatePath);
if (templateUrl == null) {
throw new IllegalArgumentException(String.format("Could not find template '%s' in classpath.", relativeTemplatePath));
}
return this;
}
public TemplateOperationBuilder withTemplate(URL templateURL) {
this.templateUrl = templateURL;
return this;
}
public TemplateOperationBuilder withTarget(File targetFilePath) {
this.target = targetFilePath;
return this;
}
public TemplateOperationBuilder withDocumentationBindings(Map documentationBindings) {
for (Map.Entry entry : documentationBindings.entrySet()){
bindings.put(entry.getKey(), documentationRegistry.getDocumentationFor(entry.getValue()));
}
return this;
}
public TemplateOperationBuilder withBindings(Map bindings) {
this.bindings.putAll(bindings);
return this;
}
public TemplateOperationBuilder withBinding(String name, String value) {
bindings.put(name, value);
return this;
}
public TemplateOperation create() {
final Set> entries = bindings.entrySet();
Map wrappedBindings = new HashMap<>(entries.size());
for (Map.Entry entry : entries) {
if (entry.getValue() == null) {
throw new IllegalArgumentException("Null value provided for binding '" + entry.getKey() + "'.");
}
wrappedBindings.put(entry.getKey(), new TemplateValue(entry.getValue()));
}
return new SimpleTemplateOperation(templateUrl, target, wrappedBindings);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy