Class: IB::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/ib/project.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Project

Returns a new instance of Project.



12
13
14
15
# File 'lib/ib/project.rb', line 12

def initialize options={}
  @platform        = options[:platform]     || detect_platform
  @project_path    = options[:project_path] || Dir.pwd
end

Instance Attribute Details

#platformObject

Returns the value of attribute platform.



3
4
5
# File 'lib/ib/project.rb', line 3

def platform
  @platform
end

#project_pathObject

Returns the value of attribute project_path.



4
5
6
# File 'lib/ib/project.rb', line 4

def project_path
  @project_path
end

Instance Method Details

#add_extra_framework(framework) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ib/project.rb', line 122

def add_extra_framework(framework)
  deployment_target = Motion::Project::App.config.deployment_target
  framework_name = framework.path.split('/').last
  framework_group = project.new_group(framework_name)
  framework_group.path = File.join(project_path, framework.path)
  framework_target = project.new_target(
    :framework, framework_name, platform, deployment_target)

  Dir.glob("#{framework.path}/**/*.{h,m}") do |file|
    file_ref = framework_group.new_file File.join(project_path, file)
    framework_target.add_file_references([file_ref])
  end
end

#add_frameworksObject



110
111
112
113
114
115
116
# File 'lib/ib/project.rb', line 110

def add_frameworks
  DEFAULT_FRAMEWORKS.each do |framework|
    target.add_system_framework framework
  end

  extra_frameworks.each { |framework| add_extra_framework framework }
end

#add_podsObject



104
105
106
107
108
# File 'lib/ib/project.rb', line 104

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

#add_resourcesObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ib/project.rb', line 92

def add_resources
  # First add reference to any asset catalogs.
  Dir.glob("#{resources.path}/**/*.xcassets") do |file|
    resources.new_reference(file)
  end
  # Add all other resources, ignoring files in existing asset catalogs
  Dir["#{resources.path}/**/*.{#{RESOURCE_EXTENSIONS.join(",")}}"]
    .reject {|f| f[%r{.*\.xcassets/.*}] }.each do |file|
    resources.new_reference(file)
  end
end

#app_filesObject



72
73
74
75
76
# File 'lib/ib/project.rb', line 72

def app_files
  Motion::Project::App.config.files.select do |file|
    file =~ /^(\.\/)?app\//
  end
end

#detect_platformObject



62
63
64
65
66
67
68
69
70
# File 'lib/ib/project.rb', line 62

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

#extra_frameworksObject



118
119
120
# File 'lib/ib/project.rb', line 118

def extra_frameworks
  Motion::Project::App.config.vendor_projects.select { |vp| vp.opts[:ib] }
end

#generate_stub_filesObject



84
85
86
87
88
89
90
# File 'lib/ib/project.rb', line 84

def generate_stub_files
  generator.write(app_files, ib_project_path)

         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

#generatorObject



46
47
48
# File 'lib/ib/project.rb', line 46

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

#ib_project_pathObject



136
137
138
# File 'lib/ib/project.rb', line 136

def ib_project_path
  File.join(project_path, IB_PROJECT_NAME)
end

#podsObject



58
59
60
# File 'lib/ib/project.rb', line 58

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

#projectObject



38
39
40
# File 'lib/ib/project.rb', line 38

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

#resourcesObject



50
51
52
# File 'lib/ib/project.rb', line 50

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

#setup_pathsObject



78
79
80
81
82
# File 'lib/ib/project.rb', line 78

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

#support_filesObject



54
55
56
# File 'lib/ib/project.rb', line 54

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

#targetObject



42
43
44
# File 'lib/ib/project.rb', line 42

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

#writeObject

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/ib/project.rb', line 27

def write
  setup_paths

  generate_stub_files
  add_resources
  add_pods
  add_frameworks

  project.save
end