Class: Marv::Global
- Inherits:
-
Object
- Object
- Marv::Global
- Defined in:
- lib/marv/global.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#layouts ⇒ Object
Returns the value of attribute layouts.
-
#path ⇒ Object
Returns the value of attribute path.
-
#plugins ⇒ Object
Returns the value of attribute plugins.
-
#servers ⇒ Object
Returns the value of attribute servers.
-
#themes ⇒ Object
Returns the value of attribute themes.
Instance Method Summary collapse
-
#ask_database_details ⇒ Object
Database details.
-
#ask_global_options ⇒ Object
Ask global options.
-
#ask_project_details ⇒ Object
Project details.
-
#ask_server_details ⇒ Object
Server details.
-
#ask_wordpress_details ⇒ Object
Wordpress details.
-
#clean_broken_links(paths) ⇒ Object
Clean broken global links.
-
#config_file ⇒ Object
Global config file.
-
#configure(from_command = false) ⇒ Object
Configure Marv global options.
-
#create_global_config ⇒ Object
Create global config.
-
#create_global_folders ⇒ Object
Create global folders.
-
#create_global_path ⇒ Object
Create global path.
-
#create_layouts_path ⇒ Object
Create layouts path.
-
#create_plugins_path ⇒ Object
Create plugins path.
-
#create_servers_path ⇒ Object
Create servers path.
-
#create_themes_path ⇒ Object
Create themes path.
-
#current_options ⇒ Object
Get current options.
-
#default_options ⇒ Object
Default config options.
-
#generate_config(from_command = false) ⇒ Object
Generate configuration.
-
#global_config ⇒ Object
Load global config file.
-
#global_layouts ⇒ Object
Global layouts array.
-
#global_options ⇒ Object
Get global options.
-
#global_path ⇒ Object
Global Marv folder path.
-
#global_plugins ⇒ Object
Global plugins array.
-
#global_projects_paths ⇒ Object
Global projects paths.
-
#global_servers_paths ⇒ Object
Global servers paths.
-
#global_themes ⇒ Object
Global themes array.
-
#initialize(task, from_command = false) ⇒ Global
constructor
A new instance of Global.
-
#layouts_path ⇒ Object
Layouts path.
-
#link_global_projects ⇒ Object
Link global projects.
-
#load_ruby_config(file) ⇒ Object
Load ruby config file.
-
#local_servers ⇒ Object
Local servers array.
-
#plugins_path ⇒ Object
Plugins path.
-
#reconfigure ⇒ Object
Reconfig Marv global options.
-
#servers_path ⇒ Object
Servers folder path.
-
#subfolders_basenames(folder) ⇒ Object
Get subfolder basenames.
-
#template(source, *args, &block) ⇒ Object
Parse template from source to destination.
-
#themes_path ⇒ Object
Themes path.
-
#username ⇒ Object
Get user name.
Constructor Details
#initialize(task, from_command = false) ⇒ Global
Returns a new instance of Global.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/marv/global.rb', line 6 def initialize(task, from_command=false) @task = task create_global_folders @current = @default = generate_config(from_command) @path = global_path @config = global_config @servers = local_servers @plugins = global_plugins @themes = global_themes @layouts = global_layouts @task.shell.mute do clean_broken_links(global_projects_paths) link_global_projects end end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
4 5 6 |
# File 'lib/marv/global.rb', line 4 def config @config end |
#layouts ⇒ Object
Returns the value of attribute layouts.
4 5 6 |
# File 'lib/marv/global.rb', line 4 def layouts @layouts end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/marv/global.rb', line 4 def path @path end |
#plugins ⇒ Object
Returns the value of attribute plugins.
4 5 6 |
# File 'lib/marv/global.rb', line 4 def plugins @plugins end |
#servers ⇒ Object
Returns the value of attribute servers.
4 5 6 |
# File 'lib/marv/global.rb', line 4 def servers @servers end |
#themes ⇒ Object
Returns the value of attribute themes.
4 5 6 |
# File 'lib/marv/global.rb', line 4 def themes @themes end |
Instance Method Details
#ask_database_details ⇒ Object
Database details
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/marv/global.rb', line 205 def ask_database_details = {} if @task.said_change?("Do you want to set default database settings?") [:db_user] = @task.ask_option "Default database username:", :default => @default[:db_user] [:db_password] = @task.ask_option "Default database password:", :default => @default[:db_password] [:db_host] = @task.ask_option "Default database host:", :default => @default[:db_host] [:db_port] = @task.ask_option "Default database port:", :default => @default[:db_port] end return end |
#ask_global_options ⇒ Object
Ask global options
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/marv/global.rb', line 235 def = @default .merge!(ask_project_details) .merge!(ask_server_details) .merge!(ask_database_details) .merge!(ask_wordpress_details) @options = end |
#ask_project_details ⇒ Object
Project details
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/marv/global.rb', line 178 def ask_project_details = {} if @task.said_change?("Do you want to set default project details?") [:uri] = @task.ask_option "Default project URI:", :default => @default[:uri] [:author] = @task.ask_option "Default project author:", :default => @default[:author] [:author_uri] = @task.ask_option "Default project author URI:", :default => @default[:author_uri] [:license_name] = @task.ask_option "Default project license name:", :default => @default[:license_name] [:license_uri] = @task.ask_option "Default project license URI:", :default => @default[:license_uri] end return end |
#ask_server_details ⇒ Object
Server details
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/marv/global.rb', line 193 def ask_server_details = {} if @task.said_change?("Do you want to set default server settings?") [:server_host] = @task.ask_option "Default host for servers:", :default => @default[:server_host] [:server_port] = @task.ask_option "Default port for servers:", :default => @default[:server_port] end return end |
#ask_wordpress_details ⇒ Object
Wordpress details
219 220 221 222 223 224 225 226 227 |
# File 'lib/marv/global.rb', line 219 def ask_wordpress_details = {} if @task.said_change?("Do you want to set default WordPress version?") [:wp_version] = @task.ask_option "Default WordPress version?", :default => @default[:wp_version] end return end |
#clean_broken_links(paths) ⇒ Object
Clean broken global links
303 304 305 306 307 308 309 |
# File 'lib/marv/global.rb', line 303 def clean_broken_links(paths) paths.each do |path| unless ::File.exists?(path) ::FileUtils.rm_r path end end end |
#config_file ⇒ Object
Global config file
78 79 80 |
# File 'lib/marv/global.rb', line 78 def config_file ::File.join(::File.join(global_path, 'config.rb')) end |
#configure(from_command = false) ⇒ Object
Configure Marv global options
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/marv/global.rb', line 259 def configure(from_command=false) @task.say_warning "You do not have a global configuration file.", false @task.say_info "This will create a new global configuration file.", true if @task.said_change?("Do you want to change the default options?") end @options = @default create_global_config @task.say_success "Global configuration created successfully.", !from_command, true end |
#create_global_config ⇒ Object
Create global config
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/marv/global.rb', line 247 def create_global_config unless ::File.exists?(config_file) @task.shell.mute do layout = ::File.join(Marv.root, 'layouts', 'config', 'global.rb') filepath = ::File.join(global_path, 'config.rb') template layout, filepath, instance_eval('binding') end end end |
#create_global_folders ⇒ Object
Create global folders
132 133 134 135 136 137 138 139 140 |
# File 'lib/marv/global.rb', line 132 def create_global_folders @task.shell.mute do create_global_path create_servers_path create_themes_path create_plugins_path create_layouts_path end end |
#create_global_path ⇒ Object
Create global path
143 144 145 146 147 |
# File 'lib/marv/global.rb', line 143 def create_global_path unless ::File.directory?(global_path) @task.empty_directory global_path end end |
#create_layouts_path ⇒ Object
Create layouts path
171 172 173 174 175 |
# File 'lib/marv/global.rb', line 171 def create_layouts_path unless ::File.directory?(layouts_path) @task.empty_directory layouts_path end end |
#create_plugins_path ⇒ Object
Create plugins path
164 165 166 167 168 |
# File 'lib/marv/global.rb', line 164 def create_plugins_path unless ::File.directory?(plugins_path) @task.empty_directory plugins_path end end |
#create_servers_path ⇒ Object
Create servers path
150 151 152 153 154 |
# File 'lib/marv/global.rb', line 150 def create_servers_path unless ::File.directory?(servers_path) @task.empty_directory servers_path end end |
#create_themes_path ⇒ Object
Create themes path
157 158 159 160 161 |
# File 'lib/marv/global.rb', line 157 def create_themes_path unless ::File.directory?(themes_path) @task.empty_directory themes_path end end |
#current_options ⇒ Object
Get current options
59 60 61 62 63 64 65 |
# File 'lib/marv/global.rb', line 59 def if ::File.exists?(config_file) global_config.reject { |opt| opt.nil? || opt == '' } else {} end end |
#default_options ⇒ Object
Default config options
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/marv/global.rb', line 39 def defaults = { :server_host => "localhost", :server_port => "3000", :db_user => "root", :db_password => "root", :db_host => "localhost", :db_port => "3306", :wp_version => "latest", :uri => "https://wordpress.org", :author => username, :author_uri => "https://wordpress.org", :license_name => "GPLv3", :license_uri => "http://www.gnu.org/licenses/gpl.html" } defaults.merge(@current) end |
#generate_config(from_command = false) ⇒ Object
Generate configuration
30 31 32 33 34 35 36 |
# File 'lib/marv/global.rb', line 30 def generate_config(from_command=false) if from_command ::File.exists?(config_file) ? reconfigure : configure(from_command) else configure unless ::File.exists?(config_file) end end |
#global_config ⇒ Object
Load global config file
83 84 85 86 87 88 89 |
# File 'lib/marv/global.rb', line 83 def global_config if ::File.exists?(config_file) load_ruby_config(config_file) else {} end end |
#global_layouts ⇒ Object
Global layouts array
127 128 129 |
# File 'lib/marv/global.rb', line 127 def global_layouts @layouts = subfolders_basenames(layouts_path) end |
#global_options ⇒ Object
Get global options
230 231 232 |
# File 'lib/marv/global.rb', line 230 def @options end |
#global_path ⇒ Object
Global Marv folder path
73 74 75 |
# File 'lib/marv/global.rb', line 73 def global_path ::File.join(ENV['HOME'], '.marv') end |
#global_plugins ⇒ Object
Global plugins array
122 123 124 |
# File 'lib/marv/global.rb', line 122 def global_plugins @plugins = subfolders_basenames(plugins_path) end |
#global_projects_paths ⇒ Object
Global projects paths
290 291 292 293 294 295 |
# File 'lib/marv/global.rb', line 290 def global_projects_paths paths = ::Dir.glob(::File.join(plugins_path, '*')) paths = paths + ::Dir.glob(::File.join(themes_path, '*')) return paths end |
#global_servers_paths ⇒ Object
Global servers paths
298 299 300 |
# File 'lib/marv/global.rb', line 298 def global_servers_paths ::Dir.glob(::File.join(servers_path, '*')) end |
#global_themes ⇒ Object
Global themes array
117 118 119 |
# File 'lib/marv/global.rb', line 117 def global_themes @themes = subfolders_basenames(themes_path) end |
#layouts_path ⇒ Object
Layouts path
112 113 114 |
# File 'lib/marv/global.rb', line 112 def layouts_path ::File.join(global_path, 'layouts') end |
#link_global_projects ⇒ Object
Link global projects
312 313 314 315 316 317 318 319 |
# File 'lib/marv/global.rb', line 312 def link_global_projects global_projects_paths.each do |project| global_servers_paths.each do |server| target = project.gsub(global_path, ::File.join(server, 'wp-content')) @task.create_link target, project unless ::File.exists?(target) end end end |
#load_ruby_config(file) ⇒ Object
Load ruby config file
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/marv/global.rb', line 322 def load_ruby_config(file) config = {} begin # Config file is just executed as straight ruby eval(::File.read(file)) rescue Exception => e @task.say_error "Error while evaluating config file:", e. end return config end |
#local_servers ⇒ Object
Local servers array
97 98 99 |
# File 'lib/marv/global.rb', line 97 def local_servers @servers = subfolders_basenames(servers_path) end |
#plugins_path ⇒ Object
Plugins path
102 103 104 |
# File 'lib/marv/global.rb', line 102 def plugins_path ::File.join(global_path, 'plugins') end |
#reconfigure ⇒ Object
Reconfig Marv global options
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/marv/global.rb', line 274 def reconfigure @task.say_warning "This will overwrite your global configuration file." if @task.said_change?("Do you want to continue?") @task.shell.mute do @task.remove_file config_file create_global_config end @task.say_success "Global configuration updated successfully.", false, true end end |
#servers_path ⇒ Object
Servers folder path
92 93 94 |
# File 'lib/marv/global.rb', line 92 def servers_path ::File.join(global_path, 'servers') end |
#subfolders_basenames(folder) ⇒ Object
Get subfolder basenames
351 352 353 354 355 356 357 358 359 |
# File 'lib/marv/global.rb', line 351 def subfolders_basenames(folder) subfolders = [] ::Dir.glob(::File.join(folder, '*')).each do |subfolder| subfolders << ::File.basename(subfolder) end return subfolders end |
#template(source, *args, &block) ⇒ Object
Parse template from source to destination
336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/marv/global.rb', line 336 def template(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first || source.sub(/\.tt$/, '') context = args.last || instance_eval('binding') source = ::File.(@task.find_in_source_paths(source.to_s)) @task.create_file destination, nil, config do content = ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context) content = block.call(content) if block content end end |
#themes_path ⇒ Object
Themes path
107 108 109 |
# File 'lib/marv/global.rb', line 107 def themes_path ::File.join(global_path, 'themes') end |
#username ⇒ Object
Get user name
68 69 70 |
# File 'lib/marv/global.rb', line 68 def username ENV['USERNAME'] || 'marv' end |