Class: Zen::Bin::Create
- Inherits:
-
Shebang::Command
- Object
- Shebang::Command
- Zen::Bin::Create
- Defined in:
- lib/zen/bin/create.rb
Overview
Command that can be used to create new Zen projects.
Constant Summary
- PROTOTYPE =
Path to the prototype application to use for new projects.
__DIR__('../../../proto/app')
- SERVER_CONFIGS =
Hash containing various Rack servers and their default configuration files to use.
{ :unicorn => __DIR__('../../../proto/rack/unicorn.rb'), :thin => __DIR__('../../../proto/rack/thin.yml') }
- PROJECT_SETTINGS =
Hash containing the options to use for creating a new project.
{ :name => 'zen_project', :database => { :adapter => 'sqlite', :database => 'database.db', :username => nil, :password => nil }, :server_config => nil }
Instance Method Summary (collapse)
-
- (Object) create_project
private
Creates a new project based on the settings specified by the user.
-
- (Object) database_settings
private
Asks the user for various database settings.
-
- (Object) error(message)
private
Shows an error and exists the script.
-
- (Object) heading(heading)
private
Displays a heading in yellow.
-
- (Object) index
Method that is called when this command is invoked without any additional parameters.
-
- (Object) intro
private
Displays a short introduction to the installer.
-
- (String) readline(message, default = nil, newline = true)
private
Shows a prompt using Readline.readline and returns the value entered by the user.
-
- (Object) server_config
private
Asks the user if he/she would like to use a default configuration file for Unicorn or Thin.
-
- (Object) setup_name
private
Sets the name of the project.
-
- (String) wrap_string(input)
private
Wraps a string at 80 characters and returns it.
Instance Method Details
- (Object) create_project (private)
Creates a new project based on the settings specified by the user.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/zen/bin/create.rb', line 175 def create_project destination = File.join(Dir.pwd, PROJECT_SETTINGS[:name]) # Copy the application if File.directory?(destination) and !option(:f) error('There already is a directory using your project name') else puts 'Creating base files...' FileUtils.rm_rf(destination) FileUtils.cp_r(PROTOTYPE, destination) end # Copy the default Rack configuration file. if !PROJECT_SETTINGS[:server_config].nil? \ and !PROJECT_SETTINGS[:server_config].empty? puts 'Creating server configuration file...' config = SERVER_CONFIGS[PROJECT_SETTINGS[:server_config]] FileUtils.cp( config, File.join(destination, 'config', File.basename(config)) ) end # Replace all the ERB tags puts 'Setting variables...' templates = Dir.glob(File.join(destination, '**', '*.erb')) templates.each do |template| processed = File.read(template, File.size(template)) processed = ERB.new(processed).result(binding) File.open(template, 'w') do |handle| handle.write(processed) end # Remove the .erb extension FileUtils.mv( template, File.join(File.dirname(template), File.basename(template, '.erb')) ) end puts wrap_string( "Your project has been created, you can find it in #{destination}" ) end |
- (Object) database_settings (private)
Asks the user for various database settings.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/zen/bin/create.rb', line 109 def database_settings heading('Database Settings') puts wrap_string( 'Zen stores it\'s data in a SQL database such as PostgreSQL or ' \ 'MySQL. In order to connect to such a database you must specify ' \ 'the connection details such as the username and database name.' ) PROJECT_SETTINGS[:database][:adapter] = readline( 'Adapter', PROJECT_SETTINGS[:database][:adapter] ) PROJECT_SETTINGS[:database][:database] = readline( 'Database', PROJECT_SETTINGS[:database][:database], false ) PROJECT_SETTINGS[:database][:username] = readline('Username', nil, false) PROJECT_SETTINGS[:database][:password] = readline('Password', nil, false) end |
- (Object) error(message) (private)
Shows an error and exists the script.
299 300 301 302 |
# File 'lib/zen/bin/create.rb', line 299 def error() puts .red exit(false) end |
- (Object) heading(heading) (private)
Displays a heading in yellow.
259 260 261 |
# File 'lib/zen/bin/create.rb', line 259 def heading(heading) puts "\n" + heading.yellow + "\n\n" end |
- (Object) index
Method that is called when this command is invoked without any additional parameters.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/zen/bin/create.rb', line 49 def index if option(:l) create_project return end intro setup_name database_settings server_config create_project end |
- (Object) intro (private)
Displays a short introduction to the installer.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/zen/bin/create.rb', line 70 def intro puts wrap_string( 'Welcome to the interactive project creator of Zen. ' \ 'This installer will guide you through the process of creating ' \ 'a new project using Zen.' ) puts puts wrap_string( 'Keep in mind that all settings specified for each project can be ' \ 'changed to anything you like. Zen tries to make as little ' \ 'assumptions as possible.' ) end |
- (String) readline(message, default = nil, newline = true) (private)
Shows a prompt using Readline.readline and returns the value entered by the user.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/zen/bin/create.rb', line 274 def readline(, default = nil, newline = true) puts if newline prompt = "> #{}" if default prompt += " (default: #{default})" end prompt += ': ' value = Readline.readline(prompt.blue).strip if value.empty? and !default.nil? value = default end return value end |
- (Object) server_config (private)
Asks the user if he/she would like to use a default configuration file for Unicorn or Thin.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/zen/bin/create.rb', line 139 def server_config heading('Server Configuration') puts wrap_string( 'Zen comes with a few default configuration files that you can use ' \ 'for your favorite Rack server. Zen comes with default ' \ 'configuration files for the following Rack servers:' ) puts SERVER_CONFIGS.each do |name, path| puts "* #{name}" end puts puts wrap_string( 'If you are using a different server or simply don\'t want a ' \ 'default configuration file you can choose "none".' ) server = readline('Default server configuration file', 'none').downcase if !server.empty? and server != 'none' \ and !SERVER_CONFIGS.keys.include?(server.to_sym) error('The specified configuration file is invalid') end PROJECT_SETTINGS[:server_config] = server.to_sym unless server == 'none' end |
- (Object) setup_name (private)
Sets the name of the project.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/zen/bin/create.rb', line 90 def setup_name heading('Project Name') puts wrap_string( 'Each Zen project requires a name. This name is used for the ' \ 'directory name as well as the session ID.' ) name = readline('Name of your project', PROJECT_SETTINGS[:name]) name = name.downcase.gsub(/\s+/, '_') PROJECT_SETTINGS[:name] = name unless name.empty? end |
- (String) wrap_string(input) (private)
Wraps a string at 80 characters and returns it.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/zen/bin/create.rb', line 233 def wrap_string(input) input = input.split(/\s+/) output = '' chars = 0 input.each do |chunk| length = chunk.length if ( chars + length ) <= ( 80 - length ) output += "#{chunk} " chars += length else output += "\n#{chunk} " chars = 0 end end return output.strip end |