-
Notifications
You must be signed in to change notification settings - Fork 3
Add Resources API with Path-based methods #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/add-new-resources-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c1d60c
Initial plan
Copilot 47df1a9
Add new Resources API with Path-based methods
Copilot b9f0758
Address code review feedback on Resources API documentation
Copilot 495f4ad
Use LegacySupport to get Maven project basedir in Resources.getPath()…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/main/java/org/codehaus/plexus/build/resources/DefaultResources.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| Copyright (c) 2008 Sonatype, Inc. All rights reserved. | ||
|
|
||
| This program is licensed to you under the Apache License Version 2.0, | ||
| and you may not use this file except in compliance with the Apache License Version 2.0. | ||
| You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the Apache License Version 2.0 is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
| */ | ||
| package org.codehaus.plexus.build.resources; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.apache.maven.plugin.LegacySupport; | ||
| import org.apache.maven.project.MavenProject; | ||
| import org.codehaus.plexus.build.BuildContext; | ||
|
|
||
| /** | ||
| * Default implementation of the Resources interface. | ||
| * <p> | ||
| * This implementation delegates to the BuildContext for compatibility with existing | ||
| * build infrastructure. It provides a transition path from the File-based API to the | ||
| * modern Path-based API. | ||
| * </p> | ||
| */ | ||
| @Named("default") | ||
| @Singleton | ||
| public class DefaultResources implements Resources { | ||
|
|
||
| private final BuildContext buildContext; | ||
| private final LegacySupport legacySupport; | ||
|
|
||
| /** | ||
| * Creates a new DefaultResources instance. | ||
| * | ||
| * @param buildContext the BuildContext to which operations will be delegated | ||
| * @param legacySupport the LegacySupport to get the current Maven project | ||
| */ | ||
| @Inject | ||
| public DefaultResources(BuildContext buildContext, LegacySupport legacySupport) { | ||
| this.buildContext = buildContext; | ||
| this.legacySupport = legacySupport; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasDelta(Path file) { | ||
| if (file == null) { | ||
| return false; | ||
| } | ||
| return buildContext.hasDelta(file.toFile()); | ||
| } | ||
|
|
||
| @Override | ||
| public void refresh(Path file) { | ||
| if (file != null) { | ||
| buildContext.refresh(file.toFile()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public OutputStream newOutputStream(Path file) throws IOException { | ||
| if (file == null) { | ||
| throw new IllegalArgumentException("file cannot be null"); | ||
| } | ||
| return buildContext.newFileOutputStream(file.toFile()); | ||
| } | ||
|
|
||
| @Override | ||
| public OutputStream newOutputStream(Path file, boolean derived) throws IOException { | ||
| OutputStream outputStream = newOutputStream(file); | ||
| if (derived) { | ||
| // Mark the file as derived after creating the output stream. | ||
| // In the default implementation, markDerived is a no-op, so the timing doesn't matter. | ||
| // Custom implementations that track derived files should consider wrapping the stream | ||
| // to defer marking until successful close() to avoid marking files on failed writes. | ||
| markDerived(file); | ||
| } | ||
| return outputStream; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isUptodate(Path target, Path source) { | ||
| if (target == null || source == null) { | ||
| return false; | ||
| } | ||
| return buildContext.isUptodate(target.toFile(), source.toFile()); | ||
| } | ||
|
|
||
| @Override | ||
| public Path getPath(String relpath) { | ||
| if (relpath == null) { | ||
| throw new IllegalArgumentException("relpath cannot be null"); | ||
| } | ||
| // Get the basedir from the current Maven project via LegacySupport | ||
| MavenProject project = | ||
| legacySupport.getSession() != null ? legacySupport.getSession().getCurrentProject() : null; | ||
| if (project != null) { | ||
| File basedir = project.getBasedir(); | ||
| if (basedir != null) { | ||
| return basedir.toPath().resolve(relpath); | ||
| } | ||
| } | ||
| // Fallback to current working directory if project is not available | ||
| return java.nio.file.Paths.get(relpath); | ||
| } | ||
|
|
||
| @Override | ||
| public void markDerived(Path file) { | ||
| // No-op in the default implementation | ||
| // Custom implementations (e.g., IDE integrations) can override this | ||
| // to provide actual derived file tracking | ||
| } | ||
|
|
||
| @Override | ||
| public void copy(Path source, Path target) throws IOException { | ||
| if (source == null) { | ||
| throw new IllegalArgumentException("source cannot be null"); | ||
| } | ||
| if (target == null) { | ||
| throw new IllegalArgumentException("target cannot be null"); | ||
| } | ||
|
|
||
| // Only copy if the target is not up-to-date with the source | ||
| if (!isUptodate(target, source)) { | ||
| // Ensure parent directory exists | ||
| Path parentDir = target.getParent(); | ||
| if (parentDir != null && !Files.exists(parentDir)) { | ||
| Files.createDirectories(parentDir); | ||
| } | ||
|
|
||
| // Copy using Files API and newOutputStream to ensure proper change detection | ||
| try (InputStream in = Files.newInputStream(source); | ||
| OutputStream out = newOutputStream(target)) { | ||
| byte[] buffer = new byte[8192]; | ||
| int bytesRead; | ||
| while ((bytesRead = in.read(buffer)) != -1) { | ||
| out.write(buffer, 0, bytesRead); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use
LegacySupporthere to get the current maven project and return the basedir then.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use LegacySupport to get the current Maven project and resolve paths against the project basedir. Falls back to current working directory if project is not available. See commit 495f4ad.