All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.bamboo.specs.codegen.emitters.jobcache.JobCacheEmitter Maven / Gradle / Ivy

There is a newer version: 2.1.7
Show newest version
package com.atlassian.bamboo.specs.codegen.emitters.jobcache;

import com.atlassian.bamboo.specs.api.builders.jobcache.CacheItem;
import com.atlassian.bamboo.specs.api.builders.jobcache.JobCache;
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.jobcache.CacheItemProperties;
import com.atlassian.bamboo.specs.api.model.jobcache.JobCacheProperties;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;

public class JobCacheEmitter implements CodeEmitter {

    @NotNull
    @Override
    public String emitCode(@NotNull final CodeGenerationContext context, @NotNull final JobCacheProperties entity)
            throws CodeGenerationException {
        if (entity.getCaches().isEmpty()) {
            return "";
        }
        context.importClassName(AllOtherPluginsConfiguration.class);
        StringBuilder sb = new StringBuilder();
        sb.append("/* Add com.atlassian.buildeng:bamboo-job-cache-specs:")
                .append(version())
                .append(" as dependency to your specs pom.xml to make use of this custom entity.");
        sb.append(context.newLine());
        appendDslClass(sb, entity, context);

        sb.append("new AllOtherPluginsConfiguration()");
        context.incIndentation();
        sb.append(context.newLine()).append(".configuration(new MapBuilder()");
        context.incIndentation();
        if (!entity.getCaches().isEmpty()) {
            sb.append(context.newLine())
                    .append(".put(\"custom.job.caches\", \"")
                    .append(toJson(entity.getCaches()))
                    .append("\")");
        }
        sb.append(context.newLine()).append(".build())");
        context.decIndentation();
        context.decIndentation();
        return sb.toString();
    }

    private void appendDslClass(StringBuilder sb, JobCacheProperties entity, CodeGenerationContext context) {
        sb.append("new ").append(JobCache.class.getSimpleName()).append("()");
        dslParams(context, sb, entity);
    }

    private void dslParams(CodeGenerationContext context, StringBuilder sb, JobCacheProperties entity) {
        context.incIndentation();
        generateExtraContainerBuilder(entity.getCaches(), sb, context);
        context.decIndentation();
        sb.append(" */").append(context.newLine());
    }

    static void generateExtraContainerBuilder(
            List caches, StringBuilder sb, CodeGenerationContext context) {
        if (!caches.isEmpty()) {
            sb.append(context.newLine()).append(".caches(");
            context.incIndentation();
            sb.append(caches.stream()
                    .map((CacheItemProperties t) -> {
                        StringBuilder sb2 = new StringBuilder();
                        sb2.append(context.newLine())
                                .append("new ")
                                .append(CacheItem.class.getSimpleName())
                                .append("()");
                        context.incIndentation();
                        sb2.append(context.newLine())
                                .append(".location(\"")
                                .append(t.getLocation())
                                .append("\")");
                        context.decIndentation();
                        return sb2.toString();
                    })
                    .collect(Collectors.joining(", ")));
            context.decIndentation();
            sb.append(context.newLine()).append(")");
        }
    }

    static String toJson(List cacheItems) {
        return "["
                + cacheItems.stream()
                        .map((CacheItemProperties t) -> {
                            StringBuilder sb = new StringBuilder();
                            sb.append("{");
                            sb.append("\\\"location\\\":\\\"")
                                    .append(t.getLocation())
                                    .append("\\\"");
                            sb.append("}");
                            return sb.toString();
                        })
                        .collect(Collectors.joining(","))
                + "]";
    }

    private String version() {
        try {
            Properties props = new Properties();
            InputStream stream = JobCacheEmitter.class.getResourceAsStream(
                    "/com/atlassian/bamboo/specs/codegen/emitters/jobcache/version.properties");
            if (stream != null) {
                props.load(stream);
            }
            return props.getProperty("version", "RELEASE");
        } catch (IOException ex) {
            return "RELEASE";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy