Search in sources :

Example 1 with Dependency

use of org.talend.sdk.component.starter.server.service.domain.Dependency 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)

Example 2 with Dependency

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

the class WADLFacet method register.

void register(@Observes final GeneratorRegistration init) {
    try (final BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("generator/facet/wadl/specification.xml")))) {
        specification = reader.lines().collect(joining("\n"));
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    dependencies = singleton(new Dependency("org.apache.cxf", "cxf-rt-rs-client", CXF, "compile"));
    init.registerFacetType(this);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Dependency(org.talend.sdk.component.starter.server.service.domain.Dependency)

Example 3 with Dependency

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

the class GradleBuildGenerator method createBuild.

@Override
public Build createBuild(final ProjectRequest.BuildConfiguration buildConfiguration, final String packageBase, final Collection<Dependency> dependencies, final Collection<String> facets) {
    final Set<String> buildDependencies = new TreeSet<>();
    final List<String> configurations = new ArrayList<>();
    final Set<String> plugins = new TreeSet<>();
    final List<String> tasks = new ArrayList<>();
    final Set<String> imports = new TreeSet<>();
    final Set<String> javaMainSourceSets = new TreeSet<>();
    if (facets.contains(WADLFacet.Constants.NAME)) {
        buildDependencies.add("org.apache.cxf:cxf-tools-wadlto-jaxrs:" + CXF);
        imports.add("org.apache.cxf.tools.common.ToolContext");
        imports.add("org.apache.cxf.tools.wadlto.WADLToJava");
        tasks.add("def wadlGeneratedFolder = \"$buildDir/generated-sources/cxf\"\n" + "task generateWadlClient {\n" + "  def wadl = \"$projectDir/src/main/resources/wadl/client.xml\"\n" + "\n" + "  inputs.file(wadl)\n" + "  outputs.dir(wadlGeneratedFolder)\n" + "\n" + "  doLast {\n" + "    new File(wadlGeneratedFolder).mkdirs()\n" + "\n" + "    new WADLToJava([\n" + "      \"-d\", wadlGeneratedFolder,\n" + "      \"-p\", \"com.application.client.wadl\",\n" + "      wadl\n" + "    ] as String[]).run(new ToolContext())\n" + "  }\n" + "}");
        javaMainSourceSets.add("srcDir wadlGeneratedFolder");
        javaMainSourceSets.add("project.tasks.compileJava.dependsOn project.tasks.generateWadlClient");
    }
    final GradleBuild model = new GradleBuild(Versions.KIT, buildConfiguration, dependencies.stream().map(d -> "test".equals(d.getScope()) ? new Dependency(d, "testCompile") : d).map(d -> "runtime".equals(d.getScope()) ? new Dependency(d, "compile") : d).collect(toList()), buildDependencies, configurations, plugins, tasks, imports, javaMainSourceSets);
    return new Build(buildConfiguration.getArtifact(), buildConfiguration.getGroup(), buildConfiguration.getVersion(), "src/main/java", "src/test/java", "src/main/resources", "src/test/resources", "src/main/webapp", "build.gradle", tpl.render("generator/gradle/build.mustache", model), "build", generateWrapperFiles());
}
Also used : FacetGenerator(org.talend.sdk.component.starter.server.service.facet.FacetGenerator) Build(org.talend.sdk.component.starter.server.service.domain.Build) Versions(org.talend.sdk.component.starter.server.Versions) GeneratorRegistration(org.talend.sdk.component.starter.server.service.event.GeneratorRegistration) WADLFacet(org.talend.sdk.component.starter.server.service.facet.wadl.WADLFacet) Collection(java.util.Collection) Set(java.util.Set) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CXF(org.talend.sdk.component.starter.server.Versions.CXF) Inject(javax.inject.Inject) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Resources(org.talend.sdk.component.starter.server.service.Resources) Arrays.asList(java.util.Arrays.asList) Observes(javax.enterprise.event.Observes) TemplateRenderer(org.talend.sdk.component.starter.server.service.template.TemplateRenderer) Data(lombok.Data) 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) TreeSet(java.util.TreeSet) Build(org.talend.sdk.component.starter.server.service.domain.Build) ArrayList(java.util.ArrayList) Dependency(org.talend.sdk.component.starter.server.service.domain.Dependency)

Aggregations

Dependency (org.talend.sdk.component.starter.server.service.domain.Dependency)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2 Collectors.toList (java.util.stream.Collectors.toList)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Inject (javax.inject.Inject)2 Versions (org.talend.sdk.component.starter.server.Versions)2 Build (org.talend.sdk.component.starter.server.service.domain.Build)2 ProjectRequest (org.talend.sdk.component.starter.server.service.domain.ProjectRequest)2 GeneratorRegistration (org.talend.sdk.component.starter.server.service.event.GeneratorRegistration)2 FacetGenerator (org.talend.sdk.component.starter.server.service.facet.FacetGenerator)2 TemplateRenderer (org.talend.sdk.component.starter.server.service.template.TemplateRenderer)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1