Skip to content

Commit

Permalink
Merge pull request #59 from imalijkh/im-fix-stub-paths-and-refactor
Browse files Browse the repository at this point in the history
Fix stub paths and refactor write
  • Loading branch information
yury committed Apr 4, 2014
2 parents 525a7f9 + dd60fcf commit cd5d1f7
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,7 +3,6 @@
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
Expand All @@ -17,3 +16,5 @@ test/version_tmp
tmp
ui.xcodeproj
ib.xcodeproj
.ruby-version
.rbenv-version
74 changes: 74 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,74 @@
PATH
remote: .
specs:
ib (0.4.4)
thor (~> 0.15.4)
tilt (~> 1.4.1)
xcodeproj (~> 0.16.0)

GEM
remote: https://rubygems.org/
specs:
activesupport (3.2.17)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
coderay (1.1.0)
colored (1.2)
diff-lcs (1.2.5)
ffi (1.9.3)
formatador (0.2.4)
guard (1.8.3)
formatador (>= 0.2.4)
listen (~> 1.3)
lumberjack (>= 1.0.2)
pry (>= 0.9.10)
thor (>= 0.14.6)
guard-bundler (1.0.0)
bundler (~> 1.0)
guard (~> 1.1)
guard-rspec (3.1.0)
guard (>= 1.8)
rspec (~> 2.13)
i18n (0.6.9)
listen (1.3.1)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
rb-kqueue (>= 0.2)
lumberjack (1.0.5)
method_source (0.8.2)
multi_json (1.9.2)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
rake (10.2.2)
rb-fsevent (0.9.4)
rb-inotify (0.9.3)
ffi (>= 0.5.0)
rb-kqueue (0.2.2)
ffi (>= 0.5.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.8)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
slop (3.5.0)
thor (0.15.4)
tilt (1.4.1)
xcodeproj (0.16.0)
activesupport (~> 3.0)
colored (~> 1.2)

PLATFORMS
ruby

DEPENDENCIES
bundler
guard-bundler
guard-rspec
ib!
rake
rspec (~> 2.0)
11 changes: 11 additions & 0 deletions Guardfile
@@ -0,0 +1,11 @@
guard 'bundler' do
watch('Gemfile')
watch(/^.+\.gemspec/)
end

guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

12 changes: 7 additions & 5 deletions ib.gemspec
Expand Up @@ -15,10 +15,12 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = IB::VERSION

gem.add_dependency "xcodeproj", ">= 0.11.1"
gem.add_dependency "thor", ">= 0.15.4"
gem.add_dependency "tilt", ">= 1.4.1"
gem.add_dependency 'xcodeproj', '~> 0.16.0'
gem.add_dependency 'thor', '~> 0.15.4'
gem.add_dependency 'tilt', '~> 1.4.1'

gem.add_development_dependency "rspec", ">= 2.0"
gem.add_development_dependency "rake"
gem.add_development_dependency 'rspec', '~> 2.0'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'guard-bundler'
end
106 changes: 77 additions & 29 deletions lib/ib/project.rb
@@ -1,22 +1,70 @@
# -*- encoding : utf-8 -*-
class IB::Project
attr_accessor :platform, :app_path, :resources_path, :pods_headers_path, :project_path
attr_accessor :platform
attr_accessor :project_path

IB_PROJECT_NAME = 'ib.xcodeproj'
DEFAULT_FRAMEWORKS = %W{QuartzCore CoreGraphics CoreData}
RESOURCE_EXTENSIONS = %W{xcdatamodeld png jpg jpeg storyboard xib lproj}

def initialize options={}
@platform = options[:platform] || detect_platform || :ios
@project_path = options[:project_path] || Dir.pwd
@app_path = options[:app_path] || "#{project_path}/app"
@resources_path = options[:resources_path] || "#{project_path}/resources"
@pods_headers_path = options[:pods_headers_path] || "#{project_path}/vendor/Pods/Headers"
@platform = options[:platform] || detect_platform
@project_path = options[:project_path] || Dir.pwd
end

# Writes a new ib.xcodeproj to the provided `#project_path`.
# The following steps will occur
#
# * `setup_paths` - This step sets up the paths to your project's 'resources',
# 'pods' and 'supporting files'.
# * `generate_stub_files` - generates stub files and adds it to the build
# * `add_resources` - Adds all resources from your RubyMotion project
# * `add_pods` - Adds pods (if any) to ib.xcodeproj
# * `add_frameworks` - Adds standard frameworks to your project
# * project will then be saved
def write
setup_paths

generate_stub_files
add_resources
add_pods
add_frameworks

project.save
end

def project
@project ||= Xcodeproj::Project.new(ib_project_path)
end

def target
@target ||= project.new_target(:static_library, 'ib', platform)
end

def generator
@generator ||= IB::Generator.new(detect_platform)
end

def resources
@resources ||= project.new_group("Resources")
end

def support_files
@support_files ||= project.new_group("Supporting Files")
end

def pods
@pods ||= project.new_group("Pods")
end

def detect_platform
# TODO: find a better way to detect platform
if defined?(Motion::Project::Config)
if Motion::Project::App.config.respond_to?(:platforms)
Motion::Project::App.config.platforms[0] == 'MacOSX' ? :osx : :ios
return Motion::Project::App.config.platforms[0] == 'MacOSX' ? :osx : :ios
end
end
return :ios
end

