-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also as suggested in the review, ResourceMappers were adjusted to save only output file paths in the mapping (now renamed to registry) file, as that is all we need.
- Loading branch information
Showing
11 changed files
with
161 additions
and
145 deletions.
There are no files selected for viewing
This file contains 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 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
122 changes: 0 additions & 122 deletions
122
modules/build/src/main/scala/scala/build/ResourceMapper.scala
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
modules/build/src/main/scala/scala/build/internal/resource/BaseResourceMapper.scala
This file contains 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,47 @@ | ||
package scala.build.internal.resource | ||
|
||
trait BaseResourceMapper { | ||
private def readRegistryIfExists(registryFile: os.Path): Option[List[os.Path]] = | ||
if (os.exists(registryFile)) { | ||
val registryContent = os.read(registryFile) | ||
if (registryContent.isEmpty()) Some(List.empty) | ||
else Some( | ||
registryContent.linesIterator.filter(_.nonEmpty).map(os.Path(_)).toList | ||
) | ||
} | ||
else None | ||
|
||
private def writeRegistryFromMapping(registryPath: os.Path, mapping: Map[os.Path, os.Path]) = { | ||
val registryContent = mapping.map { case (inputPath, outputPath) => | ||
outputPath | ||
}.mkString("\n") | ||
os.write.over(registryPath, registryContent) | ||
} | ||
|
||
protected def copyResourcesToDirWithMapping( | ||
output: os.Path, | ||
registryFilePath: os.Path, | ||
newMapping: Map[os.Path, os.Path] | ||
): Unit = { | ||
|
||
val oldRegistry = readRegistryIfExists(registryFilePath).getOrElse(List.empty) | ||
val removedFiles = oldRegistry.toSet -- newMapping.values.toSet | ||
|
||
removedFiles.foreach { outputPath => | ||
// Delete only if in output path, to avoid causing any harm elsewhere | ||
if (output.startsWith(output)) | ||
os.remove(outputPath) | ||
} | ||
|
||
newMapping.toList.foreach { case (inputPath, outputPath) => | ||
os.copy( | ||
inputPath, | ||
outputPath, | ||
replaceExisting = true, | ||
createFolders = true | ||
) | ||
} | ||
|
||
writeRegistryFromMapping(registryFilePath, newMapping) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
modules/build/src/main/scala/scala/build/internal/resource/NativeResourceMapper.scala
This file contains 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,28 @@ | ||
package scala.build.internal.resource | ||
|
||
import scala.build.{Build, Inputs} | ||
|
||
object NativeResourceMapper extends BaseResourceMapper { | ||
|
||
private def scalaNativeCFileMapping(build: Build.Successful): Map[os.Path, os.Path] = | ||
build.inputs.flattened().collect { | ||
case cfile: Inputs.CFile => | ||
val inputPath = cfile.path | ||
val destPath = build.output / "scala-native" / cfile.subPath | ||
(inputPath, destPath) | ||
}.toMap | ||
|
||
private def resolveProjectCFileRegistryPath(nativeWorkDir: os.Path) = | ||
nativeWorkDir / ".native_registry" | ||
|
||
/** Copies and maps c file resources from their original path to the destination path in build | ||
* output, also caching output paths in a file. | ||
* | ||
* Remembering the mapping this way allows for the resource to be removed if the original file is | ||
* renamed/deleted. | ||
*/ | ||
def copyCFilesToScalaNativeDir(build: Build.Successful, nativeWorkDir: os.Path): Unit = { | ||
val mappingFilePath = resolveProjectCFileRegistryPath(nativeWorkDir) | ||
copyResourcesToDirWithMapping(build.output, mappingFilePath, scalaNativeCFileMapping(build)) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
modules/build/src/main/scala/scala/build/internal/resource/ResourceMapper.scala
This file contains 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,40 @@ | ||
package scala.build.internal.resource | ||
|
||
import scala.build.Build | ||
import scala.build.internal.Constants | ||
|
||
object ResourceMapper extends BaseResourceMapper { | ||
|
||
private def resourceMapping(build: Build.Successful) = { | ||
val seq = for { | ||
resourceDirPath <- build.sources.resourceDirs.filter(os.exists(_)) | ||
resourceFilePath <- os.walk(resourceDirPath).filter(os.isFile(_)) | ||
relativeResourcePath = resourceFilePath.relativeTo(resourceDirPath) | ||
// dismiss files generated by scala-cli | ||
if !relativeResourcePath.startsWith(os.rel / Constants.workspaceDirName) | ||
} yield { | ||
val inputPath = resourceFilePath | ||
val outputPath = build.output / relativeResourcePath | ||
(resourceFilePath, outputPath) | ||
} | ||
|
||
seq.toMap | ||
} | ||
|
||
private def resolveResourceRegistryPath(classDir: os.Path): os.Path = | ||
classDir / os.up / os.up / ".resource_registry" | ||
|
||
/** Copies and maps resources from their original path to the destination path in build output, | ||
* also caching output paths in a file. | ||
* | ||
* Remembering the mapping this way allows for the resource to be removed if the original file is | ||
* renamed/deleted. | ||
*/ | ||
def copyResourceToClassesDir(build: Build): Unit = build match { | ||
case b: Build.Successful => | ||
val fullFilePath = resolveResourceRegistryPath(b.output) | ||
copyResourcesToDirWithMapping(b.output, fullFilePath, resourceMapping(b)) | ||
|
||
case _ => | ||
} | ||
} |
This file contains 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 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 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 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 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