Skip to content

Commit

Permalink
Solara
Browse files Browse the repository at this point in the history
  • Loading branch information
IdeaS0ft committed Sep 14, 2024
1 parent a6338bd commit 101be95
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 2 deletions.
176 changes: 176 additions & 0 deletions solara/lib/core/brands/brand_switcher 1.rb
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
4 changes: 2 additions & 2 deletions solara/lib/core/scripts/brand_config_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions solara/lib/solara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 101be95

Please sign in to comment.