Module: Laravel::Helpers

Included in:
App, AppSupport, Configuration
Defined in:
lib/laravel/helpers.rb

Constant Summary collapse

CacheFolder =

the path to the folder where the sources will be locally cached.

File.join(ENV['HOME'], %w[ .laravel repos ])
LaravelRepo =

the official Laravel repository URL which is also the default source for us.

"http://github.com/laravel/laravel"

Instance Method Summary collapse

Instance Method Details

#download_resource(path, source, using) ⇒ Object

Download a given resource at a particular path

Parameters

path

Path where the downloaded content will be saved. This can either be the path to a single file or a directory. If this is a directory, git will be used to download the source, otherwise, curl will be used for the same. Therefore, please, make sure that the source is a git repository when path is a directory, and that the source is an online file when path is a file.

source

Source URL/directory from where the content of the resource will be downloaded. Please, read information about path

using

can be either ‘curl’ or ‘git’ to download the resource

Return

boolean

true, if the resource was downloaded successfully.

Raises

LaravelError

if the required executable/binary is missing

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/laravel/helpers.rb', line 73

def download_resource(path, source, using)
  using = "curl" if using == "shell"

  # default to `wget` if `curl` is not found
  using = "wget" if `which curl`.empty?
  # raise an error if required library is not found
  message = "#{using} is required! Please, install it!"
  raise LaravelError, message if `which #{using}`.empty?

  # download the resource
  case using
  when "git"  then system("git clone -q #{source} #{path} &>/dev/null")
  when "curl" then system("curl -s #{source} > #{path}")
  when "wget" then system("wget #{source} -O #{path}")
  end
end

#is_blank?(var) ⇒ Boolean

checks if a given variable is blank

Parameters

var

the variable to check

Return

boolean

true, if var is nil, or if it is an empty string

Returns:

  • (Boolean)


118
119
120
# File 'lib/laravel/helpers.rb', line 118

def is_blank?(var)
  var.nil? or (var.is_a?(String) and var.strip.empty?)
end

#is_current_directory?(dir = nil) ⇒ Boolean

Check whether the given directory is the current directory.

Parameters

dir

the direcotry to check

Return

boolean

True, if the app directory is the current directory.

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/laravel/helpers.rb', line 34

def is_current_directory?(dir = nil)
  dir ||= Dir.pwd
  dir = File.expand_path(dir)
  File.directory?(dir) and (dir == File.expand_path(Dir.pwd))
end

#is_empty_directory?(dir = nil) ⇒ Boolean

Check whether the given directory is empty?

Parameters

dir

the directory to check

Return

boolean

True, if the app directory is an empty one.

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/laravel/helpers.rb', line 48

def is_empty_directory?(dir = nil)
  dir ||= Dir.pwd
  dir = File.expand_path(dir)
  File.directory?(dir) and (Dir.entries(dir).size == 2)
end

#laravel_exists_in_directory?(directory = nil, relative_to = nil) ⇒ Boolean

check if laravel framework exists in a specified directory

Parameters

directory

directory to check for the existance of laravel framework this can be the relative path to the current app directory or the absolute path of the directory.

relative_to

if the directory is a relative path, we can define the base directory here.

Return

boolean

true, if laravel exists in the given directory

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/laravel/helpers.rb', line 102

def laravel_exists_in_directory?(directory = nil, relative_to = nil)
  return false unless directory
  directory = File.expand_path(directory, relative_to)
  return false unless File.exists? File.join(directory, "artisan")
  return false unless File.directory? File.join(directory, "laravel")
  true
end

#make_md5(string = nil) ⇒ Object

convert a string to MD5 hash - useful to generate quick random strings.

Parameters

string

optional, the input string to be hashed

a random string will be used for hashing, if this string is not provided

Return

string

a 32-character long MD5’ed string



19
20
21
22
23
24
# File 'lib/laravel/helpers.rb', line 19

def make_md5(string = nil)
  # create a random string if one is not provided
  string ||= (0...32).map{ ('a'..'z').to_a[rand(26)] }.join
  # hash it
  (Digest::MD5.new << string).to_s
end

#say(status, message = "", log_status = true) ⇒ Object

This method, simply, imitates the ‘say’ method that the Thor gem provides us. I preferred to use this method, since it gives us a very nice UI at the CLI :)



125
126
127
128
129
# File 'lib/laravel/helpers.rb', line 125

def say(status, message = "", log_status = true)
  shell = Thor::Shell::Color.new
  log_status = false if @options and @options[:quiet]
  shell.say_status(status, message, log_status)
end

#say_failed(message) ⇒ Object

Show a failed message to the user in Yellow.



142
143
144
# File 'lib/laravel/helpers.rb', line 142

def say_failed(message)
  say "Failed!!", message, :yellow
end

#say_success(message) ⇒ Object

Show a success message to the user in Green.



137
138
139
# File 'lib/laravel/helpers.rb', line 137

def say_success(message)
  say "Success", message, :green
end

#show_info(message) ⇒ Object

Show some information to the user in Cyan.



132
133
134
# File 'lib/laravel/helpers.rb', line 132

def show_info(message)
  say "Information", message, :cyan
end