wordpress-deploy 1.0.0.alpha2 → 1.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/.gitignore +7 -1
  2. data/Guardfile +1 -16
  3. data/README.md +56 -0
  4. data/lib/wordpress_deploy.rb +43 -9
  5. data/lib/wordpress_deploy/cli/helpers.rb +6 -3
  6. data/lib/wordpress_deploy/cli/utility.rb +56 -33
  7. data/lib/wordpress_deploy/config.rb +77 -0
  8. data/lib/wordpress_deploy/database/mysql.rb +281 -20
  9. data/lib/wordpress_deploy/environment.rb +85 -43
  10. data/lib/wordpress_deploy/environments.rb +89 -0
  11. data/lib/wordpress_deploy/logger.rb +3 -3
  12. data/lib/wordpress_deploy/{transfer_protocols → storage}/ftp.rb +52 -116
  13. data/lib/wordpress_deploy/storage/local.rb +12 -0
  14. data/lib/wordpress_deploy/version.rb +1 -1
  15. data/lib/wordpress_deploy/wordpress/salts.rb +68 -0
  16. data/spec/data/blue.rb +52 -0
  17. data/spec/data/development.rb +49 -0
  18. data/spec/data/green.rb +52 -0
  19. data/spec/data/production.rb +52 -0
  20. data/spec/data/red.rb +52 -0
  21. data/spec/data/wp-config.php +90 -0
  22. data/spec/spec_helper.rb +16 -5
  23. data/spec/wordpress_deploy/cli/utility_spec.rb +48 -0
  24. data/spec/{environment_spec.rb → wordpress_deploy/config_spec.rb} +9 -7
  25. data/spec/wordpress_deploy/database/mysql_spec.rb +147 -0
  26. data/spec/wordpress_deploy/environment_spec.rb +96 -0
  27. data/spec/wordpress_deploy/environments_spec.rb +65 -0
  28. data/spec/wordpress_deploy/storage/ftp_spec.rb +58 -0
  29. data/spec/wordpress_deploy/storage/local_spec.rb +0 -0
  30. data/spec/wordpress_deploy/wordpress/salts_spec.rb +70 -0
  31. data/{spec/data/wp-config-sample.php → templates/wp-config.erb} +17 -17
  32. data/wordpress_deploy.gemspec +7 -6
  33. metadata +64 -30
  34. data/Gemfile.lock +0 -83
  35. data/lib/wordpress_deploy/wordpress/configuration.rb +0 -196
  36. data/spec/data/ftp.yml +0 -4
  37. data/spec/data/wp-config.yml +0 -128
  38. data/spec/database/mysql_spec.rb +0 -93
  39. data/spec/transfer_protocols/ftp_spec.rb +0 -193
  40. data/spec/wordpress/configuration_spec.rb +0 -202
@@ -1,68 +1,110 @@
1
1
  module WordpressDeploy
2
- ##
3
- # Environment defines all of the locations of input and
4
- # output files. Specifically, the locations of the test
5
- # definitions, the locations of the test results, and
6
- # the locations of the build applications.
7
- module Environment
2
+ class Environment
8
3
 
9
- ##
10
- # Setup required paths based on the given options
11
- def self.set_options(options = {})
12
- options.each do |option, value|
13
- method = "#{option}="
14
- send(method, value) if respond_to? method and !value.empty?
15
- end
4
+ attr_reader :name
5
+
6
+ def initialize(name, store = true, &block)
7
+ @name = name
8
+ @base_url ||= "localhost"
9
+ @wplang ||= ""
10
+ @wpdebug ||= false
11
+
12
+ # Get an Erb template
13
+ @template = ERB.new(File.read(File.join(WordpressDeploy::TEMPLATE_PATH, "wp-config.erb")))
14
+ @output = Environment.output_file
15
+
16
+ instance_eval(&block) if block_given?
17
+
18
+ Environments << self if store
16
19
  end
