createMacosBuildFile function
Creates the NativeSplashScreen.swift
file in dirPath
.
If force
is true, it will overwrite the file if it exists.
Implementation
void createMacosBuildFile(String dirPath, [bool? force]) {
final file = File('$dirPath/NativeSplashScreen.swift');
if (file.existsSync() && force != true) {
logger.w(
'Swift file already exists at ${file.path}\n'
'Use --force to overwrite.',
);
return;
}
const swiftContent = r'''
// Generated file - do not edit
// Generated by native_splash_screen_cli
// --------------------------------------------------------------------------------
// IT IS GENERATED TO PROVIDE BUILD-CONFIGURATION-SPECIFIC DATA
// TO THE NATIVE SPLASH SCREEN. DO NOT EDIT MANUALLY unless you
// understand the implications, as your changes might be overwritten.
//
// How it works:
// 1. The CLI tool generates three other files:
// - NativeSplashScreen_Debug.swift
// - NativeSplashScreen_Profile.swift
// - NativeSplashScreen_Release.swift
// Each of these files defines a struct (e.g., DebugNativeSplashScreenConfiguration)
// containing the splash screen settings for that specific build mode.
//
// 2. This NativeSplashScreenConfiguration class uses Swift's conditional
// compilation blocks (#if DEBUG, #elseif PROFILE, #else) to determine
// the current build mode.
//
// 3. Based on the build mode, it instantiates the corresponding
// configuration struct (e.g., DebugNativeSplashScreenConfiguration for DEBUG builds).
//
// 4. This class then conforms to the NativeSplashScreenConfigurationProvider
// protocol (defined in the NativeSplashScreen plugin) by forwarding
// all property requests to the active underlying configuration instance.
//
// 5. Your AppDelegate.swift will create an instance of this
// NativeSplashScreenConfiguration class and pass it to the NativeSplashScreen
// plugin to configure and display the splash screen.
// --------------------------------------------------------------------------------
import native_splash_screen_macos // This imports the plugin's module to access the
// NativeSplashScreenConfigurationProvider protocol.
// This is the main class that AppDelegate will instantiate.
// It acts as a dispatcher to the correct build-specific configuration.
class NativeSplashScreenConfiguration: NativeSplashScreenConfigurationProvider {
// Private property to hold the actual configuration provider instance
// determined by the build flags.
private let activeConfiguration: NativeSplashScreenConfigurationProvider
init() {
// Conditional compilation:
// The Swift compiler, when building for a specific mode (Debug, Profile, Release),
// will only include the code within the matching block.
// Flutter sets these flags (DEBUG, PROFILE) automatically for its build modes.
#if DEBUG
// In Debug mode, instantiate the debug-specific configuration.
// The DebugNativeSplashScreenConfiguration struct is defined in NativeSplashScreen_Debug.swift.
self.activeConfiguration = DebugNativeSplashScreenConfiguration()
// Optional: A print statement to confirm which config is used during development.
// Can be removed for cleaner console output once verified.
// print("[NativeSplashScreenConfig] Initialized with DEBUG settings.")
#elseif PROFILE
// In Profile mode, instantiate the profile-specific configuration.
// The ProfileNativeSplashScreenConfiguration struct is defined in NativeSplashScreen_Profile.swift.
self.activeConfiguration = ProfileNativeSplashScreenConfiguration()
// print("[NativeSplashScreenConfig] Initialized with PROFILE settings.")
#else // RELEASE (or any other configuration not matching DEBUG or PROFILE)
// In Release mode (or any other mode), instantiate the release-specific configuration.
// The ReleaseNativeSplashScreenConfiguration struct is defined in NativeSplashScreen_Release.swift.
self.activeConfiguration = ReleaseNativeSplashScreenConfiguration()
// In release, it's generally best to avoid print statements for production code.
// print("[NativeSplashScreenConfig] Initialized with RELEASE settings.")
#endif
}
// MARK: - NativeSplashScreenConfigurationProvider Conformance
// These properties simply forward the call to the `activeConfiguration` instance.
var windowWidth: Int { activeConfiguration.windowWidth }
var windowHeight: Int { activeConfiguration.windowHeight }
var windowTitle: String { activeConfiguration.windowTitle }
var withAnimation: Bool { activeConfiguration.withAnimation }
var imagePixels: [UInt8] { activeConfiguration.imagePixels }
var imageWidth: Int { activeConfiguration.imageWidth }
var imageHeight: Int { activeConfiguration.imageHeight }
}
''';
file.writeAsStringSync(swiftContent.trim());
logger.success('Swift file created at ${file.path}');
}