-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# Load required files | ||
Dir[File.expand_path('scripts/*.rb', __dir__)].each { |file| require_relative file } | ||
Dir[File.expand_path('platform/android/*.rb', __dir__)].each { |file| require_relative file } | ||
Dir[File.expand_path('platform/ios/*.rb', __dir__)].each { |file| require_relative file } | ||
Dir[File.expand_path('platform/flutter/*.rb', __dir__)].each { |file| require_relative file } | ||
|
||
class BrandSwitcher | ||
def initialize(brand_key, ignore_health_check: false) | ||
@brand_key = brand_key | ||
@ignore_health_check = ignore_health_check | ||
@platform = SolaraSettingsManager.instance.platform | ||
@health_checker = HealthChecker.new(@brand_key, @ignore_health_check) | ||
@artifacts_switcher= ArtifactSwitcher.new(@platform) | ||
@theme_switcher = ThemeSwitcher.new(@brand_key) | ||
@brand_config_switcher = BrandConfigSwitcher.new(@brand_key) | ||
@resource_manager = BrandResourcesManager.new(@brand_key) | ||
end | ||
|
||
def start | ||
Solara.logger.header("Switching to #{@brand_key}") | ||
|
||
@health_checker.check_health | ||
BrandsManager.instance.save_current_brand(@brand_key) | ||
@artifacts_switcher.create_dirs | ||
switch | ||
SolaraConfigurator.new.start | ||
|
||
Solara.logger.success("Switched to #{@brand_key} successfully.") | ||
end | ||
|
||
private | ||
|
||
def switch | ||
case @platform | ||
when Platform::Flutter | ||
switch_flutter | ||
switch_android | ||
switch_ios | ||
when Platform::IOS | ||
switch_ios | ||
when Platform::Android | ||
switch_android | ||
else | ||
raise ArgumentError, "Invalid platform: #{@platform}" | ||
end | ||
end | ||
|
||
def switch_flutter | ||
@theme_switcher.generate_dart_theme | ||
@brand_config_switcher.generate_dart_config | ||
@resource_manager.update_flutter | ||
end | ||
|
||
def switch_android | ||
BrandConfigManager.new(@brand_key).generate_android_properties | ||
|
||
config_path = FilePath.android_brand_config(@brand_key) | ||
config = JSON.parse(File.read(config_path)) | ||
|
||
GradleSwitcher.new(@brand_key).update_build_gradle | ||
AndroidManifestSwitcher.new.update_manifest(config) | ||
AndroidStringsSwitcher.new.update(config) | ||
@theme_switcher.generate_kotlin_theme | ||
@brand_config_switcher.generate_kotlin_config | ||
@resource_manager.update_android | ||
end | ||
|
||
def switch_ios | ||
BrandConfigManager.new(@brand_key).generate_ios_xcconfig | ||
@theme_switcher.generate_swift_theme | ||
@brand_config_switcher.generate_swift_config | ||
update_xcode_project | ||
@resource_manager.update_ios | ||
end | ||
|
||
def update_xcode_project | ||
Solara.logger.start_step("Update Xcode project") | ||
project_path = IOSFilePathManager.instance.xcode_project | ||
XcodeProjectSwitcher.new(project_path, @brand_key).switch | ||
Solara.logger.end_step("Update Xcode project") | ||
end | ||
end | ||
|
||
class HealthChecker | ||
def initialize(brand_key, ignore_health_check) | ||
@brand_key = brand_key | ||
@ignore_health_check = ignore_health_check | ||
end | ||
|
||
def check_health | ||
health_errors = SolaraManager.new.doctor([@brand_key]).select { |issue| issue.type == Issue::ERROR } | ||
return if health_errors.empty? | ||
|
||
unless @ignore_health_check | ||
errors_with_index = health_errors.each_with_index.map { |error, index| "#{index + 1}: #{error}" } | ||
raise "Health check completed with errors: \n\n#{errors_with_index.join("\n")}." | ||
end | ||
end | ||
end | ||
|
||
class ArtifactSwitcher | ||
def initialize(platform) | ||
@platform = platform | ||
end | ||
|
||
def create_dirs | ||
Solara.logger.start_step("Create artifacts directories") | ||
directories = case @platform | ||
when Platform::Flutter | ||
[FilePath::flutter_artifacts, FilePath::android_artifacts, IOSFilePathManager.instance.artifacts] | ||
when Platform::IOS | ||
[IOSFilePathManager.instance.artifacts] | ||
when Platform::Android | ||
[FilePath::android_artifacts] | ||
else | ||
raise ArgumentError, "Invalid platform: #{@platform}" | ||
end | ||
DirectoryCreator.create_directories(directories, delete_if_exists: true) | ||
Solara.logger.end_step("Create artifacts directories") | ||
end | ||
end | ||
|
||
class ThemeSwitcher | ||
def initialize(brand_key) | ||
@brand_key = brand_key | ||
end | ||
|
||
def generate_dart_theme | ||
generate_theme('brand_theme.dart', Language::Dart, Platform::Flutter) | ||
end | ||
|
||
def generate_kotlin_theme | ||
generate_theme('BrandTheme.kt', Language::Kotlin, Platform::Android) | ||
end | ||
|
||
def generate_swift_theme | ||
generate_theme('BrandTheme.swift', Language::Swift, Platform::IOS) | ||
end | ||
|
||
private | ||
|
||
def generate_theme(name, language, platform) | ||
Solara.logger.start_step("Generate #{name} for #{platform}") | ||
generator = ThemeGenerator.new( | ||
FilePath.brand_theme(@brand_key), | ||
FilePath.generated_config(name, platform), | ||
language | ||
) | ||
generator.generate | ||
Solara.logger.end_step("Generate #{name} for #{platform}") | ||
end | ||
end | ||
|
||
class BrandConfigSwitcher | ||
def initialize(brand_key) | ||
@brand_key = brand_key | ||
end | ||
|
||
def generate_dart_config | ||
generate_brand_config('brand_config.dart', Language::Dart, Platform::Flutter) | ||
end | ||
|
||
def generate_kotlin_config | ||
generate_brand_config('BrandConfig.kt', Language::Kotlin, Platform::Android) | ||
end | ||
|
||
def generate_swift_config | ||
generate_brand_config('BrandConfig.swift', Language::Swift, Platform::IOS) | ||
end | ||
|
||
private | ||
|
||
def generate_brand_config(name, language, platform) | ||
BrandConfigManager.new(@brand_key).generate_brand_config(name, language, platform) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters