Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make-mo: Add destination file support #373

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ wp i18n make-mo <source> [<destination>]
Path to an existing PO file or a directory containing multiple PO files.

[<destination>]
Path to the destination directory for the resulting MO files. Defaults to the source directory.
Path to the destination file or directory for the resulting MO files. Defaults to the source directory.

**EXAMPLES**

Expand All @@ -222,6 +222,9 @@ wp i18n make-mo <source> [<destination>]
# Create a MO file from a single PO file in a specific directory.
$ wp i18n make-mo example-plugin-de_DE.po languages

# Create a MO file from a single PO file to a specific file destination
$ wp i18n make-mo example-plugin-de_DE.po languages/bar.mo



### wp i18n update-po
Expand Down
46 changes: 44 additions & 2 deletions features/makemo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ Feature: Generate MO files from PO files
Error: Source file or directory does not exist!
"""
And the return code should be 1

Scenario: Bail for destination being a file when source is a folder
Given an empty foo directory
And a foo/foo.po file:
"""
"""
When I try `wp i18n make-mo foo test.mo `
Then STDERR should contain:
"""
Error: Destination file not supported when source is a directory!
"""
And the return code should be 1
Scenario: Uses source folder as destination by default
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin-de_DE.po file:
Expand Down Expand Up @@ -44,7 +54,39 @@ Feature: Generate MO files from PO files
"""
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.mo file should exist

Scenario: Uses the provided destination file name
Given a foo.po file:
"""
"""
When I run `wp i18n make-mo foo.po bar.mo`
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the bar.mo file should exist
Scenario: Uses the provided destination file name with no extension
Given a foo.po file:
"""
"""
When I run `wp i18n make-mo foo.po bar`
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the bar file should exist
Scenario: Preserves the provided source name with no destination
Given a foo.po file:
"""
"""
When I run `wp i18n make-mo foo.po`
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the foo.mo file should exist
Scenario: Allows setting custom destination directory
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin-de_DE.po file:
Expand Down
27 changes: 22 additions & 5 deletions src/MakeMoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MakeMoCommand extends WP_CLI_Command {
* : Path to an existing PO file or a directory containing multiple PO files.
*
* [<destination>]
* : Path to the destination directory for the resulting MO files. Defaults to the source directory.
* : Path to the destination file or directory for the resulting MO files. Defaults to the source directory.
*
* ## EXAMPLES
*
Expand All @@ -30,6 +30,9 @@ class MakeMoCommand extends WP_CLI_Command {
* # Create a MO file from a single PO file in a specific directory.
* $ wp i18n make-mo example-plugin-de_DE.po languages
*
* # Create a MO file from a single PO file to a specific file destination
* $ wp i18n make-mo example-plugin-de_DE.po languages/bar.mo
*
* @when before_wp_load
*
* @throws WP_CLI\ExitException
Expand All @@ -40,9 +43,19 @@ public function __invoke( $args, $assoc_args ) {
WP_CLI::error( 'Source file or directory does not exist!' );
}

$destination = is_file( $source ) ? dirname( $source ) : $source;
$destination = is_file( $source ) ? dirname( $source ) : $source;
$custom_file_name = null;
if ( isset( $args[1] ) ) {
$destination = $args[1];
$destination = $args[1];
$destination_pathinfo = pathinfo( $destination );
// Destination is a file, make sure source is also a file
if ( ! empty( $destination_pathinfo['filename'] ) && ! empty( $destination_pathinfo['extension'] ) ) {
if ( ! is_file( $source ) ) {
WP_CLI::error( 'Destination file not supported when source is a directory!' );
}
$destination = $destination_pathinfo['dirname'];
$custom_file_name = $destination_pathinfo['filename'] . '.' . $destination_pathinfo['extension'];
}
}

// Two is_dir() checks in case of a race condition.
Expand Down Expand Up @@ -71,8 +84,12 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

$file_basename = basename( $file->getFilename(), '.po' );
$destination_file = "{$destination}/{$file_basename}.mo";
$file_basename = basename( $file->getFilename(), '.po' );
$file_name = $file_basename . '.mo';
if ( $custom_file_name ) {
$file_name = $custom_file_name;
}
$destination_file = "{$destination}/{$file_name}";

$translations = Translations::fromPoFile( $file->getPathname() );
if ( ! $translations->toMoFile( $destination_file ) ) {
Expand Down