def app_files
Expand All @@ -25,40 +73,40 @@ def app_files
end
end

def write
ib_project = "ib.xcodeproj"
project = Xcodeproj::Project.new(ib_project)
target = project.new_target(:static_library, 'ib', platform)

resources = project.new_group("Resources")
resources.path = resources_path

support = project.new_group("Supporting Files")
support.path = ib_project

pods = project.new_group("Pods")
pods.path = pods_headers_path
def setup_paths
resources.path = File.join(project_path, 'resources')
support_files.path = File.join(project_path, IB_PROJECT_NAME)
pods.path = File.join(project_path, 'vendor/Pods/Headers')
end

generator = IB::Generator.new(detect_platform)
generator.write(app_files, ib_project)
def generate_stub_files
generator.write(app_files, ib_project_path)

support.new_file "ib.xcodeproj/Stubs.h"
file = support.new_file "ib.xcodeproj/Stubs.m"
support_files.new_file File.join(ib_project_path, 'Stubs.h')
file = support_files.new_file File.join(ib_project_path, 'Stubs.m')
target.add_file_references([ file ])
end

resource_exts = %W{xcdatamodeld png jpg jpeg storyboard xib lproj}
Dir.glob("#{resources_path}/**/*.{#{resource_exts.join(",")}}") do |file|
def add_resources
Dir.glob("#{resources.path}/**/*.{#{RESOURCE_EXTENSIONS.join(",")}}") do |file|
resources.new_reference(file)
end
end

Dir.glob("#{pods_headers_path}/**/*.h") do |file|
def add_pods
Dir.glob("#{pods.path}/**/*.h") do |file|
pods.new_reference(file)
end
end

%W{QuartzCore CoreGraphics CoreData}.each do |framework|
def add_frameworks
DEFAULT_FRAMEWORKS.each do |framework|
target.add_system_framework framework
end
end

project.save(ib_project)
def ib_project_path
File.join(project_path, IB_PROJECT_NAME)
end

end
111 changes: 97 additions & 14 deletions spec/lib/ib/project_spec.rb
Expand Up @@ -3,21 +3,104 @@
require "ib/project"

describe IB::Project do
it "generates xcode project" do
IB::Project.new(app_path:"spec/fixtures/common").write()

context 'structure' do
let(:project) { described_class.new(project_path: 'spec/fixtures/common')}

it "defines #write" do
expect(project).to respond_to :write
end

it "defines #project" do
expect(project).to respond_to :project
end

it "defines #target" do
expect(project).to respond_to :target
end

it "defines #generator and is an IB::Generator" do
expect(project).to respond_to :generator
expect(project.generator).to be_kind_of IB::Generator
end

it "defines #resources" do
expect(project).to respond_to :resources
end

it "defines #support_files" do
expect(project).to respond_to :support_files
end

it "defines #pods" do
expect(project).to respond_to :pods
end

it "defines #detect_platform" do
expect(project).to respond_to :detect_platform
end
end

describe '#initialize' do
it "sets right path for xcode project" do
project = described_class.new(project_path: 'spec/fixtures/common')
expect(project.ib_project_path).to eq 'spec/fixtures/common/ib.xcodeproj'
end
end

context 'creating groups' do
let(:project) { described_class.new }

describe '#resources' do
it 'creates a group called Resources' do
expect(project.resources.name).to eq 'Resources'
end
it 'does not create multiple group records' do
project.resources
project.resources
resources = project.project.groups.select { |g| g.name == 'Resources'}
expect(resources.length).to be 1
end
end

describe '#support_files' do
it 'creates a group called Support Files' do
expect(project.support_files.name).to eq 'Supporting Files'
end
end

describe '#pods' do
it 'creates a group called Pods' do
expect(project.pods.name).to eq 'Pods'
end
end
end

describe '#detect_platform' do
it 'defaults to ios' do
project = described_class.new
expect(project.platform).to eq :ios
end

it 'can be set from parameter' do
project = described_class.new platform: :osx
expect(project.platform).to eq :osx
end
end

it "app_files" do
Motion::Project::App.config.stub('files').and_return(
[
'/usr/local/xxx/gems/xxx/lib/xxxx.rb',
'User/xxx/gems/xxx/lib/motion/xxxx.rb',
'./app/aaa.rb',
'./app/controllers/bbb.rb',
]
)
expect(IB::Project.new(app_path:"spec/fixtures/common").app_files).to eq(
['./app/aaa.rb', './app/controllers/bbb.rb',]
)
describe '#app_files' do
before do
Motion::Project::App.config.stub('files').and_return(
[
'/usr/local/xxx/gems/xxx/lib/xxxx.rb',
'User/xxx/gems/xxx/lib/motion/xxxx.rb',
'./app/aaa.rb',
'./app/controllers/bbb.rb',
]
)
end
it "returns a list of files under app directory of the project" do
expect(IB::Project.new.app_files).to eq ['./app/aaa.rb', './app/controllers/bbb.rb']
end
end
end

0 comments on commit cd5d1f7

Please sign in to comment.