From bd19a47a7545c1b0872f193c98b0c1ffe1b868aa Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 27 Dec 2020 22:07:20 +0300 Subject: [PATCH 1/7] Update feature toggles work Add templates Add template manager Updaet features generation scripts --- .../features_generator/features_generator.rb | 31 +++-------------- .../features_generator/features_generator.sh | 2 +- xcode/commonFastfile | 8 ++--- .../touchlane/lib/touchlane/features.rb | 17 +++++++--- xcode/managers/lib/template_manager.rb | 19 +++++++++++ xcode/managers/managers.rb | 1 + xcode/templates/templates.rb | 3 ++ .../templates/templates/features_templates.rb | 34 +++++++++++++++++++ 8 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 xcode/managers/lib/template_manager.rb create mode 100644 xcode/templates/templates.rb create mode 100755 xcode/templates/templates/features_templates.rb diff --git a/xcode/build_phases/features_generator/features_generator.rb b/xcode/build_phases/features_generator/features_generator.rb index 6601ba6..8a1bebd 100755 --- a/xcode/build_phases/features_generator/features_generator.rb +++ b/xcode/build_phases/features_generator/features_generator.rb @@ -1,37 +1,12 @@ require 'yaml' -require 'erb' require_relative '../../managers/managers' +require_relative '../../templates/templates' # Input files paths build_settings_file_path = ARGV[0] generated_features_enum_file_path = ARGV[1] -features_enum_template = -" -//MARK: - Feature toggles - -public enum FeatureToggle: String, Codable, RawRepresentable, CaseIterable { - <% for @feature in @features %> - case <%= @feature %> - <% end %> -} -" - -class FeatureUtils - include ERB::Util - - attr_accessor :features - - def initialize(features) - @features = features - end - - def render(template) - ERB.new(template).result(binding) - end -end - build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"] if build_settings_features_list.nil? or build_settings_features_list.empty? @@ -39,7 +14,9 @@ if build_settings_features_list.nil? or build_settings_features_list.empty? end # Generate enum Feature Toggles -utils = FeatureUtils.new(build_settings_features_list) +features_enum_template = Templates::FeatureTemplates.features_enum +utils = Managers::TemplateManager.new(build_settings_features_list) + rendered_enum = utils.render(features_enum_template).strip Managers::FileManager.save_data_to_file(generated_features_enum_file_path, rendered_enum) diff --git a/xcode/build_phases/features_generator/features_generator.sh b/xcode/build_phases/features_generator/features_generator.sh index b41cb44..66da7cc 100755 --- a/xcode/build_phases/features_generator/features_generator.sh +++ b/xcode/build_phases/features_generator/features_generator.sh @@ -1,6 +1,6 @@ # Input paths readonly BUILD_SETTINGS_FILE_PATH=${1:-${PROJECT_DIR}/common/build_settings.yaml} -readonly FEATURES_ENUM_FILE_PATH=${2:-${PROJECT_DIR}/${PRODUCT_NAME}/Resources/Features/FeatureToggle.swift} +readonly FEATURES_ENUM_FILE_PATH=${2:-${PROJECT_DIR}/${PRODUCT_NAME}/Resources/Features/Feature.swift} # Features enunm generator script readonly CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" diff --git a/xcode/commonFastfile b/xcode/commonFastfile index f6fbb5a..1ad1c9f 100644 --- a/xcode/commonFastfile +++ b/xcode/commonFastfile @@ -138,7 +138,7 @@ private_lane :buildConfiguration do |options| installDependencies(options) unless options[:features].nil? - generateFeaturesFile(options) + generate_enabled_features_extension(options) end if !(options[:uploadToFabric] || options[:uploadToAppStore]) @@ -402,10 +402,10 @@ def get_google_services_plist_path(app_target_folder_name, configuration_type) File.expand_path "../#{app_target_folder_name}/Resources/#{configuration_type.prefix}-GoogleService-Info.plist" end -def generateFeaturesFile(options) +def generate_enabled_features_extension(options) app_target_folder_name = options[:appName] || $appName - project_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features/Features.json" + project_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features/Enabled.swift" build_settings_file_path = File.expand_path "../common/build_settings.yaml" builder_features_list = options[:features] @@ -413,7 +413,7 @@ def generateFeaturesFile(options) build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"] - features_hash = Touchlane::Features.generate_features_hash(builder_features_list, build_settings_features_list) + enabled_features_hash = Touchlane::Features.generate_enabled_features_hash(builder_features_list, build_settings_features_list) Managers::FileManager.save_data_to_file_in_json(project_features_file_path, features_hash) end diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb index e6e65c5..13501e6 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/features.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -1,7 +1,13 @@ +require 'yaml' +require 'erb' + +require_relative '../../managers/managers' +require_relative '../../templates/templates' + module Touchlane class Features - def self.generate_features_hash(builder_features_list, build_settings_features_list) + def self.generate_enabled_features_hash(builder_features_list, build_settings_features_list) # Check is entered features contains in configuration file features_diff = builder_features_list - build_settings_features_list @@ -10,10 +16,11 @@ module Touchlane raise "Unexpected features: " + features_diff.join(', ') end - # Generate hash from feature names - feature_bodies = builder_features_list.map { |feature_name| { :name => feature_name, :enabled => true } } - features_full_body = { :features => feature_bodies } - features_full_body.to_hash() + # Generate FeatureToggle extension hash from feature names + enabled_features_extension_template = Templates::FeatureTemplates.enabled_features_extension + utils = Managers::TemplateManager.new(builder_features_list) + + utils.render(enabled_features_extension_template).strip.to_hash() end private_class_method :new diff --git a/xcode/managers/lib/template_manager.rb b/xcode/managers/lib/template_manager.rb new file mode 100644 index 0000000..adb59c5 --- /dev/null +++ b/xcode/managers/lib/template_manager.rb @@ -0,0 +1,19 @@ +require 'erb' + +module Managers + class TemplateManager + + include ERB::Util + + attr_accessor :items + + def initialize(items) + @items = items + end + + def render(template) + ERB.new(template).result(binding) + end + + end +end diff --git a/xcode/managers/managers.rb b/xcode/managers/managers.rb index 81e6279..9c3c53d 100644 --- a/xcode/managers/managers.rb +++ b/xcode/managers/managers.rb @@ -1,3 +1,4 @@ module Managers require_relative "lib/file_manager" + require_relative "lib/template_manager" end diff --git a/xcode/templates/templates.rb b/xcode/templates/templates.rb new file mode 100644 index 0000000..c26788d --- /dev/null +++ b/xcode/templates/templates.rb @@ -0,0 +1,3 @@ +module Templates + require_relative "templates/features_templates" +end diff --git a/xcode/templates/templates/features_templates.rb b/xcode/templates/templates/features_templates.rb new file mode 100755 index 0000000..ba579ab --- /dev/null +++ b/xcode/templates/templates/features_templates.rb @@ -0,0 +1,34 @@ +module Templates + module FeatureTemplates + + def self.features_enum +" +//MARK: - Feature toggles + +public enum Feature: String, Codable, RawRepresentable, CaseIterable { + <% for @item in @items %> + case <%= @item %> + <% end %> +} +" + end + + def self.enabled_features_extension +" +// MARK: - Enabled features + +public extension Feature { + + static var enabled: [Feature] { + [ + <% for @item in @items %> + .<%= @item %>, + <% end %> + ] + } +} +" + end + + end +end \ No newline at end of file From 07b0df8a46cb5ce86067af5fb820840c961e5902 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 27 Dec 2020 22:09:28 +0300 Subject: [PATCH 2/7] Code correction --- xcode/fastlane/touchlane/lib/touchlane/features.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb index 13501e6..bebd4bf 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/features.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -1,6 +1,3 @@ -require 'yaml' -require 'erb' - require_relative '../../managers/managers' require_relative '../../templates/templates' From 417ddaafba539419908b71be87a9ee68e0dad4ce Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 27 Dec 2020 22:22:52 +0300 Subject: [PATCH 3/7] Update naming --- xcode/templates/templates/features_templates.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xcode/templates/templates/features_templates.rb b/xcode/templates/templates/features_templates.rb index ba579ab..9c7cb44 100755 --- a/xcode/templates/templates/features_templates.rb +++ b/xcode/templates/templates/features_templates.rb @@ -3,7 +3,7 @@ module Templates def self.features_enum " -//MARK: - Feature toggles +//MARK: - Generated feature toggles public enum Feature: String, Codable, RawRepresentable, CaseIterable { <% for @item in @items %> @@ -15,7 +15,7 @@ public enum Feature: String, Codable, RawRepresentable, CaseIterable { def self.enabled_features_extension " -// MARK: - Enabled features +// MARK: - Generated enabled features public extension Feature { From 2af414680f90f47b4d5adce2501926b00ce074d4 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 27 Dec 2020 22:32:45 +0300 Subject: [PATCH 4/7] Fix paths --- xcode/fastlane/touchlane/lib/touchlane/features.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb index bebd4bf..37b0f29 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/features.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -1,5 +1,5 @@ -require_relative '../../managers/managers' -require_relative '../../templates/templates' +require_relative '../../../../managers/managers' +require_relative '../../../../templates/templates' module Touchlane class Features From a042e43450eacac76aa7485863de07e37d776dce Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 27 Dec 2020 23:23:36 +0300 Subject: [PATCH 5/7] Fix script logic --- xcode/commonFastfile | 28 +++++++++++++------ .../touchlane/lib/touchlane/features.rb | 6 ++-- .../templates/templates/features_templates.rb | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/xcode/commonFastfile b/xcode/commonFastfile index 1ad1c9f..0f2f795 100644 --- a/xcode/commonFastfile +++ b/xcode/commonFastfile @@ -137,9 +137,7 @@ private_lane :buildConfiguration do |options| installDependencies(options) - unless options[:features].nil? - generate_enabled_features_extension(options) - end + generate_enabled_features_extension_if_needed(options) if !(options[:uploadToFabric] || options[:uploadToAppStore]) options[:skip_package_ipa] = true @@ -402,20 +400,32 @@ def get_google_services_plist_path(app_target_folder_name, configuration_type) File.expand_path "../#{app_target_folder_name}/Resources/#{configuration_type.prefix}-GoogleService-Info.plist" end -def generate_enabled_features_extension(options) +def generate_enabled_features_extension_if_needed(options) app_target_folder_name = options[:appName] || $appName - project_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features/Enabled.swift" + project_enabled_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features/Enabled.swift" build_settings_file_path = File.expand_path "../common/build_settings.yaml" - builder_features_list = options[:features] - .split(",").map { |feature_name| feature_name.strip } # [ "Feature1", "Feature2", "Feature3" ] + unless is_feature_extension_needed?(options, project_enabled_features_file_path) + return + end + + if options[:features].nil? + builder_features_list = [] # If Enabled.swift exists and features option is nil we need to create empty extension to avoid unexpected features + else + builder_features_list = (options[:features] || []) + .split(",").map { |feature_name| feature_name.strip } # [ "Feature1", "Feature2", "Feature3" ] + end build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"] - enabled_features_hash = Touchlane::Features.generate_enabled_features_hash(builder_features_list, build_settings_features_list) + enabled_features_extension = Touchlane::Features.generate_enabled_features_extension(builder_features_list, build_settings_features_list) - Managers::FileManager.save_data_to_file_in_json(project_features_file_path, features_hash) + Managers::FileManager.save_data_to_file(project_enabled_features_file_path, enabled_features_extension) +end + +def is_feature_extension_needed?(options, project_enabled_features_file_path) + !options[:features].nil? || File.exists?(project_enabled_features_file_path) end def set_xcconfig_for_configuration_of_project(lane_name, configuration, xcodeproj_path) diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb index 37b0f29..838eb0e 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/features.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -4,12 +4,12 @@ require_relative '../../../../templates/templates' module Touchlane class Features - def self.generate_enabled_features_hash(builder_features_list, build_settings_features_list) + def self.generate_enabled_features_extension(builder_features_list, build_settings_features_list) # Check is entered features contains in configuration file features_diff = builder_features_list - build_settings_features_list - if !features_diff.empty? + unless features_diff.empty? raise "Unexpected features: " + features_diff.join(', ') end @@ -17,7 +17,7 @@ module Touchlane enabled_features_extension_template = Templates::FeatureTemplates.enabled_features_extension utils = Managers::TemplateManager.new(builder_features_list) - utils.render(enabled_features_extension_template).strip.to_hash() + utils.render(enabled_features_extension_template).strip end private_class_method :new diff --git a/xcode/templates/templates/features_templates.rb b/xcode/templates/templates/features_templates.rb index 9c7cb44..ee049cf 100755 --- a/xcode/templates/templates/features_templates.rb +++ b/xcode/templates/templates/features_templates.rb @@ -22,7 +22,7 @@ public extension Feature { static var enabled: [Feature] { [ <% for @item in @items %> - .<%= @item %>, + \.<%= @item %>, <% end %> ] } From 175a97fc5f8884881dfeaa87dc8d19aa9371bf6b Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 28 Dec 2020 08:30:36 +0300 Subject: [PATCH 6/7] Code correction --- xcode/commonFastfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcode/commonFastfile b/xcode/commonFastfile index 0f2f795..4feb6df 100644 --- a/xcode/commonFastfile +++ b/xcode/commonFastfile @@ -413,7 +413,7 @@ def generate_enabled_features_extension_if_needed(options) if options[:features].nil? builder_features_list = [] # If Enabled.swift exists and features option is nil we need to create empty extension to avoid unexpected features else - builder_features_list = (options[:features] || []) + builder_features_list = options[:features] .split(",").map { |feature_name| feature_name.strip } # [ "Feature1", "Feature2", "Feature3" ] end From f684dbc0293d00ccab7f39a96b6eb7f134029d72 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 29 Dec 2020 14:43:47 +0300 Subject: [PATCH 7/7] Fix PR --- xcode/fastlane/touchlane/lib/touchlane/features.rb | 2 +- xcode/templates/templates/features_templates.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb index 838eb0e..cadfb0f 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/features.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -13,7 +13,7 @@ module Touchlane raise "Unexpected features: " + features_diff.join(', ') end - # Generate FeatureToggle extension hash from feature names + # Generate enabled features extension from feature names enabled_features_extension_template = Templates::FeatureTemplates.enabled_features_extension utils = Managers::TemplateManager.new(builder_features_list) diff --git a/xcode/templates/templates/features_templates.rb b/xcode/templates/templates/features_templates.rb index ee049cf..69eeeaf 100755 --- a/xcode/templates/templates/features_templates.rb +++ b/xcode/templates/templates/features_templates.rb @@ -3,7 +3,7 @@ module Templates def self.features_enum " -//MARK: - Generated feature toggles +// MARK: - Generated feature toggles public enum Feature: String, Codable, RawRepresentable, CaseIterable { <% for @item in @items %>