com.atlassian.bamboo.specs.codegen.emitters.owner.PlanOwnerEmitter Maven / Gradle / Ivy
package com.atlassian.bamboo.specs.codegen.emitters.owner;
import com.atlassian.bamboo.specs.api.builders.owner.PlanOwner;
import com.atlassian.bamboo.specs.api.builders.plan.configuration.AllOtherPluginsConfiguration;
import com.atlassian.bamboo.specs.api.codegen.CodeEmitter;
import com.atlassian.bamboo.specs.api.codegen.CodeGenerationContext;
import com.atlassian.bamboo.specs.api.codegen.CodeGenerationException;
import com.atlassian.bamboo.specs.api.model.owner.PlanOwnerProperties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.jetbrains.annotations.NotNull;
public class PlanOwnerEmitter implements CodeEmitter {
@NotNull
@Override
public String emitCode(@NotNull final CodeGenerationContext context,
@NotNull final PlanOwnerProperties entity) throws CodeGenerationException {
context.importClassName(AllOtherPluginsConfiguration.class);
StringBuilder sb = new StringBuilder();
sb.append("/* Add com.atlassian.buildeng:bamboo-plan-ownership-specs:").append(version())
.append(" as dependency to your specs pom.xml to make use of this custom entity.");
sb.append(context.newLine());
sb.append("new ").append(PlanOwner.class.getSimpleName()).append("(\"")
.append(entity.getOwner()).append("\") */");
sb.append(context.newLine()).append("new AllOtherPluginsConfiguration()");
context.incIndentation();
sb.append(context.newLine()).append(".configuration(new MapBuilder()");
context.incIndentation();
sb.append(context.newLine()).append(".put(\"custom.planownership.bamboo.plugin.plan.config.ownerOfBuild\", \"")
.append(entity.getOwner()).append("\")");
sb.append(context.newLine()).append(".build())");
context.decIndentation();
context.decIndentation();
return sb.toString();
}
private String version() {
try {
Properties props = new Properties();
InputStream stream = PlanOwnerEmitter.class
.getResourceAsStream("/com/atlassian/bamboo/specs/codegen/emitters/owner/version.properties");
if (stream != null) {
props.load(stream);
}
return props.getProperty("version", "RELEASE");
} catch (IOException ex) {
return "RELEASE";
}
}
}