From 12ddca85d9ac02877f34cb28c1f25cd0008652b6 Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Tue, 28 May 2024 19:33:30 -0400 Subject: [PATCH 1/2] Implement `AssetHelper.deleteStaleAssets()`. --- src/lime/tools/AssetHelper.hx | 19 +++++++++++++++++++ src/lime/tools/PlatformTarget.hx | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/lime/tools/AssetHelper.hx b/src/lime/tools/AssetHelper.hx index 2f9ef857c..a109a4d0d 100644 --- a/src/lime/tools/AssetHelper.hx +++ b/src/lime/tools/AssetHelper.hx @@ -191,6 +191,25 @@ class AssetHelper return manifests; } + public static function deleteStaleAssets(project:HXProject, targetDirectory:String):Void + { + var newAssets = [for (asset in project.assets) asset.targetPath]; + + var recordFile:String = targetDirectory + "/.assets"; + if (FileSystem.exists(recordFile)) + { + for (oldAsset in File.getContent(recordFile).split("\n")) + { + if (oldAsset.length > 0 && newAssets.indexOf(oldAsset) < 0) + { + System.deleteFile(targetDirectory + "/bin/" + oldAsset); + } + } + } + + File.saveContent(recordFile, newAssets.join("\n")); + } + private static function getAssetData(project:HXProject, pathGroups:Map>, libraries:Map, library:String, asset:Asset):Dynamic { diff --git a/src/lime/tools/PlatformTarget.hx b/src/lime/tools/PlatformTarget.hx index 87cf6e1d0..2feba96b9 100644 --- a/src/lime/tools/PlatformTarget.hx +++ b/src/lime/tools/PlatformTarget.hx @@ -103,6 +103,8 @@ class PlatformTarget // AssetHelper.processLibraries (project, targetDirectory); // #end update(); + + AssetHelper.deleteStaleAssets(project, targetDirectory); } if (command == "build" || command == "test") From 224c0a0e9313cd7b1bb20428bc880a887dfcd2e0 Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Wed, 29 May 2024 20:55:09 -0400 Subject: [PATCH 2/2] Delete stale dependencies in addition to stale assets. --- src/lime/tools/AssetHelper.hx | 19 ------ src/lime/tools/PlatformTarget.hx | 91 ++++++++++++++++++++++++-- tools/platforms/AndroidPlatform.hx | 8 +-- tools/platforms/HTML5Platform.hx | 2 +- tools/platforms/IOSPlatform.hx | 2 +- tools/platforms/TVOSPlatform.hx | 2 +- tools/platforms/WebAssemblyPlatform.hx | 2 +- tools/platforms/WindowsPlatform.hx | 4 +- 8 files changed, 97 insertions(+), 33 deletions(-) diff --git a/src/lime/tools/AssetHelper.hx b/src/lime/tools/AssetHelper.hx index a109a4d0d..2f9ef857c 100644 --- a/src/lime/tools/AssetHelper.hx +++ b/src/lime/tools/AssetHelper.hx @@ -191,25 +191,6 @@ class AssetHelper return manifests; } - public static function deleteStaleAssets(project:HXProject, targetDirectory:String):Void - { - var newAssets = [for (asset in project.assets) asset.targetPath]; - - var recordFile:String = targetDirectory + "/.assets"; - if (FileSystem.exists(recordFile)) - { - for (oldAsset in File.getContent(recordFile).split("\n")) - { - if (oldAsset.length > 0 && newAssets.indexOf(oldAsset) < 0) - { - System.deleteFile(targetDirectory + "/bin/" + oldAsset); - } - } - } - - File.saveContent(recordFile, newAssets.join("\n")); - } - private static function getAssetData(project:HXProject, pathGroups:Map>, libraries:Map, library:String, asset:Asset):Dynamic { diff --git a/src/lime/tools/PlatformTarget.hx b/src/lime/tools/PlatformTarget.hx index 2feba96b9..fe4ea957d 100644 --- a/src/lime/tools/PlatformTarget.hx +++ b/src/lime/tools/PlatformTarget.hx @@ -4,6 +4,8 @@ import haxe.rtti.Meta; import hxp.*; import lime.tools.AssetHelper; import lime.tools.CommandHelper; +import sys.FileSystem; +import sys.io.File; class PlatformTarget { @@ -99,12 +101,12 @@ class PlatformTarget if (command == "update" || command == "build" || command == "test") { logCommand("update"); - // #if lime - // AssetHelper.processLibraries (project, targetDirectory); - // #end + + _touchedFiles = []; update(); - AssetHelper.deleteStaleAssets(project, targetDirectory); + deleteStaleFiles(_touchedFiles); + _touchedFiles = null; } if (command == "build" || command == "test") @@ -170,4 +172,85 @@ class PlatformTarget @ignore public function update():Void {} @ignore public function watch():Void {} + + // Functions to track and delete stale files + + /** + Files that were copied into the output directory due to something in + project.xml, but which might not be included next time. + + `PlatformTarget` will handle assets and templates, but subclasses are + responsible for adding any other files they copy (e.g., dependencies). + **/ + private var _touchedFiles:Array = null; + + /** + Calls `System.copyIfNewer()` with the given arguments, then records the + file in `_touchedFiles`. See `_touchedFiles` for information about what + needs to be recorded. + **/ + private function copyIfNewer(source:String, destination:String):Void + { + System.copyIfNewer(source, destination); + + if (_touchedFiles != null) + { + _touchedFiles.push(destination); + } + } + + private function deleteStaleFiles(touchedFiles:Array):Void + { + if (project.defines.exists("lime-ignore-stale-files")) return; + + for (asset in project.assets) + { + touchedFiles.push(targetDirectory + "/bin/" + asset.targetPath); + } + + var record:String = targetDirectory + "/.files"; + if (FileSystem.exists(record)) + { + for (oldFile in File.getContent(record).split("\n")) + { + if (oldFile.length > 0 && touchedFiles.indexOf(oldFile) < 0) + { + System.deleteFile(oldFile); + } + } + } + + File.saveContent(record, touchedFiles.join("\n")); + } + + /** + Calls `System.recursiveCopy()` with the given arguments, then records + the files in `_touchedFiles`. See `_touchedFiles` for information about + what needs to be recorded. + **/ + private function recursiveCopy(source:String, destination:String, context:Dynamic = null, process:Bool = true):Void + { + System.recursiveCopy(source, destination, context, process); + + if (_touchedFiles == null || !FileSystem.exists(source)) return; + + function recurse(source:String, destination:String):Void + { + for (file in FileSystem.readDirectory(source)) + { + if (file.charAt(0) == ".") continue; + + if (FileSystem.isDirectory(source + "/" + file)) + { + recurse(source + "/" + file, destination + "/" + file); + } + else + { + _touchedFiles.push(destination + "/" + file); + } + } + } + + recurse(source, destination); + } } diff --git a/tools/platforms/AndroidPlatform.hx b/tools/platforms/AndroidPlatform.hx index 56fa6aeae..1300cfd78 100644 --- a/tools/platforms/AndroidPlatform.hx +++ b/tools/platforms/AndroidPlatform.hx @@ -561,17 +561,17 @@ class AndroidPlatform extends PlatformTarget { if (FileSystem.isDirectory(javaPath)) { - System.recursiveCopy(javaPath, sourceSet + "/java", context, true); + recursiveCopy(javaPath, sourceSet + "/java", context, true); } else { if (Path.extension(javaPath) == "jar") { - System.copyIfNewer(javaPath, destination + "/app/libs/" + Path.withoutDirectory(javaPath)); + copyIfNewer(javaPath, destination + "/app/libs/" + Path.withoutDirectory(javaPath)); } else { - System.copyIfNewer(javaPath, sourceSet + "/java/" + Path.withoutDirectory(javaPath)); + copyIfNewer(javaPath, sourceSet + "/java/" + Path.withoutDirectory(javaPath)); } } } @@ -584,7 +584,7 @@ class AndroidPlatform extends PlatformTarget for (library in cast(context.ANDROID_LIBRARY_PROJECTS, Array)) { - System.recursiveCopy(library.source, destination + "/deps/" + library.name, context, true); + recursiveCopy(library.source, destination + "/deps/" + library.name, context, true); } ProjectHelper.recursiveSmartCopyTemplate(project, "android/template", destination, context); diff --git a/tools/platforms/HTML5Platform.hx b/tools/platforms/HTML5Platform.hx index f0d5e22e1..0531b29aa 100644 --- a/tools/platforms/HTML5Platform.hx +++ b/tools/platforms/HTML5Platform.hx @@ -429,7 +429,7 @@ class HTML5Platform extends PlatformTarget var name = Path.withoutDirectory(dependency.path); context.linkedLibraries.push("./" + dependencyPath + "/" + name); - System.copyIfNewer(dependency.path, Path.combine(destination, Path.combine(dependencyPath, name))); + copyIfNewer(dependency.path, Path.combine(destination, Path.combine(dependencyPath, name))); } } } diff --git a/tools/platforms/IOSPlatform.hx b/tools/platforms/IOSPlatform.hx index 9d5fa3346..25ec55432 100644 --- a/tools/platforms/IOSPlatform.hx +++ b/tools/platforms/IOSPlatform.hx @@ -851,7 +851,7 @@ class IOSPlatform extends PlatformTarget fileName = "lib" + fileName; } - System.copyIfNewer(dependency.path, projectDirectory + "/lib/" + arch + "/" + fileName); + copyIfNewer(dependency.path, projectDirectory + "/lib/" + arch + "/" + fileName); } } } diff --git a/tools/platforms/TVOSPlatform.hx b/tools/platforms/TVOSPlatform.hx index 54aae23b7..cd28df0e3 100644 --- a/tools/platforms/TVOSPlatform.hx +++ b/tools/platforms/TVOSPlatform.hx @@ -628,7 +628,7 @@ class TVOSPlatform extends PlatformTarget fileName = "lib" + fileName; } - System.copyIfNewer(dependency.path, projectDirectory + "/lib/" + arch + "/" + fileName); + copyIfNewer(dependency.path, projectDirectory + "/lib/" + arch + "/" + fileName); } } } diff --git a/tools/platforms/WebAssemblyPlatform.hx b/tools/platforms/WebAssemblyPlatform.hx index 69fbdb2f2..da18b3d3f 100644 --- a/tools/platforms/WebAssemblyPlatform.hx +++ b/tools/platforms/WebAssemblyPlatform.hx @@ -466,7 +466,7 @@ class WebAssemblyPlatform extends PlatformTarget var name = Path.withoutDirectory(dependency.path); context.linkedLibraries.push("./" + dependencyPath + "/" + name); - System.copyIfNewer(dependency.path, Path.combine(destination, Path.combine(dependencyPath, name))); + copyIfNewer(dependency.path, Path.combine(destination, Path.combine(dependencyPath, name))); } } } diff --git a/tools/platforms/WindowsPlatform.hx b/tools/platforms/WindowsPlatform.hx index 018194bb0..74a1a579e 100644 --- a/tools/platforms/WindowsPlatform.hx +++ b/tools/platforms/WindowsPlatform.hx @@ -286,7 +286,7 @@ class WindowsPlatform extends PlatformTarget if (StringTools.endsWith(dependency.path, ".dll")) { var fileName = Path.withoutDirectory(dependency.path); - System.copyIfNewer(dependency.path, applicationDirectory + "/" + fileName); + copyIfNewer(dependency.path, applicationDirectory + "/" + fileName); } } @@ -1037,7 +1037,7 @@ class WindowsPlatform extends PlatformTarget var name = Path.withoutDirectory(dependency.path); context.linkedLibraries.push("./js/lib/" + name); - System.copyIfNewer(dependency.path, Path.combine(destination, Path.combine("js/lib", name))); + copyIfNewer(dependency.path, Path.combine(destination, Path.combine("js/lib", name))); } }