Skip to content

Commit

Permalink
Add destination file support to MakePoCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
sdnunca committed Nov 16, 2023
1 parent e84691f commit dc14056
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
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
22 changes: 20 additions & 2 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 @@ -41,10 +44,21 @@ public function __invoke( $args, $assoc_args ) {
}

$destination = is_file( $source ) ? dirname( $source ) : $source;

Check warning on line 46 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
$custom_file_name = null;
if ( isset( $args[1] ) ) {
$destination = $args[1];

Check warning on line 49 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space
$destionation_pathinfo = pathinfo( $destination );
// Destination is a file, make sure source is also a file
if( ! empty( $destionation_pathinfo['filename'] ) ) {

Check failure on line 52 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Space after opening control structure is required

Check failure on line 52 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

No space before opening parenthesis is prohibited

Check failure on line 52 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Expected 1 space(s) after IF keyword; 0 found
if( !is_file( $source ) ) {

Check failure on line 53 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Space after opening control structure is required

Check failure on line 53 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

No space before opening parenthesis is prohibited

Check failure on line 53 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Expected 1 space(s) after IF keyword; 0 found

Check failure on line 53 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Expected 1 space after "!"; 0 found
WP_CLI::error( 'Destination file not supported when source is a directory!' );
}
$destination = $destionation_pathinfo['dirname'];

Check warning on line 56 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
$custom_file_name = $destionation_pathinfo['filename'] . '.' . $destionation_pathinfo['extension'] ;

Check failure on line 57 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Space found before semicolon; expected "];" but found "] ;"
}

Check failure on line 58 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Tabs must be used to indent lines; spaces are not allowed
}

Check failure on line 60 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Functions must not contain multiple empty lines in a row; found 2 empty lines

// Two is_dir() checks in case of a race condition.
if ( ! is_dir( $destination )
&& ! mkdir( $destination, 0777, true )
Expand Down Expand Up @@ -72,7 +86,11 @@ public function __invoke( $args, $assoc_args ) {
}

$file_basename = basename( $file->getFilename(), '.po' );

Check warning on line 88 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces
$destination_file = "{$destination}/{$file_basename}.mo";
$file_name = $file_basename . '.mo';

Check warning on line 89 in src/MakeMoCommand.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
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
74 changes: 74 additions & 0 deletions tests/MakeMoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace WP_CLI\I18n\Tests;

use Gettext\Translation;
use WP_CLI\I18n\PotGenerator;
use Gettext\Translations;
use WP_CLI\Tests\TestCase;
use WP_CLI\I18n\MakeMoCommand;
use WP_CLI\Utils;

class MakeMoTest extends TestCase {
public function set_up() {
parent::set_up();
$this->temp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-make-po-', true ) . '/';
mkdir( $this->temp_dir );
}

public function tear_down() {
rmdir( $this->temp_dir );

parent::tear_down();
}

/**
* Test wp i18n make-mo foo.po
*/
public function test_single_file_generation() {
$file_path = $this->temp_dir . '/test.po';
touch( $file_path );
$make_mo = new MakeMoCommand();
$make_mo( array(
$file_path,
), array() );
$this->assertTrue( is_file( $this->temp_dir . '/test.mo' ), 'MO file not generated' );
unlink( $file_path );
unlink( $this->temp_dir . '/test.mo' );
}

/**
* Test wp i18n make-mo foo.po bar.mo
*/
public function test_single_file_generation_named() {
$file_path = $this->temp_dir . '/test.po';
$mo_file_path = $this->temp_dir . '/bar.mo';
touch( $file_path );
$make_mo = new MakeMoCommand();
$make_mo( array(
$file_path,
$mo_file_path,
), array() );
$this->assertTrue( is_file( $mo_file_path ), 'MO file not generated' );
unlink( $file_path );
unlink( $mo_file_path );
}

/**
* Test wp i18n make-mo foo/
*/
public function test_whole_directory_generation() {
$base_directory = $this->temp_dir . '/foo';
mkdir( $base_directory, 0777, true );
$file_path = $base_directory . '/test.po';
touch( $file_path );
$make_mo = new MakeMoCommand();
$make_mo( array(
$base_directory,
), array() );
$this->assertTrue( is_file( $base_directory . '/test.mo' ), 'MO file not generated' );
unlink( $file_path );
unlink( $base_directory . '/test.mo' );
rmdir( $base_directory );
}
}

0 comments on commit dc14056

Please sign in to comment.