11module 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
0 commit comments