Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 9496ecc

Browse files
committed
Implemented pod playgrounds command
1 parent 61ceb65 commit 9496ecc

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Style/Documentation:
77
- 'test/**/*'
88
- 'bin/playground'
99
- 'lib/cocoapods-playgrounds/generate.rb'
10+
- 'lib/cocoapods-playgrounds/command/playgrounds.rb'
1011

1112
Style/FileName:
1213
Exclude:
@@ -15,3 +16,9 @@ Style/FileName:
1516
Lint/Void:
1617
Exclude:
1718
- 'spec/**/*'
19+
20+
Metrics/AbcSize:
21+
Max: 20
22+
23+
Metrics/MethodLength:
24+
Max: 20

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# This Could Be Us But You Playing
22

3-
:warning: WIP
4-
53
Generates a Swift Playground for any Pod.
64

75
## Usage
@@ -13,9 +11,8 @@ To generate a Playground from the commandline:
1311

1412
To generate a Playground for a specific Pod:
1513

16-
$ pod playgrounds POD_NAME
14+
$ pod playgrounds Alamofire
1715

1816
## Installation
1917

2018
$ gem install cocoapods-playgrounds
21-

lib/cocoapods-playgrounds/command/playgrounds.rb

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
module Pod
22
class Command
3-
# This is an example of a cocoapods plugin adding a top-level subcommand
4-
# to the 'pod' command.
5-
#
6-
# You can also create subcommands of existing or new commands. Say you
7-
# wanted to add a subcommand to `list` to show newly deprecated pods,
8-
# (e.g. `pod list deprecated`), there are a few things that would need
9-
# to change.
10-
#
11-
# - move this file to `lib/pod/command/list/deprecated.rb` and update
12-
# the class to exist in the the Pod::Command::List namespace
13-
# - change this class to extend from `List` instead of `Command`. This
14-
# tells the plugin system that it is a subcommand of `list`.
15-
# - edit `lib/cocoapods_plugins.rb` to require this file
16-
#
17-
# @todo Create a PR to add your plugin to CocoaPods/cocoapods.org
18-
# in the `plugins.json` file, once your plugin is released.
19-
#
203
class Playgrounds < Command
21-
self.summary = 'Short description of cocoapods-playgrounds.'
4+
self.summary = 'Generates a Swift Playground for any Pod.'
225

236
self.description = <<-DESC
24-
Longer description of cocoapods-playgrounds.
7+
Generates a Swift Playground for any Pod.
258
DESC
269

2710
self.arguments = [CLAide::Argument.new('NAME', true)]
2811

2912
def initialize(argv)
3013
@name = argv.shift_argument
14+
@platform = :ios # TODO: Should be configurable
15+
@deployment_target = '9.0' # TODO: Should be configurable
3116
super
3217
end
3318

@@ -37,7 +22,64 @@ def validate!
3722
end
3823

3924
def run
40-
UI.puts "Add your implementation for the cocoapods-playgrounds plugin in #{__FILE__}"
25+
generate_project
26+
27+
Dir.chdir(target_dir) do
28+
generate_podfile
29+
Pod::Executable.execute_command('pod', ['install', '--no-repo-update'])
30+
generator = Pod::PlaygroundGenerator.new(@platform)
31+
path = generator.generate(@name)
32+
File.open(path + 'Contents.swift', 'w') do |f|
33+
f.write("//: Please build the scheme '#{target_name}' first\n")
34+
f.write("import XCPlayground\n")
35+
f.write("XCPlaygroundPage.currentPage.needsIndefiniteExecution = true\n\n")
36+
f.write("import #{@name}\n\n")
37+
end
38+
end
39+
40+
`open #{workspace_path}`
41+
end
42+
43+
private
44+
45+
def target_dir
46+
Pathname.new(target_name)
47+
end
48+
49+
def target_name
50+
"#{@name}Playground"
51+
end
52+
53+
def workspace_path
54+
target_dir + "#{@name}.xcworkspace"
55+
end
56+
57+
def generate_podfile
58+
contents = "use_frameworks!\n\n"
59+
contents << "target '#{target_name}' do\n"
60+
contents << "pod '#{@name}'\n"
61+
contents << "end\n"
62+
File.open('Podfile', 'w') { |f| f.write(contents) }
63+
end
64+
65+
def generate_project
66+
`rm -fr #{target_dir}`
67+
FileUtils.mkdir_p(target_dir)
68+
69+
project_path = "#{target_dir}/#{@name}.xcodeproj"
70+
project = Xcodeproj::Project.new(project_path)
71+
72+
target = project.new_target(:framework,
73+
target_name,
74+
@platform,
75+
@deployment_target)
76+
target.build_configurations.each do |config|
77+
config.build_settings['DEFINES_MODULE'] = 'NO'
78+
end
79+
80+
# TODO: Should be at the root of the project
81+
project.new_file("#{@name}.playground")
82+
project.save
4183
end
4284
end
4385
end

lib/cocoapods-playgrounds/generate.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def initialize(platform)
1111

1212
def generate(name)
1313
FileUtils.cp_r(@template, "#{name}.playground")
14+
Pathname.new("#{name}.playground")
1415
end
1516

1617
def self.platforms

0 commit comments

Comments
 (0)