Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Commit

Permalink
Refactor to make it easier to support multiple SDKs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Holland committed Jun 11, 2010
1 parent ed33dfa commit 592ae03
Show file tree
Hide file tree
Showing 41 changed files with 151 additions and 78 deletions.
31 changes: 14 additions & 17 deletions ext/iCuke/Rakefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
require 'rake/clean'
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'icuke', 'sdk')

SDK_VERSION = '3.1.2'
SDK_ROOT = "/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator#{SDK_VERSION}.sdk"
CFLAGS = '-arch i386 -pipe -ggdb -std=c99 -DTARGET_OS_IPHONE'
SDK_CFLAGS = "-isysroot #{SDK_ROOT} -F/System/Library/PrivateFrameworks -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000"
CC = '/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2'
SUPPORTED_SDKS = ['3']

rule '.o' => '.m' do |o|
sh "#{CC} -I. -Ijson -c -o #{o.name} -x objective-c #{CFLAGS} #{SDK_CFLAGS} #{o.source}"
ICuke::SDK.major_versions.each do |sdk|
unless SUPPORTED_SDKS.include?(sdk)
puts "iCuke does not currently support version #{sdk} of the iPhone SDK"
next
end

task :install do
sh "cd sdk#{sdk} && rake"
end
task :clean do
sh "cd sdk#{sdk} && rake clean"
end
end

CLEAN.include('**/*.o')

file 'libicuke.dylib' => FileList['**/*.m'].ext('.o') do |t|
sh "#{CC} -dynamiclib -o #{t.name} #{CFLAGS} #{SDK_CFLAGS} -framework Foundation -framework GraphicsServices -framework UIKit -framework CFNetwork -framework AXRuntime #{t.prerequisites.join(' ')}"
end

CLEAN.include('libicuke.dylib')

task :install => 'libicuke.dylib'
task :default => :install
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions ext/iCuke/sdk3/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'icuke', 'sdk')
require 'rake/clean'

version = 3
ICuke::SDK.use_latest(version)

CFLAGS = '-arch i386 -pipe -ggdb -std=c99 -DTARGET_OS_IPHONE'
SDK_CFLAGS = "-isysroot #{ICuke::SDK.root} -F/System/Library/PrivateFrameworks -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000"

rule '.o' => '.m' do |o|
sh "xcrun -sdk #{ICuke::SDK.fullname} gcc -I. -Ijson -c -o #{o.name} -x objective-c #{CFLAGS} #{SDK_CFLAGS} #{o.source}"
end

CLEAN.include('**/*.o')

file "../#{ICuke::SDK.dylib}" => FileList['**/*.m'].ext('.o') do |t|
sh "xcrun -sdk #{ICuke::SDK.fullname} gcc -dynamiclib -o #{t.name} #{CFLAGS} #{SDK_CFLAGS} -framework Foundation -framework GraphicsServices -framework UIKit -framework CFNetwork -framework AXRuntime #{t.prerequisites.join(' ')}"
end

CLEAN.include("../#{ICuke::SDK.dylib}")

task :install => "../#{ICuke::SDK.dylib}"
task :default => :install
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions ext/iCuke/iCukeServer.h → ext/iCuke/sdk3/iCukeServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@
@interface iCukeServer : iCukeHTTPServer {
}

+ (void) start;

@end
39 changes: 19 additions & 20 deletions ext/iCuke/iCukeServer.m → ext/iCuke/sdk3/iCukeServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
#include <unistd.h>
#include <stdlib.h>

static NSAutoreleasePool *pool;

@implementation iCukeServer

