rcli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +193 -0
  3. data/README.rdoc +194 -0
  4. data/Rakefile +61 -0
  5. data/bin/rcli +12 -0
  6. data/lib/commands/compile.rb +15 -0
  7. data/lib/commands/debug.rb +86 -0
  8. data/lib/commands/edit.rb +34 -0
  9. data/lib/commands/generate.rb +43 -0
  10. data/lib/commands/help.rb +47 -0
  11. data/lib/commands/install.rb +86 -0
  12. data/lib/commands/list.rb +28 -0
  13. data/lib/commands/uninstall.rb +36 -0
  14. data/lib/commands/version.rb +11 -0
  15. data/lib/core/actions/create_file.rb +106 -0
  16. data/lib/core/actions/empty_directory.rb +88 -0
  17. data/lib/core/actions/file_binary_read.rb +9 -0
  18. data/lib/core/actions.rb +9 -0
  19. data/lib/core/command.rb +137 -0
  20. data/lib/core/commander.rb +48 -0
  21. data/lib/core/console.rb +5 -0
  22. data/lib/core/global_functions.rb +16 -0
  23. data/lib/core/shared/rcli_installation.rb +9 -0
  24. data/lib/core/thor_actions/create_file.rb +100 -0
  25. data/lib/core/thor_actions/directory.rb +89 -0
  26. data/lib/core/thor_actions/empty_directory.rb +134 -0
  27. data/lib/core/thor_actions/file_binary_read.rb +9 -0
  28. data/lib/core/thor_actions/file_manipulation.rb +223 -0
  29. data/lib/core/thor_actions/inject_into_file.rb +102 -0
  30. data/lib/core/thor_actions.rb +302 -0
  31. data/lib/core/traceable_factory.rb +20 -0
  32. data/lib/core/traceable_object.rb +45 -0
  33. data/lib/core/tracer.rb +23 -0
  34. data/lib/rcli.rb +65 -0
  35. data/lib/templates/new_app/RCLIAPPNAME.rb +9 -0
  36. data/lib/templates/new_app/lib/commands/default.rb +12 -0
  37. data/lib/templates/new_app/lib/commands/version.rb +6 -0
  38. data/lib/vendor/mainline/lib/trollop.rb +782 -0
  39. data/lib/vendor/mainline/test/test_trollop.rb +1094 -0
  40. data/lib/vendor/trollop.rb +782 -0
  41. data/test/helper.rb +10 -0
  42. data/test/test_rcli-gem.rb +7 -0
  43. metadata +137 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Eduardo Del Balso
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # rcli
2
+
3
+ ## WHAT IS THIS
4
+
5
+ rcli is a simple tool for writing and managing command-line applications. It
6
+ spawned simply from me not liking the idea that I have to type 'thor' before
7
+ my command line scripts. This is my take on getting this done a little more
8
+ cleanly.
9
+
10
+ ## CAVEAT
11
+
12
+ This was done a project to learn ruby, and is by no means perfect. If you like
13
+ it, please let me know. If you would like to add features, also let me
14
+ know as I'll be glad to hear your ideas and integrate your code.
15
+
16
+ Most importantly, if you have ideas of things I did wrong and could do better,
17
+ let me know! The point is for me to learn, hopefully you find use in this at
18
+ the same time.
19
+
20
+ ## INSTALLATION
21
+
22
+ Find a nice place to put this repo. I like:
23
+
24
+ mkdir -p ~/lib/ruby
25
+ cd ~/lib/ruby
26
+ git clone git://github.com/edelbalso/rcli.git
27
+
28
+ Now build the gem
29
+
30
+ cd rcli
31
+ build rcli.gemspec
32
+ gem install rcli-0.6.gem
33
+
34
+ Then install the required gems:
35
+
36
+ gem install highline
37
+ gem install text
38
+
39
+ and you should be good to go. Try:
40
+
41
+ rcli
42
+
43
+ If you don't get a page full of exceptions, you're good to go. If so,
44
+ get in touch with me and I will help you out.
45
+
46
+ ## ADVANCED INSTALLATION
47
+
48
+ *WARNING! WARNING!*
49
+
50
+ I suggest you back up your ~/.bash_profile before completing this
51
+ step. You've been warned.
52
+
53
+ *END WARNING!*
54
+
55
+ rcli has the capability of 'installing' the applications you make with
56
+ it so that they're available system-wide. It does this by creating a
57
+ '.rcli' hidden folder and storing the locations of your applications
58
+ once they're installed.
59
+
60
+ To set up this folder in your home directory, type:
61
+
62
+ rcli install
63
+
64
+ It will then prompt you to add a line to your .bash_profile. If you've
65
+ succesfully backed up your .bash_profile, go ahead and accept this. The
66
+ uninstall doesn't yet remove the added lines, so if you've done this once,
67
+ no point in doing it again or it will just mess up your file unecessarily.
68
+
69
+ Once installed, do this to load the path into your terminal:
70
+
71
+ source ~/.bash_profile
72
+
73
+ ## WRITING YOUR FIRST APP
74
+
75
+ To get started with rcli, simply create a folder anywhere in your home
76
+ folder where you'd like to keep your applications. I like:
77
+
78
+ mkdir -p ~/lib/ruby/rcli_apps/
79
+
80
+ but any folder will do. Now create your app:
81
+
82
+ cd ~/lib/ruby/rcli_apps/
83
+ rcli generate gemate
84
+ rcli install gemate/gemate.rb gemate
85
+
86
+ You should now have created an application 'gemate' that can be used
87
+ from anywhere on your system. Go ahead and confirm this:
88
+
89
+ rcli list
90
+
91
+ 'gemate' should be listed here. If you have textmate installed, you
92
+ can now go ahead and edit the generate app by typing:
93
+
94
+ rcli edit gemate
95
+
96
+ and you can go ahead and rename the command located at 'lib/commands/example.rb'
97
+ to 'lib/commands/view.rb'
98
+
99
+ mv gemate/lib/commands/example.rb gemate/lib/commands/view.rb
100
+
101
+ and put the following code in the file:
102
+
103
+ class ViewCommand < Command
104
+
105
+ def after_init
106
+ # called after Command::initialize
107
+ @description = "Opens a gem (or gems) to be viewed in Textmate"
108
+ end
109
+
110
+ def main
111
+
112
+ if @params[:args].size != 1
113
+ puts "ERROR: please provide a gem name to look up"
114
+ end
115
+
116
+ name = @params[:args][0]
117
+ term = name + '*'
118
+
119
+ puts "Searching for '" + term + "'..."
120
+
121
+ # get the path to the gem directory from rubygems and search for any matching directories
122
+ path = `gem env gemdir`
123
+ new_path = File.join([path.chomp, 'gems'])
124
+ Dir.chdir(new_path)
125
+ @d_arr = Dir.glob(term)
126
+
127
+ if @d_arr.empty?
128
+ puts "Sorry, nothing like that found"
129
+ elsif @d_arr.size == 1
130
+ view_one()
131
+ else
132
+ view_list()
133
+ end
134
+
135
+ end
136
+
137
+ private
138
+
139
+ def view_one
140
+ puts @d_arr.first
141
+ print 'Do you want to view this? [Y/n]: '
142
+ resp = $stdin.gets.chomp!
143
+ if resp.empty? or resp.downcase == 'y'
144
+ exec "mate #{@d_arr.first}"
145
+ end
146
+ end
147
+
148
+ def view_list
149
+ @d_arr.each_with_index {|d,i| puts "#{i + 1}: #{d}"}
150
+ puts
151
+ print 'Choose a directory or quit(q): '
152
+ idx = gets.chomp!
153
+ if idx.downcase == 'q'
154
+ exit
155
+ elsif (1..@d_arr.size).include?(idx.to_i)
156
+ exec "mate #{@d_arr.at(idx.to_i - 1)}"
157
+ else
158
+ puts 'not a valid option'
159
+ end
160
+ end
161
+
162
+ end
163
+
164
+ This code has been ripped off from one of the Thor tutorials. My apologies.
165
+
166
+ Anyways, after saving this file, you should now be able to type
167
+
168
+ gemate view thor
169
+
170
+ and textmate should pop up with the thor. You should similarly be able to
171
+ view any other gem installed on your system.
172
+
173
+ Congrats, you've made your first command-line app with rcli.
174
+
175
+ ## TO DO
176
+
177
+ * Trollop is being used to parse commands. I need to clean up command-level
178
+ customization of trollop
179
+ * Wrap shell commands provided by highline into something clean.
180
+
181
+ ## DEPENDENCIES
182
+
183
+ sudo gem install highline
184
+ sudo gem install text
185
+
186
+
187
+ ## ACKNOWLEDGEMENTS
188
+
189
+ * Yehuda Katz for code I copied from Thor::Actions modules, as well as a bunch of other chunks. :)
190
+
191
+ ## AUTHOR
192
+
193
+ Eduardo Del Balso
data/README.rdoc ADDED
@@ -0,0 +1,194 @@
1
+ = rcli
2
+
3
+ == WHAT IS THIS
4
+
5
+ rcli is a simple tool for writing and managing command-line applications. It
6
+ spawned simply from me not liking the idea that I have to type 'thor' before
7
+ my command line scripts. This is my take on getting this done a little more
8
+ cleanly.
9
+
10
+ == CAVEAT
11
+
12
+ This was done a project to learn ruby, and is by no means perfect. If you like
13
+ it, please let me know. If you would like to add features, also let me
14
+ know as I'll be glad to hear your ideas and integrate your code.
15
+
16
+ Most importantly, if you have ideas of things I did wrong and could do better,
17
+ let me know! The point is for me to learn, hopefully you find use in this at
18
+ the same time.
19
+
20
+ == INSTALLATION
21
+
22
+ Find a nice place to put this repo. I like:
23
+
24
+ mkdir -p ~/lib/ruby
25
+ cd ~/lib/ruby
26
+ git clone git://github.com/edelbalso/rcli.git
27
+
28
+ Now build the gem
29
+
30
+ cd rcli
31
+ build rcli.gemspec
32
+ gem install rcli-0.6.gem
33
+
34
+ Then install the required gems:
35
+
36
+ gem install highline
37
+ gem install text
38
+
39
+ and you should be good to go. Try:
40
+
41
+ rcli
42
+
43
+ If you don't get a page full of exceptions, you're good to go. If so,
44
+ get in touch with me and I will help you out.
45
+
46
+ == ADVANCED INSTALLATION
47
+
48
+ *WARNING! WARNING!*
49
+
50
+ I suggest you back up your ~/.bash_profile before completing this
51
+ step. You've been warned.
52
+
53
+ *END WARNING!*
54
+
55
+ rcli has the capability of 'installing' the applications you make with
56
+ it so that they're available system-wide. It does this by creating a
57
+ '.rcli' hidden folder and storing the locations of your applications
58
+ once they're installed.
59
+
60
+ To set up this folder in your home directory, type:
61
+
62
+ rcli install
63
+
64
+ It will then prompt you to add a line to your .bash_profile. If you've
65
+ succesfully backed up your .bash_profile, go ahead and accept this. The
66
+ uninstall doesn't yet remove the added lines, so if you've done this once,
67
+ no point in doing it again or it will just mess up your file unecessarily.
68
+
69
+ Once installed, do this to load the path into your terminal:
70
+
71
+ source ~/.bash_profile
72
+
73
+ == WRITING YOUR FIRST APP
74
+
75
+ To get started with rcli, simply create a folder anywhere in your home
76
+ folder where you'd like to keep your applications. I like:
77
+
78
+ mkdir -p ~/lib/ruby/rcli_apps/
79
+
80
+ but any folder will do. Now create your app:
81
+
82
+ cd ~/lib/ruby/rcli_apps/
83
+ rcli generate gemate
84
+ rcli install gemate/gemate.rb gemate
85
+
86
+ You should now have created an application 'gemate' that can be used
87
+ from anywhere on your system. Go ahead and confirm this:
88
+
89
+ rcli list
90
+
91
+ 'gemate' should be listed here. If you have textmate installed, you
92
+ can now go ahead and edit the generate app by typing:
93
+
94
+ rcli edit gemate
95
+
96
+ and you can go ahead and rename the command located at 'lib/commands/example.rb'
97
+ to 'lib/commands/view.rb'
98
+
99
+ mv gemate/lib/commands/example.rb gemate/lib/commands/view.rb
100
+
101
+ and put the following code in the file:
102
+
103
+ class ViewCommand < Command
104
+
105
+ def after_init
106
+ # called after Command::initialize
107
+ @description = "Opens a gem (or gems) to be viewed in Textmate"
108
+ end
109
+
110
+ def main
111
+
112
+ if @params[:args].size != 1
113
+ puts "ERROR: please provide a gem name to look up"
114
+ end
115
+
116
+ name = @params[:args][0]
117
+ term = name + '*'
118
+
119
+ puts "Searching for '" + term + "'..."
120
+
121
+ # get the path to the gem directory from rubygems and search for any matching directories
122
+ path = `gem env gemdir`
123
+ new_path = File.join([path.chomp, 'gems'])
124
+ Dir.chdir(new_path)
125
+ @d_arr = Dir.glob(term)
126
+
127
+ if @d_arr.empty?
128
+ puts "Sorry, nothing like that found"
129
+ elsif @d_arr.size == 1
130
+ view_one()
131
+ else
132
+ view_list()
133
+ end
134
+
135
+ end
136
+
137
+ private
138
+
139
+ def view_one
140
+ puts @d_arr.first
141
+ print 'Do you want to view this? [Y/n]: '
142
+ resp = $stdin.gets.chomp!
143
+ if resp.empty? or resp.downcase == 'y'
144
+ exec "mate #{@d_arr.first}"
145
+ end
146
+ end
147
+
148
+ def view_list
149
+ @d_arr.each_with_index {|d,i| puts "#{i + 1}: #{d}"}
150
+ puts
151
+ print 'Choose a directory or quit(q): '
152
+ idx = gets.chomp!
153
+ if idx.downcase == 'q'
154
+ exit
155
+ elsif (1..@d_arr.size).include?(idx.to_i)
156
+ exec "mate #{@d_arr.at(idx.to_i - 1)}"
157
+ else
158
+ puts 'not a valid option'
159
+ end
160
+ end
161
+
162
+ end
163
+
164
+ This code has been ripped off from one of the Thor tutorials. My apologies.
165
+
166
+ Anyways, after saving this file, you should now be able to type
167
+
168
+ gemate view thor
169
+
170
+ and textmate should pop up with the thor. You should similarly be able to
171
+ view any other gem installed on your system.
172
+
173
+ Congrats, you've made your first command-line app with rcli.
174
+
175
+ == TO DO
176
+
177
+ * Trollop is being used to parse commands. I need to clean up command-level
178
+ customization of trollop
179
+ * Wrap shell commands provided by highline into something clean.
180
+
181
+ == DEPENDENCIES
182
+
183
+ sudo gem install highline
184
+ sudo gem install text
185
+
186
+
187
+ == ACKNOWLEDGEMENTS
188
+
189
+ * Yehuda Katz for code I copied from Thor::Actions modules, as well as a bunch of other chunks. :)
190
+
191
+
192
+ == Copyright
193
+
194
+ Copyright (c) 2010 Eduardo Del Balso. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rcli"
8
+ gem.summary = %Q{A rough framework for building command-line scripts in ruby.}
9
+ gem.description = %Q{This script creates, manages, installs, uninstalls and edits command-line ruby scripts. It also works as a useful library for building scripts.}
10
+ gem.email = "e.delbalso@gmail.com"
11
+ gem.homepage = "http://github.com/edelbalso/rcli"
12
+ gem.authors = ["Eduardo Del Balso"]
13
+
14
+ gem.add_dependency "text", ">= 0.2.0"
15
+ gem.add_dependency "highline", ">= 1.6.1"
16
+ #gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+
19
+ gem.executables = ['rcli']
20
+ gem.require_paths = ['lib']
21
+
22
+ gem.files = FileList['lib/**/*.rb','README*', 'LICENSE', 'Rakefile', 'test/**/*.*', 'bin/**/*.rb'].to_a
23
+ end
24
+ Jeweler::GemcutterTasks.new
25
+ rescue LoadError
26
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
27
+ end
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ begin
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+ rescue LoadError
44
+ task :rcov do
45
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
+ end
47
+ end
48
+
49
+ task :test => :check_dependencies
50
+
51
+ task :default => :test
52
+
53
+ require 'rake/rdoctask'
54
+ Rake::RDocTask.new do |rdoc|
55
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
56
+
57
+ rdoc.rdoc_dir = 'rdoc'
58
+ rdoc.title = "rcli-gem #{version}"
59
+ rdoc.rdoc_files.include('README*')
60
+ rdoc.rdoc_files.include('lib/**/*.rb')
61
+ end
data/bin/rcli ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'rcli'
5
+
6
+ #Rcli.trace_app = true # Uncomment if you want to enable tracing.
7
+
8
+ ### LAUNCH rcli
9
+ Rcli.type = :rcli
10
+ Rcli.script_root = Pathname(__FILE__).realpath.parent.parent.to_s
11
+ Rcli.go
12
+
@@ -0,0 +1,15 @@
1
+ class CompileCommand < Command
2
+
3
+ description 'Uninstalls and reinstalls rcli gem to your system from source. YMMV'
4
+ usage 'rcli compile'
5
+
6
+ def main
7
+
8
+ FileUtils.cd(Rcli::SRC_PATH)
9
+ `./comp`
10
+
11
+ puts "Done"
12
+ # exec 'comp'
13
+ end
14
+
15
+ end
@@ -0,0 +1,86 @@
1
+ class DebugCommand < Command
2
+
3
+ description "Dumps debug variables"
4
+ usage "rcli debug [console]"
5
+
6
+ def main
7
+ allowed_commands = ['console']
8
+ if @params[:args].size == 0
9
+ default_debug
10
+ elsif @params[:args].size == 1 && allowed_commands.include?( @params[:args][0] )
11
+ send( "console_debug" )
12
+ else
13
+ puts "ERROR: Invalid command"
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def console_debug
20
+ # Supported color sequences.
21
+ colors = %w{black red green yellow blue magenta cyan white}
22
+
23
+ # Using color() with symbols.
24
+ colors.each_with_index do |c, i|
25
+ say("This should be <%= color('#{c}', :#{c}) %>!")
26
+ if i == 0
27
+ say( "This should be " +
28
+ "<%= color('white on #{c}', :white, :on_#{c}) %>!")
29
+ else
30
+ say( "This should be " +
31
+ "<%= color( '#{colors[i - 1]} on #{c}',
32
+ :#{colors[i - 1]}, :on_#{c} ) %>!")
33
+ end
34
+ end
35
+
36
+ # Using color with constants.
37
+ say("This should be <%= color('bold', BOLD) %>!")
38
+ say("This should be <%= color('underlined', UNDERLINE) %>!")
39
+
40
+ # Using constants only.
41
+ say("This might even <%= BLINK %>blink<%= CLEAR %>!")
42
+
43
+ # It even works with list wrapping.
44
+ erb_digits = %w{Zero One Two Three Four} +
45
+ ["<%= color('Five', :blue) %%>"] +
46
+ %w{Six Seven Eight Nine}
47
+ say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
48
+
49
+ end
50
+ def default_debug
51
+ puts " misc"
52
+ puts " ----"
53
+ print "Dir.pwd : "
54
+ puts Dir.pwd
55
+ puts
56
+
57
+ puts " rcli"
58
+ puts " ----"
59
+ print "Rcli::GEM_ROOT : "
60
+ puts Rcli::GEM_ROOT
61
+ print "Rcli::GEM_LIB : "
62
+ puts Rcli::GEM_LIB
63
+ print "Rcli::GEM_BIN : "
64
+ puts Rcli::GEM_BIN
65
+ print "Rcli::GEM_CONFIG : "
66
+ puts Rcli::GEM_CONFIG
67
+ print "Rcli::RCLI_DOTFOLDER : "
68
+ puts Rcli::RCLI_DOTFOLDER
69
+ puts "Rcli::RCLI_CONFIG :"
70
+ pp Rcli::RCLI_CONFIG
71
+ puts
72
+
73
+ puts " script"
74
+ puts " ------"
75
+ print "Rcli.type : "
76
+ puts Rcli.type
77
+ print "Rcli.script_root : "
78
+ puts Rcli.script_root
79
+ print "Rcli.trace_app : "
80
+ puts Rcli.trace_app
81
+ puts "Rcli.script_config :"
82
+ pp Rcli.script_config
83
+
84
+
85
+ end
86
+ end
@@ -0,0 +1,34 @@
1
+ class EditCommand < Command
2
+
3
+ description "Opens a script for editing in your editor of choice"
4
+ usage "rcli edit <installed script> [-hv]"
5
+
6
+ def main
7
+ if @params[:args].size != 1
8
+ puts "ERROR: please provide a rcli app name to look up. Try 'rcli list' for a list of installed apps."
9
+ end
10
+
11
+ name = @params[:args][0]
12
+
13
+ if name == 'core'
14
+ exec "mate #{Rcli::SRC_PATH}"
15
+ exit
16
+ end
17
+
18
+ yml_file = Rcli::RCLI_DOTFOLDER + DS + 'app_info' + DS + @params[:args][0] + '.yml'
19
+ unless File.exists? yml_file
20
+ puts "ERROR: That app is not installed"
21
+ exit
22
+ end
23
+
24
+ app_info = YAML.load_file(yml_file)
25
+
26
+ unless File.directory? app_info['application_root']
27
+ puts "ERROR: The installed application's folder could not be found"
28
+ exit
29
+ end
30
+
31
+ exec "$EDITOR #{app_info['application_root']}"
32
+ end
33
+
34
+ end
@@ -0,0 +1,43 @@
1
+ class GenerateCommand < Command
2
+
3
+ include Rcli::Actions
4
+
5
+ description "Generates a template CLI script in current folder"
6
+
7
+ def main()
8
+
9
+ if @params[:args].length != 1
10
+ puts "ERROR: You must provide a unique app name as an argument."
11
+ exit
12
+ end
13
+
14
+ if File.directory? Dir.pwd + DS + @params[:args][0]
15
+ puts "ERROR: folder already exists. Please choose another name"
16
+ exit
17
+ end
18
+
19
+ app_name = @params[:args][0]
20
+
21
+ tpl_dir = Rcli::GEM_LIB + DS + 'templates' + DS + 'new_app'
22
+
23
+ # iterate through the new_app template
24
+ Dir[ tpl_dir + DS + '**' + DS + '*.*'].each do |f|
25
+ # TODO : Use Pathname#relative_path_from for this.
26
+ new_file_name = f[tpl_dir.length + 1 ,f.length - tpl_dir.length]
27
+
28
+ new_file_name.gsub!(/RCLIAPPNAME/,app_name)
29
+
30
+ puts "creating " + app_name + DS + new_file_name
31
+ create_file Dir.pwd() + DS + app_name + DS + new_file_name do
32
+ contents = File.read(f)
33
+ contents.gsub(/RCLIAPPNAME/,app_name)
34
+ end
35
+
36
+ if f == tpl_dir + DS + 'RCLIAPPNAME.rb'
37
+ puts "Changing executable mode of " + new_file_name
38
+ File.chmod(0755,Dir.pwd() + DS + app_name + DS + new_file_name)
39
+ end
40
+ end
41
+ end
42
+
43
+ end