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