17
20
 
18
- def self.logging=(new_log)
19
- # Only set @@logging if new_log is a boolean
20
- if !!new_log == new_log
21
- @@logging = new_log
22
- else
23
- @@logging = false
24
- end
21
+ ##
22
+ # This is the url to search the the database for.
23
+ def base_url(new_base = nil)
24
+ @base_url = new_base.to_s unless new_base.nil?
25
+ @base_url
25
26
  end
26
27
 
27
- def self.logging?
28
- @@logging ||= false
29
- @@logging
28
+ ##
29
+ #
30
+ def database(&block)
31
+ @database ||= WordpressDeploy::Database::MySql.new
32
+ @database.instance_eval(&block) if block_given?
33
+ @database
30
34
  end
31
35
 
32
- def self.root_dir=(new_root)
33
- @@root_dir = new_root
36
+ ##
37
+ #
38
+ def salts(&block)
39
+ @salts ||= WordpressDeploy::Wordpress::Salts.new
40
+ @salts.instance_eval(&block) if block_given?
41
+ @salts
34
42
  end
35
43
 
36
- def self.root_dir
37
- @@root_dir ||= Dir.pwd
38
- File.expand_path @@root_dir
44
+ ##
45
+ #
46
+ def transfer(type = nil, &block)
47
+ unless type.nil?
48
+ klass = get_class_from_scope(WordpressDeploy::Storage, type)
49
+ @transfer ||= klass.new
50
+ @transfer.instance_eval(&block) if block_given?
51
+ end
52
+ @transfer
39
53
  end
40
54
 
41
- def self.config_dir=(new_config_dir)
42
- @@config_dir = new_config_dir
55
+ ##
56
+ # Write the file the filesystem
57
+ def save_wp_config
58
+ # Remove the output file if is already there
59
+ FileUtils.rm @output if File.exists? @output
60
+
61
+ # Open the output file
62
+ File.open(@output, 'w') do |f|
63
+ # Evaluate the ERB template
64
+ f.write @template.result(get_binding)
65
+ end
43
66
  end
44
67
 
45
- def self.config_dir
46
- @@config_dir ||= "config"
47
- File.expand_path File.join(root_dir, @@config_dir)
68
+ ##
69
+ #
70
+ def wplang(new_wp_lang = nil)
71
+ @wplang = new_wp_lang.to_s unless new_wp_lang.nil?
72
+ @wplang
48
73
  end
49
74
 
50
- def self.wp_dir=(new_wp_dir)
51
- @@wp_dir = new_wp_dir
75
+ ##
76
+ #
77
+ def wpdebug(new_wp_debug = nil)
78
+ # Only assign if argument is a boolean value
79
+ # http://stackoverflow.com/questions/3028243/check-if-ruby-object-is-a-boolean
80
+ @wpdebug = new_wp_debug if (new_wp_debug.is_a?(TrueClass) || new_wp_debug.is_a?(FalseClass))
81
+ @wpdebug.to_s
52
82
  end
53
83
 
54
- def self.wp_dir
55
- @@wp_dir ||= "site"
56
- File.expand_path File.join(root_dir, @@wp_dir)
84
+ private
85
+
86
+ def self.output_file
87
+ File.expand_path(File.join(Config.wp_dir, "wp-config.php"))
57
88
  end
58
89
 
59
- def self.log_file
60
- @@log_file ||= "#{Time.now.strftime("%Y_%m_%d_%H_%M_%S")}.log"
61
- File.expand_path File.join(root_dir, @@log_file)
90
+ ##
91
+ # Used for ERB generation
92
+ def get_binding
93
+ binding
62
94
  end
63
95
 
