Skip to content

Commit

Permalink
Print some more helpful debug messages
Browse files Browse the repository at this point in the history
See #46.
  • Loading branch information
swissspidy committed Jul 25, 2018
1 parent bd16b5f commit 9d65089
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 14 deletions.
100 changes: 99 additions & 1 deletion features/makepot.feature
Expand Up @@ -135,7 +135,7 @@ Feature: Generate a POT file of a WordPress plugin
"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot --domain=bar`
And the foo-plugin.pot file should contain:
Then the foo-plugin.pot file should contain:
"""
msgid "Foo"
"""
Expand Down Expand Up @@ -1381,3 +1381,101 @@ Feature: Generate a POT file of a WordPress plugin
"""
msgid "Hello JS"
"""

Scenario: Prints helpful debug messages for plugin
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
* Plugin URI: https://example.com
* Description:
* Version: 0.1.0
* Author:
* Author URI:
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: foo-plugin
* Domain Path: /languages
*/
__( 'Hello World', 'foo-plugin' );
"""

When I run `wp i18n make-pot foo-plugin --debug`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should contain:
"""
Extracting all strings with text domain "foo-plugin"
"""
And STDERR should contain:
"""
Plugin file:
"""
And STDERR should contain:
"""
foo-plugin/foo-plugin.php
"""
And STDERR should contain:
"""
Destination:
"""
And STDERR should contain:
"""
foo-plugin/languages/foo-plugin.pot
"""
And STDERR should contain:
"""
Extracted 3 strings
"""
Scenario: Prints helpful debug messages for theme
Given an empty foo-theme directory
And a foo-theme/style.css file:
"""
/*
Theme Name: Foo Theme
Theme URI: https://example.com
Description:
Author:
Author URI:
Version: 0.1.0
License: GPL-2.0+
Text Domain: foo-theme
Domain Path: /languages
*/
"""

When I run `wp i18n make-pot foo-theme --debug`
Then STDOUT should be:
"""
Theme stylesheet detected.
Success: POT file successfully generated!
"""
And STDERR should contain:
"""
Extracting all strings with text domain "foo-theme"
"""
And STDERR should contain:
"""
Theme stylesheet:
"""
And STDERR should contain:
"""
foobar/style.css
"""
And STDERR should contain:
"""
Destination:
"""
And STDERR should contain:
"""
foobar/languages/foobar.pot
"""
And STDERR should contain:
"""
Extracted 2 strings
"""
58 changes: 45 additions & 13 deletions src/MakePotCommand.php
Expand Up @@ -116,6 +116,10 @@ public function __invoke( $args, $assoc_args ) {

$file_data = $this->get_main_file_data();

if ( $ignore_domain ) {
WP_CLI::debug( 'Extracting all strings regardless of text domain', 'make-pot' );
}

if ( ! $ignore_domain ) {
$this->domain = $this->slug;

Expand All @@ -124,38 +128,50 @@ public function __invoke( $args, $assoc_args ) {
}

$this->domain = Utils\get_flag_value( $assoc_args, 'domain', $this->domain );

WP_CLI::debug( sprintf( 'Extracting all strings with text domain "%s"', $this->domain ), 'make-pot' );
}

// Determine destination.
$this->destination = "{$this->source}/{$this->slug}.pot";

if ( ! empty( $file_data['Domain Path'] ) ) {
// Domain Path inside source folder.
$this->destination = sprintf( '%s/%s/%s.pot', $this->source, $file_data['Domain Path'], $this->slug );
$this->destination = sprintf(
'%s/%s/%s.pot',
$this->source,
$this->unslashit( $file_data['Domain Path'] ),
$this->slug
);
}

if ( isset( $args[1] ) ) {
$this->destination = $args[1];
}

WP_CLI::debug( sprintf( 'Destination: %s', $this->destination ), 'make-pot' );

// Two is_dir() checks in case of a race condition.
if ( ! is_dir( dirname( $this->destination ) ) && ! mkdir( dirname( $this->destination ) ) && ! is_dir( dirname( $this->destination ) ) ) {
if ( ! is_dir( dirname( $this->destination ) ) &&
! mkdir( dirname( $this->destination ), 0777, true ) &&
! is_dir( dirname( $this->destination ) )
) {
WP_CLI::error( 'Could not create destination directory!' );
}

if ( isset( $assoc_args['merge'] ) ) {
if ( true === $assoc_args['merge'] && file_exists( $this->destination ) ) {
if ( true === $assoc_args['merge'] ) {
$this->merge = $this->destination;
} elseif ( ! empty( $assoc_args['merge'] ) ) {
if ( ! file_exists( $assoc_args['merge'] ) ) {
WP_CLI::error( sprintf( 'Invalid file %s', $assoc_args['merge'] ) );
}

$this->merge = $assoc_args['merge'];
}
}

WP_CLI::debug( sprintf( 'Destination: %s', $this->destination ), 'make-pot' );
if ( isset( $this->merge ) && ! file_exists( $this->merge ) ) {
WP_CLI::warning( sprintf( 'POT file to merge with does not exist: %s', $this->merge ) );

unset( $this->merge );
}
}

if ( isset( $assoc_args['exclude'] ) ) {
$this->exclude = array_filter( array_merge( $this->exclude, explode( ',', $assoc_args['exclude'] ) ) );
Expand All @@ -173,7 +189,7 @@ public function __invoke( $args, $assoc_args ) {
/**
* Removes leading and trailing slashes of a string.
*
* @param string $string What to add the remove slashes from.
* @param string $string What to add and remove slashes from.
* @return string String without leading and trailing slashes.
*/
protected function unslashit( $string ) {
Expand All @@ -186,12 +202,15 @@ protected function unslashit( $string ) {
* @return void
*/
protected function retrieve_main_file_data() {
if ( is_file( "$this->source/style.css" ) && is_readable( "$this->source/style.css" ) ) {
$theme_data = static::get_file_data( "$this->source/style.css", array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) );
$stylesheet = sprintf( '%s/style.css', $this->source );

if ( is_file( $stylesheet ) && is_readable( $stylesheet ) ) {
$theme_data = static::get_file_data( $stylesheet, array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) );

// Stop when it contains a valid Theme Name header.
if ( ! empty( $theme_data['Theme Name'] ) ) {
WP_CLI::log( 'Theme stylesheet detected.' );
WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $stylesheet ), 'make-pot' );

$this->main_file_data = $theme_data;

Expand All @@ -205,6 +224,7 @@ protected function retrieve_main_file_data() {

/** @var DirectoryIterator $file */
foreach ( $files as $file ) {
WP_CLI::debug($file->getRealPath());
if ( $file->isFile() && $file->isReadable() && 'php' === $file->getExtension()) {
$plugin_files[] = $file->getRealPath();
}
Expand Down Expand Up @@ -283,6 +303,8 @@ protected function makepot() {

// Add existing strings first but don't keep headers.
if ( $this->merge ) {
WP_CLI::debug( sprintf( 'Mergining with existing POT file: %s', $this->merge ), 'make-pot' );

$existing_translations = new Translations();
Po::fromFile( $this->merge, $existing_translations );
$this->translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE );
Expand Down Expand Up @@ -358,7 +380,17 @@ protected function makepot() {
}
}

return PotGenerator::toFile( $this->translations, $this->destination );
$result = PotGenerator::toFile( $this->translations, $this->destination );

$translations_count = count( $this->translations );

if ( 1 === $translations_count ) {
WP_CLI::debug( sprintf( 'Extracted %d string', count( $this->translations ) ), 'make-pot' );
} else {
WP_CLI::debug( sprintf( 'Extracted %d strings', count( $this->translations ) ), 'make-pot' );
}

return $result;
}

/**
Expand Down

0 comments on commit 9d65089

Please sign in to comment.