class Marv::Global

Attributes

config[RW]
layouts[RW]
path[RW]
plugins[RW]
servers[RW]
themes[RW]

Public Class Methods

new(task, from_command=false) click to toggle source
# File lib/marv/global.rb, line 6
def initialize(task, from_command=false)
  @task = task

  create_global_folders

  @current = current_options
  @default = default_options

  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

Public Instance Methods

ask_database_details() click to toggle source

Database details

# File lib/marv/global.rb, line 205
def ask_database_details
  options = {}

  if @task.said_change?("Do you want to set default database settings?")
    options[:db_user] = @task.ask_option "Default database username:", :default => @default[:db_user]
    options[:db_password] = @task.ask_option "Default database password:", :default => @default[:db_password]
    options[:db_host] = @task.ask_option "Default database host:", :default => @default[:db_host]
    options[:db_port] = @task.ask_option "Default database port:", :default => @default[:db_port]
  end

  return options
end
ask_global_options() click to toggle source

Ask global options

# File lib/marv/global.rb, line 235
def ask_global_options
  options = @default

  options.merge!(ask_project_details)
  options.merge!(ask_server_details)
  options.merge!(ask_database_details)
  options.merge!(ask_wordpress_details)

  @options = options
end
ask_project_details() click to toggle source

Project details

# File lib/marv/global.rb, line 178
def ask_project_details
  options = {}

  if @task.said_change?("Do you want to set default project details?")
    options[:uri] = @task.ask_option "Default project URI:", :default => @default[:uri]
    options[:author] = @task.ask_option "Default project author:", :default => @default[:author]
    options[:author_uri] = @task.ask_option "Default project author URI:", :default => @default[:author_uri]
    options[:license_name] = @task.ask_option "Default project license name:", :default => @default[:license_name]
    options[:license_uri] = @task.ask_option "Default project license URI:", :default => @default[:license_uri]
  end

  return options
end
ask_server_details() click to toggle source

Server details

# File lib/marv/global.rb, line 193
def ask_server_details
  options = {}

  if @task.said_change?("Do you want to set default server settings?")
    options[:server_host] = @task.ask_option "Default host for servers:", :default => @default[:server_host]
    options[:server_port] = @task.ask_option "Default port for servers:", :default => @default[:server_port]
  end

  return options
end
ask_wordpress_details() click to toggle source

Wordpress details

# File lib/marv/global.rb, line 219
def ask_wordpress_details
  options = {}

  if @task.said_change?("Do you want to set default WordPress version?")
    options[:wp_version] = @task.ask_option "Default WordPress version?", :default => @default[:wp_version]
  end

  return options
end
config_file() click to toggle source

Global config file

# File lib/marv/global.rb, line 78
def config_file
  ::File.join(::File.join(global_path, 'config.rb'))
end
configure(from_command=false) click to toggle source

Configure Marv global options

# 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?")
    ask_global_options
  end

  @options = @default
  create_global_config

  @task.say_success "Global configuration created successfully.", !from_command, true
end
create_global_config() click to toggle source

Create global config

# 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() click to toggle source

Create global folders

# 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() click to toggle source

Create global path

# 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() click to toggle source

Create layouts path

# 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() click to toggle source

Create plugins path

# 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() click to toggle source

Create servers path

# 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() click to toggle source

Create themes path

# 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() click to toggle source

Get current options

# File lib/marv/global.rb, line 59
def current_options
  if ::File.exists?(config_file)
    global_config.reject { |opt| opt.nil? || opt == '' }
  else
    {}
  end
end
default_options() click to toggle source

Default config options

# File lib/marv/global.rb, line 39
def default_options
  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) click to toggle source

Generate configuration

# 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() click to toggle source

Load global config file

# 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() click to toggle source

Global layouts array

# File lib/marv/global.rb, line 127
def global_layouts
  @layouts = subfolders_basenames(layouts_path)
end
global_options() click to toggle source

Get global options

# File lib/marv/global.rb, line 230
def global_options
  @options
end
global_path() click to toggle source

Global Marv folder path

# File lib/marv/global.rb, line 73
def global_path
  ::File.join(ENV['HOME'], '.marv')
end
global_plugins() click to toggle source

Global plugins array

# File lib/marv/global.rb, line 122
def global_plugins
  @plugins = subfolders_basenames(plugins_path)
end
global_projects_paths() click to toggle source

Global projects paths

# 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() click to toggle source

Global servers paths

# File lib/marv/global.rb, line 298
def global_servers_paths
  ::Dir.glob(::File.join(servers_path, '*'))
end
global_themes() click to toggle source

Global themes array

# File lib/marv/global.rb, line 117
def global_themes
  @themes = subfolders_basenames(themes_path)
end
layouts_path() click to toggle source

Layouts path

# File lib/marv/global.rb, line 112
def layouts_path
  ::File.join(global_path, 'layouts')
end
load_ruby_config(file) click to toggle source

Load ruby config file

# 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.message
  end

  return config
end
local_servers() click to toggle source

Local servers array

# File lib/marv/global.rb, line 97
def local_servers
  @servers = subfolders_basenames(servers_path)
end
plugins_path() click to toggle source

Plugins path

# File lib/marv/global.rb, line 102
def plugins_path
  ::File.join(global_path, 'plugins')
end
reconfigure() click to toggle source

Reconfig Marv global options

# 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
      ask_global_options

      @task.remove_file config_file
      create_global_config
    end

    @task.say_success "Global configuration updated successfully.", false, true
  end
end
servers_path() click to toggle source

Servers folder path

# File lib/marv/global.rb, line 92
def servers_path
  ::File.join(global_path, 'servers')
end
subfolders_basenames(folder) click to toggle source

Get subfolder basenames

# 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) click to toggle source

Parse template from source to destination

# 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.expand_path(@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() click to toggle source

Themes path

# File lib/marv/global.rb, line 107
def themes_path
  ::File.join(global_path, 'themes')
end
username() click to toggle source

Get user name

# File lib/marv/global.rb, line 68
def username
  ENV['USERNAME'] || 'marv'
end