theme-juice 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,804 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module ThemeJuice
4
- module Executor
5
- class << self
6
- include ::Thor::Actions
7
- include ::Thor::Shell
8
-
9
- ###
10
- # Run installation from config
11
- #
12
- # @param {String} config_path (nil)
13
- #
14
- # @return {Void}
15
- ###
16
- def install(config_path = nil)
17
- config_path ||= File.expand_path(Dir.pwd)
18
-
19
- use_config config_path
20
-
21
- @config["commands"]["install"].each do |command|
22
- run ["cd #{config_path}", command], false
23
- end
24
- end
25
-
26
- ###
27
- # Run subcommand from config
28
- #
29
- # @param {String} subcommand
30
- # @param {String} command
31
- #
32
- # @return {Void}
33
- ###
34
- def subcommand(subcommand, command)
35
- config_path = File.expand_path(Dir.pwd)
36
-
37
- use_config config_path
38
-
39
- if @config["commands"][subcommand]
40
- run ["#{@config["commands"][subcommand]} #{command}"], false
41
- else
42
- ::ThemeJuice::UI.error "Unable to find '#{subcommand}' command in '#{config_path}/tj.yml'. Aborting mission."
43
- end
44
- end
45
-
46
- ###
47
- # Set up local development environment and site
48
- #
49
- # @param {Hash} opts
50
- #
51
- # @return {Void}
52
- ###
53
- def create(opts)
54
- @opts = opts
55
-
56
- ::ThemeJuice::UI.notice "Running setup for '#{@opts[:site_name]}'..."
57
-
58
- unless project_dir_is_setup?
59
- setup_project_dir
60
- end
61
-
62
- unless wordpress_is_setup?
63
- setup_wordpress
64
- end
65
-
66
- if config_is_setup? @opts[:site_location]
67
- use_config @opts[:site_location]
68
-
69
- install_theme_dependencies
70
- end
71
-
72
- unless vvv_is_setup?
73
- setup_vvv
74
- end
75
-
76
- unless wildcard_subdomains_is_setup?
77
- setup_wildcard_subdomains
78
- end
79
-
80
- unless hosts_is_setup?
81
- setup_hosts
82
- end
83
-
84
- unless database_is_setup?
85
- setup_database
86
- end
87
-
88
- unless nginx_is_setup?
89
- setup_nginx
90
- end
91
-
92
- unless dev_site_is_setup?
93
- setup_dev_site
94
- end
95
-
96
- unless env_is_setup?
97
- setup_env
98
- end
99
-
100
- unless synced_folder_is_setup?
101
- setup_synced_folder
102
- end
103
-
104
- unless wpcli_is_setup?
105
- setup_wpcli
106
- end
107
-
108
- if @opts[:repository]
109
- setup_repo
110
- end
111
-
112
- if setup_was_successful?
113
- ::ThemeJuice::UI.success "Setup complete!"
114
- ::ThemeJuice::UI.speak "In order to finish creating your site, you need to provision Vagrant. Do it now? (y/N)", {
115
- color: [:black, :on_blue],
116
- icon: :restart,
117
- row: true
118
- }
119
-
120
- if ::ThemeJuice::UI.agree? "", { simple: true }
121
- ::ThemeJuice::UI.speak "Restarting VVV...", {
122
- color: :yellow,
123
- icon: :general
124
- }
125
-
126
- if restart_vagrant
127
- ::ThemeJuice::UI.success "Success!"
128
-
129
- # Output setup info
130
- ::ThemeJuice::UI.list "Your settings :", :blue, [
131
- "Site name: #{@opts[:site_name]}",
132
- "Site location: #{@opts[:site_location]}",
133
- "Starter theme: #{@opts[:starter_theme]}",
134
- "Development location: #{@opts[:dev_location]}",
135
- "Development url: http://#{@opts[:dev_url]}",
136
- "Initialized repository: #{@opts[:repository]}",
137
- "Database host: #{@opts[:db_host]}",
138
- "Database name: #{@opts[:db_name]}",
139
- "Database username: #{@opts[:db_user]}",
140
- "Database password: #{@opts[:db_pass]}"
141
- ]
142
- end
143
-
144
- unless OS.windows?
145
- ::ThemeJuice::UI.notice "Do you want to open up your new site 'http://#{@opts[:dev_url]}' now? (y/N)"
146
-
147
- if ::ThemeJuice::UI.agree? "", { color: :yellow, simple: true }
148
- run ["open http://#{@opts[:dev_url]}"]
149
- end
150
- end
151
- else
152
- ::ThemeJuice::UI.notice "Remember, Vagrant needs to be provisioned before you can use your new site. Exiting..."
153
- exit
154
- end
155
- else
156
- ::ThemeJuice::UI.error "Setup failed. Running cleanup..." do
157
- delete @opts[:site_name], false
158
- end
159
- end
160
- end
161
-
162
- ###
163
- # Remove all traces of site from Vagrant
164
- #
165
- # @param {String} site
166
- # @param {Bool} restart
167
- #
168
- # @return {Void}
169
- ###
170
- def delete(site, restart)
171
-
172
- ###
173
- # @TODO This is a really hacky way to remove the theme. Eventually,
174
- # I'd like to handle state within a config file.
175
- ###
176
- @opts = {
177
- site_name: site,
178
- dev_location: File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/www/tj-#{site}")
179
- }
180
-
181
- if dev_site_is_setup?
182
- remove_dev_site
183
- else
184
- ::ThemeJuice::UI.error "Site '#{@opts[:site_name]}' does not exist."
185
- end
186
-
187
- if database_is_setup?
188
- remove_database
189
- end
190
-
191
- if synced_folder_is_setup?
192
- remove_synced_folder
193
- end
194
-
195
- if removal_was_successful?
196
- ::ThemeJuice::UI.success "Site '#{@opts[:site_name]}' successfully removed!"
197
-
198
- if restart
199
- ::ThemeJuice::UI.speak "Restarting VVV...", {
200
- color: :yellow,
201
- icon: :general
202
- }
203
-
204
- restart_vagrant
205
- end
206
- else
207
- ::ThemeJuice::UI.error "'#{@opts[:site_name]}' could not be fully be removed."
208
- end
209
- end
210
-
211
- ###
212
- # List all development sites
213
- #
214
- # @return {Array}
215
- ###
216
- def list
217
- sites = []
218
-
219
- Dir.glob(File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/www/*")).each do |f|
220
- sites << File.basename(f).gsub(/(tj-)/, "") if File.directory?(f) && f.include?("tj-")
221
- end
222
-
223
- if sites.empty?
224
- ::ThemeJuice::UI.speak "Nothing to list. Why haven't you created a site yet?", {
225
- color: :yellow,
226
- icon: :general
227
- }
228
- else
229
- ::ThemeJuice::UI.list "Your sites :", :green, sites
230
- end
231
-
232
- sites
233
- end
234
-
235
- private
236
-
237
- ###
238
- # Run system commands
239
- #
240
- # @param {Array} commands
241
- # Array of commands to run
242
- # @param {Bool} silent (true)
243
- # Silence all output from command
244
- #
245
- # @return {Void}
246
- ###
247
- def run(commands = [], silent = true)
248
- commands.map! { |cmd| cmd.to_s + " > /dev/null 2>&1" } if silent
249
- system commands.join "&&"
250
- end
251
-
252
- ###
253
- # Verify config is properly setup, set global var
254
- #
255
- # @param {String} config_path
256
- #
257
- # @return {Void}
258
- ###
259
- def use_config(config_path)
260
-
261
- if config_is_setup? config_path
262
- @config = YAML.load_file(Dir["#{config_path}/*"].select { |f| File.basename(f) =~ /^(\.)?(tj.y(a)?ml)/ }.last)
263
- else
264
- ::ThemeJuice::UI.notice "Unable to find 'tj.yml' file in '#{config_path}'."
265
-
266
- if ::ThemeJuice::UI.agree? "Would you like to create one?"
267
-
268
- setup_config(config_path)
269
-
270
- if config_is_setup? config_path
271
- ::ThemeJuice::UI.notice "Please re-run the last command to continue."
272
- exit
273
- else
274
- exit 1
275
- end
276
- else
277
- ::ThemeJuice::UI.error "A config file is needed to continue. Aborting mission."
278
- end
279
- end
280
- end
281
-
282
- ###
283
- # Restart Vagrant
284
- #
285
- # @note Normally a simple 'vagrant reload' would work, but Landrush requires
286
- # a 'vagrant up' to be fired for it to set up the DNS correctly.
287
- #
288
- # @return {Void}
289
- ###
290
- def restart_vagrant
291
- run [
292
- "cd #{::ThemeJuice::Utilities.vvv_path}",
293
- "vagrant halt",
294
- "vagrant up --provision",
295
- ], false
296
- end
297
-
298
- ###
299
- # @return {Bool}
300
- ###
301
- def setup_was_successful?
302
- vvv_is_setup? and dev_site_is_setup? and hosts_is_setup? and database_is_setup? and nginx_is_setup?
303
- end
304
-
305
- ###
306
- # @return {Bool}
307
- ###
308
- def removal_was_successful?
309
- !setup_was_successful?
310
- end
311
-
312
- ###
313
- # @return {Bool}
314
- ###
315
- def project_dir_is_setup?
316
- Dir.exist? "#{@opts[:site_location]}"
317
- end
318
-
319
- ###
320
- # @return {Bool}
321
- ###
322
- def config_is_setup?(config_path)
323
- !Dir["#{config_path}/*"].select { |f| File.basename(f) =~ /^(\.)?(tj.y(a)?ml)/ }.empty?
324
- end
325
-
326
- ###
327
- # @return {Bool}
328
- ###
329
- def vvv_is_setup?
330
- File.exist? File.expand_path(::ThemeJuice::Utilities.vvv_path)
331
- end
332
-
333
- ###
334
- # @return {Bool}
335
- ###
336
- def wildcard_subdomains_is_setup?
337
- File.readlines(File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/Vagrantfile")).grep(/(config.landrush.enabled = true)/m).any?
338
- end
339
-
340
- ###
341
- # @return {Bool}
342
- ###
343
- def dev_site_is_setup?
344
- File.exist? "#{@opts[:dev_location]}"
345
- end
346
-
347
- ###
348
- # @return {Bool}
349
- ###
350
- def hosts_is_setup?
351
- File.exist? "#{@opts[:site_location]}/vvv-hosts"
352
- end
353
-
354
- ###
355
- # @return {Bool}
356
- ###
357
- def database_is_setup?
358
- File.readlines(File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/database/init-custom.sql")).grep(/(### Begin '#{@opts[:site_name]}')/m).any?
359
- end
360
-
361
- ###
362
- # @return {Bool}
363
- ###
364
- def nginx_is_setup?
365
- File.exist? "#{@opts[:site_location]}/vvv-nginx.conf"
366
- end
367
-
368
- ###
369
- # @return {Bool}
370
- ###
371
- def wordpress_is_setup?
372
- File.exist? File.expand_path("#{@opts[:site_location]}/app")
373
- end
374
-
375
- ###
376
- # @return {Bool}
377
- ###
378
- def synced_folder_is_setup?
379
- File.readlines(File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/Vagrantfile")).grep(/(### Begin '#{@opts[:site_name]}')/m).any?
380
- end
381
-
382
- ###
383
- # @return {Bool}
384
- ###
385
- def repo_is_setup?
386
- File.exist? File.expand_path("#{@opts[:site_location]}/.git")
387
- end
388
-
389
- ###
390
- # @return {Bool}
391
- ###
392
- def env_is_setup?
393
- File.exist? File.expand_path("#{@opts[:site_location]}/.env.development")
394
- end
395
-
396
- ###
397
- # @return {Bool}
398
- ###
399
- def wpcli_is_setup?
400
- File.exist? File.expand_path("#{@opts[:site_location]}/wp-cli.local.yml")
401
- end
402
-
403
- ###
404
- # Install Vagrant plugins and clone VVV
405
- #
406
- # @return {Void}
407
- ###
408
- def setup_vvv
409
- ::ThemeJuice::UI.speak "Installing VVV into '#{File.expand_path("#{::ThemeJuice::Utilities.vvv_path}")}'...", {
410
- color: :yellow,
411
- icon: :general
412
- }
413
-
414
- run [
415
- "vagrant plugin install vagrant-hostsupdater",
416
- "vagrant plugin install vagrant-triggers",
417
- "vagrant plugin install landrush",
418
- "git clone https://github.com/Varying-Vagrant-Vagrants/VVV.git #{::ThemeJuice::Utilities.vvv_path}",
419
- "cd #{::ThemeJuice::Utilities.vvv_path}/database && touch init-custom.sql"
420
- ]
421
- end
422
-
423
- ###
424
- # Ensure project directory structure
425
- #
426
- # @return {Void}
427
- ###
428
- def setup_project_dir
429
- ::ThemeJuice::UI.speak "Creating project directory tree in '#{@opts[:site_location]}'...", {
430
- color: :yellow,
431
- icon: :general
432
- }
433
-
434
- run ["mkdir -p #{@opts[:site_location]}"]
435
- end
436
-
437
- ###
438
- # Enable Landrush for wildcard subdomains
439
- #
440
- # This will write a Landrush activation block to the global Vagrantfile
441
- # if one does not already exist.
442
- #
443
- # @return {Void}
444
- ###
445
- def setup_wildcard_subdomains
446
- ::ThemeJuice::UI.speak "Setting up wildcard subdomains...", {
447
- color: :yellow,
448
- icon: :general
449
- }
450
-
451
- open File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/Vagrantfile"), "ab+" do |file|
452
- file.puts "\n"
453
- file.puts "###"
454
- file.puts "# Enable wildcard subdomains"
455
- file.puts "#"
456
- file.puts "# This block is automatically generated by ThemeJuice. Do not edit."
457
- file.puts "###"
458
- file.puts "Vagrant.configure('2') do |config|"
459
- file.puts "\tconfig.landrush.enabled = true"
460
- file.puts "\tconfig.landrush.tld = 'dev'"
461
- file.puts "end"
462
- file.puts "\n"
463
- end
464
- end
465
-
466
- ###
467
- # Create a new directory for site that will be symlinked with the local install
468
- #
469
- # @return {Void}
470
- ###
471
- def setup_dev_site
472
- ::ThemeJuice::UI.speak "Setting up new development site at '#{@opts[:dev_location]}'...", {
473
- color: :yellow,
474
- icon: :general
475
- }
476
-
477
- run [
478
- "cd #{::ThemeJuice::Utilities.vvv_path}/www",
479
- "mkdir tj-#{@opts[:site_name]}",
480
- ]
481
- end
482
-
483
- ###
484
- # Create tj.yml file for theme settings
485
- #
486
- # @param {String} config_path
487
- #
488
- # @return {Void}
489
- ###
490
- def setup_config(config_path)
491
- watch = ::ThemeJuice::UI.prompt "Watch command to use", indent: 2, default: "bundle exec guard"
492
- server = ::ThemeJuice::UI.prompt "Deployment command to use", indent: 2, default: "bundle exec cap"
493
- vendor = ::ThemeJuice::UI.prompt "Vendor command to use", indent: 2, default: "composer"
494
- install = ::ThemeJuice::UI.prompt "Commands to run on theme install", indent: 2, default: "composer install"
495
-
496
- File.open "#{config_path}/tj.yml", "wb" do |file|
497
- file.puts "commands:"
498
- file.puts "\s\swatch: #{watch}"
499
- file.puts "\s\sserver: #{server}"
500
- file.puts "\s\svendor: #{vendor}"
501
- file.puts "\s\sinstall:"
502
- file.puts "\s\s\s\s- #{install}"
503
- end
504
-
505
- if config_is_setup? config_path
506
- ::ThemeJuice::UI.speak "Successfully added 'tj.yml' file.", {
507
- color: :green,
508
- icon: :general
509
- }
510
- else
511
- ::ThemeJuice::UI.error "Could not create 'tj.yml' file. Make sure you have write capabilities to '#{@opts[:site_location]}'."
512
- end
513
- end
514
-
515
- ###
516
- # Create vvv-hosts file
517
- #
518
- # @return {Void}
519
- ###
520
- def setup_hosts
521
- File.open "#{@opts[:site_location]}/vvv-hosts", "wb" do |file|
522
- file.puts @opts[:dev_url]
523
- end
524
-
525
- if hosts_is_setup?
526
- ::ThemeJuice::UI.speak "Successfully added 'vvv-hosts' file.", {
527
- color: :green,
528
- icon: :general
529
- }
530
- else
531
- ::ThemeJuice::UI.error "Could not create 'vvv-hosts' file. Make sure you have write capabilities to '#{@opts[:site_location]}'."
532
- end
533
- end
534
-
535
- ###
536
- # Add database block to init-custom.sql, create if not exists
537
- #
538
- # @return {Void}
539
- ###
540
- def setup_database
541
- File.open File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/database/init-custom.sql"), "ab+" do |file|
542
- file.puts "### Begin '#{@opts[:site_name]}'"
543
- file.puts "#"
544
- file.puts "# This block is automatically generated by ThemeJuice. Do not edit."
545
- file.puts "###"
546
- file.puts "CREATE DATABASE IF NOT EXISTS `#{@opts[:db_name]}`;"
547
- file.puts "GRANT ALL PRIVILEGES ON `#{@opts[:db_name]}`.* TO '#{@opts[:db_user]}'@'localhost' IDENTIFIED BY '#{@opts[:db_pass]}';"
548
- file.puts "### End '#{@opts[:site_name]}'"
549
- file.puts "\n"
550
- end
551
-
552
- if database_is_setup?
553
- ::ThemeJuice::UI.speak "Successfully added database to 'init-custom.sql'.", {
554
- color: :green,
555
- icon: :general
556
- }
557
- else
558
- ::ThemeJuice::UI.error "Could not add database info for '#{@opts[:site_name]}' to 'init-custom.sql'. Make sure you have write capabilities to '#{@opts[:site_location]}'."
559
- end
560
- end
561
-
562
- ###
563
- # Create vvv-nginx.conf file for local development site
564
- #
565
- # @return {Void}
566
- ###
567
- def setup_nginx
568
- File.open "#{@opts[:site_location]}/vvv-nginx.conf", "wb" do |file|
569
- file.puts "server {"
570
- file.puts "\tlisten 80;"
571
- file.puts "\tserver_name .#{@opts[:dev_url]};"
572
- file.puts "\troot {vvv_path_to_folder};"
573
- file.puts "\tinclude /etc/nginx/nginx-wp-common.conf;"
574
- file.puts "}"
575
- end
576
-
577
- if nginx_is_setup?
578
- ::ThemeJuice::UI.speak "Successfully added 'vvv-nginx.conf' file.", {
579
- color: :green,
580
- icon: :general
581
- }
582
- else
583
- ::ThemeJuice::UI.error "Could not create 'vvv-nginx.conf' file. Make sure you have write capabilities to '#{@opts[:site_location]}'."
584
- end
585
- end
586
-
587
- ###
588
- # Create Dotenv environment file
589
- #
590
- # @return {Void}
591
- ###
592
- def setup_env
593
- File.open "#{@opts[:site_location]}/.env.development", "wb" do |file|
594
- file.puts "DB_NAME=#{@opts[:db_name]}"
595
- file.puts "DB_USER=#{@opts[:db_user]}"
596
- file.puts "DB_PASSWORD=#{@opts[:db_pass]}"
597
- file.puts "DB_HOST=#{@opts[:db_host]}"
598
- file.puts "WP_HOME=http://#{@opts[:dev_url]}"
599
- file.puts "WP_SITEURL=http://#{@opts[:dev_url]}/wp"
600
- end
601
-
602
- if env_is_setup?
603
- ::ThemeJuice::UI.speak "Successfully added '.env.development' file.", {
604
- color: :green,
605
- icon: :general
606
- }
607
- else
608
- ::ThemeJuice::UI.error "Could not create '.env.development' file. Make sure you have write capabilities to '#{@opts[:site_location]}'."
609
- end
610
- end
611
-
612
- ###
613
- # Setup WordPress
614
- #
615
- # Clones starter theme into site location
616
- #
617
- # @return {Void}
618
- ###
619
- def setup_wordpress
620
- ::ThemeJuice::UI.speak "Setting up WordPress...", {
621
- color: :yellow,
622
- icon: :general
623
- }
624
-
625
- unless @opts[:bare_setup]
626
- run [
627
- "cd #{@opts[:site_location]}",
628
- "git clone --depth 1 #{@opts[:starter_theme]} .",
629
- ]
630
- end
631
- end
632
-
633
- ###
634
- # Install dependencies for starter theme
635
- #
636
- # @return {Void}
637
- ###
638
- def install_theme_dependencies
639
- ::ThemeJuice::UI.speak "Installing theme dependencies...", {
640
- color: :yellow,
641
- icon: :general
642
- }
643
-
644
- @config["commands"]["install"].each do |command|
645
- run ["cd #{@opts[:site_location]}", command], false
646
- end
647
- end
648
-
649
- ###
650
- # Add synced folder block to Vagrantfile
651
- #
652
- # @return {Void}
653
- ###
654
- def setup_synced_folder
655
- ::ThemeJuice::UI.speak "Syncing host theme directory '#{@opts[:site_location]}' with VM theme directory '/srv/www/tj-#{@opts[:site_name]}'...", {
656
- color: :yellow,
657
- icon: :general
658
- }
659
-
660
- open File.expand_path("#{::ThemeJuice::Utilities.vvv_path}/Vagrantfile"), "ab+" do |file|
661
- file.puts "### Begin '#{@opts[:site_name]}'"
662
- file.puts "#"
663
- file.puts "# This block is automatically generated by ThemeJuice. Do not edit."
664
- file.puts "###"
665
- file.puts "Vagrant.configure('2') do |config|"
666
- file.puts "\tconfig.vm.synced_folder '#{@opts[:site_location]}', '/srv/www/tj-#{@opts[:site_name]}', mount_options: ['dmode=777','fmode=777']"
667
- file.puts "\tconfig.landrush.host '#{@opts[:dev_url]}', '192.168.50.4'"
668
- file.puts "end"
669
- file.puts "### End '#{@opts[:site_name]}'"
670
- file.puts "\n"
671
- end
672
- end
673
-
674
- ###
675
- # Initialize Git repo, add remote, initial commit
676
- #
677
- # @return {Void}
678
- ###
679
- def setup_repo
680
- ::ThemeJuice::UI.speak "Setting up Git repository at '#{@opts[:repository]}'...", {
681
- color: :yellow,
682
- icon: :general
683
- }
684
-
685
- if repo_is_setup?
686
- run [
687
- "cd #{@opts[:site_location]}",
688
- "rm -rf .git",
689
- ]
690
- end
691
-
692
- run [
693
- "cd #{@opts[:site_location]}",
694
- "git init",
695
- "git remote add origin #{@opts[:repository]}",
696
- ]
697
- end
698
-
699
- ##
700
- # Add wp-cli-ssh block to wp-cli.yml
701
- #
702
- # @return {Void}
703
- ###
704
- def setup_wpcli
705
- File.open "#{@opts[:site_location]}/wp-cli.local.yml", "ab+" do |file|
706
- file.puts "require:"
707
- file.puts "\t- vendor/autoload.php"
708
- file.puts "ssh:"
709
- file.puts "\tvagrant:"
710
- file.puts "\t\turl: #{@opts[:dev_url]}"
711
- file.puts "\t\tpath: /srv/www/tj-#{@opts[:site_name]}"
712
- file.puts "\t\tcmd: cd #{::ThemeJuice::Utilities.vvv_path} && vagrant ssh-config > /tmp/vagrant_ssh_config && ssh -q %pseudotty% -F /tmp/vagrant_ssh_config default %cmd%"
713
- file.puts "\n"
714
- end
715
-
716
- if wpcli_is_setup?
717
- ::ThemeJuice::UI.speak "Successfully added ssh settings to 'wp-cli.local.yml' file.", {
718
- color: :green,
719
- icon: :general
720
- }
721
- else
722
- ::ThemeJuice::UI.error "Could not create 'wp-cli.local.yml' file. Make sure you have write capabilities to '#{@opts[:site_location]}'."
723
- end
724
- end
725
-
726
- ###
727
- # Remove all theme files from Vagrant directory
728
- #
729
- # @return {Void}
730
- ###
731
- def remove_dev_site
732
-
733
- unless Dir.entries("#{::ThemeJuice::Utilities.vvv_path}").include? "www"
734
- say " ↑ Cannot load VVV path. Aborting mission before something bad happens.".ljust(terminal_width), [:white, :on_red]
735
- exit 1
736
- end
737
-
738
- if run ["rm -rf #{@opts[:dev_location]}"]
739
- ::ThemeJuice::UI.speak "VVV installation for '#{@opts[:site_name]}' successfully removed.", {
740
- color: :yellow,
741
- icon: :general
742
- }
743
- else
744
- ::ThemeJuice::UI.error "Theme '#{@opts[:site_name]}' could not be removed. Make sure you have write capabilities to '#{@opts[:dev_location]}'."
745
- end
746
- end
747
-
748
- ###
749
- # Remove database block from init-custom.sql
750
- #
751
- # @return {Void}
752
- ###
753
- def remove_database
754
- if remove_traces_from_file "#{::ThemeJuice::Utilities.vvv_path}/database/init-custom.sql"
755
- ::ThemeJuice::UI.speak "Database for '#{@opts[:site_name]}' successfully removed.", {
756
- color: :yellow,
757
- icon: :general
758
- }
759
- end
760
- end
761
-
762
- ###
763
- # Remove synced folder block from Vagrantfile
764
- #
765
- # @return {Void}
766
- ###
767
- def remove_synced_folder
768
- if remove_traces_from_file "#{::ThemeJuice::Utilities.vvv_path}/Vagrantfile"
769
- ::ThemeJuice::UI.speak "Synced folders for '#{@opts[:site_name]}' successfully removed.", {
770
- color: :yellow,
771
- icon: :general
772
- }
773
- end
774
- end
775
-
776
- ###
777
- # Remove all traces of auto-generated content from file
778
- #
779
- # @param {String} input_file
780
- #
781
- # @return {Void}
782
- ###
783
- def remove_traces_from_file(input_file)
784
- begin
785
- # Create new tempfile
786
- output_file = Tempfile.new File.basename(input_file)
787
- # Copy over contents of actual file to tempfile
788
- open File.expand_path(input_file), "rb" do |file|
789
- # Remove traces of theme from contents
790
- output_file.write "#{file.read}".gsub(/(### Begin '#{@opts[:site_name]}')(.*?)(### End '#{@opts[:site_name]}')\n+/m, "")
791
- end
792
- # Move temp file to actual file location
793
- FileUtils.mv output_file, File.expand_path(input_file)
794
- rescue LoadError => err
795
- ::ThemeJuice::UI.error "#{err}"
796
- ensure
797
- # Make sure that the tempfile closes and is cleaned up, regardless of errors
798
- output_file.close
799
- output_file.unlink
800
- end
801
- end
802
- end
803
- end
804
- end