Skip to content

Commit

Permalink
Solara
Browse files Browse the repository at this point in the history
  • Loading branch information
ideyaa committed Sep 18, 2024
1 parent 378f4fe commit 7fca2f3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
32 changes: 32 additions & 0 deletions solara/lib/core/brands/brand_font_switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def switch
case SolaraSettingsManager.instance.platform
when Platform::Flutter
FlutterFontSwitcher.new(@brand_key, @font_files).switch
AndroidFontSwitcher.new(@brand_key, @font_files).switch
IOSFontSwitcher.new(@brand_key, @font_files).switch
when Platform::Android
AndroidFontSwitcher.new(@brand_key, @font_files).switch
when Platform::IOS
Expand Down Expand Up @@ -76,11 +78,41 @@ def switch
Solara.logger.start_step("Switch Android fonts")
destination = FilePath.android_project_fonts
copy_fonts(destination)
rename_fonts_in_directory(FilePath.android_project_fonts)
Solara.logger.end_step("Switch Android fonts")
end

private

def rename_fonts_in_directory(directory)
Dir.foreach(directory) do |file|
next if file == '.' || file == '..' # Skip current and parent directory entries

old_path = File.join(directory, file)
if File.file?(old_path)
new_name = sanitize_font_name(file)
new_path = File.join(directory, new_name)

# Rename the file if the new name is different
unless old_path == new_path
FileUtils.mv(old_path, new_path)
puts "Renamed: #{file} -> #{new_name}"
end
end
end
end

def sanitize_font_name(font_name)
# Convert to lowercase
sanitized_name = font_name.downcase
# Replace spaces with underscores
sanitized_name.gsub!('-', '_')
sanitized_name.gsub!(' ', '_')
# Remove special characters (except underscores and dots)
sanitized_name.gsub!(/[^a-z0-9_.]/, '')
sanitized_name
end

def copy_fonts(destination)
FontCopier.new.copy(destination, @font_files)
end
Expand Down
2 changes: 1 addition & 1 deletion solara/lib/core/scripts/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def self.ios_project_fonts
end

def self.android_project_fonts
File.join(android_project_res_artifacts, 'fonts')
File.join(android_project_res_artifacts, 'font')
end

def self.flutter_project_fonts
Expand Down
73 changes: 35 additions & 38 deletions solara/lib/core/scripts/theme_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def generate
private

def generate_kotlin
kotlin_code = "import androidx.compose.ui.graphics.Color\n"
kotlin_code += "import androidx.compose.ui.unit.dp\n"
kotlin_code += "import androidx.compose.ui.unit.sp\n\n"
kotlin_code = "import android.graphics.Color\n\n"
kotlin_code += "object BrandTheme {\n"
kotlin_code += generate_kotlin_colors
kotlin_code += generate_kotlin_typography
Expand All @@ -33,39 +31,10 @@ def generate_kotlin
kotlin_code
end

def generate_swift
swift_code = "import SwiftUI\n\n"
swift_code += "struct BrandTheme {\n"
swift_code += generate_swift_colors
swift_code += generate_swift_typography
swift_code += generate_swift_spacing
swift_code += generate_swift_border_radius
swift_code += generate_swift_elevation
swift_code += "}\n\n"
swift_code += generate_swift_colors_hext_extension
swift_code
end

def generate_dart
dart_code = "import 'package:flutter/material.dart';\n\n"
dart_code += generate_dart_colors
dart_code += generate_dart_typography
dart_code += generate_dart_spacing
dart_code += generate_dart_border_radius
dart_code += generate_dart_elevation
dart_code
end

def write_to_file(code)
FileUtils.mkdir_p(File.dirname(@output_path))
File.write(@output_path, code)
Solara.logger.debug("Generated #{@language.capitalize} theme file: #{@output_path}")
end

def generate_kotlin_colors
code = " object Colors {\n"
@theme['colors'].each do |name, value|
code += " val #{name} = Color(0xFF#{value[1..-1]})\n"
code += " val #{name} = Color.parseColor(\"#{value}\")\n"
end
code += " }\n\n"
code
Expand All @@ -80,7 +49,7 @@ def generate_kotlin_typography
code += " }\n\n"
code += " object FontSize {\n"
@theme['typography']['fontSize'].each do |name, value|
code += " val #{name} = #{value}.sp\n"
code += " val #{name} = #{value}\n" # Removed .sp
end
code += " }\n"
code += " }\n\n"
Expand All @@ -90,7 +59,7 @@ def generate_kotlin_typography
def generate_kotlin_spacing
code = " object Spacing {\n"
@theme['spacing'].each do |name, value|
code += " val #{name} = #{value}.dp\n"
code += " val #{name} = #{value}\n"
end
code += " }\n\n"
code
Expand All @@ -99,7 +68,7 @@ def generate_kotlin_spacing
def generate_kotlin_border_radius
code = " object BorderRadius {\n"
@theme['borderRadius'].each do |name, value|
code += " val #{name} = #{value}.dp\n"
code += " val #{name} = #{value}\n"
end
code += " }\n\n"
code
Expand All @@ -108,10 +77,38 @@ def generate_kotlin_border_radius
def generate_kotlin_elevation
code = " object Elevation {\n"
@theme['elevation'].each do |name, value|
code += " val #{name} = #{value}.dp\n"
code += " val #{name} = #{value}\n"
end
code += " }\n"
code
end

def generate_swift
swift_code = "import SwiftUI\n\n"
swift_code += "struct BrandTheme {\n"
swift_code += generate_swift_colors
swift_code += generate_swift_typography
swift_code += generate_swift_spacing
swift_code += generate_swift_border_radius
swift_code += generate_swift_elevation
swift_code += "}\n\n"
swift_code += generate_swift_colors_hext_extension
swift_code
end

def generate_dart
dart_code = "import 'package:flutter/material.dart';\n\n"
dart_code += generate_dart_colors
dart_code += generate_dart_typography
dart_code += generate_dart_spacing
dart_code += generate_dart_border_radius
dart_code += generate_dart_elevation
dart_code
end

def write_to_file(code)
FileUtils.mkdir_p(File.dirname(@output_path))
File.write(@output_path, code)
Solara.logger.debug("Generated #{@language.capitalize} theme file: #{@output_path}")
end

def generate_swift_colors
Expand Down

0 comments on commit 7fca2f3

Please sign in to comment.