Skip to content

Commit ba0626f

Browse files
authored
Merge pull request #1 from srbaker/create_command
Add a command for creating new projects.
2 parents 06ec279 + 560cbef commit ba0626f

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

lib/xcodeproj/command.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Xcodeproj
33
require 'claide'
44

55
class Command < CLAide::Command
6+
require 'xcodeproj/command/create'
67
require 'xcodeproj/command/config_dump'
78
require 'xcodeproj/command/target_diff'
89
require 'xcodeproj/command/project_diff'

lib/xcodeproj/command/create.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Xcodeproj
2+
class Command
3+
class Create < Command
4+
self.arguments = [
5+
CLAide::Argument.new('PROJECT', true),
6+
]
7+
8+
EXTENSION = '.xcodeproj'
9+
10+
def initialize(argv)
11+
@project_name = argv.shift_argument
12+
13+
add_extension_if_missing
14+
15+
super
16+
end
17+
18+
def validate!
19+
super
20+
help! "Project file not specified" if @project_name.nil?
21+
help! "Project already exists" if File.exist?(@project_name)
22+
end
23+
24+
def run
25+
project = Xcodeproj::Project.new(@project_name)
26+
project.save
27+
end
28+
29+
def add_extension_if_missing
30+
return unless @project_name
31+
32+
@project_name += EXTENSION unless File.extname(@project_name) == EXTENSION
33+
end
34+
end
35+
end
36+
end

spec/command/create_spec.rb

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
require File.expand_path('../../spec_helper', __FILE__)
22

3-
module Xcodeproj
4-
class Command
5-
class Create < Command
6-
self.arguments = [
7-
CLAide::Argument.new('PROJECT', true),
8-
]
9-
10-
def initialize(argv)
11-
self.xcodeproj_path = argv.shift_argument
12-
super
13-
end
14-
15-
def validate!
16-
super
17-
help! "Project file not specified" if self.xcodeproj_path.nil?
18-
end
19-
end
20-
end
21-
end
22-
3+
require 'fileutils'
234

245
describe Xcodeproj::Command::Create do
256
it 'errors if a project file has not been provided' do
@@ -30,7 +11,39 @@ def validate!
3011
end
3112
end
3213

33-
it 'errors if the specified project file already exists'
14+
it 'errors if the specified project already exists' do
15+
project_dir = 'FooBar.xcodeproj'
16+
FileUtils.mkdir(project_dir)
17+
18+
argv = CLAide::ARGV.new([project_dir])
19+
create = Xcodeproj::Command::Create.new(argv)
20+
should_raise_help 'Project already exists' do
21+
create.validate!
22+
end
23+
ensure
24+
FileUtils.rm_r(project_dir)
25+
end
26+
27+
it 'creates a project file' do
28+
project_dir = 'FooBar.xcodeproj'
29+
argv = CLAide::ARGV.new([project_dir])
30+
create = Xcodeproj::Command::Create.new(argv)
31+
create.run
32+
33+
File.exist?(project_dir).should.be.true
34+
ensure
35+
FileUtils.rm_r(project_dir)
36+
end
37+
38+
it 'adds the suffix if one is not provided' do
39+
project_name = 'FooBar'
40+
project_dir = 'FooBar.xcodeproj'
41+
argv = CLAide::ARGV.new([project_name])
42+
create = Xcodeproj::Command::Create.new(argv)
43+
create.run
3444

35-
it 'creates a project file'
45+
File.exist?(project_dir).should.be.true
46+
ensure
47+
FileUtils.rm_r(project_dir)
48+
end
3649
end

0 commit comments

Comments
 (0)