001/* 002 * Copyright (C) 2022 - 2023, the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.github.ascopes.jct.filemanagers.config; 017 018import static java.util.function.Predicate.not; 019 020import io.github.ascopes.jct.filemanagers.JctFileManager; 021import io.github.ascopes.jct.workspaces.Workspace; 022import java.util.Set; 023import javax.tools.StandardLocation; 024import org.apiguardian.api.API; 025import org.apiguardian.api.API.Status; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029/** 030 * Configurer for a file manager that creates missing required locations to the file manager. 031 * 032 * <p>These locations will be created as empty paths in the workspace. 033 * 034 * @author Ashley Scopes 035 * @since 0.0.1 (0.0.1-M7) 036 */ 037@API(since = "0.0.1", status = Status.STABLE) 038public final class JctFileManagerRequiredLocationsConfigurer implements JctFileManagerConfigurer { 039 040 private static final Logger LOGGER 041 = LoggerFactory.getLogger(JctFileManagerRequiredLocationsConfigurer.class); 042 043 // Locations that we have to ensure exist before the compiler is run. 044 private static final Set<StandardLocation> REQUIRED_LOCATIONS = Set.of( 045 // We have to manually create this one as javac will not attempt to access it lazily. Instead, 046 // it will just abort if it is not present. This means we cannot take advantage of the 047 // container group creating the roots as we try to access them for this specific case. 048 StandardLocation.SOURCE_OUTPUT, 049 // Annotation processors that create files will need this directory to exist if it is to 050 // work properly. 051 StandardLocation.CLASS_OUTPUT, 052 // We need to provide a header output path in case header generation is enabled at any stage. 053 // I might make this disabled by default in the future if there is too much overhead from 054 // doing this by default. 055 StandardLocation.NATIVE_HEADER_OUTPUT 056 ); 057 058 private final Workspace workspace; 059 060 /** 061 * Initialise this configurer. 062 * 063 * @param workspace the workspace to bind to. 064 */ 065 public JctFileManagerRequiredLocationsConfigurer(Workspace workspace) { 066 this.workspace = workspace; 067 } 068 069 @Override 070 public JctFileManager configure(JctFileManager fileManager) { 071 LOGGER.debug("Configuring required locations that do not yet exist"); 072 073 REQUIRED_LOCATIONS 074 .stream() 075 .filter(not(fileManager::hasLocation)) 076 .forEach(location -> { 077 LOGGER.trace( 078 "Required location {} does not exist, so will be created in the workspace", 079 location 080 ); 081 fileManager.addPath(location, workspace.createPackage(location)); 082 }); 083 return fileManager; 084 } 085}