-
Notifications
You must be signed in to change notification settings - Fork 213
Implement resource mapping support (#2592) #2834
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
Open
krrish175-byte
wants to merge
2
commits into
scalacenter:main
Choose a base branch
from
krrish175-byte:feature/resource-mapping
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package bloop.io | ||
|
|
||
| import java.nio.file.Files | ||
| import java.nio.file.StandardCopyOption | ||
|
|
||
| import scala.util.control.NonFatal | ||
|
|
||
| import bloop.logging.DebugFilter | ||
| import bloop.logging.Logger | ||
| import bloop.task.Task | ||
|
|
||
| /** | ||
| * Utility for handling resource file mappings. | ||
| * | ||
| * Resource mappings allow files to be copied from source locations to custom | ||
| * target paths, similar to SBT's "mappings" field. | ||
| */ | ||
| object ResourceMapper { | ||
| private implicit val filter: DebugFilter.All.type = DebugFilter.All | ||
|
|
||
| /** | ||
| * Copy mapped resources to the target directory. | ||
| * | ||
| * @param mappings List of (source, targetRelativePath) tuples | ||
| * @param classesDir Base directory where resources should be copied | ||
| * @param logger Logger for debug/error messages | ||
| * @return Task that completes when all resources are copied | ||
| */ | ||
| def copyMappedResources( | ||
| mappings: List[(AbsolutePath, String)], | ||
| classesDir: AbsolutePath, | ||
| logger: Logger | ||
| ): Task[Unit] = { | ||
| val tasks = mappings.map { | ||
| case (source, targetRelPath) => | ||
| Task { | ||
| val target = classesDir.resolve(targetRelPath) | ||
| if (!target.getParent.exists) { | ||
| Files.createDirectories(target.getParent.underlying) | ||
| } | ||
|
|
||
| if (source.isDirectory) { | ||
| import java.nio.file.FileVisitResult | ||
| import java.nio.file.Path | ||
| import java.nio.file.SimpleFileVisitor | ||
| import java.nio.file.attribute.BasicFileAttributes | ||
|
|
||
| val sourcePath = source.underlying | ||
| val targetPath = target.underlying | ||
| Files.walkFileTree( | ||
| sourcePath, | ||
| new SimpleFileVisitor[Path] { | ||
| override def visitFile( | ||
| file: Path, | ||
| attrs: BasicFileAttributes | ||
| ): FileVisitResult = { | ||
| val relPath = sourcePath.relativize(file) | ||
| val targetFile = targetPath.resolve(relPath) | ||
| Files.createDirectories(targetFile.getParent) | ||
| Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING) | ||
| FileVisitResult.CONTINUE | ||
| } | ||
| } | ||
| ) | ||
| } else if (source.exists) { | ||
| Files.copy(source.underlying, target.underlying, StandardCopyOption.REPLACE_EXISTING) | ||
| } else { | ||
| logger.warn(s"Source file $source does not exist, skipping mapping to $targetRelPath") | ||
| } | ||
| () | ||
| } | ||
| } | ||
| Task.gatherUnordered(tasks).map(_ => ()) | ||
| } | ||
|
|
||
| /** | ||
| * Check if any mapped resources have changed since the given timestamp. | ||
| * | ||
| * Note: This is currently not used. Incremental compilation change detection | ||
| * for resource mappings would require integration with Zinc's analysis. | ||
| * For now, mapped resources are always copied during compilation. | ||
| * | ||
| * @param mappings List of (source, targetRelativePath) tuples | ||
| * @param lastModified Timestamp to compare against | ||
| * @return true if any source file is newer than lastModified | ||
| */ | ||
| private[bloop] def hasMappingsChanged( | ||
| mappings: List[(AbsolutePath, String)], | ||
| lastModified: Long | ||
| ): Boolean = { | ||
| import java.nio.file.Files | ||
| mappings.exists { | ||
| case (source, _) => | ||
| if (source.isDirectory) { | ||
| hasDirectoryChanged(source, lastModified) | ||
| } else { | ||
| source.exists && Files.getLastModifiedTime(source.underlying).toMillis > lastModified | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def hasDirectoryChanged(dir: AbsolutePath, lastModified: Long): Boolean = { | ||
| if (dir.exists) { | ||
| val stream = Files.walk(dir.underlying) | ||
| try { | ||
| stream.anyMatch(path => Files.getLastModifiedTime(path).toMillis > lastModified) | ||
| } finally { | ||
| stream.close() | ||
| } | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
| } | ||
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
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
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.