Class: GHUnit::Project

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_path, target_name, test_target_name, logger = nil) ⇒ Project

Returns a new instance of Project.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ghunit/project.rb', line 13

def initialize(project_path, target_name, test_target_name, logger=nil)
  @target_name = target_name
  @project_path = project_path
  @test_target_name = test_target_name
  @logger ||= begin
    logger = Logger.new(STDOUT)
    logger.formatter = proc do |severity, datetime, progname, msg|
      case severity
      when "ERROR"
        "#{msg}\n".red
      when "DEBUG"
        "#{msg}\n".green
      else
        "#{msg}\n"
      end
    end
    logger
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/ghunit/project.rb', line 9

def logger
  @logger
end

#main_targetObject (readonly)

Returns the value of attribute main_target.



11
12
13
# File 'lib/ghunit/project.rb', line 11

def main_target
  @main_target
end

#projectObject (readonly)

Returns the value of attribute project.



11
12
13
# File 'lib/ghunit/project.rb', line 11

def project
  @project
end

#project_pathObject (readonly)

Returns the value of attribute project_path.



9
10
11
# File 'lib/ghunit/project.rb', line 9

def project_path
  @project_path
end

#target_nameObject (readonly)

Returns the value of attribute target_name.



9
10
11
# File 'lib/ghunit/project.rb', line 9

def target_name
  @target_name
end

#test_target_nameObject (readonly)

Returns the value of attribute test_target_name.



9
10
11
# File 'lib/ghunit/project.rb', line 9

def test_target_name
  @test_target_name
end

Class Method Details

.open(project_path, target_name, test_target_name, logger = nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/ghunit/project.rb', line 52

def open(project_path, target_name, test_target_name, logger=nil)
  project = GHUnit::Project.new(project_path, target_name, test_target_name, logger)
  if project.open
    project
  else
    nil
  end
end

Instance Method Details

#add_test_file(path) ⇒ Object

Add a file to the test target



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ghunit/project.rb', line 167

def add_test_file(path)
  test_target = find_test_target
  if !test_target
    logger.error "No test target to add to"
    return false
  end

  tests_group = project.groups.select { |g| g.name == test_target_name }.first
  tests_group ||= project.new_group(test_target_name)

  test_file = tests_group.find_file_by_path(path)
  if !test_file
    test_file = tests_group.new_file(path)
  end
  test_target.add_file_references([test_file])
  true
end

#check_podObject

Check the Podfile or just display some Podfile help



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ghunit/project.rb', line 193

def check_pod
  logger.info "\nAdd the following to your Podfile and run pod install.\n\n\#{template(\"Podfile\")}\n\nMake sure to open the .xcworkspace.\n\n"
end

#create_test(name) ⇒ Object



185
186
187
188
189
# File 'lib/ghunit/project.rb', line 185

def create_test(name)
  name = "#{name}.m" unless name.end_with?(".m")
  path = create_test_file(name, template("Test.m"))
  logger.debug "Created test: #{path}"
end

#create_test_file(file_name, content, force = false) ⇒ Object

Create a file with content and add to the test target



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ghunit/project.rb', line 150

def create_test_file(file_name, content, force=false)
  # Create main.m for test target
  path = File.join(test_target_name, file_name)

  if !force && File.exists?(path)
    logger.info "Test file already exists, skipping"
  end

  logger.debug "Creating: #{path}"
  File.open(path, "w") { |f| f.write(content) }

  add_test_file(path)
  path
end

#create_test_targetObject

Create the test target and setup everything



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ghunit/project.rb', line 68

def create_test_target
  Dir.chdir(File.dirname(project_path))
  FileUtils.mkdir_p(test_target_name)

  # Write the Tests-Info.plist
  test_info = {
    "CFBundleDisplayName" => "${PRODUCT_NAME}",
    "CFBundleExecutable" => "${EXECUTABLE_NAME}",
    "CFBundleIdentifier" =>  "tests.${PRODUCT_NAME:rfc1034identifier}",
    "CFBundleInfoDictionaryVersion" => "6.0",
    "CFBundleName" => "${PRODUCT_NAME}",
    "CFBundlePackageType" => "APPL",
    "CFBundleShortVersionString" => "1.0",
    "CFBundleVersion" => "1.0",
    "LSRequiresIPhoneOS" => true,
    "UISupportedInterfaceOrientations" => ["UIInterfaceOrientationPortrait"]
  }
  test_info_path = File.join(test_target_name, "#{test_target_name}-Info.plist")
  if !File.exists?(test_info_path)
    logger.debug "Creating: #{test_info_path}"
    Xcodeproj.write_plist(test_info, test_info_path)
  else
    logger.debug "#{test_info_path} already exists, skipping..."
  end

  test_target = find_test_target
  if !test_target

    # Create the test target
    logger.debug "Creating target: #{test_target_name}"
    test_target = project.new_target(:application, test_target_name, :ios, "7.0")
    test_target.add_dependency(main_target)

    create_test_file("main.m", template("main.m"), true)
    create_test("MyTest")

  else
    logger.debug "Test target already exists, skipping..."
  end

  # Use same resources build phase as main target
  # Have to compare with class name because of funky const loading in xcodeproj gem
  resources_build_phase = main_target.build_phases.select { |p|
    p.class.to_s == "Xcodeproj::Project::Object::PBXResourcesBuildPhase" }.first
  test_target.build_phases << resources_build_phase if resources_build_phase

  # Get main target prefix header
  prefix_header = main_target.build_settings("Debug")["GCC_PREFIX_HEADER"]

  # Clear default OTHER_LDFLAGS (otherwise CocoaPods gives a warning)
  test_target.build_configurations.each do |c|
    c.build_settings.delete("OTHER_LDFLAGS")
    c.build_settings["INFOPLIST_FILE"] = test_info_path
    c.build_settings["GCC_PREFIX_HEADER"] = prefix_header if prefix_header
  end

  # Create test scheme if it doesn't exist
  logger.debug "Checking for Test scheme..."
  schemes = Xcodeproj::Project.schemes(project_path)
  test_scheme = schemes.select { |s| s == test_target_name }.first
  if !test_scheme
    logger.debug "Test scheme not found, creating..."
    scheme = Xcodeproj::XCScheme.new
    scheme.set_launch_target(test_target)
    scheme.save_as(project_path, test_target_name)
  else
    logger.debug "Test scheme already exists, skipping..."
  end

  logger.debug "Saving project..."
  project.save

  check_pod
end

#find_test_targetObject



62
63
64
# File 'lib/ghunit/project.rb', line 62

def find_test_target
  project.targets.select { |t| t.name == test_target_name }.first
end

#openObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ghunit/project.rb', line 33

def open
  if !File.exists?(project_path)
    logger.error "Can't find project path at #{project_path}"
    return false
  end

  @project = Xcodeproj::Project.open(project_path)

  # Find the main target for the test dependency
  @main_target = project.targets.select { |t| t.name == target_name }.first
  if !@main_target
    logger.error "No target with name #{target_name}"
    return false
  end

  true
end

#template(name) ⇒ Object



143
144
145
146
# File 'lib/ghunit/project.rb', line 143

def template(name)
  template_path = File.join(File.dirname(__FILE__), "templates", name)
  File.read(template_path)
end