Skip to content

Commit

Permalink
Merge pull request #34 from CocoaPods/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
fabiopelosin committed Oct 21, 2012
2 parents da9a356 + fbd4902 commit 034904f
Show file tree
Hide file tree
Showing 113 changed files with 11,687 additions and 1,966 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ ext/xcodeproj/conftest.dSYM
doc
tmp
.DS_Store
xcuserdata
3 changes: 2 additions & 1 deletion .kick
Expand Up @@ -9,7 +9,8 @@ process do |files|
execute 'rake ext:cleanbuild'
'spec/xcodeproj_ext_spec.rb'
when %r{lib/xcodeproj/(.+?)\.rb$}
s = Dir.glob("spec/**/#{$1}_spec.rb")
s = Dir.glob("spec/**#{$1}_spec.rb")
puts
s unless s.empty?
end
end
Expand Down
2 changes: 2 additions & 0 deletions .yardopts
@@ -0,0 +1,2 @@
--markup-provider=redcarpet
--markup=markdown
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -10,5 +10,6 @@ end

group :documentation do
gem "redcarpet"
gem "github-markup"
gem "yard"
end
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
xcodeproj (0.3.5)
activesupport (~> 3.2.6)
colored (~> 1.2)

GEM
remote: http://rubygems.org/
Expand All @@ -11,7 +12,9 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
bacon (1.1.0)
colored (1.2)
ffi (1.1.5)
github-markup (0.7.4)
i18n (0.6.1)
kicker (2.6.1)
listen
Expand All @@ -34,6 +37,7 @@ PLATFORMS

DEPENDENCIES
bacon
github-markup
kicker
rake
redcarpet
Expand Down
95 changes: 4 additions & 91 deletions bin/xcodeproj
@@ -1,100 +1,13 @@
#!/usr/bin/env ruby

if $0 == __FILE__
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
require "rubygems"
require "bundler/setup"
$:.unshift File.expand_path('../../ext', __FILE__)
$:.unshift File.expand_path('../../lib', __FILE__)
end

require 'xcodeproj'

require 'optparse'
$options = {}
$option_parser = OptionParser.new do |opts|
opts.banner = "Usage: xcodeproj [command] [options]"
opts.separator ""
opts.separator "Commands:"
opts.separator ""
opts.separator " targets-diff [target 1] [target 2]\tShows the difference between two targets. (Only build source files atm.)"
opts.separator ""

opts.separator "Options:"
opts.separator ""

opts.on('--project PATH', "The Xcode project document to use.") do |xcodeproj|
$options[:xcodeproj] = xcodeproj
end

opts.on("--version", "Show xcodeproj version.") do |v|
puts Xcodeproj::VERSION
exit
end
end
$option_parser.parse!


def invalid_command(msg = nil)
if msg
puts msg
puts
end
puts $option_parser
exit 1
end


def xcodeproj
unless xcodeproj = $options[:xcodeproj]
projects = Dir.glob('*.xcodeproj')
if projects.size == 1
xcodeproj = projects.first
elsif projects.size > 1
invalid_command('There are more than one Xcode project documents in the current working directory. Please specify which to use.')
else
invalid_command('No Xcode project document found in the current working directory. Please specify which to use.')
end
end
File.expand_path(xcodeproj)
end


class Xcodeproj::Project::Object::PBXGroup
attributes :path
end
class Xcodeproj::Project::Object::PBXFileReference
alias_method :_path, :path
def path
# TODO somethings not working right, I'm seeing the dir being prepended twice with ZipKit
if (sourceTree == '<group>') && dir = group.path
File.join(dir, _path)
else
_path
end
end
end
def target_source_files(project, target_name)
project.targets.object_named(target_name).source_build_phases.map do |source_build_phase|
source_build_phase.files.map { |buildFile| buildFile.file.path }
end.flatten.sort
end


case cmd = ARGV.shift
when 'targets-diff'
target1, target2 = ARGV.first(2)
if target1 && target2
project = Xcodeproj::Project.new(xcodeproj)
target1_files = target_source_files(project, target1)
target2_files = target_source_files(project, target2)

diff = target1_files - target2_files
puts "Files in `#{target1}' which are not present in `#{target2}':\n* #{diff.join("\n* ")}\n\n" unless diff.empty?

