Skip to content

Instantly share code, notes, and snippets.

@kam800
Created January 19, 2015 09:07
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 kam800/9e42c8e8f115e3f0de09 to your computer and use it in GitHub Desktop.
Save kam800/9e42c8e8f115e3f0de09 to your computer and use it in GitHub Desktop.
Prints files that where not added to all xcode targets given in parameter
#
# pbxproj_unused_files.rb
# v.1.0.0
#
# Copyright (c) 2015 Kamil Borzym
# Released under the MIT License
#
require 'rubygems'
require 'json'
class PbxStructure
attr_accessor :target_names
def initialize(pbx_tree)
@pbx_tree = pbx_tree
end
def scanFiles
pbx_objects = @pbx_tree["objects"]
root_object = pbx_objects[@pbx_tree["rootObject"]]
root_object["targets"].select do |target_id|
@target_names.include?(pbx_objects[target_id]["name"])
end.map do |target_id|
pbx_objects[target_id]["buildPhases"]
end.flatten.select do |phase_id|
pbx_objects[phase_id]["isa"] == "PBXSourcesBuildPhase"
end.map do |phase_id|
pbx_objects[phase_id]["files"]
end.flatten.map do |file_id|
pbx_objects[file_id]["fileRef"]
end.group_by do |file_id|
file_id
end.inject(Hash.new) do |hash, (k,v)|
hash[k] = v.length
hash
end.select do |k,v|
v.to_i < @target_names.length
end.keys.map do |file_id|
pbx_objects[file_id]["path"]
end.each do |path|
print path, "\n"
end
end
end
if __FILE__ == $0
def usage
abort "Prints files that where not added to all xcode targets given in parameter
usage:
ruby #{__FILE__} pbx_path [target_name:...]"
end
pbx_path = ARGV[0]
target_names = ARGV[1]
if pbx_path.nil? or target_names.nil?
usage
end
pbx_data = `plutil -convert json -o - #{ARGV[0]}`
if $? != 0
abort "Could not read project file!"
end
pbx_tree = JSON.parse(pbx_data)
pbx_structure = PbxStructure.new(pbx_tree)
pbx_structure.target_names = target_names.split(":")
pbx_structure.scanFiles
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment