Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
miya0001 committed Oct 14, 2016
2 parents 714661e + 9624d70 commit f94e187
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 95 deletions.
105 changes: 71 additions & 34 deletions cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
return;
}

use WP_CLI\Utils;

/**
* Generate a Movefile for Wordmove.
*
Expand All @@ -29,14 +31,8 @@ class WP_CLI_Scaffold_Movefile extends WP_CLI_Command
*
* ## OPTIONS
*
* [--environment=<environment>]
* : The environment such as local, production, staging, etc.
* ---
* default: production
* ---
*
* [--movefile=<movefile>]
* : Path to the Movefile.
* [--force]
* : Overwrite Movefile that already exist.
*
* ## EXAMPLES
*
Expand All @@ -47,38 +43,79 @@ class WP_CLI_Scaffold_Movefile extends WP_CLI_Command
* wp scaffold movefile --movefile=/path/to/Movefile
*
*/
function __invoke( $args, $assoc_args ) {
$yaml = spyc_load_file( dirname( __FILE__ ) . '/templates/Movefile' );
unset( $yaml['production']['ssh'] );
function __invoke( $args, $assoc_args )
{
$vars = array(
'site_url' => site_url(),
'wordpress_path' => WP_CLI::get_runner()->config['path'],
'db_name' => DB_NAME,
'db_user' => DB_USER,
'db_pass' => DB_PASSWORD,
'db_host' => DB_HOST,
'db_charset' => DB_CHARSET,
);

if ( ! empty( $assoc_args['movefile'] ) ) {
if ( file_exists( realpath( $assoc_args['movefile'] ) ) ) {
$yaml = spyc_load_file( $assoc_args['movefile'] );
}
$movefile = Utils\mustache_render(
dirname( __FILE__ ) . '/templates/Movefile.mustache',
$vars
);

if ( empty( $args[0] ) ) {
$filename = getcwd() . "/Movefile";
} else {
$filename = $args[0];
}

$yaml[ $assoc_args['environment'] ]['vhost'] = site_url();
$yaml[ $assoc_args['environment'] ]['wordpress_path'] = WP_CLI::get_runner()->config['path'];
$yaml[ $assoc_args['environment'] ]['database']['name'] = DB_NAME;
$yaml[ $assoc_args['environment'] ]['database']['user'] = DB_USER;
$yaml[ $assoc_args['environment'] ]['database']['password'] = DB_PASSWORD;
$yaml[ $assoc_args['environment'] ]['database']['host'] = DB_HOST;
$yaml[ $assoc_args['environment'] ]['database']['charset'] = DB_CHARSET;
if ( 'local' !== $assoc_args['environment'] ) {
if ( empty( $yaml[ $assoc_args['environment'] ]['exclude'] ) ) {
$yaml[ $assoc_args['environment'] ]['exclude'] = $this->exclude;
}
if ( empty( $yaml[ $assoc_args['environment'] ]['ssh'] ) ) {
$yaml[ $assoc_args['environment'] ]['ssh'] = array(
'host' => preg_replace( "#https?://#", "", home_url() ),
'user' => exec( 'whoami' )
);
$force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force' );
$result = $this->create_file( $filename, $movefile, $force );

if ( $result ) {
WP_CLI::success( $filename );
} else {
WP_CLI::success( "Movefile wasn't overwrited." );
}
}

private function create_file( $filename, $contents, $force )
{
$wp_filesystem = $this->init_wp_filesystem();

$should_write_file = $this->prompt_if_files_will_be_overwritten( $filename, $force );
if ( $should_write_file ) {
$wp_filesystem->mkdir( dirname( $filename ) );
if ( ! $wp_filesystem->put_contents( $filename, $contents ) ) {
WP_CLI::error( "Error creating file: $filename" );
}
return true;
}

return false;
}

/**
* Initialize WP Filesystem
*/
private function init_wp_filesystem()
{
global $wp_filesystem;
WP_Filesystem();
return $wp_filesystem;
}

private function prompt_if_files_will_be_overwritten( $filename, $force )
{
$should_write_file = false;
if ( ! file_exists( $filename ) ) {
return true;
}
WP_CLI::warning( 'File already exists.' );
if ( $force ) {
$should_write_file = true;
} else {
$should_write_file = cli\confirm( 'Do you want to overwrite', false );
}

$yaml = Spyc::YAMLDump( $yaml, 2, 0 );
$yaml = substr( $yaml, 4 ); // Spyc::YAMLDump() prepends "---\n" :(
WP_CLI::line( $yaml );
return $should_write_file;
}
}

Expand Down
105 changes: 51 additions & 54 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions templates/Movefile → templates/Movefile.mustache
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local:
vhost: "http://vccw.dev"
wordpress_path: "/var/www/wordpress" # use an absolute path here
vhost: "{{ site_url }}"
wordpress_path: "{{ wordpress_path }}" # use an absolute path here

database:
name: "wordpress"
user: "wordpress"
password: "wordpress"
host: "localhost"
charset: "utf8"
name: "{{ db_name }}"
user: "{{ db_user }}"
password: "{{ db_pass }}"
host: "{{ db_host }}"
charset: "{{ db_charset }}"

# paths: # you can customize wordpress internal paths
# wp_content: "wp-content"
Expand Down

0 comments on commit f94e187

Please sign in to comment.