diff --git a/xcode/build_phases/features_generator/features_generator.rb b/xcode/build_phases/features_generator/features_generator.rb new file mode 100755 index 0000000..6601ba6 --- /dev/null +++ b/xcode/build_phases/features_generator/features_generator.rb @@ -0,0 +1,45 @@ +require 'yaml' +require 'erb' + +require_relative '../../managers/managers' + +# 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? + raise "There are no features in " + build_settings_file_path +end + +# Generate enum Feature Toggles +utils = FeatureUtils.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 new file mode 100755 index 0000000..b41cb44 --- /dev/null +++ b/xcode/build_phases/features_generator/features_generator.sh @@ -0,0 +1,19 @@ +# 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} + +# Features enunm generator script +readonly CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +readonly GENERATOR_SCRIPT=${CURRENT_DIR}/features_generator.rb + +if ! [ -e ${BUILD_SETTINGS_FILE_PATH} ]; then + echo "File ${BUILD_SETTINGS_FILE_PATH} does not exist. Add this file and try again." + exit 1 +fi + +if ! [ -e ${FEATURES_ENUM_FILE_PATH} ]; then + echo "File ${FEATURES_ENUM_FILE_PATH} does not exist. Add this file and try again." + exit 1 +fi + +ruby ${GENERATOR_SCRIPT} ${BUILD_SETTINGS_FILE_PATH} ${FEATURES_ENUM_FILE_PATH} \ No newline at end of file diff --git a/xcode/commonFastfile b/xcode/commonFastfile index 3886625..f6fbb5a 100644 --- a/xcode/commonFastfile +++ b/xcode/commonFastfile @@ -1,6 +1,7 @@ $appName = File.basename(Dir['../*.xcworkspace'].first, '.*') require_relative 'fastlane/touchlane/lib/touchlane' +require_relative 'managers/managers' # ugly hack to add support for custom storage @@ -136,6 +137,10 @@ private_lane :buildConfiguration do |options| installDependencies(options) + unless options[:features].nil? + generateFeaturesFile(options) + end + if !(options[:uploadToFabric] || options[:uploadToAppStore]) options[:skip_package_ipa] = true @@ -397,6 +402,22 @@ 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) + app_target_folder_name = options[:appName] || $appName + + project_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features/Features.json" + 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" ] + + 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) + + Managers::FileManager.save_data_to_file_in_json(project_features_file_path, features_hash) +end + def set_xcconfig_for_configuration_of_project(lane_name, configuration, xcodeproj_path) require 'xcodeproj' diff --git a/xcode/fastlane/touchlane/lib/touchlane.rb b/xcode/fastlane/touchlane/lib/touchlane.rb index f366ab1..7ece7cd 100644 --- a/xcode/fastlane/touchlane/lib/touchlane.rb +++ b/xcode/fastlane/touchlane/lib/touchlane.rb @@ -1,5 +1,6 @@ module Touchlane require_relative "touchlane/configuration_type" require_relative "touchlane/configuration" + require_relative "touchlane/features" require_relative "match/storage/local_storage" -end \ No newline at end of file +end diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb new file mode 100644 index 0000000..e6e65c5 --- /dev/null +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -0,0 +1,22 @@ +module Touchlane + class Features + + def self.generate_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 + + if !features_diff.empty? + 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() + end + + private_class_method :new + + end +end diff --git a/xcode/managers/lib/file_manager.rb b/xcode/managers/lib/file_manager.rb new file mode 100644 index 0000000..45bdda8 --- /dev/null +++ b/xcode/managers/lib/file_manager.rb @@ -0,0 +1,33 @@ +require 'yaml' +require 'json' + +module Managers + class FileManager + + def self.save_data_to_file(path, data) + unless File.exists? path + raise "Unable to save data to file at #{path}" + else + File.open(path, "w") do |f| + f.write(data) + end + end + end + + def self.load_from_file_YAML(path) + unless File.exists? path + raise "Unable to load data from file at #{path}" + else + YAML.load_file(path) + end + end + + def self.save_data_to_file_in_json(path, data) + json_data = JSON.pretty_generate(data) + save_data_to_file(path, json_data) + end + + private_class_method :new + + end +end diff --git a/xcode/managers/managers.rb b/xcode/managers/managers.rb new file mode 100644 index 0000000..81e6279 --- /dev/null +++ b/xcode/managers/managers.rb @@ -0,0 +1,3 @@ +module Managers + require_relative "lib/file_manager" +end