diff = target2_files - target1_files
puts "Files in `#{target2}' which are not present in `#{target1}':\n* #{diff.join("\n* ")}\n\n" unless diff.empty?
else
invalid_command("Needs the names of the two targets to diff.")
end

else
invalid_command(cmd.nil? ? nil : "Unknown command: #{cmd}")
end
Xcodeproj::Command.run(*ARGV)
12 changes: 12 additions & 0 deletions lib/xcodeproj.rb
@@ -1,7 +1,19 @@
module Xcodeproj
VERSION = '0.3.5'

class PlainInformative < StandardError
end

class Informative < PlainInformative
def message
super !~ /\[!\]/ ? "[!] #{super}\n".red : super
end
end

autoload :Config, 'xcodeproj/config'
autoload :Command, 'xcodeproj/command'
autoload :Constants, 'xcodeproj/constants'
autoload :Helper, 'xcodeproj/helper'
autoload :Project, 'xcodeproj/project'
autoload :Workspace, 'xcodeproj/workspace'
end
53 changes: 53 additions & 0 deletions lib/xcodeproj/Command/project_diff.rb
@@ -0,0 +1,53 @@
module Xcodeproj
class Command
class ProjectDiff < Command
def self.banner
%{Installing dependencies of a project:
$ project-diff PROJECT_1 PROJECT_2
Shows the difference between two projects in an UUID agnostic fashion.
To reduce the noise (and to simplify implementation) differences in the
other of arrays are ignored.
}
end

def self.options
[ ["--ignore KEY", "A key to ignore in the comparison. Can be specified multiple times."] ].concat(super)
end

def initialize(argv)
@path_project1 = argv.shift_argument
@path_project2 = argv.shift_argument
@keys_to_ignore = []
while (idx = argv.index('--ignore'))
@keys_to_ignore << argv.delete_at(idx + 1)
argv.delete_at(idx)
end
super unless argv.empty?
end


def run
hash_1 = Project.new(@path_project1).to_tree_hash
hash_2 = Project.new(@path_project2).to_tree_hash
(@keys_to_ignore).each do |key|
hash_1.recursive_delete(key)
hash_2.recursive_delete(key)
end

diff = hash_1.recursive_diff(hash_2, @path_project1, @path_project2)
diff.recursive_delete('displayName')

require 'yaml'
yaml = diff.to_yaml
yaml = yaml.gsub(@path_project1, @path_project1.cyan)
yaml = yaml.gsub(@path_project2, @path_project2.magenta)
puts yaml
end
end
end
end


35 changes: 35 additions & 0 deletions lib/xcodeproj/Command/show.rb
@@ -0,0 +1,35 @@
module Xcodeproj
class Command
class Show < Command
def self.banner
%{Installing dependencies of a project:
$ project-diff PROJECT_1 PROJECT_2
Shows a YAML reppresentation of a project.
}
end

def self.options
[
["--project PATH", "The Xcode project document to use."],
].concat(super)
end

def initialize(argv)
if argv.option('--project')
@xcodeproj_path = File.expand_path(argv.shift_argument)
end
super unless argv.empty?
end

def run
require 'yaml'
yaml = xcodeproj.to_tree_hash.to_yaml
puts yaml
end
end
end
end


43 changes: 43 additions & 0 deletions lib/xcodeproj/Command/target_diff.rb
@@ -0,0 +1,43 @@
module Xcodeproj
class Command
class TargetDiff < Command
def self.banner
%{Installing dependencies of a project:
$ targets-diff [target 1] [target 2]
Shows the difference between two targets. (Only build source files atm.)
}
end

def self.options
[
["--project PATH", "The Xcode project document to use."],
].concat(super)
end

def initialize(argv)
@target1 = argv.shift_argument
@target2 = argv.shift_argument
if argv.option('--project')
@xcodeproj_path = File.expand_path(argv.shift_argument)
end
super unless argv.empty?
end

def run
require 'yaml'
differ = Helper::TargetDiff.new(xcodeproj, @target1, @target2)
files = differ.new_source_build_files.map do |build_file|
{
'Name' => build_file.file_ref.name,
'Path' => build_file.file_ref.path,
'Build settings' => build_file.settings,
}
end
puts files.to_yaml
end
end
end
end

0 comments on commit 034904f

Please sign in to comment.