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 deletion of stale templates. #28

Closed
wants to merge 1 commit into from

Conversation

player-03
Copy link
Contributor

@player-03 player-03 commented Jun 13, 2022

Template files have a habit of sticking around in the output folder, even if the project no longer needs or wants them. But if we save a list of templates after a build, we can compare against that list in future builds to locate no-longer-used files.

The intent is for deleteStaleTemplates() to run every time. It doesn't just delete the files, it saves the list for next time. A drawback of this implementation is that if it gets skipped, the list doesn't get updated, and if the list isn't updated, the next run might not find all the stale templates. This can of course be fixed with a clean build.

Like #27, this is one half of a potential solution to openfl/lime#1546. (The other half is openfl/lime#1550.) And I'd argue it's a better solution, requiring less maintenance and having no 31-character-long function names.

Template files have a habit of sticking around in the output folder,
even if the project no longer needs or wants them. But if compare the
list of templates that just got copied to the previous list, it becomes
easy to locate the stale files.
@player-03
Copy link
Contributor Author

Just for future reference, I considered implementing part of this as a binary search, since that would reduce the time complexity from O(n²) to O(n log n). In the end I didn't think it was worth it, but here's the code in case anyone wants it.

public static function deleteStaleTemplates(targetDirectory:String)
{
	_templates.sort(Reflect.compare);

	var templatesFile = Path.combine(targetDirectory, ".templates");
	if (FileSystem.exists(templatesFile))
	{
		for (template in File.getContent(templatesFile).split("\n"))
		{
			// Do a binary search to save time.
			var stillInUse:Bool = false;
			var start:Int = 0;
			var end:Int = _templates.length - 1;
			while (start <= end)
			{
				var mid = Std.int((end + start) / 2);

				if (template == _templates[mid])
				{
					stillInUse = true;
					break;
				}
				else if (template < _templates[mid])
					end = mid - 1;
				else
					start = mid + 1;
			}

			if (!stillInUse)
			{
				deleteFile(template);
			}
		}
	}

	File.saveContent(templatesFile, _templates.join("\n"));
	_templates.resize(0);
}

@player-03 player-03 changed the title Allow deletion of no-longer-used templates. Allow deletion of stale templates. Aug 15, 2022
@player-03
Copy link
Contributor Author

Update: I think I've found a better approach.

@player-03 player-03 closed this May 30, 2024
@player-03 player-03 deleted the stale_templates branch May 30, 2024 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant