marv 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.rspec +1 -0
  2. data/CHANGELOG.md +13 -0
  3. data/Gemfile +3 -4
  4. data/Gemfile.lock +70 -28
  5. data/LICENSE +24 -0
  6. data/Rakefile +7 -1
  7. data/VERSION +1 -1
  8. data/layouts/bramble/functions/functions.php.erb +12 -12
  9. data/layouts/bramble/includes/options.php.erb +4 -4
  10. data/layouts/config/{config.tt → project-config.rb} +4 -1
  11. data/layouts/config/rack-config.ru +38 -0
  12. data/layouts/config/wp-config.php.erb +105 -0
  13. data/layouts/default/functions/functions.php.erb +25 -25
  14. data/layouts/default/includes/filters-admin.php.erb +8 -8
  15. data/layouts/default/includes/filters.php.erb +11 -11
  16. data/layouts/default/includes/helpers.php.erb +1 -1
  17. data/layouts/default/templates/404.php.erb +2 -2
  18. data/layouts/default/templates/archive.php.erb +5 -5
  19. data/layouts/default/templates/author.php.erb +1 -1
  20. data/layouts/default/templates/partials/comments.php.erb +8 -8
  21. data/layouts/default/templates/partials/content-none.php.erb +3 -3
  22. data/layouts/default/templates/partials/content.php.erb +2 -2
  23. data/layouts/default/templates/partials/searchform.php.erb +3 -3
  24. data/layouts/default/templates/search.php.erb +2 -2
  25. data/lib/guard/marv/assets.rb +11 -3
  26. data/lib/guard/marv/config.rb +2 -0
  27. data/lib/guard/marv/folders.rb +37 -0
  28. data/lib/guard/marv/functions.rb +13 -9
  29. data/lib/guard/marv/templates.rb +12 -3
  30. data/lib/marv.rb +1 -0
  31. data/lib/marv/builder.rb +49 -15
  32. data/lib/marv/cli.rb +135 -10
  33. data/lib/marv/engines.rb +3 -1
  34. data/lib/marv/generator.rb +13 -8
  35. data/lib/marv/guard.rb +4 -1
  36. data/lib/marv/project.rb +18 -10
  37. data/lib/marv/server.rb +301 -0
  38. data/lib/marv/version.rb +1 -1
  39. data/marv.gemspec +18 -16
  40. data/spec/lib/marv/project_spec.rb +3 -4
  41. metadata +27 -38
data/lib/marv/engines.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Tilt
2
2
  class LessTemplateWithPaths < LessTemplate
3
+
3
4
  class << self
4
5
  attr_accessor :load_path
5
6
  end
@@ -8,5 +9,6 @@ module Tilt
8
9
  parser = ::Less::Parser.new(:filename => eval_file, :line => line, :paths => [self.class.load_path])
9
10
  @engine = parser.parse(data)
10
11
  end
12
+
11
13
  end
12
- end
14
+ end
@@ -1,17 +1,18 @@
1
-
2
1
  module Marv
3
2
  class Generator
3
+
4
4
  class << self
5
- def run(project, layout)
6
- generator = self.new(project, layout)
5
+ def run(project, layout, local_layout)
6
+ generator = self.new(project, layout, local_layout)
7
7
  generator.run
8
8
  end
9
9
  end
10
10
 
11
- def initialize(project, layout)
11
+ def initialize(project, layout, local_layout)
12
12
  @project = project
13
13
  @task = project.task
14
14
  @layout = layout
15
+ @local = local_layout
15
16
  end
16
17
 
17
18
  def create_structure
@@ -24,9 +25,7 @@ module Marv
24
25
  ['assets', 'stylesheets'],
25
26
 
26
27
  ['functions'],
27
-
28
28
  ['includes'],
29
- ['extras'],
30
29
 
31
30
  ['templates', 'pages'],
32
31
  ['templates', 'partials'],
@@ -93,7 +92,11 @@ module Marv
93
92
  end
94
93
 
95
94
  def layout_path
96
- @layout_path ||= File.join(Marv::ROOT, 'layouts', @layout)
95
+ if @local
96
+ @layout_path ||= File.join(ENV['HOME'], '.marv', 'layouts', @layout)
97
+ else
98
+ @layout_path ||= File.join(Marv::ROOT, 'layouts', @layout)
99
+ end
97
100
  end
98
101
 
99
102
  def run
@@ -118,7 +121,7 @@ module Marv
118
121
  end
119
122
  end
120
123
 
121
- write_template(['config', 'config.tt'], @project.config_file)
124
+ write_template(['config', 'project-config.rb'], @project.config_file)
122
125
 
123
126
  self
124
127
  end
@@ -134,6 +137,7 @@ module Marv
134
137
  end
