Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliSoftware/447c5ed5e04d2f25f14d1922757048b7 to your computer and use it in GitHub Desktop.
Save AliSoftware/447c5ed5e04d2f25f14d1922757048b7 to your computer and use it in GitHub Desktop.
This script walks thru all the files in a Xcode project's groups belong to the target they are supposed to, according to group names found while walking the groups hierarchy.
#!/usr/bin/env ruby
# This script walks thru all the files in a Xcode project's groups belong to the target they are supposed to
# according to group names found while walking the groups hierarchy.
#
# This example only focuses on test files and groups/targets ending with "...Tests"
require 'xcodeproj'
def check(group, all_targets, walk_path, ctx_target_name = nil, ctx_expected_files_list = nil)
group_name = group.name || group.path
walk_path += "/#{group_name}"
# If folder ends with "...Tests", try to find a target matching its name
if group_name.end_with?('Tests')
matching_target = all_targets.find { |t| t.name == group_name }
unless matching_target.nil?
ctx_target_name = matching_target.name
ctx_expected_files_list = matching_target
.build_phases
.find { |p| p.is_a? Xcodeproj::Project::Object::PBXSourcesBuildPhase }
.files_references
end
end
# Loop on the group's files and check they are in the ctx_expected_files_list (if matching target has been found)
if ctx_expected_files_list
group.files.each do |file_ref|
next unless file_ref.path.end_with?('.swift')
unless ctx_expected_files_list.include?(file_ref)
puts "File #{file_ref.path} in #{walk_path} was expected to be found in #{ctx_target_name}"
end
end
end
# Recursion: Loop on the group's subgroups
group.groups.each do |group|
check(group, all_targets, walk_path, ctx_target_name, ctx_expected_files_list)
end
end
x = Xcodeproj::Project.open('Babylon.xcodeproj')
x.groups.each do |group|
check(group, x.targets, File.basename(x.path))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment