Skip to content

Commit 9fd1846

Browse files
authored
Merge pull request #16 from uWaterloo/set-core-and-package
Allow setting core and package keys
2 parents ec531e8 + 00abc69 commit 9fd1846

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed

src/DrupalInfo.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ public function rollbackRewrite(Event $event)
120120
protected function doWriteInfoFiles(PackageInterface $package)
121121
{
122122
if ($writer = $this->getWriter($package)) {
123-
$writer->rewrite($this->findVersion($package), $this->findTimestamp($package));
123+
$writer->rewrite(
124+
$this->findVersion($package),
125+
$this->findTimestamp($package),
126+
$this->findCore($package),
127+
$this->findPackage($package)
128+
);
124129
} elseif ($this->io->isVerbose()) {
125130
$this->io->write(
126131
'<info>No info files found for ' .$package->getPrettyName() . '</info>'
@@ -195,6 +200,46 @@ protected function findTimestamp(PackageInterface $package)
195200
return time();
196201
}
197202

203+
/**
204+
* Find the Drupal core version this package is for.
205+
*
206+
* @param PackageInterface $package
207+
*
208+
* @return string|null
209+
* Drupal core version, such as "8.x". NULL if unable to determine.
210+
*/
211+
protected function findCore(PackageInterface $package)
212+
{
213+
// Attempt to determine from getExtra().
214+
$extra = $package->getExtra();
215+
if (isset($extra['drupal']['core'])) {
216+
return $extra['drupal']['core'];
217+
}
218+
219+
// Attempt to determine from version number.
220+
$version = $this->findVersion($package);
221+
if (preg_match('/^(\d+\.x)-/', $version, $backref)) {
222+
return $backref[1];
223+
}
224+
}
225+
226+
/**
227+
* Find the name of the packge.
228+
*
229+
* @param PackageInterface $package
230+
*
231+
* @return string|null
232+
* The package name. NULL if unable to determine.
233+
*/
234+
protected function findPackage(PackageInterface $package)
235+
{
236+
// Attempt to determine from getExtra().
237+
$extra = $package->getExtra();
238+
if (isset($extra['drupal']['package'])) {
239+
return $extra['drupal']['package'];
240+
}
241+
}
242+
198243
/**
199244
* Determine if this package should be processed.
200245
*

src/Writer/Drupal.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function set(array $paths)
3030
/**
3131
* {@inheritdoc}
3232
*/
33-
public function rewrite($version, $timestamp)
33+
public function rewrite($version, $timestamp, $core = null, $project = null)
3434
{
3535
foreach ($this->paths as $info_file) {
3636
// Don't write to files that already contain version information.
3737
if (!$this->hasVersionInfo($info_file)) {
3838
$file = fopen($info_file, 'a+');
39-
fwrite($file, $this->formatInfo($version, $timestamp));
39+
fwrite($file, $this->formatInfo($version, $timestamp, $core, $project));
4040
fclose($file);
4141
}
4242
}
@@ -58,17 +58,25 @@ public function rollback()
5858
/**
5959
* Format version and timestamp into YAML.
6060
*/
61-
protected function formatInfo($version, $timestamp)
61+
protected function formatInfo($version, $timestamp, $core = null, $project = null)
6262
{
6363
$date = gmdate('c', $timestamp);
64-
$info = <<<EOL
65-
66-
# Information added by drupal-composer/info-rewrite on $date.
67-
version: '$version'
68-
datestamp: $timestamp
64+
$info = array();
65+
// Always start with EOL character.
66+
$info[] = '';
67+
$info[] = "# Information added by drupal-composer/info-rewrite on $date.";
68+
$info[] = "version: '$version'";
69+
if ($core) {
70+
$info[] = "core: '$core'";
71+
}
72+
if ($project) {
73+
$info[] = "project: '$project'";
74+
}
75+
$info[] = "datestamp: $timestamp";
76+
// Always end with EOL character.
77+
$info[] = '';
6978

70-
EOL;
71-
return $info;
79+
return implode("\n", $info);
7280
}
7381

7482
/**

src/Writer/Drupal7.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@ class Drupal7 extends Drupal
1515
/**
1616
* Format version and timestamp into INI format.
1717
*/
18-
protected function formatInfo($version, $timestamp)
18+
protected function formatInfo($version, $timestamp, $core = null, $project = null)
1919
{
2020
$date = gmdate('c', $timestamp);
21-
$info = <<<EOL
22-
; Information added by drupal-composer/info-rewrite on $date.
23-
version = "$version"
24-
timestamp = "$timestamp"
21+
$info = array();
22+
// Always start with EOL character.
23+
$info[] = '';
24+
$info[] = "; Information added by drupal-composer/info-rewrite on $date.";
25+
$info[] = "version = \"$version\"";
26+
if ($core) {
27+
$info[] = "core = \"$core\"";
28+
}
29+
if ($project) {
30+
$info[] = "project = \"$project\"";
31+
}
32+
$info[] = "datestamp = \"$timestamp\"";
33+
// Always end with EOL character.
34+
$info[] = '';
2535

26-
EOL;
36+
return implode("\n", $info);
2737
}
2838
}

src/Writer/WriterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function set(array $paths);
2222
* @param string $timestamp
2323
* @return
2424
*/
25-
public function rewrite($version, $timestamp);
25+
public function rewrite($version, $timestamp, $core = null, $project = null);
2626

2727
/**
2828
* Rollback the info files to their download/unprocessed state.

0 commit comments

Comments
 (0)