Skip to content
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

Allow file system operations to be tracked for safe deletion of untouched files #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/hxp/System.hx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class System
private static var _hostPlatform:HostPlatform;
private static var _isText:Map<String, Bool>;
private static var _processorCores:Int = 0;
private static var _unknownFileState:Bool = false;
private static var _untouchedFiles:Map<String, Bool>;

private static function __init__():Void
{
Expand Down Expand Up @@ -220,6 +222,8 @@ class System

if (textFile)
{
markFileAsTouched(destination);

// Log.info ("", " - \x1b[1mProcessing template file:\x1b[0m " + source + " \x1b[3;37m->\x1b[0m " + destination);

var fileContents:String = File.getContent(source);
Expand Down Expand Up @@ -276,6 +280,8 @@ class System
{
// allFiles.push (destination);

markFileAsTouched(destination);

if (!isNewer(source, destination))
{
return;
Expand Down Expand Up @@ -306,6 +312,25 @@ class System
}
}

public static function deleteUntouchedFiles():Void
{
if (_unknownFileState)
{
_unknownFileState = false;
_untouchedFiles = null;
return;
}

var paths = [for (key in _untouchedFiles.keys()) key];
paths.sort((a, b) -> a < b ? -1 : a > b ? 1 : 0);
for (path in paths)
{
Log.info("", " - \x1b[1mDeleting file:\x1b[0m " + path);
FileSystem.deleteFile(path);
}
_untouchedFiles = null;
}

public static function findTemplate(templatePaths:Array<String>, path:String, warnIfNotFound:Bool = true):String
{
var matches = findTemplates(templatePaths, path, warnIfNotFound);
Expand Down Expand Up @@ -351,6 +376,8 @@ class System

public static function deleteFile(path:String)
{
markFileAsTouched(path);

try
{
if (FileSystem.exists(path) && !FileSystem.isDirectory(path))
Expand Down Expand Up @@ -551,6 +578,8 @@ class System

public static function linkFile(source:String, destination:String, symbolic:Bool = true, overwrite:Bool = false)
{
markFileAsTouched(destination);

if (!isNewer(source, destination))
{
return;
Expand Down Expand Up @@ -592,6 +621,58 @@ class System
mkdir(directory);
}

public static function markAllFilesInFolderAsUntouched(path:String, pathsToIgnore:Array<String> = null):Void
{
_untouchedFiles = [];
_unknownFileState = false;
var ignoredPathsMap:Map<String, Bool> = null;
if (pathsToIgnore != null) ignoredPathsMap = [for (pathToIgnore in pathsToIgnore) path + "/" + pathToIgnore => true];
if (FileSystem.exists(path))
{
_scanFilesRecursively(path, _untouchedFiles, ignoredPathsMap);
}
}

private static function _scanFilesRecursively(path:String, map:Map<String, Bool>, ignorePaths:Map<String, Bool>):Void
{
for (file in FileSystem.readDirectory(path))
{
if (file.charAt(0) == ".")
{
continue;
}

var fullPath = Path.combine(path, file);

if (ignorePaths.exists(fullPath))
{
continue;
}

if (FileSystem.isDirectory(fullPath))
{
_scanFilesRecursively(fullPath, map, ignorePaths);
}
else
{
map.set(fullPath, true);
}
}
}

public static function markAllFilesStateAsUnknown():Void
{
_unknownFileState = true;
}

public static function markFileAsTouched(path:String):Void
{
if (_untouchedFiles != null)
{
_untouchedFiles.remove(path);
}
}

public static function mkdir(directory:String):Void
{
try
Expand Down Expand Up @@ -889,6 +970,7 @@ class System
}
else
{
markFileAsTouched(path);
FileSystem.deleteFile(path);
}
}
Expand Down Expand Up @@ -945,6 +1027,7 @@ class System
if (index > -1)
{
output = output.substr(0, index) + replacement + output.substr(index + replaceString.length);
markFileAsTouched(sourcePath);
File.saveContent(sourcePath, output);
}
}
Expand Down