Class
MTLProtectedAreaManager
Thread-safe manager for preserving protected area content across regenerations.
actor MTLProtectedAreaManager
Overview
Protected areas allow user code to be preserved when templates are regenerated, enabling a generate-modify-regenerate workflow where custom code survives regeneration cycles.
Overview
Protected areas work by:
-
Marking sections: Templates define protected areas with unique IDs
-
Scanning existing files: Before regeneration, existing files are scanned
-
Extracting content: Content between protection markers is extracted
-
Preserving during regeneration: Extracted content replaces default content
-
Maintaining markers: Protection markers are preserved in output
Protection Markers
Protected areas are delimited by start/end markers:
// START PROTECTED REGION user-imports
import MyCustomLibrary
// END PROTECTED REGION user-imports
The markers can have custom prefixes (e.g., //, <!--, #) to match the comment syntax of the target language.
Example Usage
let manager = MTLProtectedAreaManager()
// Scan existing file before regeneration
try await manager.scanFile("/output/Person.swift")
// During regeneration, retrieve preserved content
if let content = await manager.getContent("user-imports") {
// Use preserved content instead of default
}
// After regeneration, check what was preserved
let preserved = await manager.getAllContent()
print("Preserved \(preserved.count) protected areas")
Topics
Structures
Represents a protected area with its content and markers.
Initializers
Creates a new protected area manager.
Instance Methods
Clears all protected area content.
Enables or disables debug logging.
Generates standard protection markers for a given ID and prefix.
Returns all protected area content.
Retrieves the complete protected area record.
Retrieves the preserved content for a protected area.
Checks if content exists for a protected area.
Removes a protected area from the manager.
Scans text content for protected areas and extracts them.
Scans a file for protected areas and extracts their content.
Stores protected area content manually.
Returns a summary of protected area statistics.