Module: LaneKit

Defined in:
lib/lanekit.rb,
lib/lanekit.rb,
lib/lanekit/new.rb,
lib/lanekit/version.rb,
lib/lanekit/lanefile.rb,
lib/lanekit/generate/model.rb,
lib/lanekit/generate/provider.rb,
lib/lanekit/generate/urbanairship.rb,
lib/lanekit/generate/tableviewcontroller.rb

Defined Under Namespace

Classes: CLI, Generate, Lanefile

Constant Summary collapse

VERSION =
"0.9.0"
@@objc_types =
{
  "array" => "NSArray *"
}

Class Method Summary collapse

Class Method Details

.add_acknowledgements_to_podfile(app_name) ⇒ Object

Adds a acknowledgements.plist line to a CocoaPods Podfile



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lanekit.rb', line 59

def self.add_acknowledgements_to_podfile(app_name)
  podfile_path = File.expand_path('Podfile')
  if !File.exists?(podfile_path)
    puts "Can't find Podfile #{podfile_path}"
    return
  end

  str = "
post_install do | installer |
require 'fileutils'
FileUtils.cp_r('Pods/Target Support Files/Pods/Pods-acknowledgements.plist', '#{app_name}/Resources/Settings.bundle/Acknowledgements.plist', :remove_destination => true)
end
"

  if !self.does_text_exist_in_file?(podfile_path, "acknowledgements.plist")
    open(podfile_path, 'a') do |file|
      file.puts str
    end
  
    system "pod install"
  else
    puts "The acknowledgements.plist already exists in Podfile"
  end
end

.add_file_to_project(file_name, group_name, project_path, target_name = nil) ⇒ Object

Adds a file to a group in an Xcode project For example: LaneKit.add_file_to_project(‘Models/Video.m’, ‘SportsFrames/Models’, ‘SportsFrames’, ‘SportsFrames’)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lanekit.rb', line 21

def self.add_file_to_project(file_name, group_name, project_path, target_name=nil)
  # Open the existing Xcode project
  project = Xcodeproj::Project.open(project_path)
  
  #puts "group_name: #{group_name}"
  #puts "groups: #{project.groups}"
  #puts "group: #{project[group_name]}"
  
  group = project[group_name]
  
  # Avoid duplicates
  ref = group.find_file_by_path(file_name)
  return if ref
   
  # Add a file to the project in the main group
  file = group.new_reference(file_name)

  if target_name == "@all"
    # Add the file to the main target
    
    project.targets.each do |target|
      target.add_file_references([file])
    end
  elsif target_name
    # Add the file to the main target
    
    unless target = project.targets.find { |target| target.name == target_name }
      raise ArgumentError, "Target by name `#{target_name}' not found in the project."
    end
    
    target.add_file_references([file])
  end
   
  # Save the project file
  project.save
end

.add_pod_to_podfile(pod_name) ⇒ Object

Adds a pod file line to a CocoaPods Podfile



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lanekit.rb', line 85

def self.add_pod_to_podfile(pod_name)
  podfile_path = File.expand_path('Podfile')
  if !File.exists?(podfile_path)
    puts "Can't find Podfile #{podfile_path}"
    return
  end

  if !self.does_text_exist_in_file?(podfile_path, pod_name)
    open(podfile_path, 'a') do |file|
      file.puts "pod '#{pod_name}'"
    end
  
    system "pod install"
  else
    puts "The pod '#{pod_name}' already exists in Podfile"
  end
end

.controllers_folder(lanefile) ⇒ Object



263
264
265
# File 'lib/lanekit.rb', line 263

def self.controllers_folder(lanefile)
  "#{lanefile.app_project_name}/#{lanefile.app_project_name}/Controllers"
end

.controllers_group(lanefile) ⇒ Object



267
268
269
# File 'lib/lanekit.rb', line 267

def self.controllers_group(lanefile)
  "#{lanefile.app_project_name}/Controllers"
end

.derive_app_name(app_path) ⇒ Object

Returns an app name from a folder path. “Tracker” => “Tracker”, “~/Projects/Runner” => “Runner”



105
106
107
# File 'lib/lanekit.rb', line 105

def self.derive_app_name(app_path)
  File.basename(app_path).to_s
end

.derive_class_name(name) ⇒ Object

Objective-C class names are capitalized “car” => “Car”, “bigbird” => “Bigbird”



118
119
120
121
# File 'lib/lanekit.rb', line 118

def self.derive_class_name(name)
  class_name = name.to_s.capitalize
  class_name
end

.derive_file_name(name) ⇒ Object

File names are the same as class names



124
125
126
127
# File 'lib/lanekit.rb', line 124

def self.derive_file_name(name)
  file_name = name.to_s.capitalize
  file_name
end

.derive_model_name(name) ⇒ Object

Model names are lower case “Car” => “car”, “Bigbird” => “bigbird”



111
112
113
114
# File 'lib/lanekit.rb', line 111

def self.derive_model_name(name)
  model_name = name.to_s.downcase
  model_name
end

.does_text_exist_in_file?(file_path, text) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/lanekit.rb', line 129

