Skip to content

Commit 9a04152

Browse files
committed
Adds support for composer.json directives
1 parent f846311 commit 9a04152

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ of your root `composer.json`.
4040
"extra": {
4141
"drupal-l10n": {
4242
"destination": "translations/contrib",
43+
"core-only": true,
4344
"languages": [
4445
"fr",
4546
"es"
@@ -55,6 +56,9 @@ translation files. By default the destination is
5556

5657
The `languages` parameter specify the languages you want to retrieve.
5758

59+
The `core-only` parameter (default `false`) limits downloads to Drupal core
60+
translations only, and skips contrib modules/themes/profiles.
61+
5862
## Drupal configuration
5963

6064
You can say to Drupal to not download translations files by updating your

src/Handler.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ public function downloadLocalization($dev = TRUE, array $packages = [], $coreOnl
150150

151151
$webroot = realpath($this->getWebRoot());
152152

153+
// Collect options.
154+
$options = $this->getOptions();
155+
156+
// The command line option takes precedence, but the composer.json setting
157+
// can enable this mode as well.
158+
$coreOnly = $coreOnly || !empty($options['core-only']);
159+
153160
// Prepare a list of Drupal project to download the translations.
154161
$drupal_projects = [];
155162
if (empty($packages)) {
@@ -181,9 +188,6 @@ public function downloadLocalization($dev = TRUE, array $packages = [], $coreOnl
181188
// Get the Drupal core version.
182189
$core_version = $this->getDrupalCoreVersion($drupal_core_package);
183190

184-
// Collect options.
185-
$options = $this->getOptions();
186-
187191
$httpDownloader = new HttpDownloader($this->io, $this->composer->getConfig());
188192

189193
$fetcher = new FileFetcher($this->io, $httpDownloader, $options, $core_version, $this->progress);
@@ -312,6 +316,7 @@ protected function getOptions() {
312316
$options = $extra['drupal-l10n'] + [
313317
'destination' => 'sites/default/files/translations',
314318
'languages' => [],
319+
'core-only' => FALSE,
315320
];
316321
return $options;
317322
}

tests/PluginTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ public function testCoreOnlyOption() {
171171
$this->assertFileDoesNotExist($contrib_translation_file, 'Contrib translation should not exist after --core-only command.');
172172
}
173173

174+
/**
175+
* Tests that core-only option from composer.json skips contrib translations.
176+
*/
177+
public function testCoreOnlyOptionFromComposerJson() {
178+
$composer_json = $this->composerJsonDefaults();
179+
$composer_json['extra']['drupal-l10n']['core-only'] = TRUE;
180+
$this->writeComposerJson($composer_json);
181+
182+
$core_version = '9.5.3';
183+
$contrib_module = 'entity_share';
184+
$contrib_composer_version = '3.0.0-rc4';
185+
$contrib_drupal_version = '8.x-3.0-rc4';
186+
$translations_directory = $this->tmpDir . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR . 'contrib';
187+
$core_translation_file = $translations_directory . DIRECTORY_SEPARATOR . 'drupal-' . $core_version . '.fr.po';
188+
$contrib_translation_file = $translations_directory . DIRECTORY_SEPARATOR . $contrib_module . '-' . $contrib_drupal_version . '.fr.po';
189+
190+
$this->composer('install');
191+
$this->composer('require --update-with-dependencies drupal/core:"' . $core_version . '"');
192+
$this->composer('require drupal/' . $contrib_module . ':"' . $contrib_composer_version . '"');
193+
$this->assertFileExists($core_translation_file, 'Drupal core translation should exist after install.');
194+
$this->assertFileExists($contrib_translation_file, 'Contrib translation should exist after install.');
195+
196+
$this->fs->remove($core_translation_file);
197+
$this->fs->remove($contrib_translation_file);
198+
$this->assertFileDoesNotExist($core_translation_file, 'Drupal core translation should not exist after removal.');
199+
$this->assertFileDoesNotExist($contrib_translation_file, 'Contrib translation should not exist after removal.');
200+
201+
$this->composer('drupal:l10n');
202+
$this->assertFileExists($core_translation_file, 'Drupal core translation should exist after composer.json core-only mode.');
203+
$this->assertFileDoesNotExist($contrib_translation_file, 'Contrib translation should not exist after composer.json core-only mode.');
204+
}
205+
174206
/**
175207
* Tests that on Drupal 7, core and contrib modules are handled.
176208
*/
@@ -219,8 +251,11 @@ public function testDrupal10() {
219251
/**
220252
* Writes the default composer json to the temp directory.
221253
*/
222-
protected function writeComposerJson() {
223-
$json = json_encode($this->composerJsonDefaults(), JSON_PRETTY_PRINT);
254+
protected function writeComposerJson(array $composer_json = NULL) {
255+
if (is_null($composer_json)) {
256+
$composer_json = $this->composerJsonDefaults();
257+
}
258+
$json = json_encode($composer_json, JSON_PRETTY_PRINT);
224259
// Write composer.json.
225260
file_put_contents($this->tmpDir . '/composer.json', $json);
226261
}

0 commit comments

Comments
 (0)