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));
}
Aggregations