Skip to content

Commit abe3b2b

Browse files
authored
Merge pull request #173 from wp-cli/copilot/add-format-argument-core-install
Add `--format` flag to language core install command
2 parents ab6cb0d + 9f5f438 commit abe3b2b

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

features/language-core.feature

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,50 @@ Feature: Manage core translation files for a WordPress install
476476
| en_US | active |
477477
| nl_NL | installed |
478478
And STDERR should be empty
479+
480+
@require-wp-4.0
481+
Scenario: Install languages with different output formats
482+
Given a WP install
483+
And an empty cache
484+
485+
When I run `wp language core install de_DE nl_NL --format=csv`
486+
Then the return code should be 0
487+
And STDOUT should be:
488+
"""
489+
locale,status
490+
de_DE,installed
491+
nl_NL,installed
492+
"""
493+
And STDERR should be empty
494+
495+
When I run `wp language core install de_DE nl_NL --format=summary`
496+
Then the return code should be 0
497+
And STDOUT should contain:
498+
"""
499+
Success: Installed 0 of 2 languages (2 skipped).
500+
"""
501+
And STDERR should be empty
502+
503+
When I run `wp language core install de_DE nl_NL invalid_lang --format=json`
504+
Then the return code should be 0
505+
And STDOUT should be JSON containing:
506+
"""
507+
[{"locale":"de_DE","status":"already installed"},{"locale":"nl_NL","status":"already installed"},{"locale":"invalid_lang","status":"not available"}]
508+
"""
509+
And STDERR should be empty
510+
511+
When I run `wp language core install fr_FR --format=table`
512+
Then the return code should be 0
513+
And STDOUT should contain:
514+
"""
515+
locale
516+
"""
517+
And STDOUT should contain:
518+
"""
519+
fr_FR
520+
"""
521+
And STDOUT should contain:
522+
"""
523+
installed
524+
"""
525+
And STDERR should be empty

src/Core_Language_Command.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ public function is_installed( $args ) {
183183
* [--activate]
184184
* : If set, the language will be activated immediately after install.
185185
*
186+
* [--format=<format>]
187+
* : Render output in a particular format (for one or more languages).
188+
* ---
189+
* options:
190+
* - table
191+
* - csv
192+
* - json
193+
* - summary
194+
* ---
195+
*
186196
* ## EXAMPLES
187197
*
188198
* # Install the Brazilian Portuguese language.
@@ -198,7 +208,7 @@ public function is_installed( $args ) {
198208
* @subcommand install
199209
*
200210
* @param string[] $args Positional arguments.
201-
* @param array{activate?: bool} $assoc_args Associative arguments.
211+
* @param array{activate?: bool, format?: string} $assoc_args Associative arguments.
202212
*/
203213
public function install( $args, $assoc_args ) {
204214
$language_codes = (array) $args;
@@ -208,15 +218,36 @@ public function install( $args, $assoc_args ) {
208218
WP_CLI::error( 'Only a single language can be active.' );
209219
}
210220

221+
$has_format_flag = isset( $assoc_args['format'] );
222+
223+
if ( $has_format_flag ) {
224+
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
225+
$logger = new \WP_CLI\Loggers\Quiet();
226+
\WP_CLI::set_logger( $logger );
227+
}
228+
}
229+
211230
$available = $this->get_installed_languages();
212231

232+
$results = array();
233+
213234
$successes = 0;
214235
$errors = 0;
215236
$skips = 0;
216237
foreach ( $language_codes as $language_code ) {
238+
$result = [];
239+
240+
if ( $has_format_flag ) {
241+
$result = [
242+
'locale' => $language_code,
243+
];
244+
}
217245

218246
if ( in_array( $language_code, $available, true ) ) {
219247
\WP_CLI::log( "Language '{$language_code}' already installed." );
248+
if ( $has_format_flag ) {
249+
$result['status'] = 'already installed';
250+
}
220251
++$skips;
221252
} else {
222253
$response = $this->download_language_pack( $language_code );
@@ -227,19 +258,38 @@ public function install( $args, $assoc_args ) {
227258

228259
// Skip if translation is not yet available.
229260
if ( 'not_found' === $response->get_error_code() ) {
261+
if ( $has_format_flag ) {
262+
$result['status'] = 'not available';
263+
}
230264
++$skips;
231265
} else {
266+
if ( $has_format_flag ) {
267+
$result['status'] = 'not installed';
268+
}
232269
++$errors;
233270
}
234271
} else {
235272
\WP_CLI::log( "Language '{$language_code}' installed." );
273+
if ( $has_format_flag ) {
274+
$result['status'] = 'installed';
275+
}
236276
++$successes;
237277
}
238278
}
239279

240280
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) {
241281
$this->activate_language( $language_code );
242282
}
283+
284+
if ( $has_format_flag ) {
285+
$results[] = (object) $result;
286+
}
287+
}
288+
289+
if ( $has_format_flag ) {
290+
if ( 'summary' !== $assoc_args['format'] ) {
291+
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'locale', 'status' ) );
292+
}
243293
}
244294

245295
\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );

0 commit comments

Comments
 (0)