Skip to content

Commit

Permalink
Re-implement #597 in this branch
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-fazzi committed Nov 15, 2021
1 parent 6627543 commit e2c13c0
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 41 deletions.
1 change: 1 addition & 0 deletions lib/wordmove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require 'wordmove/wordpress_directory'
require 'wordmove/version'
require 'wordmove/environments_list'
require 'wordmove/wpcli'

require 'wordmove/generators/movefile_adapter'
require 'wordmove/generators/movefile'
Expand Down
24 changes: 4 additions & 20 deletions lib/wordmove/actions/ssh/wpcli_adapter/adapt_local_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module WpcliAdapter
class AdaptLocalDb
extend ::LightService::Action
include Wordmove::Actions::Helpers
include Wordmove::Wpcli

expects :local_options,
:remote_options,
Expand All @@ -31,7 +32,7 @@ class AdaptLocalDb
)
)

unless context.cli_options[:no_adapt]
if !context.cli_options[:no_adapt]
Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
Expand All @@ -43,6 +44,8 @@ class AdaptLocalDb
logger: context.logger,
command: wpcli_search_replace_command(context, :wordpress_path)
)
else
context.logger.warn 'Skipping DB adapt'
end

Wordmove::Actions::RunLocalCommand.execute(
Expand All @@ -69,25 +72,6 @@ class AdaptLocalDb
)
)
end

# This should be extracted into a concern
def self.wpcli_search_replace_command(context, config_key)
[
'wp search-replace',
"--path=#{context.local_options[:wordpress_path]}",
context.remote_options[config_key],
context.local_options[config_key],
'--quiet',
'--skip-columns=guid',
'--all-tables',
'--allow-root'
].join(' ')
end

# This should be extracted into a concern
def self.wp_in_path?
system('which wp > /dev/null 2>&1')
end
end
end
end
Expand Down
22 changes: 1 addition & 21 deletions lib/wordmove/actions/ssh/wpcli_adapter/adapt_remote_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module WpcliAdapter
class AdaptRemoteDb
extend ::LightService::Action
include Wordmove::Actions::Helpers
include Wordmove::Wpcli

expects :local_options,
:remote_options,
Expand Down Expand Up @@ -56,27 +57,6 @@ class AdaptRemoteDb
command: wpcli_search_replace_command(context, :wordpress_path)
)
end

# This should be extracted into a concern
def self.wpcli_search_replace_command(context, config_key)
wordpress_path = context.local_options[:wordpress_path]

[
'wp search-replace',
"--path=#{wordpress_path}",
context.remote_options[config_key],
context.local_options[config_key],
'--quiet',
'--skip-columns=guid',
'--all-tables',
'--allow-root'
].join(' ')
end

# This should be extracted into a concern
def self.wp_in_path?
system('which wp > /dev/null 2>&1')
end
end
end
end
Expand Down
45 changes: 45 additions & 0 deletions lib/wordmove/wpcli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Wordmove
module Wpcli
extend ActiveSupport::Concern

included do
private_class_method :load_from_wpcli, :load_from_yml
end

class_methods do # rubocop:disable Metrics/BlockLength
def wp_in_path?
system('which wp > /dev/null 2>&1')
end

def wpcli_search_replace_command(context, config_key)
[
'wp search-replace',
"--path=#{wpcli_config_path(context)}",
context.remote_options[config_key],
context.local_options[config_key],
'--quiet',
'--skip-columns=guid',
'--all-tables',
'--allow-root'
].join(' ')
end

def wpcli_config_path(context)
load_from_yml(context) || load_from_wpcli || context.local_options[:wordpress_path]
end

def load_from_yml(context)
yml_path = File.join(context.local_options[:wordpress_path], 'wp-cli.yml')

return unless File.exist?(yml_path)

YAML.load_file(yml_path).with_indifferent_access['path']
end

def load_from_wpcli
wpcli_config = JSON.parse(`wp cli param-dump --with-values`, symbolize_names: true)
wpcli_config.dig(:path, :current)
end
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/wp-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
path: /path/to/steak
8 changes: 8 additions & 0 deletions spec/support/fixture_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def fixture_root_relative_path_for(filename)
def movefile_path_for(filename)
fixture_root_relative_path_for("movefiles/#{filename}")
end

def fixture_folder_path
File.join(__dir__, '..', 'fixtures')
end

def fixture_folder_root_relative_path
File.join('spec', 'fixtures')
end
end

RSpec.configure do |config|
Expand Down
47 changes: 47 additions & 0 deletions spec/wpcli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe Wordmove::Wpcli do
subject do
Class.new do
include Wordmove::Wpcli
end
end

let(:a_context) do
OrganizerContextFactory.make_for(Wordmove::Actions::Ssh::WpcliAdapter::AdaptLocalDb, :pull)
end

context '.wpcli_config_path' do
context 'when having wp-cli.yml in wordpress root directory' do
it 'returns the path configured in YAML file' do
a_context[:local_options][:wordpress_path] = fixture_folder_root_relative_path

expect(subject.wpcli_config_path(a_context)).to eq('/path/to/steak')
end
end

context 'when there is not wp-cli.yml in wordpress root directory' do
context 'if wp-cli is configured someway with a custom path' do
before do
allow(subject)
.to receive(:`)
.with('wp cli param-dump --with-values')
.and_return("{\"path\":{\"current\":\"\/path\/to\/pudding\"}}")
end

it 'returns the configured path' do
expect(subject.wpcli_config_path(a_context)).to eq('/path/to/pudding')
end
end
end
end

context '.wpcli_search_replace_command' do
it 'returns the expected command' do
a_context[:local_options][:wordpress_path] = fixture_folder_root_relative_path
expect(subject.wpcli_search_replace_command(a_context, :wordpress_path))
.to eq('wp search-replace --path=/path/to/steak /var/www/your_site spec/fixtures --quiet '\
'--skip-columns=guid --all-tables --allow-root')
end
end
end

0 comments on commit e2c13c0

Please sign in to comment.