Add manager module and code correction

This commit is contained in:
Vlad 2020-12-15 10:59:14 +03:00
parent b3751302a9
commit 1f321c27c9
7 changed files with 67 additions and 59 deletions

View File

@ -1,6 +1,8 @@
require 'yaml' require 'yaml'
require 'erb' require 'erb'
require_relative 'managers/managers'
# Input files paths # Input files paths
build_settings_file_path = ARGV[0] build_settings_file_path = ARGV[0]
generated_features_enum_file_path = ARGV[1] generated_features_enum_file_path = ARGV[1]
@ -9,9 +11,9 @@ features_enum_template =
" "
//MARK: - Feature toggles //MARK: - Feature toggles
public enum FeatureToggles: String, Codable, RawRepresentable, CaseIterable { public enum FeatureToggle: String, Codable, RawRepresentable, CaseIterable {
<% for @item in @items %> <% for @feature in @features %>
case <%= @item %> case <%= @feature %>
<% end %> <% end %>
} }
" "
@ -19,10 +21,10 @@ public enum FeatureToggles: String, Codable, RawRepresentable, CaseIterable {
class FeatureUtils class FeatureUtils
include ERB::Util include ERB::Util
attr_accessor :items attr_accessor :features
def initialize(items) def initialize(features)
@items = items @features = features
end end
def render(template) def render(template)
@ -30,26 +32,9 @@ class FeatureUtils
end end
end end
def save(path, data) build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"]
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
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) utils = FeatureUtils.new(build_settings_features_list)
rendered_enum = utils.render(features_enum_template).strip
data = utils.render(features_enum_template).strip Managers::FileManager.save_data_to_file(generated_features_enum_file_path, rendered_enum)
save(generated_features_enum_file_path, data)

View File

@ -1,14 +1,18 @@
readonly build_settings_file_path="${PROJECT_DIR}/common/build_settings.yaml" # Input paths
readonly generated_file_path="${PROJECT_DIR}/${PRODUCT_NAME}/Resources/Features/FeatureToggles.swift" 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 # Features enunm generator script
echo "File ${PROJECT_DIR}/common/build_settings.yaml does not exist. Add this file and try again." 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 exit 1
fi fi
if ! [ -e ${generated_file_path} ]; then if ! [ -e ${FEATURES_ENUM_FILE_PATH} ]; then
echo "File ${PROJECT_DIR}/${PRODUCT_NAME}/Resources/Features/FeatureToggles.swift does not exist. Add this file and try again." echo "File ${FEATURES_ENUM_FILE_PATH} does not exist. Add this file and try again."
exit 1 exit 1
fi 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}

View File

@ -106,17 +106,17 @@ private_lane :addShield do |options|
end end
end end
private_lane :uploadFeaturesToProject do |options| private_lane :generateFeaturesJSONFile do |options|
app_target_folder_name = options[:appName] || $appName app_target_folder_name = options[:appName] || $appName
project_features_file_path = File.expand_path "../#{app_target_folder_name}/Resources/Features.json" 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] builder_features_list = options[:features]
.split(",").map { |feature_name| feature_name.strip } # [ "Feature1", "Feature2", "Feature3" ] .split(",").map { |feature_name| feature_name.strip } # [ "Feature1", "Feature2", "Feature3" ]
.reject { |feature_name| feature_name.empty? } .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 end
private_lane :buildConfiguration do |options| private_lane :buildConfiguration do |options|
@ -149,7 +149,7 @@ private_lane :buildConfiguration do |options|
installDependencies(options) installDependencies(options)
if options[:features] unless options[:features].nil?
uploadFeaturesToProject(options) uploadFeaturesToProject(options)
end end

View File

@ -1,5 +1,7 @@
require "yaml" require "yaml"
require_relative 'managers/managers'
module Touchlane module Touchlane
class Configuration class Configuration
def initialize(type, app_identifier, apple_id, team_id, itc_team_id) def initialize(type, app_identifier, apple_id, team_id, itc_team_id)

View File

@ -1,14 +1,15 @@
require 'json' require 'json'
require 'yaml'
require_relative 'managers/managers'
module Touchlane module Touchlane
class Features class Features
def self.generate_features_file_in_project(builder_features_list, common_features_file_path, project_features_file_path) def self.generate_features_file_in_project(builder_features_list, build_settings_file_path, project_features_file_path)
common_features_list = get_features_from_file(common_features_file_path)["features"] build_settings_features_list = Managers::FileManager.load_from_file_YAML(build_settings_file_path)["features"]
# Check is entered features contains in configuration file # 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? if !features_diff.empty?
raise "Unexpected features: " + features_diff.join(', ') raise "Unexpected features: " + features_diff.join(', ')
@ -16,27 +17,13 @@ module Touchlane
# Generate JSON from feature names # Generate JSON from feature names
feature_bodies = builder_features_list.map { |feature_name| { :name => feature_name, :enabled => true} } feature_bodies = builder_features_list.map { |feature_name| { :name => feature_name, :enabled => true} }
features = { :features => feature_bodies } features_full_body = { :features => feature_bodies }
features_json = JSON.pretty_generate(features) features_json = JSON.pretty_generate(features_full_body)
unless File.exists? project_features_file_path Managers::FileManager.save_data_to_file(project_features_file_path, features_json)
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
end end
def self.get_features_from_file(path) private_class_method :new
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
end end
end end

View File

@ -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

View File

@ -0,0 +1,3 @@
module Managers
require_relative "lib/file_manager"
end