64
- def self.clean!
65
- FileUtils.rm Dir.glob(File.join(root_dir, "*.log"))
96
+ ##
97
+ # Returns the class/model specified by +name+ inside of +scope+.
98
+ # +scope+ should be a Class/Module.
99
+ # +name+ should be a Symbol representation of any namespace which exists
100
+ # under +scope+.
101
+ #
102
+ # Examples:
103
+ # get_class_from_scope(DaavTest::Executables, :Pioneer3)
104
+ # returns the class DaavTest::Executables::Pioneer3
105
+ def get_class_from_scope(scope, name)
106
+ scope.const_get(name)
66
107
  end
108
+
67
109
  end
68
110
  end
@@ -0,0 +1,89 @@
1
+
2
+ module WordpressDeploy
3
+ module Environments
4
+
5
+ class << self
6
+
7
+ ##
8
+ # Load the environment files in the configuration directory.
9
+ def load
10
+ files = Dir[File.join(WordpressDeploy::Config.config_dir, "**/*.rb")]
11
+ files.each { |file| require file }
12
+ nil
13
+ end
14
+
15
+ ##
16
+ # Add a new environment. If the environment name is already in use
17
+ # it is not added.
18
+ #
19
+ # Return the number of configurations currently loaded.
20
+ def <<(*environments)
21
+ environments.flatten!
22
+ environments.each do |env|
23
+ env_name = (env.respond_to? :name) ? env.name : nil
24
+ envs << env unless name? env_name
25
+ end
26
+ envs.count
27
+ end
28
+
29
+ ##
30
+ # Test if the name is a valid Configuration name.
31
+ #
32
+ # Return true if it is; false otherwise.
33
+ def name?(name)
34
+ name_to_find = name.to_sym
35
+ !envs.index { |env| env.name === name_to_find }.nil?
36
+ end
37
+
38
+ ##
39
+ #
40
+ def find(name)
41
+ name_to_find = name.to_sym
42
+ index = envs.index { |env| env.name === name_to_find }
43
+ raise Exception,
44
+ "#{name} is not a valid environment" if index.nil?
45
+ envs[index]
46
+ end
47
+
48
+ ##
49
+ # Return an array of the available configuration symbol names.
50
+ def names
51
+ envs.map { |config| config.name }
52
+ end
53
+ alias :available_names :names
54
+
55
+ ##
56
+ # Respond with the named configuration if the method name is a
57
+ # a valid configuration that has been loaded.
58
+ def method_missing(method, *args, &block)
59
+ if name?(method.to_sym)
60
+ find(method.to_sym)
61
+ else
62
+ super
63
+ end
64
+ end
65
+
66
+ ##
67
+ # Respond to an configuration name as though it is a method.
68
+ def respond_to?(method)
69
+ return true if name?(method.to_sym)
70
+ super
71
+ end
72
+
73
+ private
74
+
75
+ ##
76
+ # Create a private instance variable @configurations
77
+ # if one has not already been defined.
78
+ #
79
+ # Return the @configurations variable.
80
+ def envs
81
+ @envs ||= []
82
+ @envs
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -67,7 +67,7 @@ module WordpressDeploy
67
67
  end
68
68
 
69
69
  def truncate!(max_bytes = 500_000)
70
- log_file = Environment.log_file
70
+ log_file = Config.log_file
71
71
  return unless File.exist?(log_file)
72
72
 
73
73
  if File.stat(log_file).size > max_bytes
@@ -118,8 +118,8 @@ module WordpressDeploy
118
118
  ##
119
119
  # Receives an Array of Strings to be written to the log file.
120
120
  def to_file(lines)
121
- dir_name = File.dirname(Environment.log_file)
122
- File.open(Environment.log_file, 'a') do |file|
121
+ dir_name = File.dirname(Config.log_file)
122
+ File.open(Config.log_file, 'a') do |file|
123
123
  lines.each {|line| file.puts line }
124
124
  end if File.writable? dir_name
125
125
  messages.push(*lines)
@@ -1,34 +1,62 @@
1
1
  ##
2
2
  # Only load the Net::FTP library/gem when the
