diff --git a/solara/lib/core/brands/brand_switcher 1.rb b/solara/lib/core/brands/brand_switcher 1.rb new file mode 100644 index 0000000..04e4a70 --- /dev/null +++ b/solara/lib/core/brands/brand_switcher 1.rb @@ -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 \ No newline at end of file diff --git a/solara/lib/core/scripts/brand_config_manager.rb b/solara/lib/core/scripts/brand_config_manager.rb index a5d1818..89506a9 100644 --- a/solara/lib/core/scripts/brand_config_manager.rb +++ b/solara/lib/core/scripts/brand_config_manager.rb @@ -10,7 +10,7 @@ def generate_brand_config( name, language, platform) - Solara.logger.start_step("Generate #{language} brand config for #{platform}") + Solara.logger.start_step("Generate #{name} for #{platform}") config = load_config(FilePath.brand_config(@brand_key)) add_basic_brand_info(config, platform) config_generator = BrandConfigGenerator.new( @@ -20,7 +20,7 @@ def generate_brand_config( platform ) config_generator.generate - Solara.logger.end_step("Generate #{language} brand config for #{platform}") + Solara.logger.end_step("Generate #{name} for #{platform}") end def generate_android_properties diff --git a/solara/lib/solara.rb b/solara/lib/solara.rb index b69d719..ed26555 100644 --- a/solara/lib/solara.rb +++ b/solara/lib/solara.rb @@ -143,6 +143,7 @@ def switch begin SolaraManager.new.switch(brand_key) rescue StandardError => e + Solara.logger.error(e) Solara.logger.fatal("Switching to #{brand_key} failed.") exit 1 end