Skip to content

Commit

Permalink
Asset Include / Exclude, needs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Aug 31, 2023
1 parent a587b1b commit e11c916
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
38 changes: 32 additions & 6 deletions indigo-plugin/indigo-plugin/src/indigoplugin/IndigoOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ final case class IndigoOptions(
/** Sets the asset directory path */
def withAssetDirectory(path: String): IndigoOptions =
this.copy(assets = assets.withAssetDirectory(path))
def withAssetDirectory(path: os.Path): IndigoOptions =
def withAssetDirectory(path: os.RelPath): IndigoOptions =
this.copy(assets = assets.withAssetDirectory(path))

def includeAssets(rules: os.RelPath => Boolean*): IndigoOptions =
this.copy(assets = assets.includeRules(rules.toList))

def excludeAssets(rules: os.RelPath => Boolean*): IndigoOptions =
this.copy(assets = assets.excludeRules(rules.toList))

/** Set the window start width */
def withWindowWidth(value: Int): IndigoOptions =
this.copy(metadata = metadata.withWindowWidth(value))
Expand Down Expand Up @@ -254,26 +260,46 @@ object IndigoElectronOptions {
* Project relative path to a directory that contains all of the assets the game needs to load. Default './assets'.
*/
final case class IndigoAssets(
gameAssetsDirectory: os.Path
gameAssetsDirectory: os.RelPath,
include: PartialFunction[os.RelPath, Boolean],
exclude: PartialFunction[os.RelPath, Boolean]
) {

/** Sets the asset directory path */
def withAssetDirectory(path: String): IndigoAssets =
this.copy(
gameAssetsDirectory =
if (path.startsWith("/")) os.Path(path)
else os.RelPath(path).resolveFrom(os.pwd)
if (path.startsWith("/")) os.Path(path).relativeTo(os.pwd)
else os.RelPath(path)
)
def withAssetDirectory(path: os.Path): IndigoAssets =
def withAssetDirectory(path: os.RelPath): IndigoAssets =
this.copy(gameAssetsDirectory = path)

def withInclude(p: PartialFunction[os.RelPath, Boolean]): IndigoAssets =
this.copy(include = p)

def includeRules(rules: List[os.RelPath => Boolean]): IndigoAssets =
withInclude { case path =>
rules.find(_(path)).map(_(path)).getOrElse(false)
}

def withExclude(p: PartialFunction[os.RelPath, Boolean]): IndigoAssets =
this.copy(exclude = p)

def excludeRules(rules: List[os.RelPath => Boolean]): IndigoAssets =
withExclude { case path =>
rules.find(_(path)).map(_(path)).getOrElse(false)
}

}

object IndigoAssets {

/** Default settings for an Indigo game's asset management */
val defaults: IndigoAssets =
IndigoAssets(
gameAssetsDirectory = os.pwd / "assets"
gameAssetsDirectory = os.RelPath("assets"),
_ => false,
_ => false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import indigoplugin.templates.SupportScriptTemplate
import indigoplugin.datatypes.DirectoryStructure
import indigoplugin.utils.Utils
import indigoplugin.IndigoOptions
import java.nio.file.CopyOption
import java.nio.file.LinkOption
import java.nio.file.StandardCopyOption
import indigoplugin.IndigoAssets

object IndigoBuild {

Expand All @@ -22,7 +26,7 @@ object IndigoBuild {
IndigoBuild.copyScript(scriptPathBase, directoryStructure.artefacts, scriptName)

// copy assets into folder
IndigoBuild.copyAssets(options.assets.gameAssetsDirectory, directoryStructure.assets)
IndigoBuild.copyAssets(options.assets, directoryStructure.assets)

// copy built js source map file into scripts dir
IndigoBuild.copyScript(
Expand Down Expand Up @@ -75,15 +79,23 @@ object IndigoBuild {
}

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def copyAssets(gameAssetsDirectoryPath: Path, destAssetsFolder: Path): Unit =
if (!os.exists(gameAssetsDirectoryPath))
throw new Exception("Supplied game assets path does not exist: " + gameAssetsDirectoryPath.toString())
else if (!os.isDir(gameAssetsDirectoryPath))
def copyAssets(indigoAssets: IndigoAssets, destAssetsFolder: Path): Unit = {
val absPath = indigoAssets.gameAssetsDirectory.resolveFrom(os.pwd)

if (!os.exists(absPath))
throw new Exception("Supplied game assets path does not exist: " + indigoAssets.gameAssetsDirectory.toString())
else if (!os.isDir(absPath))
throw new Exception("Supplied game assets path was not a directory")
else {
println("Copying assets...")
os.copy(gameAssetsDirectoryPath, destAssetsFolder, true, true, true, false, false)
copyAllWithFilters(
absPath,
destAssetsFolder,
indigoAssets.include.orElse(_ => false),
indigoAssets.exclude.orElse(_ => false)
)
}
}

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def copyScript(scriptPathBase: Path, destScriptsFolder: Path, fileName: String): Unit = {
Expand All @@ -104,4 +116,47 @@ object IndigoBuild {
outFile
}

/** This is taken and modified from the os-lib code. */
def copyAllWithFilters(
from: Path,
to: Path,
include: RelPath => Boolean,
exclude: RelPath => Boolean
): Unit = {
makeDir.all(to / up)

require(
!to.startsWith(from),
s"Can't copy a directory into itself: $to is inside $from"
)

def copyOne(p: Path): java.nio.file.Path = {
val rel = p.relativeTo(from)
val target = to / p.relativeTo(from)

def doCopy(): java.nio.file.Path =
java.nio.file.Files.copy(
p.wrapped,
target.wrapped,
LinkOption.NOFOLLOW_LINKS,
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES
)

if (include(rel))
// Specifically include, even if in an excluded location
doCopy()
else if (exclude(rel))
// Specifically excluded, do nothing
target.wrapped
else
// Otherwise, no specific instruction so assume copy.
doCopy()
}

copyOne(from)

if (stat(from, followLinks = true).isDir) walk(from).map(copyOne)
}

}

0 comments on commit e11c916

Please sign in to comment.