class ProjectGenerationInvoker<R extends ProjectRequest> {

	private final ApplicationContext parentApplicationContext;

	private final ApplicationEventPublisher eventPublisher;

	private final ProjectRequestToDescriptionConverter<R> requestConverter;

	private final ProjectAssetGenerator<Path> projectAssetGenerator = new DefaultProjectAssetGenerator();

	private final transient Map<Path, List<Path>> temporaryFiles = new ConcurrentHashMap<>();

	public ProjectGenerationInvoker(ApplicationContext parentApplicationContext,
			ProjectRequestToDescriptionConverter<R> requestConverter) {
		this(parentApplicationContext, parentApplicationContext, requestConverter);
	}

	protected ProjectGenerationInvoker(ApplicationContext parentApplicationContext,
			ApplicationEventPublisher eventPublisher, ProjectRequestToDescriptionConverter<R> requestConverter) {
		this.parentApplicationContext = parentApplicationContext;
		this.eventPublisher = eventPublisher;
		this.requestConverter = requestConverter;
	}

	/**
	 * Invokes the project generation API that generates the entire project structure for
	 * the specified {@link ProjectRequest}.
	 * @param request the project request
	 * @return the {@link ProjectGenerationResult}
	 */
	public ProjectGenerationResult invokeProjectStructureGeneration(R request) {
		InitializrMetadata metadata = this.parentApplicationContext.getBean(InitializrMetadataProvider.class).get();
		try {
			ProjectDescription description = this.requestConverter.convert(request, metadata);
			ProjectGenerator projectGenerator = new ProjectGenerator((
					projectGenerationContext) -> customizeProjectGenerationContext(projectGenerationContext, metadata));
			ProjectGenerationResult result = projectGenerator.generate(description,
					generateProject(description, request));
			addTempFile(result.getRootDirectory(), result.getRootDirectory());
			return result;
		}
		catch (ProjectGenerationException ex) {
			publishProjectFailedEvent(request, metadata, ex);
			throw ex;
		}
	}

	private ProjectAssetGenerator<ProjectGenerationResult> generateProject(ProjectDescription description, R request) {
		return (context) -> {
			Path projectDir = getProjectAssetGenerator(description).generate(context);
			publishProjectGeneratedEvent(request, context);
			return new ProjectGenerationResult(context.getBean(ProjectDescription.class), projectDir);
		};
	}

	/**
	 * Return the {@link ProjectAssetGenerator} to use to generate the project structure
	 * for the specified {@link ProjectDescription}.
	 * @param description the project description
	 * @return an asset generator for the specified request
	 */
	protected ProjectAssetGenerator<Path> getProjectAssetGenerator(ProjectDescription description) {
		return this.projectAssetGenerator;
	}

	/**
	 * Invokes the project generation API that knows how to just write the build file.
	 * Returns a directory containing the project for the specified
	 * {@link ProjectRequest}.
	 * @param request the project request
	 * @return the generated build content
	 */
	public byte[] invokeBuildGeneration(R request) {
		InitializrMetadata metadata = this.parentApplicationContext.getBean(InitializrMetadataProvider.class).get();
		try {
			ProjectDescription description = this.requestConverter.convert(request, metadata);
			ProjectGenerator projectGenerator = new ProjectGenerator((
					projectGenerationContext) -> customizeProjectGenerationContext(projectGenerationContext, metadata));
			return projectGenerator.generate(description, generateBuild(request));
		}
		catch (ProjectGenerationException ex) {
			publishProjectFailedEvent(request, metadata, ex);
			throw ex;
		}
	}

	private ProjectAssetGenerator<byte[]> generateBuild(R request) {
		return (context) -> {
			byte[] content = generateBuild(context);
			publishProjectGeneratedEvent(request, context);
			return content;
		};
	}

	/**
	 * Create a file in the same directory as the given directory using the directory name
	 * and extension.
	 * @param dir the directory used to determine the path and name of the new file
	 * @param extension the extension to use for the new file
	 * @return the newly created file
	 */
	public Path createDistributionFile(Path dir, String extension) {
		Path download = dir.resolveSibling(dir.getFileName() + extension);
		addTempFile(dir, download);
		return download;
	}

	private void addTempFile(Path group, Path file) {
		this.temporaryFiles.compute(group, (path, paths) -> {
			List<Path> newPaths = (paths != null) ? paths : new ArrayList<>();
			newPaths.add(file);
			return newPaths;
		});
	}

	/**
	 * Clean all the temporary files that are related to this root directory.
	 * @param dir the directory to clean
	 * @see #createDistributionFile
	 */
	public void cleanTempFiles(Path dir) {
		List<Path> tempFiles = this.temporaryFiles.remove(dir);
		if (!tempFiles.isEmpty()) {
			tempFiles.forEach((path) -> {
				try {
					FileSystemUtils.deleteRecursively(path);
				}
				catch (IOException ex) {
					// Continue
				}
			});
		}
	}

	private byte[] generateBuild(ProjectGenerationContext context) throws IOException {<FILL_FUNCTION_BODY>}

	private void customizeProjectGenerationContext(AnnotationConfigApplicationContext context,
			InitializrMetadata metadata) {
		context.setParent(this.parentApplicationContext);
		context.registerBean(InitializrMetadata.class, () -> metadata);
		context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata,
				context.getBean(ProjectDescription.class).getPlatformVersion()));
		context.registerBean(MetadataProjectDescriptionCustomizer.class,
				() -> new MetadataProjectDescriptionCustomizer(metadata));
	}

	private void publishProjectGeneratedEvent(R request, ProjectGenerationContext context) {
		InitializrMetadata metadata = context.getBean(InitializrMetadata.class);
		ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, metadata);
		this.eventPublisher.publishEvent(event);
	}

	private void publishProjectFailedEvent(R request, InitializrMetadata metadata, Exception cause) {
		ProjectFailedEvent event = new ProjectFailedEvent(request, metadata, cause);
		this.eventPublisher.publishEvent(event);
	}

}

		ProjectDescription description = context.getBean(ProjectDescription.class);
		StringWriter out = new StringWriter();
		BuildWriter buildWriter = context.getBeanProvider(BuildWriter.class).getIfAvailable();
		if (buildWriter != null) {
			buildWriter.writeBuild(out);
			return out.toString().getBytes();
		}
		else {
			throw new IllegalStateException("No BuildWriter implementation found for " + description.getLanguage());
		}
	
<no_super_class>