split swiftlint.rb file to some files

This commit is contained in:
Maxim Sorokin 2020-07-29 11:41:36 +03:00
parent ce8f6746bf
commit 2285e1d18e
9 changed files with 259 additions and 245 deletions

View File

@ -0,0 +1,5 @@
class Array
def nilOrEmpty?
self.nil? or self.empty?
end
end

View File

@ -0,0 +1,6 @@
class CommandUtils
def self.make_command(command)
command = command.to_s
return `#{command}`
end
end

View File

@ -0,0 +1,35 @@
require_relative 'array_extension.rb'
require_relative 'command_utils.rb'
require_relative 'string_extension.rb'
class GitСaretaker < CommandUtils
def self.get_modified_files
non_indexed_files = get_files_from('git diff --name-only | sed s/.*/"&,"/ ')
indexed_files = get_files_from('git diff --cached --name-only | sed s/.*/"&,"/ ')
modified_files = non_indexed_files + indexed_files
unique_modified_files = modified_files.uniq
unique_modified_swift_files = []
if not unique_modified_files.nilOrEmpty?
unique_modified_swift_files = unique_modified_files.select { |file_path|
file_path.to_s.filter_allowed_symbol_into_path
file_path.to_s.include? '.swift'
}
end
return unique_modified_swift_files
end
def self.get_creation_date(file_path)
git_command = 'git log --follow --format=%cD --reverse -- ' + file_path + ' | head -1'
return make_command(git_command)
end
private
def self.get_files_from(command)
files_as_string = make_command(command)
return files_as_string.split(',')
end
end

View File

@ -0,0 +1,63 @@
require 'optparse'
require 'ostruct'
require_relative 'array_extension.rb'
class SettingOption
def initialize
@options = OpenStruct.new
OptionParser.new do |opt|
opt.on('-s', '--source_directory STRING', 'The directory of source') { |option| @options.source_directory = option }
opt.on('-p', '--pods_directory STRING', 'The directory of pods') { |option| @options.pods_directory = option }
opt.on('-c', '--check_mode MODE', 'The mode of check is "fully" or "simplified"') { |option| @options.check_mode = option }
opt.on('-u', '--use_multiple BOOL', 'The flag indicates the use of multiple yaml swiftlint configurations') { |option| @options.use_multiple = option }
opt.on('-d', '--source_date DATE', 'The date of grouping files according touchin and old swiftlint rules') { |option| @options.source_date = option }
opt.on('-y', '--touchin_swiftlint_yaml_path STRING', 'The path to the touchin swiftlint yaml relative to the source directory') { |option| @options.touchin_swiftlint_yaml_path = option }
opt.on('-g', '--depth_git_count Int', 'The depth between the git directory and sources directory') { |option| @options.depth_git_count = option }
end.parse!
if @options.check_mode.to_s.nilOrEmpty?
@options.check_mode = 'fully'
end
if @options.use_multiple.to_s.nilOrEmpty?
@options.use_multiple = 'false'
end
if @options.depth_git_count.to_s.nilOrEmpty?
@options.depth_git_count = 0
end
if @options.touchin_swiftlint_yaml_path.to_s.nilOrEmpty?
@options.touchin_swiftlint_yaml_path = '/build-scripts/xcode/.swiftlint.yml'
end
end
def source_directory
@options.source_directory
end
def source_date
@options.source_date
end
def pods_directory
@options.pods_directory
end
def check_mode
@options.check_mode
end
def use_multiple
@options.use_multiple
end
def depth_git_count
@options.depth_git_count
end
def touchin_swiftlint_yaml_path
@options.touchin_swiftlint_yaml_path
end
end

View File

