From 1f321c27c97cb4d9c3268af67954d906f6ef2f4c Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 15 Dec 2020 10:59:14 +0300 Subject: [PATCH] Add manager module and code correction --- .../features_generator/features_generator.rb | 37 ++++++------------- .../features_generator/features_generator.sh | 18 +++++---- xcode/commonFastfile | 8 ++-- .../touchlane/lib/touchlane/configuration.rb | 2 + .../touchlane/lib/touchlane/features.rb | 31 +++++----------- xcode/managers/lib/file_manager.rb | 27 ++++++++++++++ xcode/managers/managers.rb | 3 ++ 7 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 xcode/managers/lib/file_manager.rb create mode 100644 xcode/managers/managers.rb diff --git a/xcode/build_phases/features_generator/features_generator.rb b/xcode/build_phases/features_generator/features_generator.rb index c770014..e35bb41 100755 --- a/xcode/build_phases/features_generator/features_generator.rb +++ b/xcode/build_phases/features_generator/features_generator.rb @@ -1,6 +1,8 @@ require 'yaml' require 'erb' +require_relative 'managers/managers' + # Input files paths build_settings_file_path = ARGV[0] generated_features_enum_file_path = ARGV[1] @@ -9,9 +11,9 @@ features_enum_template = " //MARK: - Feature toggles -public enum FeatureToggles: String, Codable, RawRepresentable, CaseIterable { - <% for @item in @items %> - case <%= @item %> +public enum FeatureToggle: String, Codable, RawRepresentable, CaseIterable { + <% for @feature in @features %> + case <%= @feature %> <% end %> } " @@ -19,10 +21,10 @@ public enum FeatureToggles: String, Codable, RawRepresentable, CaseIterable { class FeatureUtils include ERB::Util - attr_accessor :items + attr_accessor :features - def initialize(items) - @items = items + def initialize(features) + @features = features end def render(template) @@ -30,26 +32,9 @@ class FeatureUtils end end -def save(path, data) - unless File.exists? path - raise "Unable to safe features to file at #{path}" - else - File.open(path, "w") do |f| - f.write(data) - end - end -end +build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"] -def get_features_from_file(path) - unless File.exists? path - raise "Unable to load features from file at #{path}" - else - YAML.load_file(path) - end -end - -build_settings_features_list = get_features_from_file(build_settings_file_path)["features"] utils = FeatureUtils.new(build_settings_features_list) +rendered_enum = utils.render(features_enum_template).strip -data = utils.render(features_enum_template).strip -save(generated_features_enum_file_path, data) +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 2c8f94b..dbe7b22 100755 --- a/xcode/build_phases/features_generator/features_generator.sh +++ b/xcode/build_phases/features_generator/features_generator.sh @@ -1,14 +1,18 @@ -readonly build_settings_file_path="${PROJECT_DIR}/common/build_settings.yaml" -readonly generated_file_path="${PROJECT_DIR}/${PRODUCT_NAME}/Resources/Features/FeatureToggles.swift" +# 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/FeatureToggles.swift} -if ! [ -e ${build_settings_file_path} ]; then - echo "File ${PROJECT_DIR}/common/build_settings.yaml does not exist. Add this file and try again." +# Features enunm generator script +readonly GENERATOR_SCRIPT=${${PROJECT_DIR}/build-scripts/xcode/build_phases/features_generator/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 ${generated_file_path} ]; then - echo "File ${PROJECT_DIR}/${PRODUCT_NAME}/Resources/Features/FeatureToggles.swift does not exist. Add this file and try again." +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 ${PROJECT_DIR}/build-scripts/xcode/build_phases/features_generator/features_generator.rb ${build_settings_file_path} ${generated_file_path} +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 4efcb84..07f75da 100644 --- a/xcode/commonFastfile +++ b/xcode/commonFastfile @@ -106,17 +106,17 @@ private_lane :addShield do |options| end end -private_lane :uploadFeaturesToProject do |options| +private_lane :generateFeaturesJSONFile do |options| app_target_folder_name = options[:appName] || $appName project_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features.json" - common_features_file_path = File.expand_path "../common/build_options/Features.yaml" + 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" ] .reject { |feature_name| feature_name.empty? } - Touchlane::Features.generate_features_file_in_project(builder_features_list, common_features_file_path, project_features_file_path) + Touchlane::Features.generate_features_file_in_project(builder_features_list, build_settings_file_path, project_features_file_path) end private_lane :buildConfiguration do |options| @@ -149,7 +149,7 @@ private_lane :buildConfiguration do |options| installDependencies(options) - if options[:features] + unless options[:features].nil? uploadFeaturesToProject(options) end diff --git a/xcode/fastlane/touchlane/lib/touchlane/configuration.rb b/xcode/fastlane/touchlane/lib/touchlane/configuration.rb index 02cc4d3..00b93a1 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/configuration.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/configuration.rb @@ -1,5 +1,7 @@ require "yaml" +require_relative 'managers/managers' + module Touchlane class Configuration def initialize(type, app_identifier, apple_id, team_id, itc_team_id) diff --git a/xcode/fastlane/touchlane/lib/touchlane/features.rb b/xcode/fastlane/touchlane/lib/touchlane/features.rb index 00a4e3e..0173dc4 100644 --- a/xcode/fastlane/touchlane/lib/touchlane/features.rb +++ b/xcode/fastlane/touchlane/lib/touchlane/features.rb @@ -1,14 +1,15 @@ require 'json' -require 'yaml' + +require_relative 'managers/managers' module Touchlane class Features - def self.generate_features_file_in_project(builder_features_list, common_features_file_path, project_features_file_path) - common_features_list = get_features_from_file(common_features_file_path)["features"] + def self.generate_features_file_in_project(builder_features_list, build_settings_file_path, project_features_file_path) + build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"] # Check is entered features contains in configuration file - features_diff = builder_features_list - common_features_list + features_diff = builder_features_list - build_settings_features_list if !features_diff.empty? raise "Unexpected features: " + features_diff.join(', ') @@ -16,27 +17,13 @@ module Touchlane # Generate JSON from feature names feature_bodies = builder_features_list.map { |feature_name| { :name => feature_name, :enabled => true} } - features = { :features => feature_bodies } - features_json = JSON.pretty_generate(features) + features_full_body = { :features => feature_bodies } + features_json = JSON.pretty_generate(features_full_body) - unless File.exists? project_features_file_path - raise "Unable to load features to file at #{path}" - else - File.open(project_features_file_path, "w") do |f| - f.write(features_json) - end - end + Managers::FileManager.save_data_to_file(project_features_file_path, features_json) end - def self.get_features_from_file(path) - unless File.exists? path - raise "Unable to load features from file at #{path}" - else - YAML.load_file(path) - end - end - - private_class_method :new, :get_features_from_file + 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..72381f5 --- /dev/null +++ b/xcode/managers/lib/file_manager.rb @@ -0,0 +1,27 @@ +require 'yaml' + +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 + + 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