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

Commit

Permalink
Implemented pod playgrounds command
Browse files Browse the repository at this point in the history
  • Loading branch information
neonichu committed Feb 10, 2016
1 parent 61ceb65 commit 9496ecc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Style/Documentation:
- 'test/**/*'
- 'bin/playground'
- 'lib/cocoapods-playgrounds/generate.rb'
- 'lib/cocoapods-playgrounds/command/playgrounds.rb'

Style/FileName:
Exclude:
Expand All @@ -15,3 +16,9 @@ Style/FileName:
Lint/Void:
Exclude:
- 'spec/**/*'

Metrics/AbcSize:
Max: 20

Metrics/MethodLength:
Max: 20
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# This Could Be Us But You Playing

:warning: WIP

Generates a Swift Playground for any Pod.

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

To generate a Playground for a specific Pod:

$ pod playgrounds POD_NAME
$ pod playgrounds Alamofire

## Installation

$ gem install cocoapods-playgrounds

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

self.description = <<-DESC
Longer description of cocoapods-playgrounds.
Generates a Swift Playground for any Pod.
DESC

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

def initialize(argv)
@name = argv.shift_argument
@platform = :ios # TODO: Should be configurable
@deployment_target = '9.0' # TODO: Should be configurable
super
end

Expand All @@ -37,7 +22,64 @@ def validate!
end

def run
UI.puts "Add your implementation for the cocoapods-playgrounds plugin in #{__FILE__}"
generate_project

Dir.chdir(target_dir) do
generate_podfile
Pod::Executable.execute_command('pod', ['install', '--no-repo-update'])
generator = Pod::PlaygroundGenerator.new(@platform)
path = generator.generate(@name)
File.open(path + 'Contents.swift', 'w') do |f|
f.write("//: Please build the scheme '#{target_name}' first\n")
f.write("import XCPlayground\n")
f.write("XCPlaygroundPage.currentPage.needsIndefiniteExecution = true\n\n")
f.write("import #{@name}\n\n")
end
end

`open #{workspace_path}`
end

private

def target_dir
Pathname.new(target_name)
end

def target_name
"#{@name}Playground"
end

def workspace_path
target_dir + "#{@name}.xcworkspace"
end

def generate_podfile
contents = "use_frameworks!\n\n"
contents << "target '#{target_name}' do\n"
contents << "pod '#{@name}'\n"
contents << "end\n"
File.open('Podfile', 'w') { |f| f.write(contents) }
end

def generate_project
`rm -fr #{target_dir}`
FileUtils.mkdir_p(target_dir)

project_path = "#{target_dir}/#{@name}.xcodeproj"
project = Xcodeproj::Project.new(project_path)

target = project.new_target(:framework,
target_name,
@platform,
@deployment_target)
target.build_configurations.each do |config|
config.build_settings['DEFINES_MODULE'] = 'NO'
end

# TODO: Should be at the root of the project
project.new_file("#{@name}.playground")
project.save
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/cocoapods-playgrounds/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(platform)

def generate(name)
FileUtils.cp_r(@template, "#{name}.playground")
Pathname.new("#{name}.playground")
end

def self.platforms
Expand Down

0 comments on commit 9496ecc

Please sign in to comment.