@ -1,229 +1,10 @@
#https://github.com/TouchInstinct/Styleguide/blob/multiple_swiftlint/IOS/Guides/BuildScripts/Multiple_Swiftlint_Guide.md
require 'yaml'
require 'optparse'
require 'ostruct'
require 'date'
require 'fileutils'
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
class String
def with_wrapped_whitespace
self.gsub(/\s+/, '\ ')
end
def filter_allowed_symbol_into_path
self.gsub!(/[^0-9A-Za-z \-+.\/]/, '')
end
def true?
self.to_s.downcase == "true"
end
def add_back_to_path(count)
string = self
count.to_i.times { |i|
string = '../' + string
}
return string
end
def nilOrEmpty?
self.nil? or self.empty?
end
end
class Array
def nilOrEmpty?
self.nil? or self.empty?
end
end
class YamlManager
def initialize(swiftlint_yaml_path)
@swiftlint_yaml_path = swiftlint_yaml_path
@configuration ||= YAML.load(File.read(@swiftlint_yaml_path))
end
def get_configuration(key)
@configuration[key]
end
def update(key, new_configuration_values)
@configuration[key] = new_configuration_values
save_settings(@configuration)
end
private
def save_settings(settings)
File.write(@swiftlint_yaml_path, settings.to_yaml)
end
end
class SettingOption
def initialize
@options = OpenStruct.new
OptionParser.new do |opt|
opt.on('-s', '--source_directory STRING', 'The directory of source') { |option| @options.source_directory = option }
opt.on('-p', '--pods_directory STRING', 'The directory of pods') { |option| @options.pods_directory = option }
opt.on('-c', '--check_mode MODE', 'The mode of check is "fully" or "simplified"') { |option| @options.check_mode = option }
opt.on('-u', '--use_multiple BOOL', 'The flag indicates the use of multiple yaml swiftlint configurations') { |option| @options.use_multiple = option }
opt.on('-d', '--source_date DATE', 'The date of grouping files according touchin and old swiftlint rules') { |option| @options.source_date = option }
opt.on('-y', '--touchin_swiftlint_yaml_path STRING', 'The path to the touchin swiftlint yaml relative to the source directory') { |option| @options.touchin_swiftlint_yaml_path = option }
opt.on('-g', '--depth_git_count Int', 'The depth between the git directory and sources directory') { |option| @options.depth_git_count = option }
end.parse!
if @options.check_mode.to_s.nilOrEmpty?
@options.check_mode = 'fully'
end
if @options.use_multiple.to_s.nilOrEmpty?
@options.use_multiple = 'false'
end
if @options.depth_git_count.to_s.nilOrEmpty?
@options.depth_git_count = 0
end
if @options.touchin_swiftlint_yaml_path.to_s.nilOrEmpty?
@options.touchin_swiftlint_yaml_path = '/build-scripts/xcode/.swiftlint.yml'
end
end
def source_directory
@options.source_directory
end
def source_date
@options.source_date
end
def pods_directory
@options.pods_directory
end
def check_mode
@options.check_mode
end
def use_multiple
@options.use_multiple
end
def depth_git_count
@options.depth_git_count
end
def touchin_swiftlint_yaml_path
@options.touchin_swiftlint_yaml_path
end
end
class CommandUtils
def self.make_command(command)
command = command.to_s
return `#{command}`
end
end
class GitСaretaker < CommandUtils
def self.get_modified_files
non_indexed_files = get_files_from('git diff --name-only | sed s/.*/"&,"/ ')
indexed_files = get_files_from('git diff --cached --name-only | sed s/.*/"&,"/ ')
modified_files = non_indexed_files + indexed_files
unique_modified_files = modified_files.uniq
unique_modified_swift_files = []
if not unique_modified_files.nilOrEmpty?
unique_modified_swift_files = unique_modified_files.select { |file_path|
file_path.to_s.filter_allowed_symbol_into_path
file_path.to_s.include? '.swift'
}
end
return unique_modified_swift_files
end
def self.get_creation_date(file_path)
git_command = 'git log --follow --format=%cD --reverse -- ' + file_path + ' | head -1'
return make_command(git_command)
end
private
def self.get_files_from(command)
files_as_string = make_command(command)
return files_as_string.split(',')
end
end
class SwiftFileManager
def initialize(excluded_files, source_date)
if not source_date.nilOrEmpty?
@source_date = Date.parse(source_date)
end
@excluded_files = excluded_files
@new_files = []
@old_files = []
end
def old_files
@old_files
end
def new_files
@new_files
end
def find_list_file_paths(start_folder)
swift_files = File.join('**', '*.swift')
Dir.glob(swift_files, base: start_folder) { |file_path|
if not is_excluded_file(file_path)
compare_timestamp(file_path)
end
}
end
def find_list_file_paths_from(files_path)
files_path.each { |file_path|
if not is_excluded_file(file_path)
compare_timestamp(file_path)
end
}
end
def is_excluded_file(file_path)
@excluded_files.each do |exclude_file_path|
if file_path.include? exclude_file_path
return true
end
end
return false
end
private
def compare_timestamp(file_path)
wrapped_whitespace_file_path = file_path.with_wrapped_whitespace
creation_date_string = GitСaretaker.get_creation_date(wrapped_whitespace_file_path)
puts file_path
if creation_date_string.nilOrEmpty?
@old_files.push(file_path)
puts 'Not found the creation date'
else
creation_date = Date.parse(creation_date_string)
puts creation_date
if @source_date < creation_date
@new_files.push(file_path)
else
@old_files.push(file_path)
end
end
end
end
require_relative 'array_extension.rb'
require_relative 'git_caretaker.rb'
require_relative 'string_extension.rb'
require_relative 'swift_file_manager.rb'
require_relative 'yaml_manager.rb'
class StrategyMaker
def initialize(source_directory, pods_directory, touchin_swiftlint_yaml_path)
@ -354,6 +135,13 @@ class StrategyMaker
def run_bash_command(bash_command)
exit (exec bash_command)
end
def create_yaml_managers_and_copy_temporary_files
create_copy_temporary_files
@touchin_swiftlint_yaml_manager = YamlManager.new(@touchin_swiftlint_yaml_temporary_path)
@old_swiftlint_yaml_manager = YamlManager.new(@old_swiftlint_yaml_temporary_path)
end
def create_copy_temporary_files
create_copy_temporary_touchin_files
@ -365,13 +153,6 @@ class StrategyMaker
FileUtils.cp @touchin_swiftlint_yaml_path, @touchin_swiftlint_yaml_temporary_path
end
def create_yaml_managers_and_copy_temporary_files
create_copy_temporary_files
@touchin_swiftlint_yaml_manager = YamlManager.new(@touchin_swiftlint_yaml_temporary_path)
@old_swiftlint_yaml_manager = YamlManager.new(@old_swiftlint_yaml_temporary_path)
end
def unique_exclude_files(touchin_swiftlint_yaml_manager, old_swiftlint_yaml_manager)
touchin_excluded_files = touchin_swiftlint_yaml_manager.get_configuration('excluded')
old_excluded_files = old_swiftlint_yaml_manager.get_configuration('excluded')
@ -379,17 +160,4 @@ class StrategyMaker
unique_exclude_files = common_exclude_files.uniq
return unique_exclude_files
end
end
setting = SettingOption.new
maker = StrategyMaker.new(setting.source_directory, setting.pods_directory, setting.touchin_swiftlint_yaml_path)
if setting.check_mode.eql? 'fully' and setting.use_multiple.true?
maker.make_fully_multiple_strategy(setting.source_date)
elsif setting.check_mode.eql? 'fully' and not setting.use_multiple.true?
maker.make_fully_single_strategy
elsif setting.check_mode.eql? 'simplified' and setting.use_multiple.true?
maker.make_simplified_multiple_strategy(setting.source_date, setting.depth_git_count)
elsif setting.check_mode.eql? 'simplified' and not setting.use_multiple.true?
maker.make_simplified_single_strategy(setting.depth_git_count)
end
end

