Class: Ptero::Application
- Inherits:
-
Object
- Object
- Ptero::Application
- Defined in:
- lib/ptero/application.rb
Overview
Each instance of this class represents a Ptero application. Within its directory, this class is used to test the presence of PHP, download the composer.phar file, install composer dependencies, and use Generators to automatically create new files and components in the application.
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.app_for(dir = Dir.pwd) ⇒ Application
Return the first verified application in the specified directory or its parents.
-
.create(name, dir = Pathname.new(Dir.pwd)) ⇒ Application
Create a new application with basic files and Composer dependencies.
-
.current_app ⇒ Application
Return the verified Application object corresponding to Dir.pwd or its parent directories.
Instance Method Summary collapse
- #dependencies_installed? ⇒ Boolean
-
#destroy ⇒ Object
Remove the directory of this application.
-
#generate(klass, *params) ⇒ Object
Generate a file using the given arguments.
- #generated?(klass, *params) ⇒ Boolean
-
#get_composer ⇒ Application, Boolean
Download the composer.phar file into the Application directory using the command ‘curl -sS getcomposer.org/installer | php’ as prescribed on the Composer website.
-
#has_composer? ⇒ Boolean
Find out if this Application has the composer.phar file.
-
#initialize(dir = Dir.pwd) ⇒ Application
constructor
Input a directory String or Pathname to make a new Application from that path.
-
#install_dependencies ⇒ Application
Install all dependencies listed in the composer.json file using Composer ‘php composer.phar install’.
-
#method_missing(method_name, *params) ⇒ Object
If the method name is of the type “generate_NAME(*params)”, such as generate_javascript(‘contact’,‘Display contact information’), call self.generate(NAME,*params), so self.generate(‘javascript’,‘contact’,‘Display contact information’).
-
#reload(klass, *params) ⇒ Object
Remove and reload an existing file.
-
#remove(klass, *params) ⇒ Object
Remove a previously generated file.
-
#remove_composer ⇒ Application, Boolean
Remove the composer.phar file.
- #remove_dependencies ⇒ Object
-
#route(path, controller) ⇒ Object
Add a route to the Application routes file.
-
#routes ⇒ Hash
Find all routes set in the current Application.
-
#test_php ⇒ Boolean
Find out whether or not the user has the PHP command-line tool installed so that they can use Composer.
-
#unroute(path) ⇒ Object
Remove a rotue from the Application routes file.
-
#update_dependencies ⇒ Application
Update all dependencies listed in the composer.json file using Composer ‘php composer.phar update’.
-
#verify ⇒ Boolean
Find out if this folder really contains a Ptero application using metadata in composer.json.
Constructor Details
#initialize(dir = Dir.pwd) ⇒ Application
Input a directory String or Pathname to make a new Application from that path. (Defaults to Dir.pwd)
20 21 22 23 24 25 |
# File 'lib/ptero/application.rb', line 20 def initialize(dir=Dir.pwd) @name = dir.to_s.split('/').last.capitalize @dir = Pathname.new(dir) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *params) ⇒ Object
If the method name is of the type “generate_NAME(*params)”, such as generate_javascript(‘contact’,‘Display contact information’), call self.generate(NAME,*params), so self.generate(‘javascript’,‘contact’,‘Display contact information’)
197 198 199 200 201 202 203 204 |
# File 'lib/ptero/application.rb', line 197 def method_missing(method_name,*params) match = method_name.to_s.match /^generate_(\w+)$/ super unless match type = match[1] generate(type, *params) rescue NameError => e # Couldn't load the proper constant super end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
27 28 29 |
# File 'lib/ptero/application.rb', line 27 def dir @dir end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/ptero/application.rb', line 27 def name @name end |
Class Method Details
.app_for(dir = Dir.pwd) ⇒ Application
Return the first verified application in the specified directory or its parents
263 264 265 266 267 268 269 270 271 |
# File 'lib/ptero/application.rb', line 263 def app_for(dir=Dir.pwd) path = Pathname.new dir path.realpath.ascend do |level| app = self.new(level) return app if app.verify end raise Ptero::Exception::ApplicationException, "Couldn't find composer.json with dinosaur information in #{dir} or parent directories" end |
.create(name, dir = Pathname.new(Dir.pwd)) ⇒ Application
Create a new application with basic files and Composer dependencies. Yield the newly-created Application object to a block if one is given.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/ptero/application.rb', line 227 def create(name,dir=Pathname.new(Dir.pwd)) Dir.chdir dir do raise Ptero::Exception::ApplicationException, "Cannot create project #{name} because a file already exists with that name" if File.exist? name puts "Creating directory #{name} in #{Dir.pwd}" Dir.mkdir(name) Dir.chdir(name) do puts "Loading default composer.json" composer = JSON.parse( File.read("#{__dir__}/composer_default.json") ) composer[:dinosaur] = { name: name, root: true } puts "Writing project composer.json" File.open('composer.json','w') do |file| file.puts JSON.pretty_generate(composer) end puts "Initializing app" app = app_for(Dir.pwd) yield app if block_given? return app end end end |
.current_app ⇒ Application
Return the verified Application object corresponding to Dir.pwd or its parent directories
275 276 277 |
# File 'lib/ptero/application.rb', line 275 def current_app app_for Dir.pwd end |
Instance Method Details
#dependencies_installed? ⇒ Boolean
115 116 117 |
# File 'lib/ptero/application.rb', line 115 def dependencies_installed? @dir.join('vendor').directory? end |
#destroy ⇒ Object
Remove the directory of this application
207 208 209 210 |
# File 'lib/ptero/application.rb', line 207 def destroy FileUtils.rm_r dir nil end |
#generate(klass, *params) ⇒ Object
Generate a file using the given arguments
132 133 134 135 136 137 |
# File 'lib/ptero/application.rb', line 132 def generate(klass,*params) klass = generator_for_name(klass) if klass.is_a? String # if klass is a string, make sure it's a class generator = klass.new(*params) generator.generate self end |
#generated?(klass, *params) ⇒ Boolean
139 140 141 142 143 |
# File 'lib/ptero/application.rb', line 139 def generated?(klass,*params) klass = generator_for_name(klass) if klass.is_a? String generator = klass.new(*params) generator.generated? end |
#get_composer ⇒ Application, Boolean
Download the composer.phar file into the Application directory using the command ‘curl -sS getcomposer.org/installer | php’ as prescribed on the Composer website
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ptero/application.rb', line 41 def get_composer raise Ptero::Exception::ApplicationException, "Application #{self} already has composer.phar file" if has_composer? Dir.chdir @dir do # Download code from the composer website # https://getcomposer.org Open3.popen3("php -r \"readfile('https://getcomposer.org/installer');\" | php") do |stdin,stdout,stderr| print 'Downloading composer' stdout.each_line do |line| print '.' end puts puts 'Done!' end end self end |
#has_composer? ⇒ Boolean
Find out if this Application has the composer.phar file.
68 69 70 71 72 |
# File 'lib/ptero/application.rb', line 68 def has_composer? Dir.chdir @dir do File.exist? 'composer.phar' end end |
#install_dependencies ⇒ Application
Install all dependencies listed in the composer.json file using Composer ‘php composer.phar install’
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ptero/application.rb', line 77 def install_dependencies raise Ptero::Exception::ApplicationException, "Not a dinosaur root: #{Dir.pwd}" unless verify raise Ptero::Exception::ApplicationException, "PHP command-line tool failed, update your version of PHP" unless test_php Dir.chdir @dir do # Install dependencies using Composer # https://getcomposer.org Open3.popen3('php composer.phar install') do |stdin,stdout,stderr| print 'Installing dependencies' stdout.each_line do |line| print '.' end puts puts 'Done!' end end self end |
#reload(klass, *params) ⇒ Object
Remove and reload an existing file
148 149 150 151 152 153 |
# File 'lib/ptero/application.rb', line 148 def reload(klass,*params) klass = generator_for_name(klass) if klass.is_a? String generator = klass.new(*params) generator.reload self end |
#remove(klass, *params) ⇒ Object
Remove a previously generated file
177 178 179 180 181 182 |
# File 'lib/ptero/application.rb', line 177 def remove(klass,*params) klass = generator_for_name(klass) if klass.is_a? String # if klass is a string, make sure it's a class generator = klass.new(*params) generator.remove self end |
#remove_composer ⇒ Application, Boolean
Remove the composer.phar file
60 61 62 63 64 |
# File 'lib/ptero/application.rb', line 60 def remove_composer raise Ptero::Exception::ApplicationException, "Application #{self} does not have a composer.phar file" unless has_composer? File.unlink(dir.join('composer.phar')) self end |
#remove_dependencies ⇒ Object
111 112 113 |
# File 'lib/ptero/application.rb', line 111 def remove_dependencies FileUtils.rm_r(dir.join('vendor')) end |
#route(path, controller) ⇒ Object
Add a route to the Application routes file
158 159 160 161 162 163 |
# File 'lib/ptero/application.rb', line 158 def route(path,controller) klass = generator_for_name('Routes') generator = klass.new generator.route(path,controller) self end |
#routes ⇒ Hash
Find all routes set in the current Application
186 187 188 189 190 191 |
# File 'lib/ptero/application.rb', line 186 def routes klass = generator_for_name('Routes') generator = klass.new generator.get_current_routes generator.routes.dup end |
#test_php ⇒ Boolean
Find out whether or not the user has the PHP command-line tool installed so that they can use Composer.
31 32 33 34 35 |
# File 'lib/ptero/application.rb', line 31 def test_php Dir.chdir @dir do `php -r'echo "Success!";'` == 'Success!' end end |
#unroute(path) ⇒ Object
Remove a rotue from the Application routes file
167 168 169 170 171 172 |
# File 'lib/ptero/application.rb', line 167 def unroute(path) klass = generator_for_name('Routes') generator = klass.new generator.unroute(path) self end |
#update_dependencies ⇒ Application
Update all dependencies listed in the composer.json file using Composer ‘php composer.phar update’
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ptero/application.rb', line 98 def update_dependencies raise Ptero::Exception::ApplicationException, "Not a dinosaur root: #{Dir.pwd}" unless verify raise Ptero::Exception::ApplicationException, "PHP command-line tool failed, update your version of PHP" unless test_php Dir.chdir @dir do # Update dependencies using Composer # https://getcomposer.org Open3.popen3('php composer.phar update') do |stdin,stdout,stderr| stdout.read end end self end |
#verify ⇒ Boolean
Find out if this folder really contains a Ptero application using metadata in composer.json
121 122 123 124 125 126 127 |
# File 'lib/ptero/application.rb', line 121 def verify composer_path = dir.join('composer.json') return false unless File.exist? composer_path hash = JSON.parse File.read(composer_path) return false unless hash['dinosaur'] && hash['dinosaur']['root'] self end |