Search in sources :

Example 1 with ProjectRequest

use of org.talend.sdk.component.starter.server.service.domain.ProjectRequest in project component-runtime by Talend.

the class ProjectGenerator method generate.

public void generate(final ProjectRequest request, final OutputStream outputStream) {
    final BuildGenerator generator = generators.get(request.getBuildType());
    final Map<String, byte[]> files = new HashMap<>();
    // build dependencies to give them to the build
    final Collection<String> facets = ofNullable(request.getFacets()).orElse(emptyList());
    final List<Dependency> dependencies = new ArrayList<>(facets.stream().map(this.facets::get).flatMap(f -> f.dependencies(facets)).collect(toSet()));
    dependencies.sort((o1, o2) -> {
        {
            // by scope
            final int scope1 = scopesOrdering.indexOf(o1.getScope());
            final int scope2 = scopesOrdering.indexOf(o2.getScope());
            final int scopeDiff = scope1 - scope2;
            if (scopeDiff != 0) {
                return scopeDiff;
            }
        }
        {
            // by group
            final int comp = o1.getGroup().compareTo(o2.getGroup());
            if (comp != 0) {
                return comp;
            }
        }
        // by name
        return o1.getArtifact().compareTo(o2.getArtifact());
    });
    // force component-api and force it first
    dependencies.remove(Dependency.componentApi());
    dependencies.add(0, Dependency.componentApi());
    // create the build to be able to generate the files
    final Build build = generator.createBuild(request.getBuildConfiguration(), request.getPackageBase(), dependencies, facets);
    files.put(build.getBuildFileName(), build.getBuildFileContent().getBytes(StandardCharsets.UTF_8));
    // generate facet files
    final Map<FacetGenerator, List<String>> filePerFacet = facets.stream().map(s -> s.toLowerCase(Locale.ENGLISH)).collect(toMap(this.facets::get, f -> {
        final FacetGenerator g = this.facets.get(f);
        return g.create(request.getPackageBase(), build, facets, request.getSources(), request.getProcessors()).peek(file -> files.put(file.getPath(), file.getContent())).map(FacetGenerator.InMemoryFile::getPath).collect(toList());
    }));
    // generate README.adoc if needed
    if (!files.containsKey("README.adoc")) {
        files.put("README.adoc", readmeGenerator.createReadme(request.getBuildConfiguration().getName(), filePerFacet).getBytes(StandardCharsets.UTF_8));
    }
    // handle logging - centralized since we want a single setup per project
    filePerFacet.keySet().stream().map(FacetGenerator::loggingScope).reduce((s1, s2) -> {
        final List<String> scopes = asList(s1, s2);
        if (scopes.contains("compile")) {
            return "compile";
        }
        if (scopes.contains("provided")) {
            return "provided";
        }
        if (scopes.contains("test")) {
            return "test";
        }
        return s1;
    }).filter(s -> !s.isEmpty()).ifPresent(scope -> {
        dependencies.add(new Dependency("org.apache.logging.log4j", "log4j-slf4j-impl", Versions.LOG4J2_VERSION, scope));
        files.put(("test".equals(scope) ? build.getTestResourcesDirectory() : build.getMainResourcesDirectory()) + "/log4j2.xml", tpl.render("generator/logging/log4j2.mustache", emptyMap()).getBytes(StandardCharsets.UTF_8));
    });
    componentGenerator.create(request.getPackageBase(), build, request.getFamily(), request.getCategory(), request.getSources(), request.getProcessors()).forEach(file -> files.put(file.getPath(), file.getContent()));
    // add wrapper build files
    build.getWrapperFiles().forEach(f -> files.put(f.getPath(), f.getContent()));
    // now create the zip prefixing it with the artifact value
    final String rootName = request.getBuildConfiguration().getArtifact();
    final Set<String> createdFolders = new HashSet<>();
    try (final ZipOutputStream zip = new ZipOutputStream(outputStream)) {
        // first create folders
        new HashSet<>(files.keySet()).forEach(path -> {
            final String[] segments = (rootName + '/' + path).split("/");
            final StringBuilder current = new StringBuilder();
            for (int i = 0; i < segments.length; i++) {
                if (i == segments.length - 1) {
                    break;
                }
                current.append(segments[i]).append('/');
                final String folder = current.toString();
                if (createdFolders.add(folder)) {
                    try {
                        zip.putNextEntry(new ZipEntry(folder));
                        zip.closeEntry();
                    } catch (final IOException e) {
                        throw new IllegalStateException(e);
                    }
                }
            }
        });
        // now create files entries
        files.forEach((path, content) -> {
            try {
                zip.putNextEntry(new ZipEntry(rootName + '/' + path));
                zip.write(content);
                zip.closeEntry();
            } catch (final IOException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    onCreate.fire(new CreateProject(request));
}
Also used : BuildGenerator(org.talend.sdk.component.starter.server.service.build.BuildGenerator) ZipOutputStream(java.util.zip.ZipOutputStream) Build(org.talend.sdk.component.starter.server.service.domain.Build) Getter(lombok.Getter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) Locale(java.util.Locale) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) CreateProject(org.talend.sdk.component.starter.server.service.event.CreateProject) Collectors.toSet(java.util.stream.Collectors.toSet) OutputStream(java.io.OutputStream) Event(javax.enterprise.event.Event) Collections.emptyMap(java.util.Collections.emptyMap) FacetGenerator(org.talend.sdk.component.starter.server.service.facet.FacetGenerator) Versions(org.talend.sdk.component.starter.server.Versions) BuildGenerator(org.talend.sdk.component.starter.server.service.build.BuildGenerator) GeneratorRegistration(org.talend.sdk.component.starter.server.service.event.GeneratorRegistration) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) ComponentGenerator(org.talend.sdk.component.starter.server.service.facet.component.ComponentGenerator) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) TreeMap(java.util.TreeMap) PostConstruct(javax.annotation.PostConstruct) TemplateRenderer(org.talend.sdk.component.starter.server.service.template.TemplateRenderer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Dependency(org.talend.sdk.component.starter.server.service.domain.Dependency) ProjectRequest(org.talend.sdk.component.starter.server.service.domain.ProjectRequest) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) CreateProject(org.talend.sdk.component.starter.server.service.event.CreateProject) Build(org.talend.sdk.component.starter.server.service.domain.Build) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) HashSet(java.util.HashSet) Dependency(org.talend.sdk.component.starter.server.service.domain.Dependency) IOException(java.io.IOException) FacetGenerator(org.talend.sdk.component.starter.server.service.facet.FacetGenerator) ZipOutputStream(java.util.zip.ZipOutputStream)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 ZipEntry (java.util.zip.ZipEntry)1