View File

@ -0,0 +1,25 @@
class String
def with_wrapped_whitespace
self.gsub(/\s+/, '\ ')
end
def filter_allowed_symbol_into_path
self.gsub!(/[^0-9A-Za-z \-+.\/]/, '')
end
def true?
self.to_s.downcase == "true"
end
def add_back_to_path(count)
string = self
count.to_i.times { |i|
string = '../' + string
}
return string
end
def nilOrEmpty?
self.nil? or self.empty?
end
end

View File

@ -0,0 +1,69 @@
require 'fileutils'
require 'date'
require_relative 'git_caretaker.rb'
class SwiftFileManager
def initialize(excluded_files, source_date)
if not source_date.nilOrEmpty?
@source_date = Date.parse(source_date)
end
@excluded_files = excluded_files
@new_files = []
@old_files = []
end
def old_files
@old_files
end
def new_files
@new_files
end
def find_list_file_paths(start_folder)
swift_files = File.join('**', '*.swift')
Dir.glob(swift_files, base: start_folder) { |file_path|
if not is_excluded_file(file_path)
compare_timestamp(file_path)
end
}
end
def find_list_file_paths_from(files_path)
files_path.each { |file_path|
if not is_excluded_file(file_path)
compare_timestamp(file_path)
end
}
end
def is_excluded_file(file_path)
@excluded_files.each do |exclude_file_path|
if file_path.include? exclude_file_path
return true
end
end
return false
end
private
def compare_timestamp(file_path)
wrapped_whitespace_file_path = file_path.with_wrapped_whitespace
creation_date_string = GitСaretaker.get_creation_date(wrapped_whitespace_file_path)
puts file_path
if creation_date_string.nilOrEmpty?
@old_files.push(file_path)
puts 'Not found the creation date'
else
creation_date = Date.parse(creation_date_string)
puts creation_date
if @source_date < creation_date
@new_files.push(file_path)
else
@old_files.push(file_path)
end
end
end
end

View File

@ -0,0 +1,19 @@
#https://github.com/TouchInstinct/Styleguide/blob/multiple_swiftlint/IOS/Guides/BuildScripts/Multiple_Swiftlint_Guide.md
require_relative 'setting_option.rb'
require_relative 'strategy_maker.rb'
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
setting = SettingOption.new
maker = StrategyMaker.new(setting.source_directory, setting.pods_directory, setting.touchin_swiftlint_yaml_path)
if setting.check_mode.eql? 'fully' and setting.use_multiple.true?
maker.make_fully_multiple_strategy(setting.source_date)
elsif setting.check_mode.eql? 'fully' and not setting.use_multiple.true?
maker.make_fully_single_strategy
elsif setting.check_mode.eql? 'simplified' and setting.use_multiple.true?
maker.make_simplified_multiple_strategy(setting.source_date, setting.depth_git_count)
elsif setting.check_mode.eql? 'simplified' and not setting.use_multiple.true?
maker.make_simplified_single_strategy(setting.depth_git_count)
end

View File

@ -0,0 +1,24 @@
require 'yaml'
require 'fileutils'
class YamlManager
def initialize(swiftlint_yaml_path)
@swiftlint_yaml_path = swiftlint_yaml_path
@configuration ||= YAML.load(File.read(@swiftlint_yaml_path))
end
def get_configuration(key)
@configuration[key]
end
def update(key, new_configuration_values)
@configuration[key] = new_configuration_values
save_settings(@configuration)
end
private
def save_settings(settings)
File.write(@swiftlint_yaml_path, settings.to_yaml)
end
end