135
138
 
136
139
  protected
140
+
137
141
  def render_directory(source, target)
138
142
  Dir.glob("#{source}/**/*") do |file|
139
143
  unless File.directory?(file)
@@ -154,5 +158,6 @@ module Marv
154
158
  end
155
159
  end
156
160
  end
161
+
157
162
  end
158
163
  end
data/lib/marv/guard.rb CHANGED
@@ -43,7 +43,9 @@ module Marv
43
43
  guard 'marvfunctions' do
44
44
  watch(%r{#{source_path}/functions/*})
45
45
  watch(%r{#{source_path}/includes/*})
46
- watch(%r{#{source_path}/extras/*})
46
+ end
47
+ guard 'marvfolders' do
48
+ watch(%r{#{source_path}/*})
47
49
  end
48
50
  }
49
51
 
@@ -61,5 +63,6 @@ module Marv
61
63
  end
62
64
  ::Guard.start({ :guardfile_contents => guardfile_contents }).join
63
65
  end
66
+
64
67
  end
65
68
  end
data/lib/marv/project.rb CHANGED
@@ -2,12 +2,13 @@ require 'pathname'
2
2
 
3
3
  module Marv
4
4
  class Project
5
+
5
6
  class << self
6
- def create(root, config, task, layout)
7
+ def create(root, config, task, layout, local_layout)
7
8
  root = File.expand_path(root)
8
9
 
9
10
  project = self.new(root, task, config)
10
- Generator.run(project, layout)
11
+ Generator.run(project, layout, local_layout)
11
12
 
12
13
  project
13
14
  end
@@ -52,10 +53,6 @@ module Marv
52
53
  File.join(self.source_path, 'includes')
53
54
  end
54
55
 
55
- def extras_path
56
- File.join(self.source_path, 'extras')
57
- end
58
-
59
56
  def config_file
60
57
  @config_file ||= File.join(self.root, 'config.rb')
61
58
  end
@@ -68,6 +65,12 @@ module Marv
68
65
  def link(source)
69
66
  source = File.expand_path(source)
70
67
 
68
+ unless File.writable?(File.dirname(source))
69
+ @task.say "Permission Denied!", :red
70
+ @task.say "You do not have write permissions for the destination folder"
71
+ abort
72
+ end
73
+
71
74
  unless File.directory?(File.dirname(source))
72
75
  raise Marv::LinkSourceDirNotFound
73
76
  end
@@ -75,10 +78,14 @@ module Marv
75
78
  @task.link_file build_path, source
76
79
  end
77
80
 
78
- def theme_id
81
+ def project_id
79
82
  File.basename(self.root).gsub(/\W/, '_')
80
83
  end
81
84
 
85
+ def project_php_file
86
+ "#{File.basename(self.root).gsub(/\W/, '-').downcase}.php"
87
+ end
88
+
82
89
  def load_config
83
90
  config = {}
84
91
 
@@ -91,8 +98,9 @@ module Marv
91
98
  if File.exists?(self.config_file)
92
99
  config.merge!(load_ruby_config(self.config_file))
93
100
  else
94
- raise Error, "Could not find the config file, are you sure you're in a
95
- marv project directory?"
101
+ @task.say "Could not find the config file!", :red
102
+ @task.say "Are you sure you're in a marv project directory?"
103
+ abort
96
104
  end
97
105
 
98
106
  @config = config
@@ -116,7 +124,7 @@ module Marv
116
124
  eval(File.read(file))
117
125
  rescue Exception => e
118
126
  @task.say "Error while evaluating config file:"
119
- @task.say e.message, Thor::Shell::Color::RED
127
+ @task.say e.message, :red
120
128
  end
121
129
 
122
130
  return config
@@ -0,0 +1,301 @@
1
+ require 'net/http'
2
+ require 'mysql2'
3
+
4
+ module Marv
5
+ class Server
6
+
7
+ def initialize(name, config, options)
8
+ @task = config
9
+
10
+ # Deploy variables
11
+ @server_name = name
12
+ @server_path = server_path
13
+
14
+ @rack_config = rack_config
15
+ @wp_config = wp_config
16
+
17
+ @db_name = 'marv_'+name.gsub(/\W/, '_').downcase
18
+
19
+ # CLI Options
20
+ unless options.nil?
21
+ if options[:password] == "required"
22
+ @task.say "Mysql password is required. Aborting...", :red
23
+ abort
24
+ end
25
+
26
+ @wp_version = options[:version]
27
+ @db_user = options[:user]
28
+ @db_password = options[:password]
29
+ @db_host = options[:host]
30
+ @db_port = options[:port]
31
+ end
32
+ end
33
+
34
+ def servers_root
35
+ File.join(ENV['HOME'], '.marv', 'servers')
36
+ end
37
+
38
+ def server_path
39
+ File.join(ENV['HOME'], '.marv', 'servers', @server_name)
40
+ end
41
+
42
+ def rack_config
43
+ File.join(ENV['HOME'], '.marv', @server_path, 'config.ru')
44
+ end
45
+
46
+ def wp_config
47
+ File.join(ENV['HOME'], '.marv', @server_path, 'wp-config.php')
48
+ end
49
+
50
+ def db_name
51
+ @db_name
52
+ end
53
+
54
+ def db_password
55
+ @db_password
56
+ end
57
+
58
+ def db_host
59
+ @db_host
60
+ end
61
+
62
+ def create_server
63
+ create_server_directory
64
+
65
+ download_wordpress
66
+ extract_wordpress
67
+ add_rack_config
68
+
69
+ create_server_database
70
+ add_wordpress_config
71
+
72
+ add_global_content
73
+
74
+ @task.say "Server #{@server_name} created", :green
75
+ exit
76
+ end
77
+
78
+ def start_server
79
+ update_global_content
80
+
81
+ begin
82
+ @task.shell.mute do
83
+ system("cd #{server_path}; rackup --daemonize --pid=ruby.pid")
84
+ end
85
+ rescue Exception => e
86
+ @task.say "Error while starting server:"
87
+ @task.say e.message + "\n", :red
88
+ exit
89
+ end
90
+ @task.say "Server #{@server_name} running", :green
91
+ end
92
+
93
+ def stop_server
94
+ begin
95
+ @task.shell.mute do
96
+ ruby_pid_file = File.join(@server_path, 'ruby.pid')
97
+ php_pid_file = File.join(@server_path, 'php.pid')
98
+
99
+ if File.exist?(ruby_pid_file) && File.exist?(php_pid_file) then
100
+ ruby_pid = File.read(ruby_pid_file).to_i
101
+ php_pid = File.read(php_pid_file).to_i
102
+
103
+ Process.kill(9, ruby_pid, php_pid)
104
+ @task.say "Server #{@server_name} stopped", :green
105
+ else
106
+ @task.say "Server #{@server_name} is not running", :yellow
107
+ end
108
+ end
109
+ rescue Exception => e
110
+ @task.say "Error while stoping server:"
111
+ @task.say e.message + "\n", :red
112
+ exit
113
+ end
114
+ end
115
+
116
+ def restart_server
117
+ stop_server
118
+ start_server
119
+ end
120
+
121
+ def remove_server
122
+ @task.say "Removing server...", :green
123
+ @task.shell.mute do
124
+ stop_server
125
+ end
126
+ @task.say "Removing server files...", :green
127
+
128
+ begin
129
+ @task.shell.mute do
130
+ FileUtils.rm_r(@server_path)
131
+ end
132
+ rescue Exception => e
133
+ @task.say "Error while removing server files:"
134
+ @task.say e.message + "\n", :red
135
+ end
136
+
137
+ remove_server_database
138
+
139
+ @task.say "Server successfuly removed", :yellow
140
+ exit
141
+ end
142
+
143
+ private
144
+
145
+ def create_server_directory
146
+ unless File.exist?(self.servers_root)
147
+ Dir.mkdir(servers_root)
148
+ end
149
+
150
+ if File.exist?(@server_path)
151
+ @task.say "A server with name #{@server_name} already exists", :red
152
+ exit
153
+ end
154
+ end
155
+
156
+ def download_wordpress
157
+ @task.say "Starting server installation:", :green
158
+
159
+ begin
160
+ @task.shell.mute do
161
+ if !File.exists?("/tmp/wordpress-#{@wp_version}.tar.gz")
162
+ @task.say "Downloading wordpress-#{@wp_version}.tar.gz..."
163
+
164
+ Net::HTTP.start('wordpress.org') do |http|
165
+ resp = http.get("/wordpress-#{@wp_version}.tar.gz")
166
+ open("/tmp/wordpress-#{@wp_version}.tar.gz", 'w') do |file|
167
+ file.write(resp.body)
168
+ end
169
+ end
170
+ end
171
+ end
172
+ rescue Exception => e
173
+ @task.say "Error while downloading wordpress-#{@wp_version}.tar.gz:"
174
+ @task.say e.message + "\n", :red
175
+ exit
176
+ end
177
+ @task.say "wordpress-#{@wp_version}.tar.gz downloaded"
178
+ end
179
+
180
+ def extract_wordpress
181
+ begin
182
+ @task.shell.mute do
183
+ filestamp = Time.now.to_i
184
+ download_location = File.join('/tmp', "wordpress-#{@wp_version}.tar.gz")
185
+ tmp_dir = "/tmp/wordpress-latest-#{filestamp}"
186
+
187
+ Dir.mkdir(tmp_dir)
188
+ `cd #{tmp_dir}; tar -xzf #{download_location}`
189
+
190
+ FileUtils.mv("#{tmp_dir}/wordpress", @server_path)
191
+ FileUtils.rm_r(tmp_dir)
192
+ end
193
+ rescue Exception => e
194
+ @task.say "Error while extracting wordpress-#{@wp_version}.tar.gz:"
195
+ @task.say e.message + "\n", :red
196
+ exit
197
+ end
198
+ end
199
+
200
+ def add_wordpress_config
201
+ unless File.exists?(@wp_config)
202
+ wp_config = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'layouts', 'config', 'wp-config.php.erb'))
203
+ wp_config_template = ERB.new(::File.binread(wp_config), nil, '-', '@output_buffer')
204
+
205
+ File.open(File.join(@server_path, 'wp-config.php'), 'w') do |file|
206
+ file.write(wp_config_template.result(binding))
207
+ end
208
+ end
209
+ end
210
+
211
+ def add_rack_config
212
+ unless File.exists?(@rack_config)
213
+ config_wp = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'layouts', 'config', 'rack-config.ru'))
214
+ config_ru_template = ERB.new(::File.binread(config_wp), nil, '-', '@output_buffer')
215
+
216
+ File.open(File.join(@server_path, 'config.ru'), 'w') do |file|
217
+ file.write(config_ru_template.result(binding))
218
+ end
219
+ end
220
+ end
221
+
222
+ def create_server_database
223
+ begin
224
+ @task.shell.mute do
225
+ client = Mysql2::Client.new(:host => @db_host, :port => @db_port, :username => @db_user, :password => @db_password)
226
+ client.query("CREATE DATABASE IF NOT EXISTS #{@db_name}")
227
+ client.query("GRANT ALL PRIVILEGES ON #{@db_name}.* TO '#{@db_user}'@'#{@db_host}'")
228
+ client.query("FLUSH PRIVILEGES")
229
+ client.close
230
+ end
231
+ rescue Exception => e
232
+ @task.say "Error while creating Mysql database:"
233
+ @task.say e.message + "\n", :red
234
+ exit
235
+ end
236
+ @task.say "Mysql database created", :green
237
+ end
238
+
239
+ def remove_server_database
240
+ begin
241
+ @task.shell.mute do
242
+ client = Mysql2::Client.new(:host => @db_host, :port => @db_port, :username => @db_user, :password => @db_password)
243
+ client.query("DROP DATABASE IF EXISTS #{@db_name}")
244
+ client.query("REVOKE ALL PRIVILEGES ON #{@db_name}.* FROM '#{@db_user}'@'#{@db_host}'")
245
+ client.query("FLUSH PRIVILEGES")
246
+ client.close
247
+ end
248
+ rescue Exception => e
249
+ @task.say "Error while removing Mysql database:"
250
+ @task.say e.message + "\n", :red
251
+ exit
252
+ end
253
+ @task.say "Mysql database removed", :yellow
254
+ end
255
+
256
+ def add_global_content
257
+ @task.shell.mute do
258
+ global = File.join(ENV['HOME'], '.marv')
259
+ themes = Dir.glob(File.join(global, 'themes', '*'))
260
+
261
+ themes.each do |theme|
262
+ @task.create_link File.join(@server_path, 'wp-content', 'themes', File.basename(theme)), theme
263
+ end
264
+
265
+ plugins = Dir.glob(File.join(global, 'plugins', '*'))
266
+
267
+ plugins.each do |plugin|
268
+ @task.create_link File.join(@server_path, 'wp-content', 'plugins', File.basename(plugin)), plugin
269
+ end
270
+ end
271
+ end
272
+
273
+ def update_global_content
274
+ @task.shell.mute do
275
+ themes = Dir.glob(File.join(@server_path, 'wp-content', 'themes', '*'))
276
+
277
+ themes.each do |theme|
278
+ if File.symlink?(theme)
279
+ unless File.exists?(theme)
280
+ File.delete(theme)
281
+ File.delete(File.join(ENV['HOME'], '.marv', 'themes', File.basename(theme)))
282
+ end
283
+ end
284
+ end
285
+
286
+ plugins = Dir.glob(File.join(@server_path, 'wp-content', 'plugins', '*'))
287
+
288
+ plugins.each do |plugin|
289
+ if File.symlink?(plugin)
290
+ unless File.exists?(plugin)
291
+ File.delete(plugin)
292
+ File.delete(File.join(ENV['HOME'], '.marv', 'themes', File.basename(plugin)))
293
+ end
294
+ end
295
+ end
296
+
297
+ end
298
+ end
299
+
300
+ end
301
+ end