diff --git a/lib/constants.dart b/lib/constants.dart index 641d0bb..dd2aaa4 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -46,6 +46,9 @@ const _copyrightKey = 'copyright_notice'; const _languageKey = 'lang'; const _executableKey = 'exe_name'; const _overrideOldPackageKey = 'override_old_package'; +const _customDirPath = 'custom_dir_path'; +const _host = 'host'; +const _excludeHost = 'exclude_host'; // ! Directory Paths // ? Android @@ -128,9 +131,9 @@ final _majorTaskDoneLine = '━' * _outputLength; const _androidKotlinMainActivityTemplate = ''' package {{packageName}} -import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.android.FlutterFragmentActivity -class MainActivity : FlutterActivity() +class MainActivity : FlutterFragmentActivity() '''; const _androidJavaMainActivityTemplate = ''' diff --git a/lib/platforms/android.dart b/lib/platforms/android.dart index 4cb01ca..0134a1d 100644 --- a/lib/platforms/android.dart +++ b/lib/platforms/android.dart @@ -7,12 +7,15 @@ void _setAndroidConfigurations(dynamic androidConfig) { final androidConfigMap = Map.from(androidConfig); - _setAndroidAppName(androidConfigMap[_appNameKey]); - _setAndroidPackageName(androidConfigMap[_packageNameKey]); + _setAndroidAppName( + androidConfigMap[_appNameKey], androidConfigMap[_customDirPath], androidConfigMap[_host], androidConfigMap[_excludeHost]); + _setAndroidPackageName( + androidConfigMap[_packageNameKey], androidConfigMap[_customDirPath]); _createNewMainActivity( lang: androidConfigMap[_languageKey], packageName: androidConfigMap[_packageNameKey], overrideOldPackage: androidConfigMap[_overrideOldPackageKey], + customDirPath: androidConfigMap[_customDirPath], ); } on _PackageRenameException catch (e) { _logger @@ -28,22 +31,48 @@ void _setAndroidConfigurations(dynamic androidConfig) { } } -void _setAndroidAppName(dynamic appName) { +void _setAndroidAppName(dynamic appName, String? customDirPath, dynamic host, dynamic excludeHost) { try { if (appName == null) return; if (appName is! String) throw _PackageRenameErrors.invalidAppName; - final androidManifestFile = File(_androidMainManifestFilePath); + final androidMainManifestFilePath = + (customDirPath is String && customDirPath.isNotEmpty) + ? _androidMainManifestFilePath.replaceAll( + _androidAppDirPath, customDirPath) + : _androidMainManifestFilePath; + + final androidManifestFile = File(androidMainManifestFilePath); if (!androidManifestFile.existsSync()) { throw _PackageRenameErrors.androidMainManifestNotFound; } final androidManifestString = androidManifestFile.readAsStringSync(); - final newLabelAndroidManifestString = androidManifestString.replaceAll( + String newLabelAndroidManifestString = androidManifestString.replaceAll( RegExp('android:label="(.*)"'), 'android:label="$appName"', ); + + + + if (host is String && host.isNotEmpty) { + if (excludeHost is String && excludeHost.isNotEmpty) { + // Replace all host attributes except the excluded one + newLabelAndroidManifestString = newLabelAndroidManifestString.replaceAll( + RegExp('android:host="(?!$excludeHost")(.*?)"'), + 'android:host="$host"', + ); + _logger.i('Android Host set to: `$host` excluding: `$excludeHost` (main AndroidManifest.xml)'); + } else { + newLabelAndroidManifestString = newLabelAndroidManifestString.replaceAll( + RegExp('android:host="(.*?)"'), + 'android:host="$host"', + ); + _logger.i('Android Host set to: `$host` (main AndroidManifest.xml)'); + } + } + androidManifestFile.writeAsStringSync(newLabelAndroidManifestString); _logger.i('Android label set to: `$appName` (main AndroidManifest.xml)'); @@ -61,15 +90,24 @@ void _setAndroidAppName(dynamic appName) { } } -void _setAndroidPackageName(dynamic packageName) { +void _setAndroidPackageName(dynamic packageName, String? customDirPath) { try { if (packageName == null) return; if (packageName is! String) throw _PackageRenameErrors.invalidPackageName; final androidManifestFilePaths = [ - _androidMainManifestFilePath, - _androidDebugManifestFilePath, - _androidProfileManifestFilePath, + (customDirPath is String && customDirPath.isNotEmpty) + ? _androidMainManifestFilePath.replaceAll( + _androidAppDirPath, customDirPath) + : _androidMainManifestFilePath, + (customDirPath is String && customDirPath.isNotEmpty) + ? _androidDebugManifestFilePath.replaceAll( + _androidAppDirPath, customDirPath) + : _androidDebugManifestFilePath, + (customDirPath is String && customDirPath.isNotEmpty) + ? _androidProfileManifestFilePath.replaceAll( + _androidAppDirPath, customDirPath) + : _androidProfileManifestFilePath, ]; _setManifestPackageName( @@ -78,8 +116,15 @@ void _setAndroidPackageName(dynamic packageName) { ); _setBuildGradlePackageName( - buildGradleFilePath: _androidAppLevelBuildGradleFilePath, - kotlinBuildGradleFilePath: _androidAppLevelKotlinBuildGradleFilePath, + buildGradleFilePath: (customDirPath is String && customDirPath.isNotEmpty) + ? _androidAppLevelBuildGradleFilePath.replaceAll( + _androidAppDirPath, customDirPath) + : _androidAppLevelBuildGradleFilePath, + kotlinBuildGradleFilePath: + (customDirPath is String && customDirPath.isNotEmpty) + ? _androidAppLevelKotlinBuildGradleFilePath.replaceAll( + _androidAppDirPath, customDirPath) + : _androidAppLevelKotlinBuildGradleFilePath, packageName: packageName, ); } on _PackageRenameException catch (e) { @@ -183,6 +228,7 @@ void _createNewMainActivity({ required dynamic lang, required dynamic packageName, required dynamic overrideOldPackage, + String? customDirPath, }) { try { if (packageName == null) return; @@ -205,7 +251,10 @@ void _createNewMainActivity({ if (overrideOldPackage == null) { final packageDirs = packageName.replaceAll('.', '/'); - final langDir = '$_androidMainDirPath/$lang'; + final dirPath = (customDirPath is String && customDirPath.isNotEmpty) + ? '${_androidMainDirPath.replaceAll(_androidAppDirPath, customDirPath)}/$lang' + : '$_androidMainDirPath/$lang'; + final langDir = dirPath; final mainActivityFile = File( '$langDir/$packageDirs/MainActivity.$fileExtension', @@ -240,7 +289,10 @@ void _createNewMainActivity({ // to new package directory structure final oldPackageDirs = overrideOldPackage.replaceAll('.', '/'); final newPackageDirs = packageName.replaceAll('.', '/'); - final langDir = '$_androidMainDirPath/$lang'; + final dirPath = (customDirPath is String && customDirPath.isNotEmpty) + ? '${_androidMainDirPath.replaceAll(_androidAppDirPath, customDirPath)}/$lang' + : '$_androidMainDirPath/$lang'; + final langDir = dirPath; final oldMainActivityDir = Directory('$langDir/$oldPackageDirs'); if (!oldMainActivityDir.existsSync()) { @@ -294,7 +346,20 @@ void _createNewMainActivity({ } } - _logger.i('New MainActivity.${lang == 'kotlin' ? 'kt' : 'java'} created'); + if (overrideOldPackage == null) { + _logger.i( + 'New MainActivity.${lang == 'kotlin' ? 'kt' : 'java'} created at: ' + '${(customDirPath is String && customDirPath.isNotEmpty) ? customDirPath : _androidAppDirPath}/' + 'src/main/$lang/${packageName.replaceAll('.', '/')}', + ); + } else { + _logger.i( + 'MainActivity.${lang == 'kotlin' ? 'kt' : 'java'} moved from package: ' + '`$overrideOldPackage` to `$packageName` at: ' + '${(customDirPath is String && customDirPath.isNotEmpty) ? customDirPath : _androidAppDirPath}/' + 'src/main/$lang/${packageName.replaceAll('.', '/')}', + ); + } } on _PackageRenameException catch (e) { _logger ..e('${e.message}ERR Code: ${e.code}') diff --git a/lib/platforms/ios.dart b/lib/platforms/ios.dart index 1914109..b95438d 100644 --- a/lib/platforms/ios.dart +++ b/lib/platforms/ios.dart @@ -6,10 +6,11 @@ void _setIOSConfigurations(dynamic iosConfig) { if (iosConfig is! Map) throw _PackageRenameErrors.invalidIOSConfig; final iosConfigMap = Map.from(iosConfig); + final customDirPath = iosConfigMap[_customDirPath]; - _setIOSDisplayName(iosConfigMap[_appNameKey]); - _setIOSBundleName(iosConfigMap[_bundleNameKey]); - _setIOSPackageName(iosConfigMap[_packageNameKey]); + _setIOSDisplayName(iosConfigMap[_appNameKey], customDirPath: customDirPath); + _setIOSBundleName(iosConfigMap[_bundleNameKey], customDirPath: customDirPath); + _setIOSPackageName(iosConfigMap[_packageNameKey], customDirPath: customDirPath); } on _PackageRenameException catch (e) { _logger ..e('${e.message}ERR Code: ${e.code}') @@ -24,12 +25,15 @@ void _setIOSConfigurations(dynamic iosConfig) { } } -void _setIOSDisplayName(dynamic appName) { +void _setIOSDisplayName(dynamic appName, {String? customDirPath}) { try { if (appName == null) return; if (appName is! String) throw _PackageRenameErrors.invalidAppName; - final iosInfoPlistFile = File(_iosInfoPlistFilePath); + final iosInfoPlistFilePath = (customDirPath is String && customDirPath.isNotEmpty) + ? _iosInfoPlistFilePath.replaceFirst(_iosDirPath, customDirPath) + : _iosInfoPlistFilePath; + final iosInfoPlistFile = File(iosInfoPlistFilePath); if (!iosInfoPlistFile.existsSync()) { throw _PackageRenameErrors.iosInfoPlistNotFound; } @@ -57,7 +61,7 @@ void _setIOSDisplayName(dynamic appName) { } } -void _setIOSBundleName(dynamic bundleName) { +void _setIOSBundleName(dynamic bundleName, {String? customDirPath}) { try { if (bundleName == null) return; if (bundleName is! String) throw _PackageRenameErrors.invalidBundleName; @@ -68,7 +72,10 @@ void _setIOSBundleName(dynamic bundleName) { ); } - final iosInfoPlistFile = File(_iosInfoPlistFilePath); + final iosInfoPlistFilePath = (customDirPath is String && customDirPath.isNotEmpty) + ? _iosInfoPlistFilePath.replaceFirst(_iosDirPath, customDirPath) + : _iosInfoPlistFilePath; + final iosInfoPlistFile = File(iosInfoPlistFilePath); if (!iosInfoPlistFile.existsSync()) { throw _PackageRenameErrors.iosInfoPlistNotFound; } @@ -96,12 +103,15 @@ void _setIOSBundleName(dynamic bundleName) { } } -void _setIOSPackageName(dynamic packageName) { +void _setIOSPackageName(dynamic packageName, {String? customDirPath}) { try { if (packageName == null) return; if (packageName is! String) throw _PackageRenameErrors.invalidPackageName; - final iosProjectFile = File(_iosProjectFilePath); + final iosProjectFilePath = (customDirPath is String && customDirPath.isNotEmpty) + ? _iosProjectFilePath.replaceFirst(_iosDirPath, customDirPath) + : _iosProjectFilePath; + final iosProjectFile = File(iosProjectFilePath); if (!iosProjectFile.existsSync()) { throw _PackageRenameErrors.iosProjectFileNotFound; }