001/*
002 * Copyright (C) 2022 - 2024, 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.utils.StringUtils;
022import io.github.ascopes.jct.workspaces.Workspace;
023import java.util.Set;
024import javax.tools.StandardLocation;
025import org.apiguardian.api.API;
026import org.apiguardian.api.API.Status;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Configurer for a file manager that creates missing required locations to the file manager.
032 *
033 * <p>These locations will be created as empty paths in the workspace.
034 *
035 * @author Ashley Scopes
036 * @since 0.0.1
037 */
038@API(since = "0.0.1", status = Status.STABLE)
039public final class JctFileManagerRequiredLocationsConfigurer implements JctFileManagerConfigurer {
040
041  private static final Logger LOGGER
042      = LoggerFactory.getLogger(JctFileManagerRequiredLocationsConfigurer.class);
043
044  // Locations that we have to ensure exist before the compiler is run.
045  private static final Set<StandardLocation> REQUIRED_LOCATIONS = Set.of(
046      // We have to manually create this one as javac will not attempt to access it lazily. Instead,
047      // it will just abort if it is not present. This means we cannot take advantage of the
048      // container group creating the roots as we try to access them for this specific case.
049      StandardLocation.SOURCE_OUTPUT,
050      // Annotation processors that create files will need this directory to exist if it is to
051      // work properly.
052      StandardLocation.CLASS_OUTPUT,
053      // We need to provide a header output path in case header generation is enabled at any stage.
054      // I might make this disabled by default in the future if there is too much overhead from
055      // doing this.
056      StandardLocation.NATIVE_HEADER_OUTPUT
057  );
058
059  private final Workspace workspace;
060
061  /**
062   * Initialise this configurer.
063   *
064   * @param workspace the workspace to bind to.
065   */
066  public JctFileManagerRequiredLocationsConfigurer(Workspace workspace) {
067    this.workspace = workspace;
068  }
069
070  @Override
071  public JctFileManager configure(JctFileManager fileManager) {
072    LOGGER.debug("Configuring required locations that do not yet exist");
073
074    REQUIRED_LOCATIONS
075        .stream()
076        .filter(not(fileManager::hasLocation))
077        .peek(location -> LOGGER.atTrace()
078            .setMessage("Required location {} does not exist, so will be created in the workspace")
079            .addArgument(() -> StringUtils.quoted(location.getName()))
080            .log())
081        .forEach(location -> fileManager.addPath(location, workspace.createPackage(location)));
082
083    return fileManager;
084  }
085}