Class: Marv::Global

Inherits:
Object
  • Object
show all
Defined in:
lib/marv/global.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = 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

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/marv/global.rb', line 4

def config
  @config
end

#layoutsObject

Returns the value of attribute layouts.



4
5
6
# File 'lib/marv/global.rb', line 4

def layouts
  @layouts
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/marv/global.rb', line 4

def path
  @path
end

#pluginsObject

Returns the value of attribute plugins.



4
5
6
# File 'lib/marv/global.rb', line 4

def plugins
  @plugins
end

#serversObject

Returns the value of attribute servers.



4
5
6
# File 'lib/marv/global.rb', line 4

def servers
  @servers
end

#themesObject

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_detailsObject

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
  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_optionsObject

Ask global options



235
236
237
238
239
240
241
242
243
244
# 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_detailsObject

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
  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_detailsObject

Server details



193
194
195
196
197
198
199
200
201
202
# 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_detailsObject

Wordpress details



219
220
221
222
223
224
225
226
227
# 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

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_fileObject

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

  @options = @default
  create_global_config

  @task.say_success "Global configuration created successfully.", !from_command, true
end

#create_global_configObject

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_foldersObject

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_pathObject

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_pathObject

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_pathObject

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_pathObject

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_pathObject

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_optionsObject

Get current options



59
60
61
62
63
64
65
# 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_optionsObject

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 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) ⇒ 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_configObject

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_layoutsObject

Global layouts array



127
128
129
# File 'lib/marv/global.rb', line 127

def global_layouts
  @layouts = subfolders_basenames(layouts_path)
end

#global_optionsObject

Get global options



230
231
232
# File 'lib/marv/global.rb', line 230

def global_options
  @options
end

#global_pathObject

Global Marv folder path



73
74
75
# File 'lib/marv/global.rb', line 73

def global_path
  ::File.join(ENV['HOME'], '.marv')
end

#global_pluginsObject

Global plugins array



122
123
124
# File 'lib/marv/global.rb', line 122

def global_plugins
  @plugins = subfolders_basenames(plugins_path)
end

#global_projects_pathsObject

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_pathsObject

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_themesObject

Global themes array



117
118
119
# File 'lib/marv/global.rb', line 117

def global_themes
  @themes = subfolders_basenames(themes_path)
end

#layouts_pathObject

Layouts path



112
113
114
# File 'lib/marv/global.rb', line 112

def layouts_path
  ::File.join(global_path, 'layouts')
end

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

  return config
end

#local_serversObject

Local servers array



97
98
99
# File 'lib/marv/global.rb', line 97

def local_servers
  @servers = subfolders_basenames(servers_path)
end

#plugins_pathObject

Plugins path



102
103
104
# File 'lib/marv/global.rb', line 102

def plugins_path
  ::File.join(global_path, 'plugins')
end

#reconfigureObject

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
      ask_global_options

      @task.remove_file config_file
      create_global_config
    end

    @task.say_success "Global configuration updated successfully.", false, true
  end
end

#servers_pathObject

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.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_pathObject

Themes path



107
108
109
# File 'lib/marv/global.rb', line 107

def themes_path
  ::File.join(global_path, 'themes')
end

#usernameObject

Get user name



68
69
70
# File 'lib/marv/global.rb', line 68

def username
  ENV['USERNAME'] || 'marv'
end