3
- # WordpressDeploy::TransferProtocols::Ftp class is loaded
3
+ # WordpressDeploy::Storage::Ftp class is loaded
4
4
  require "net/ftp"
5
5
  require "pathname"
6
6
  require "action_view"
7
7
 
8
8
  module WordpressDeploy
9
- module TransferProtocols
9
+ module Storage
10
10
  class Ftp
11
11
  include ActionView::Helpers::NumberHelper
12
12
 
13
- attr_reader :configuration
14
- attr_accessor :available_names
15
- alias :names :available_names
16
-
17
13
  ##
18
14
  # Create a new instance of the Ftp object
19
- def initialize(config_name = nil)
20
- # Set the configuration
21
- @yaml = YAML.load_file(File.join(Environment.config_dir, "wp-config.yml"))
22
- @available_names = @yaml.map { |key, val| key }
15
+ def initialize(&block)
16
+ @host ||= "localhost"
17
+ @port ||= 21
18
+ @user ||= "root"
19
+ @password ||= ""
20
+ @destination ||= "/"
21
+
22
+ instance_eval(&block) if block_given?
23
+ end
23
24
 
24
- self.name = config_name unless config_name.nil?
25
+ def host(new_host = nil)
26
+ unless new_host.nil?
27
+ match = /(?<host>.*?)(?=:|\z)(:(?<port>\d+))?/.match(new_host.to_s)
28
+ @host = match[:host].to_s unless match[:host].nil?
25
29
 
26
- # Actually open the connection
27
- connect
30
+ # Set the port information
31
+ unless match[:port].nil?
32
+ @port = match[:port].to_i
33
+ end
34
+
35
+ # Has port is true; unless a socket was set
36
+ @has_port = !@has_socket
37
+ end
38
+
39
+ # return the host
40
+ @host
41
+ end
42
+
43
+ def port
44
+ @port
28
45
  end
29
46
 
30
- def configuration=(new_config)
31
- @configuration = new_config if new_config.instance_of? WordpressDeploy::Wordpress::Configuration
47
+ def user(new_user = nil)
48
+ @user = new_user.to_s unless new_user.nil?
49
+ @user
50
+ end
51
+
52
+ def password(new_pass = nil)
53
+ @password = new_pass.to_s unless new_pass.nil?
54
+ @password
55
+ end
56
+
57
+ def destination(new_dest = nil)
58
+ @destination = new_dest.to_s unless new_dest.nil?
59
+ @destination
32
60
  end
33
61
 
34
62
  ##
@@ -49,10 +77,13 @@ module WordpressDeploy
49
77
  ##
50
78
  #
51
79
  def transmit!
52
- files = Dir.glob(File.join(Environment.wp_dir, "**/*")).sort
80
+ connect
81
+ files = Dir.glob(File.join(Config.wp_dir, "**/*")).sort
53
82
  files.each do |file|
54
83
  put_file file
55
84
  end
85
+ ensure
86
+ close
56
87
  end
57
88
 
58
89
  def receive
@@ -82,7 +113,7 @@ module WordpressDeploy
82
113
  files.each do |file|
83
114
 
84
115
  # Build the file name to save it to
85
- local_file = Pathname.new(File.join(Environment.wp_dir, rel_remote_path, File.basename(file))).cleanpath.to_s
116
+ local_file = Pathname.new(File.join(Config.wp_dir, rel_remote_path, File.basename(file))).cleanpath.to_s
86
117
  if directory? file
87
118
  Logger.debug "[mkdir] #{local_file}"
88
119
  FileUtils.mkdir_p local_file
@@ -99,101 +130,6 @@ module WordpressDeploy
99
130
 
100
131
  end
101
132
 