+ (void)start {
pool = [[NSAutoreleasePool alloc] init];

+ (void)load
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[[iCukeHTTPServer sharediCukeHTTPServer] start];

NSFileManager *fileManager= [[NSFileManager alloc] init];
Expand All @@ -27,13 +26,27 @@ + (void)start {

paths = [fileManager contentsOfDirectoryAtPath: preferences error: NULL];
for (NSString *path in paths) {
NSLog(@"Found: %@", path);
if (![path hasPrefix: @"."]) {
NSLog(@"Removing: %@", path);
unlink([[preferences stringByAppendingPathComponent: path] cStringUsingEncoding: [NSString defaultCStringEncoding]]);
}
}
}

// This is a hack, I can't find a nicer way. The iPhone Simulator's
// preferences are hidden away from applications. None of the preference APIs
// allow access to them.
NSString *path = [NSString stringWithFormat: @"/Users/%@/Library/Application Support/iPhone Simulator/%@/Library/Preferences", NSUserName(), [[UIDevice currentDevice] systemVersion]];
path = [path stringByAppendingPathComponent: @"com.apple.Accessibility.plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile: path];
NSNumber *enabled = [NSNumber numberWithBool: YES];
[dict setObject: enabled forKey: @"AccessibilityEnabled"];
[dict setObject: enabled forKey: @"ApplicationAccessibilityEnabled"];
if (![dict writeToFile: path atomically: YES]) {
NSLog(@"Failed to write %@ out to %@", dict, path);
}

NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
paths = [fileManager contentsOfDirectoryAtPath: documents error: NULL];
for (NSString *path in paths) {
Expand All @@ -42,22 +55,8 @@ + (void)start {
unlink([[documents stringByAppendingPathComponent: path] cStringUsingEncoding: [NSString defaultCStringEncoding]]);
}
}
}

+ (void)stop {

[pool release];
}

@end

void start_server(void) __attribute__((constructor));
void start_server(void)
{
[iCukeServer start];
}

void stop_server(void) __attribute__((destructor));
void stop_server(void)
{
[iCukeServer stop];
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/icuke.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'icuke/simulator'
require 'icuke/sdk'
require 'icuke/simulator'
16 changes: 13 additions & 3 deletions lib/icuke/cucumber.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'nokogiri'

require 'icuke/sdk'
require 'icuke/simulator'
require 'icuke/simulate'

Expand Down Expand Up @@ -182,12 +183,21 @@ def refresh
quit
end

LIBICUKE = File.expand_path(File.dirname(__FILE__) + '/../../ext/iCuke/libicuke.dylib')
LIBICUKE_DIR = File.expand_path(File.dirname(__FILE__) + '/../../ext/iCuke')

Given /^(?:"([^\"]*)" from )?"([^\"]*)" is loaded in the simulator(?: using sdk (.*))?$/ do |target, project, sdk|
Given /^(?:"([^\"]*)" from )?"([^\"]*)" is loaded in the simulator(?: with SDK "([^\"]*)")?$/ do |target, project, sdk_version|
if sdk_version
ICuke::SDK.use(sdk_version)
else
ICuke::SDK.use_latest
end

launch File.expand_path(project),
:target => target,
:env => { 'DYLD_INSERT_LIBRARIES' => LIBICUKE }
:env => {
'DYLD_LIBRARY_PATH' => LIBICUKE_DIR,
'DYLD_INSERT_LIBRARIES' => File.join(LIBICUKE_DIR, ICuke::SDK.dylib)
}
end

Then /^I should see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, scope|
Expand Down
10 changes: 3 additions & 7 deletions lib/icuke/headless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ def launch(project_file, options = {})
app_name = File.basename(project_file, '.xcodeproj')
directory = "#{File.dirname(project_file)}/build/#{options[:configuration]}-iphonesimulator"

ENV['DYLD_ROOT_PATH'] = '/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk'
ENV['DYLD_FRAMEWORK_PATH'] = directory
ENV['IPHONE_SIMULATOR_ROOT'] = '/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk'
home = Dir.mktmpdir
FileUtils.mkdir_p File.join(home, 'Library', 'Preferences')
FileUtils.cp File.join(File.dirname(__FILE__), 'com.apple.Accessibility.plist'), File.join(home, 'Library', 'Preferences')
FileUtils.mkdir File.join(home, 'Documents')
ENV['CFFIXED_USER_HOME'] = home
ENV['DYLD_ROOT_PATH'] = ICuke::SDK.root
ENV['IPHONE_SIMULATOR_ROOT'] = ICuke::SDK.root
ENV['CFFIXED_USER_HOME'] = Dir.mktmpdir
ENV['ICUKE_KEEP_PREFERENCES'] = '1'

options[:env].each_pair do |k, v|
Expand Down
77 changes: 77 additions & 0 deletions lib/icuke/sdk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module ICuke
module SDK
def self.all
@all ||= begin
`xcodebuild -showsdks`.grep(/iphonesimulator/).map do |s|
s.sub(/.* iphonesimulator([0-9.]+).*/, '\1').chomp
end.sort
end
end

def self.installed?(sdk)
all.include?(sdk)
end

def self.major_versions
all.map { |s| s.split('.').first }.uniq
end

def self.latest(major_version = nil)
@latest ||= major_version ? all.grep(/^#{major_version}\./).last : all.last
end

def self.use(version)
unless installed?(version)
raise "The requested SDK version #{version} doesn't appear to be installed"
end

@sdk = version
end

def self.use_latest(major_version = nil)
use latest(major_version)
end

def self.version
require_sdk

@sdk
end

def self.major_version
require_sdk

version.split('.').first
end

def self.fullname
require_sdk

"iphonesimulator#{version}"
end

def self.root
require_sdk

"/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator#{version}.sdk"
end

def self.home
require_sdk

"#{ENV['HOME']}/Library/Application Support/iPhone Simulator/#{version}"
end

def self.dylib
require_sdk

"libicuke-sdk#{version.split('.').first}.dylib"
end

private

def self.require_sdk
raise "No SDK has been selected" unless @sdk
end
end
end
28 changes: 0 additions & 28 deletions lib/icuke/xcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ def launch(project_file, options = {})
settings[:active_target] = project.targets[options[:target]]
end

set_preferences

XCode.with_settings(project, settings) do
executable = project.active_executable.get
options[:env].each_pair do |name, value|
Expand Down Expand Up @@ -133,32 +131,6 @@ def launch(project_file, options = {})

def quit
IPhoneSimulator.quit
restore_preferences
end

private

def accessibility_plist
File.expand_path(File.join('~', 'Library', 'Application Support', 'iPhone Simulator', '3.1.2', 'Library', 'Preferences', 'com.apple.Accessibility.plist'))
end

def accessibility_plist_backup
accessibility_plist + '.icuke'
end

def restore_preferences
if File.exists? accessibility_plist_backup
FileUtils.mv accessibility_plist_backup, accessibility_plist
else
FileUtils.rm accessibility_plist
end
end

def set_preferences
if File.exists? accessibility_plist
FileUtils.mv accessibility_plist, accessibility_plist_backup
end
FileUtils.cp File.join(File.dirname(__FILE__), 'com.apple.Accessibility.plist'), accessibility_plist
end
end
end

0 comments on commit 592ae03

Please sign in to comment.