Class: Ptero::Application

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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)

Parameters:

  • dir (Pathname, String) (defaults to: Dir.pwd)

    the Application directory



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

#dirObject (readonly)

Returns the value of attribute dir.



27
28
29
# File 'lib/ptero/application.rb', line 27

def dir
  @dir
end

#nameObject (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

Parameters:

  • dir (String, Pathname) (defaults to: Dir.pwd)

    the starting directory

Returns:

Raises:



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.

Parameters:

  • name (String)

    the name of the new application

  • dir (String, Pathname) (defaults to: Pathname.new(Dir.pwd))

    the directory in which to create the new application

Returns:



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_appApplication

Return the verified Application object corresponding to Dir.pwd or its parent directories

Returns:

  • (Application)

    the Application object that represents the found verified application



275
276
277
# File 'lib/ptero/application.rb', line 275

def current_app
  app_for Dir.pwd
end

Instance Method Details

#dependencies_installed?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/ptero/application.rb', line 115

def dependencies_installed?
  @dir.join('vendor').directory?
end

#destroyObject

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

Parameters:

  • klass (String, Class)

    what type of file to generate

  • *params

    all other parameters to pass to the generator



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

Returns:

  • (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_composerApplication, 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

Returns:

Raises:



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.

Returns:

  • (Boolean)

    whether or not composer.phar was found



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_dependenciesApplication

Install all dependencies listed in the composer.json file using Composer ‘php composer.phar install’

Returns:

Raises:



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

Parameters:

  • klass (String, Class)

    what type of file to reload

  • *params

    all other parameters to pass to the generator



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

Parameters:

  • klass (String, Klass)

    what kind of file to remove

  • *params

    all other parameters to identify the file to be removed



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_composerApplication, Boolean

Remove the composer.phar file

Returns:

  • (Application, Boolean)

    self if composer was removed, flse if composer is not downloaded

Raises:



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_dependenciesObject



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

Parameters:

  • path (String)

    the new route’s path

  • controller (String)

    the name of the new route’s controller



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

#routesHash

Find all routes set in the current Application

Returns:

  • (Hash)

    a Hash-map of each path to its corresponding controller



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_phpBoolean

Find out whether or not the user has the PHP command-line tool installed so that they can use Composer.

Returns:

  • (Boolean)

    whether or not the user’s PHP command works.



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

Parameters:

  • path (String)

    the path to be deleted



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_dependenciesApplication

Update all dependencies listed in the composer.json file using Composer ‘php composer.phar update’

Returns:

Raises:



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

#verifyBoolean

Find out if this folder really contains a Ptero application using metadata in composer.json

Returns:

  • (Boolean)

    whether or not this application was verified successfully.



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