From 7a5ddf62434941b801965514b6267058cb57a90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Thu, 1 Aug 2019 23:52:19 +0300 Subject: [PATCH 01/11] Add nesseccery scripts --- xcode/UnusedConfig.txt | 5 + xcode/build_phases/Unused.rb | 192 +++++++++++++++++++++++++++++++++++ xcode/build_phases/unused.sh | 1 + 3 files changed, 198 insertions(+) create mode 100644 xcode/UnusedConfig.txt create mode 100644 xcode/build_phases/Unused.rb create mode 100644 xcode/build_phases/unused.sh diff --git a/xcode/UnusedConfig.txt b/xcode/UnusedConfig.txt new file mode 100644 index 0000000..3dd1ffa --- /dev/null +++ b/xcode/UnusedConfig.txt @@ -0,0 +1,5 @@ +--ignored-resources: +Generated/ +Resources/Localization +Carthage/ +Pods/ \ No newline at end of file diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb new file mode 100644 index 0000000..dacf7ae --- /dev/null +++ b/xcode/build_phases/Unused.rb @@ -0,0 +1,192 @@ +#!/usr/bin/ruby +#encoding: utf-8 +Encoding.default_external = Encoding::UTF_8 +Encoding.default_internal = Encoding::UTF_8 + +$config_argv_key = "--config" +$ignored_argv_key = "--ignored" +$ignored_resources_key = "--ignored-resources:" +$log_file_name = "UnusedLog.txt" + +class Item + def initialize(file, line, at) + @file = file + @line = line + @at = at + 1 + if match = line.match(/(func|let|var|class|enum|struct|protocol)\s+(\w+)/) + @type = match.captures[0] + @name = match.captures[1] + end + end + + def modifiers + return @modifiers if @modifiers + @modifiers = [] + if match = @line.match(/(.*?)#{@type}/) + @modifiers = match.captures[0].split(" ") + end + return @modifiers + end + + def name + @name + end + + def file + @file + end + + def to_s + serialize + end + def to_str + serialize + end + + def full_file_path + Dir.pwd + '/' + @file + end + + def serialize + "#{@type.to_s} #{@name.to_s} from: #{@file}:#{@at} is unused" + end + + def to_xcode + "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" + end + + +end +class Unused + def find + items = [] + unused_warnings = [] + + regexps = parse_arguments + + all_files = Dir.glob("**/*.swift").reject do |path| + File.directory?(path) + end + + all_files.each { |my_text_file| + file_items = grab_items(my_text_file) + file_items = filter_items(file_items) + + non_private_items, private_items = file_items.partition { |f| !f.modifiers.include?("private") && !f.modifiers.include?("fileprivate") } + items += non_private_items + + # Usage within the file + if private_items.length > 0 + unused_warnings += find_usages_in_files([my_text_file], [], private_items, regexps) + end + + } + + xibs = Dir.glob("**/*.xib") + storyboards = Dir.glob("**/*.storyboard") + + unused_warnings += find_usages_in_files(all_files, xibs + storyboards, items, regexps) + + if unused_warnings.length > 0 + # show warning + puts "#{unused_warnings.map { |e| e.to_xcode }.join("\n")}" + # write log + File.open($log_file_name, "w") do |file| + file.write("Unused code warnings count: #{unused_warnings.length}\n\n") + file.write("#{unused_warnings.map { |e| e.serialize }.join("\n")}") + end + end + end + + def parse_arguments() + resources = [] + + arguments = ARGV.clone + + until arguments.empty? + item = arguments.shift + + # find --config file + if item == $config_argv_key + fileName = arguments.shift + + # add --ignored-resources to array + File.readlines(fileName).each do |line| + if !line.include? $ignored_resources_key + resources += [line.chomp] + end + end + end + + # add --ignored files to array + if item == $ignored_argv_key + resources += [arguments.shift] + end + end + + # create and return Regexp + resources.map { |r| Regexp.new(r) } + end + + # remove files, that maches ignored Regexps array + def ignore_files_with_regexps(files, regexps) + files.select { |f| regexps.all? { |r| r.match(f.file).nil? } } + end + + def find_usages_in_files(files, xibs, items_in, regexps) + items = items_in + usages = items.map { |f| 0 } + files.each { |file| + lines = File.readlines(file).map {|line| line.gsub(/^[^\/]*\/\/.*/, "") } + words = lines.join("\n").split(/\W+/) + words_arrray = words.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten + + wf = Hash[*words_arrray] + + items.each_with_index { |f, i| + usages[i] += (wf[f.name] || 0) + } + # Remove all items which has usage 2+ + indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i } + + # reduce usage array if we found some functions already + indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) } + } + + xibs.each { |xib| + lines = File.readlines(xib).map {|line| line.gsub(/^\s*\/\/.*/, "") } + full_xml = lines.join(" ") + classes = full_xml.scan(/(class|customClass)="([^"]+)"/).map { |cd| cd[1] } + classes_array = classes.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten + + wf = Hash[*classes_array] + + items.each_with_index { |f, i| + usages[i] += (wf[f.name] || 0) + } + # Remove all items which has usage 2+ + indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i } + + # reduce usage array if we found some functions already + indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) } + + } + + items = ignore_files_with_regexps(items, regexps) + end + + def grab_items(file) + lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") } + items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)} + end + + def filter_items(items) + items.select { |f| + !f.name.start_with?("test") && !f.modifiers.include?("@IBAction") && !f.modifiers.include?("override") && !f.modifiers.include?("@objc") && !f.modifiers.include?("@IBInspectable") + } + end + +end + + +Unused.new.find diff --git a/xcode/build_phases/unused.sh b/xcode/build_phases/unused.sh new file mode 100644 index 0000000..3301156 --- /dev/null +++ b/xcode/build_phases/unused.sh @@ -0,0 +1 @@ +ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.txt From 2c91968fdc441d0406b216713e9f7a59e37a93b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 16:53:13 +0300 Subject: [PATCH 02/11] Make config file .yml. Add OptionParser --- xcode/UnusedConfig.txt | 5 - xcode/UnusedConfig.yml | 5 + xcode/build_phases/Unused.rb | 271 +++++++++++++++++------------------ xcode/build_phases/unused.sh | 2 +- 4 files changed, 140 insertions(+), 143 deletions(-) delete mode 100644 xcode/UnusedConfig.txt create mode 100644 xcode/UnusedConfig.yml diff --git a/xcode/UnusedConfig.txt b/xcode/UnusedConfig.txt deleted file mode 100644 index 3dd1ffa..0000000 --- a/xcode/UnusedConfig.txt +++ /dev/null @@ -1,5 +0,0 @@ ---ignored-resources: -Generated/ -Resources/Localization -Carthage/ -Pods/ \ No newline at end of file diff --git a/xcode/UnusedConfig.yml b/xcode/UnusedConfig.yml new file mode 100644 index 0000000..8ac9173 --- /dev/null +++ b/xcode/UnusedConfig.yml @@ -0,0 +1,5 @@ +--ignored-resources: + - Generated/ + - Resources/Localization + - Carthage/ + - Pods/ \ No newline at end of file diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index dacf7ae..416b47e 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -1,190 +1,187 @@ #!/usr/bin/ruby #encoding: utf-8 +require 'yaml' +require 'optparse' + Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 -$config_argv_key = "--config" -$ignored_argv_key = "--ignored" -$ignored_resources_key = "--ignored-resources:" -$log_file_name = "UnusedLog.txt" - class Item - def initialize(file, line, at) - @file = file - @line = line - @at = at + 1 - if match = line.match(/(func|let|var|class|enum|struct|protocol)\s+(\w+)/) - @type = match.captures[0] - @name = match.captures[1] + def initialize(file, line, at) + @file = file + @line = line + @at = at + 1 + if match = line.match(/(func|let|var|class|enum|struct|protocol)\s+(\w+)/) + @type = match.captures[0] + @name = match.captures[1] + end end - end - def modifiers - return @modifiers if @modifiers - @modifiers = [] - if match = @line.match(/(.*?)#{@type}/) - @modifiers = match.captures[0].split(" ") + def modifiers + return @modifiers if @modifiers + @modifiers = [] + if match = @line.match(/(.*?)#{@type}/) + @modifiers = match.captures[0].split(" ") + end + return @modifiers end - return @modifiers - end - def name - @name - end + def name + @name + end - def file - @file - end + def file + @file + end - def to_s - serialize - end - def to_str - serialize - end + def to_s + serialize + end + def to_str + serialize + end - def full_file_path - Dir.pwd + '/' + @file - end + def full_file_path + Dir.pwd + '/' + @file + end - def serialize - "#{@type.to_s} #{@name.to_s} from: #{@file}:#{@at} is unused" - end + def serialize + "#{@type.to_s} #{@name.to_s} from: #{@file}:#{@at} is unused" + end - def to_xcode - "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" - end + def to_xcode + "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" + end end class Unused - def find - items = [] - unused_warnings = [] + def find + items = [] + unused_warnings = [] - regexps = parse_arguments + regexps = parse_arguments - all_files = Dir.glob("**/*.swift").reject do |path| - File.directory?(path) - end + all_files = Dir.glob("**/*.swift").reject do |path| + File.directory?(path) + end - all_files.each { |my_text_file| - file_items = grab_items(my_text_file) - file_items = filter_items(file_items) + all_files.each { |my_text_file| + file_items = grab_items(my_text_file) + file_items = filter_items(file_items) - non_private_items, private_items = file_items.partition { |f| !f.modifiers.include?("private") && !f.modifiers.include?("fileprivate") } - items += non_private_items + non_private_items, private_items = file_items.partition { |f| !f.modifiers.include?("private") && !f.modifiers.include?("fileprivate") } + items += non_private_items - # Usage within the file - if private_items.length > 0 - unused_warnings += find_usages_in_files([my_text_file], [], private_items, regexps) - end + # Usage within the file + if private_items.length > 0 + unused_warnings += find_usages_in_files([my_text_file], [], private_items, regexps) + end - } + } - xibs = Dir.glob("**/*.xib") - storyboards = Dir.glob("**/*.storyboard") + xibs = Dir.glob("**/*.xib") + storyboards = Dir.glob("**/*.storyboard") - unused_warnings += find_usages_in_files(all_files, xibs + storyboards, items, regexps) + unused_warnings += find_usages_in_files(all_files, xibs + storyboards, items, regexps) - if unused_warnings.length > 0 - # show warning - puts "#{unused_warnings.map { |e| e.to_xcode }.join("\n")}" - # write log - File.open($log_file_name, "w") do |file| - file.write("Unused code warnings count: #{unused_warnings.length}\n\n") - file.write("#{unused_warnings.map { |e| e.serialize }.join("\n")}") + if unused_warnings.length > 0 + # show warning + puts "#{unused_warnings.map { |e| e.to_xcode }.join("\n")}" + # write log + File.open("UnusedLog.txt", "w") do |file| + file.write("Unused code warnings count: #{unused_warnings.length}\n\n") + file.write("#{unused_warnings.map { |e| e.serialize }.join("\n")}") + end end end - end - def parse_arguments() - resources = [] + def parse_arguments() + resources = [] - arguments = ARGV.clone + options = {} + OptionParser.new do |opts| + options[:ignore] = [] - until arguments.empty? - item = arguments.shift + opts.on("-c", "--config=FileName") { |c| options[:config] = c } + opts.on("-i", "--ignore=a, b, c", Array) { |i| options[:ignore] += i } - # find --config file - if item == $config_argv_key - fileName = arguments.shift + end.parse! - # add --ignored-resources to array - File.readlines(fileName).each do |line| - if !line.include? $ignored_resources_key - resources += [line.chomp] - end - end - end + # find --config file + if !options[:config].nil? + fileName = options[:config] + resources += YAML.load_file(fileName).fetch("--ignored-resources") + elsif + puts "---------\n Warning: Config file is now provided \n---------" + end - # add --ignored files to array - if item == $ignored_argv_key - resources += [arguments.shift] - end - end + # find --ignored files + if !options[:ignore].nil? + resources += options[:ignore] + end - # create and return Regexp - resources.map { |r| Regexp.new(r) } - end + # create and return Regexp + resources.map { |r| Regexp.new(r) } + end - # remove files, that maches ignored Regexps array - def ignore_files_with_regexps(files, regexps) - files.select { |f| regexps.all? { |r| r.match(f.file).nil? } } - end + # remove files, that maches ignored Regexps array + def ignore_files_with_regexps(files, regexps) + files.select { |f| regexps.all? { |r| r.match(f.file).nil? } } + end - def find_usages_in_files(files, xibs, items_in, regexps) - items = items_in - usages = items.map { |f| 0 } - files.each { |file| - lines = File.readlines(file).map {|line| line.gsub(/^[^\/]*\/\/.*/, "") } - words = lines.join("\n").split(/\W+/) - words_arrray = words.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten + def find_usages_in_files(files, xibs, items_in, regexps) + items = items_in + usages = items.map { |f| 0 } + files.each { |file| + lines = File.readlines(file).map {|line| line.gsub(/^[^\/]*\/\/.*/, "") } + words = lines.join("\n").split(/\W+/) + words_arrray = words.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten - wf = Hash[*words_arrray] + wf = Hash[*words_arrray] - items.each_with_index { |f, i| - usages[i] += (wf[f.name] || 0) - } - # Remove all items which has usage 2+ - indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i } + items.each_with_index { |f, i| + usages[i] += (wf[f.name] || 0) + } + # Remove all items which has usage 2+ + indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i } - # reduce usage array if we found some functions already - indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) } - } + # reduce usage array if we found some functions already + indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) } + } - xibs.each { |xib| - lines = File.readlines(xib).map {|line| line.gsub(/^\s*\/\/.*/, "") } - full_xml = lines.join(" ") - classes = full_xml.scan(/(class|customClass)="([^"]+)"/).map { |cd| cd[1] } - classes_array = classes.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten + xibs.each { |xib| + lines = File.readlines(xib).map {|line| line.gsub(/^\s*\/\/.*/, "") } + full_xml = lines.join(" ") + classes = full_xml.scan(/(class|customClass)="([^"]+)"/).map { |cd| cd[1] } + classes_array = classes.group_by { |w| w }.map { |w, ws| [w, ws.length] }.flatten - wf = Hash[*classes_array] + wf = Hash[*classes_array] - items.each_with_index { |f, i| - usages[i] += (wf[f.name] || 0) - } - # Remove all items which has usage 2+ - indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i } + items.each_with_index { |f, i| + usages[i] += (wf[f.name] || 0) + } + # Remove all items which has usage 2+ + indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i } - # reduce usage array if we found some functions already - indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) } + # reduce usage array if we found some functions already + indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) } - } + } - items = ignore_files_with_regexps(items, regexps) - end + items = ignore_files_with_regexps(items, regexps) + end - def grab_items(file) - lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") } - items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)} - end + def grab_items(file) + lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") } + items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)} + end - def filter_items(items) - items.select { |f| - !f.name.start_with?("test") && !f.modifiers.include?("@IBAction") && !f.modifiers.include?("override") && !f.modifiers.include?("@objc") && !f.modifiers.include?("@IBInspectable") - } - end + def filter_items(items) + items.select { |f| + !f.name.start_with?("test") && !f.modifiers.include?("@IBAction") && !f.modifiers.include?("override") && !f.modifiers.include?("@objc") && !f.modifiers.include?("@IBInspectable") + } + end end diff --git a/xcode/build_phases/unused.sh b/xcode/build_phases/unused.sh index 3301156..01fe69d 100644 --- a/xcode/build_phases/unused.sh +++ b/xcode/build_phases/unused.sh @@ -1 +1 @@ -ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.txt +ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.yml From 453284d37b7d8be427307acbecb0bd7eb4a01514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 17:13:50 +0300 Subject: [PATCH 03/11] Add arguments parsing in .sh file --- xcode/build_phases/unused.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xcode/build_phases/unused.sh b/xcode/build_phases/unused.sh index 01fe69d..2c12160 100644 --- a/xcode/build_phases/unused.sh +++ b/xcode/build_phases/unused.sh @@ -1 +1,4 @@ -ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.yml +arguments=("$@") +ignored_files=$(IFS=, ; echo "${arguments[*]}") + +ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.yml --ignore ${ignored_files} From c6b0b0d7bf5a0c713102dcb43d833493bebb4e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 17:26:51 +0300 Subject: [PATCH 04/11] Genetare one huge warning --- xcode/build_phases/Unused.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index 416b47e..2f88d64 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -49,8 +49,8 @@ class Item "#{@type.to_s} #{@name.to_s} from: #{@file}:#{@at} is unused" end - def to_xcode - "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" + def to_xcode(count) + "Unused Code Warning! Total Count #{count}: warning: #{serialize} is unused" end @@ -87,7 +87,7 @@ class Unused if unused_warnings.length > 0 # show warning - puts "#{unused_warnings.map { |e| e.to_xcode }.join("\n")}" + puts "#{unused_warnings.map { |e| e.to_xcode unused_warnings.length }.join("\n")}" # write log File.open("UnusedLog.txt", "w") do |file| file.write("Unused code warnings count: #{unused_warnings.length}\n\n") From 66d2c4a885fa0529345419dcfb489ad66eb60ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 17:30:42 +0300 Subject: [PATCH 05/11] Refactor code a bit --- xcode/build_phases/Unused.rb | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index 2f88d64..b85cfb9 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -96,7 +96,7 @@ class Unused end end - def parse_arguments() + def parse_arguments resources = [] options = {} @@ -125,6 +125,17 @@ class Unused resources.map { |r| Regexp.new(r) } end + def grab_items(file) + lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") } + items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)} + end + + def filter_items(items) + items.select { |f| + !f.name.start_with?("test") && !f.modifiers.include?("@IBAction") && !f.modifiers.include?("override") && !f.modifiers.include?("@objc") && !f.modifiers.include?("@IBInspectable") + } + end + # remove files, that maches ignored Regexps array def ignore_files_with_regexps(files, regexps) files.select { |f| regexps.all? { |r| r.match(f.file).nil? } } @@ -171,18 +182,6 @@ class Unused items = ignore_files_with_regexps(items, regexps) end - - def grab_items(file) - lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") } - items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)} - end - - def filter_items(items) - items.select { |f| - !f.name.start_with?("test") && !f.modifiers.include?("@IBAction") && !f.modifiers.include?("override") && !f.modifiers.include?("@objc") && !f.modifiers.include?("@IBInspectable") - } - end - end From b5d26d3d2b780024a80fe907230d04b0f96324f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 17:47:25 +0300 Subject: [PATCH 06/11] Fix typos --- xcode/UnusedConfig.yml | 2 +- xcode/build_phases/Unused.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xcode/UnusedConfig.yml b/xcode/UnusedConfig.yml index 8ac9173..ba35344 100644 --- a/xcode/UnusedConfig.yml +++ b/xcode/UnusedConfig.yml @@ -1,4 +1,4 @@ ---ignored-resources: +ignored-resources: - Generated/ - Resources/Localization - Carthage/ diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index b85cfb9..02e6dde 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -111,9 +111,9 @@ class Unused # find --config file if !options[:config].nil? fileName = options[:config] - resources += YAML.load_file(fileName).fetch("--ignored-resources") + resources += YAML.load_file(fileName).fetch("ignored-resources") elsif - puts "---------\n Warning: Config file is now provided \n---------" + puts "---------\n Warning: Config file is not provided \n---------" end # find --ignored files From 3d61e3db82ef5fb2aeb95a1e3885d05cb8c67349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 18:30:43 +0300 Subject: [PATCH 07/11] Make --ignore argiment optional --- xcode/build_phases/Unused.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index b85cfb9..48a3ffd 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -104,7 +104,7 @@ class Unused options[:ignore] = [] opts.on("-c", "--config=FileName") { |c| options[:config] = c } - opts.on("-i", "--ignore=a, b, c", Array) { |i| options[:ignore] += i } + opts.on("-i", "--ignore [a, b, c]", Array) { |i| options[:ignore] += i if !i.nil? } end.parse! From 463eda96ecc7a72db7f71f8f1c633a0354e1e55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 19:30:51 +0300 Subject: [PATCH 08/11] Refactor warnings appearence. Add script source link --- xcode/build_phases/Unused.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index 48a3ffd..230c30c 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -1,3 +1,4 @@ +# source: https://github.com/PaulTaykalo/swift-scripts/blob/master/unused.rb #!/usr/bin/ruby #encoding: utf-8 require 'yaml' @@ -46,11 +47,10 @@ class Item end def serialize - "#{@type.to_s} #{@name.to_s} from: #{@file}:#{@at} is unused" + "#{@full_file_path}:#{@at} #{@type.to_s} #{@name.to_s} is unused" end - - def to_xcode(count) - "Unused Code Warning! Total Count #{count}: warning: #{serialize} is unused" + def to_xcode + "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" end @@ -87,7 +87,8 @@ class Unused if unused_warnings.length > 0 # show warning - puts "#{unused_warnings.map { |e| e.to_xcode unused_warnings.length }.join("\n")}" + puts "Unused Code Warning!: warning: Total Count #{unused_warnings.length}" + puts "#{unused_warnings.map { |e| e.to_xcode}.join("\n")}" # write log File.open("UnusedLog.txt", "w") do |file| file.write("Unused code warnings count: #{unused_warnings.length}\n\n") From 9fe44e481e2b2358a69756feb232128e728d07ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 19:38:14 +0300 Subject: [PATCH 09/11] Fix file output string --- xcode/build_phases/Unused.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index c84321f..0c25e07 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -47,7 +47,7 @@ class Item end def serialize - "#{@full_file_path}:#{@at} #{@type.to_s} #{@name.to_s} is unused" + "#{@file}:#{@at} #{@type.to_s} #{@name.to_s} is unused" end def to_xcode "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" From 7e6a55d72f7af4a6a73627276787674fab6ad88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 2 Aug 2019 19:48:14 +0300 Subject: [PATCH 10/11] Change output log format --- xcode/build_phases/Unused.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index 0c25e07..66ff067 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -47,7 +47,7 @@ class Item end def serialize - "#{@file}:#{@at} #{@type.to_s} #{@name.to_s} is unused" + "#{@file} has unused \"#{@type.to_s} #{@name.to_s}\"" end def to_xcode "#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused" From 82ca5cbbc095403f20508c8388d49288c7d96a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B8=CC=86=20=D0=A1=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Mon, 5 Aug 2019 13:15:05 +0300 Subject: [PATCH 11/11] Rename ignore to exclude --- xcode/UnusedConfig.yml | 5 +++-- xcode/build_phases/Unused.rb | 14 +++++++------- xcode/build_phases/unused.sh | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/xcode/UnusedConfig.yml b/xcode/UnusedConfig.yml index ba35344..619dd71 100644 --- a/xcode/UnusedConfig.yml +++ b/xcode/UnusedConfig.yml @@ -1,5 +1,6 @@ -ignored-resources: +excluded-resources: - Generated/ - Resources/Localization - Carthage/ - - Pods/ \ No newline at end of file + - Pods/ + - FormattingService.swift \ No newline at end of file diff --git a/xcode/build_phases/Unused.rb b/xcode/build_phases/Unused.rb index 66ff067..478a8d2 100644 --- a/xcode/build_phases/Unused.rb +++ b/xcode/build_phases/Unused.rb @@ -102,24 +102,24 @@ class Unused options = {} OptionParser.new do |opts| - options[:ignore] = [] + options[:exclude] = [] opts.on("-c", "--config=FileName") { |c| options[:config] = c } - opts.on("-i", "--ignore [a, b, c]", Array) { |i| options[:ignore] += i if !i.nil? } + opts.on("-i", "--exclude [a, b, c]", Array) { |i| options[:exclude] += i if !i.nil? } end.parse! # find --config file if !options[:config].nil? fileName = options[:config] - resources += YAML.load_file(fileName).fetch("ignored-resources") + resources += YAML.load_file(fileName).fetch("excluded-resources") elsif puts "---------\n Warning: Config file is not provided \n---------" end - # find --ignored files - if !options[:ignore].nil? - resources += options[:ignore] + # find --exclude files + if !options[:exclude].nil? + resources += options[:exclude] end # create and return Regexp @@ -137,7 +137,7 @@ class Unused } end - # remove files, that maches ignored Regexps array + # remove files, that maches excluded Regexps array def ignore_files_with_regexps(files, regexps) files.select { |f| regexps.all? { |r| r.match(f.file).nil? } } end diff --git a/xcode/build_phases/unused.sh b/xcode/build_phases/unused.sh index 2c12160..5a0a906 100644 --- a/xcode/build_phases/unused.sh +++ b/xcode/build_phases/unused.sh @@ -1,4 +1,4 @@ arguments=("$@") ignored_files=$(IFS=, ; echo "${arguments[*]}") -ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.yml --ignore ${ignored_files} +ruby ${PROJECT_DIR}/build-scripts/xcode/build_phases/Unused.rb --config ${PROJECT_DIR}/build-scripts/xcode/UnusedConfig.yml --exclude ${ignored_files}