def self.does_text_exist_in_file?(file_path, text)
  found_text = open(file_path) { |f| f.grep(/#{text}/) }
  exists = found_text && found_text.count > 0
end

.gem_available?(gemname) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
235
236
237
238
# File 'lib/lanekit.rb', line 232

def self.gem_available?(gemname)
  if Gem::Specification.methods.include?(:find_all_by_name) 
     not Gem::Specification.find_all_by_name(gemname).empty?
   else
     Gem.available?(gemname)
   end
end

.objective_c_type(type_name) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/lanekit.rb', line 134

def self.objective_c_type(type_name)
  type = @@objc_types[type_name]
  return type if type
  
  if type_name == "array"
    type = "NSArray *"
  elsif type_name == "date"
    type = "NSDate *"
  elsif type_name == "integer"
    type = "NSNumber *"
  elsif type_name == "string"
    type = "NSString *"
  elsif type_name
    type = type_name + " *"
  else
    type = "id "
  end
  type
end

.objective_c_type_fixture_json(type_name) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/lanekit.rb', line 154

def self.objective_c_type_fixture_json(type_name)
  if type_name == "array"
    value = "[]"
  elsif type_name == "date"
    value = "03/01/2012"
  elsif type_name == "integer"
    value = "1"
  elsif type_name == "string"
    value = "MyString"
  elsif type_name
    value = ""
  else
    value = ""
  end
  value
end

.objective_c_type_fixture_value(type_name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/lanekit.rb', line 171

def self.objective_c_type_fixture_value(type_name)
  if type_name == "array"
    value = "@[]"
  elsif type_name == "date"
    value = "NSDate.new"
  elsif type_name == "integer"
    value = "[NSNumber numberWithInteger:1]"
  elsif type_name == "string"
    value = "@\"MyString\""
  elsif type_name
    value = "nil"
  else
    value = "nil"
  end
  value
end

.objective_c_type_unit_test_assert(model_name, name, type_name) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/lanekit.rb', line 188

def self.objective_c_type_unit_test_assert(model_name, name, type_name)
  if type_name == "array"
    assert = "XCTAssertNotNil(#{model_name}.#{name}, @\"#{name} is nil\")"
  elsif type_name == "date"
    assert = "XCTAssertNotNil(#{model_name}.#{name}, @\"#{name} is nil\")"
  elsif type_name == "integer"
    assert = "XCTAssertTrue(#{model_name}.#{name}.integerValue == #{self.objective_c_type_fixture_value(type_name)}.integerValue, @\"#{name} not #{self.objective_c_type_fixture_value(type_name)}\")"
  elsif type_name == "string"
    assert = "XCTAssertTrue([#{model_name}.#{name} isEqualToString:#{self.objective_c_type_fixture_value(type_name)}], @\"#{name} not correct value\")"
  elsif type_name
    assert = "XCTAssertNotNil(#{model_name}.#{name}, @\"#{name} is nil\")"
  else
    assert = "XCTAssertNotNil(#{model_name}.#{name}, @\"#{name} is nil\")"
  end
  assert
end

.template_foldersObject



15
16
17
# File 'lib/lanekit.rb', line 15

def self.template_folders
  [@template_folder]
end

.validate_app_name(app_name) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/lanekit.rb', line 205

def self.validate_app_name(app_name)
  if app_name.length < 2
    return "app name must be at least two characters long"
  elsif app_name.include? " "
    return "app name cannot include spaces"
  end
  return nil
end

.validate_bundle_id(bundle_id) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/lanekit.rb', line 223

def self.validate_bundle_id(bundle_id)
  if bundle_id.length < 2
    return "bundle id must be at least two characters long"
  elsif bundle_id.include? " "
    return "bundle id cannot include spaces"
  end
  return nil
end

.validate_lanefile(lanefile) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/lanekit.rb', line 240

def self.validate_lanefile(lanefile)
  if !lanefile.exists?
    return "Error: Cannot find 'Lanefile' in the current folder. Is this a LaneKit generated app folder?"
  end

  app_project_path = lanefile.app_project_path
  if !File.exists?(app_project_path)
    return "Lanefile Error: cannot find project: #{app_project_path}"
  end

  if !lanefile.app_project_name || !lanefile.app_project_name.length
    return "Lanefile Error: missing app_project_name"
  end

  if !lanefile.app_target_name || !lanefile.app_target_name.length
    return "Lanefile Error: missing app_target_name"
  end

  if !lanefile.app_target_tests_name || !lanefile.app_target_tests_name.length
    return "Lanefile Error: missing app_target_tests_name"
  end
end

.validate_pod_name(pod_name) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/lanekit.rb', line 214

def self.validate_pod_name(pod_name)
  if pod_name.length < 2
    return "pod name must be at least two characters long"
  elsif pod_name.include? " "
    return "pod name cannot include spaces"
  end
  return nil
end

.views_folder(lanefile) ⇒ Object



271
272
273
# File 'lib/lanekit.rb', line 271

def self.views_folder(lanefile)
  "#{lanefile.app_project_name}/#{lanefile.app_project_name}/Views"
end

.views_group(lanefile) ⇒ Object



275
276
277
# File 'lib/lanekit.rb', line 275

def self.views_group(lanefile)
  "#{lanefile.app_project_name}/Views"
end