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
040    implements JctFileManagerConfigurer {
041
042  private static final Logger log
043      = LoggerFactory.getLogger(JctFileManagerRequiredLocationsConfigurer.class);
044
045  // Locations that we have to ensure exist before the compiler is run.
046  private static final Set<StandardLocation> REQUIRED_LOCATIONS = Set.of(
047      // We have to manually create this one as javac will not attempt to access it lazily. Instead,
048      // it will just abort if it is not present. This means we cannot take advantage of the
049      // container group creating the roots as we try to access them for this specific case.
050      StandardLocation.SOURCE_OUTPUT,
051      // Annotation processors that create files will need this directory to exist if it is to
052      // work properly.
053      StandardLocation.CLASS_OUTPUT,
054      // We need to provide a header output path in case header generation is enabled at any stage.
055      // I might make this disabled by default in the future if there is too much overhead from
056      // doing this.
057      StandardLocation.NATIVE_HEADER_OUTPUT
058  );
059
060  private final Workspace workspace;
061
062  /**
063   * Initialise this configurer.
064   *
065   * @param workspace the workspace to bind to.
066   */
067  public JctFileManagerRequiredLocationsConfigurer(Workspace workspace) {
068    this.workspace = workspace;
069  }
070
071  @Override
072  public JctFileManager configure(JctFileManager fileManager) {
073    log.debug("Configuring required locations that do not yet exist");
074
075    REQUIRED_LOCATIONS
076        .stream()
077        .filter(not(fileManager::hasLocation))
078        .peek(location -> log.atTrace()
079            .setMessage("Required location {} does not exist, so will be created in the workspace")
080            .addArgument(() -> StringUtils.quoted(location.getName()))
081            .log())
082        .forEach(location -> fileManager.addPath(location, workspace.createPackage(location)));
083
084    return fileManager;
085  }
086}