102
- ##
103
- # Return the configuration's name.
104
- #
105
- # Defaults to the first configuration name.
106
- def name
107
- @name ||= available_names.first
108
- @name
109
- end
110
-
111
- ##
112
- # Set the configuration's name.
113
- #
114
- # Only performs the assignment if the proposed name is
115
- # an available name.
116
- #
117
- # Returns the Configuration's name.
118
- def name=(new_name)
119
- @name = new_name if name? new_name
120
- @name
121
- end
122
-
123
- ##
124
- # Test if the name passed in is an available configuration
125
- # name.
126
- def name?(name_to_check)
127
- available_names.include? name_to_check
128
- end
129
-
130
- def local_path
131
- Environment.wp_dir
132
- end
133
-
134
- def remote_path
135
- ftp_dir = send(:FTP_DIR)
136
- return "/" if ftp_dir.nil?
137
- ftp_dir
138
- end
139
-
140
- def username
141
- send(:FTP_USER) || ""
142
- end
143
-
144
- def password
145
- send(:FTP_PASSWORD) || ""
146
- end
147
-
148
- def port
149
- port = 21
150
- match = /:(?<port>\d+)$/.match(send(:FTP_HOST))
151
- port = match[:port].to_i unless match.nil?
152
- port
153
- end
154
-
155
- ##
156
- # Does FTP_HOST contain a port number?
157
- def port?
158
- !(send(:FTP_HOST) =~ /:(?<port>\d+)$/).nil?
159
- end
160
-
161
- ##
162
- # Get just the hostname from DB_HOST. Only different from
163
- # FTP_HOST if FTP_HOST has a port number in it.
164
- def host
165
- host = "localhost"
166
- match = /(?<host>.*?)(?=:|$)/.match(send(:FTP_HOST))
167
- host = match[:host].to_s unless match.nil?
168
- host
169
- end
170
-
171
- FTP_CONFIGURATION_ATTRIBUTES = [:FTP_DIR, :FTP_USER, :FTP_PASSWORD,
172
- :FTP_HOST]
173
-
174
- ##
175
- # Define the behaviours of the default parameters quickly
176
- def method_missing(meth, *args, &block)
177
- # Convert the method to a symbol
178
- method_symbol = meth.to_sym
179
-
180
- if FTP_CONFIGURATION_ATTRIBUTES.include? method_symbol
181
- config = @yaml[name]
182
- return config[meth.to_s] if config.include? meth.to_s
183
- else
184
- # You *must* call super if you don't handle the method, otherwise
185
- # you will mess up Ruby's method lookup.
186
- super
187
- end
188
- end
189
-
190
- ##
191
- # Define respond_to?
192
- def respond_to?(method)
193
- return true if FTP_CONFIGURATION_ATTRIBUTES.include? method.to_sym
194
- super
195
- end
196
-
197
133
  private
198
134
 
199
135
  def ftp
@@ -205,22 +141,22 @@ module WordpressDeploy
205
141
  # Establish a connection to the remote server
206
142
  def connect
207
143
  ftp.connect(host, port)
208
- ftp.login(username, password)
144
+ ftp.login(user, password)
209
145
  ftp.passive = true
210
146
  # ftp.debug_mode = true
211
- #chdir(remote_path)
147
+ #chdir(destination)
212
148
  end
213
149
 
214
150
  ##
215
151
  # Put file on remote machine
216
152
  def put_file(real_path)
217
153
  pn = Pathname.new(real_path)
218
- relative = pn.relative_path_from Pathname.new(Environment.wp_dir)
154
+ relative = pn.relative_path_from Pathname.new(Config.wp_dir)
219
155
 
220
156
  # Only try to send files; no directories
221
157
  unless pn.directory?
222
158
  local_directory, local_file_name = relative.split
223
- remote_directory = Pathname.new("#{remote_path}/#{local_directory}").cleanpath.to_s
159
+ remote_directory = Pathname.new("#{destination}/#{local_directory}").cleanpath.to_s
224
160
 
225
161
  begin
226
162
  # Make sure to be in the right directory