Skip to content

Instantly share code, notes, and snippets.

@vdugnist
Last active April 13, 2018 22:36
Show Gist options
  • Save vdugnist/99308659addc02d77bdfcb86435d160a to your computer and use it in GitHub Desktop.
Save vdugnist/99308659addc02d77bdfcb86435d160a to your computer and use it in GitHub Desktop.
Check that imported category exists in targets compile source.
require 'xcodeproj'
require 'set'
SOURCE_FOLDER=ARGV[0] ? ARGV[0] : ENV['PROJECT_DIR']
PROJECT_PATH=ARGV[1] ? ARGV[1] : ENV['PROJECT_FILE_PATH']
if !SOURCE_FOLDER || !PROJECT_PATH then
puts "usage: ruby check-categories_compiled.rb source_folder project_path"
exit 1
end
$headers_paths=Dir.glob(SOURCE_FOLDER + "/**/*.h")
$implementation_names=Dir.glob(SOURCE_FOLDER + "/**/*.m").flat_map { |path|
path.split("\/").last
}
def imported_categories_set_from_file(file_path)
if !file_path.to_path.end_with?(".h", ".m", ".mm") then
return nil
end
categories = Array.new()
open(file_path) { |f|
f.each_line.find_all { |line|
line.start_with?("#import \"") && line.include?("+")
}.each { |string|
category_header_name = string.split("\"")[1]
#add category implementation name to result
categories.push(category_header_name.chop + "m")
#add imported categories in current category recursively
$headers_paths.find_all { |path|
path.end_with?(category_header_name)
}.each { |path|
categories.insert(imported_categories_set_from_file(Pathname.new(path)))
}
}
}
return categories
end
project = Xcodeproj::Project.open(PROJECT_PATH)
has_errors=0
for target in project.targets do
imported_categories_set = Set.new []
compiled_categories_set = Set.new []
target.source_build_phase.files_references.each { |file_ref|
if (file_ref.real_path.to_path.include?("+")) then
compiled_categories_set.add(file_ref.path)
end
categories = imported_categories_set_from_file(file_ref.real_path)
if !categories then
next
end
categories.each { |category|
imported_categories_set.add(category)
}
}
imported_categories_set = imported_categories_set - compiled_categories_set
imported_categories_set = imported_categories_set & $implementation_names
imported_categories_set.each { |category_name|
puts "error: Missing \"" + category_name + "\" in \"" + target.name + "\" target."
has_errors=1
}
end
exit has_errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment