Skip to content

Commit

Permalink
Merge pull request drush-ops#944 from drush-ops/yaml-info-file
Browse files Browse the repository at this point in the history
Supports injecting info file metatdata into YAML files.
  • Loading branch information
jhedstrom committed Nov 8, 2014
2 parents fd1dc99 + d2a4cd6 commit 05d354c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
73 changes: 58 additions & 15 deletions commands/pm/pm.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2066,25 +2066,19 @@ function drush_find_empty_directories($dir, $exclude = array()) {
* TRUE on success, FALSE on any failures appending data to .info files.
*/
function drush_pm_inject_info_file_metadata($project_dir, $project_name, $version) {
$info_files = drush_scan_directory($project_dir, '/.*\.info$/');
// `drush_drupal_major_version()` cannot be used here because this may be running
// outside of a Drupal context.
$yaml_format = substr($version, 0, 1) >= 8;
$pattern = preg_quote($yaml_format ? '.info.yml' : '.info');
$info_files = drush_scan_directory($project_dir, '/.*' . $pattern . '$/');
if (!empty($info_files)) {
// Construct the string of metadata to append to all the .info files.
// Taken straight from: http://drupalcode.org/project/drupalorg.git/blob/refs/heads/6.x-3.x:/drupalorg_project/plugins/release_packager/DrupalorgProjectPackageRelease.class.php#l192
$info = "\n\n; Information added by drush on " . date('Y-m-d') . "\n";
$info .= "version = \"$version\"\n";
// .info files started with 5.x, so we don't have to worry about version
// strings like "4.7.x-1.0" in this regular expression. If we can't parse
// the version (also from an old "HEAD" release), or the version isn't at
// least 6.x, don't add any "core" attribute at all.
$matches = array();
if (preg_match('/^((\d+)\.x)-.*/', $version, $matches) && $matches[2] >= 6) {
$info .= "core = \"$matches[1]\"\n";
if ($yaml_format) {
$info = _drush_pm_generate_info_yaml_metadata($version, $project_name);
}
if (!drush_get_option('no-gitprojectinfo', FALSE)) {
$info .= "project = \"$project_name\"\n";
else {
$info = _drush_pm_generate_info_ini_metadata($version, $project_name);
}
$info .= 'datestamp = "'. time() ."\"\n";
$info .= "\n";
foreach ($info_files as $info_file) {
if (!drush_file_append_data($info_file->filename, $info)) {
return FALSE;
Expand All @@ -2093,3 +2087,52 @@ function drush_pm_inject_info_file_metadata($project_dir, $project_name, $versio
}
return TRUE;
}

/**
* Generate version information for `.info` files in ini format.
*
* Taken with some modifications from:
* http://drupalcode.org/project/drupalorg.git/blob/refs/heads/6.x-3.x:/drupalorg_project/plugins/release_packager/DrupalorgProjectPackageRelease.class.php#l192
*/
function _drush_pm_generate_info_ini_metadata($version, $project_name) {
$matches = array();
$extra = '';
if (preg_match('/^((\d+)\.x)-.*/', $version, $matches) && $matches[2] >= 6) {
$extra .= "\ncore = \"$matches[1]\"";
}
if (!drush_get_option('no-gitprojectinfo', FALSE)) {
$extra = "\nproject = \"$project_name\"";
}
$time = time();
$date = date('Y-m-d');
$info = <<<METADATA
; Information added by drush on {$date}
version = "{$version}"{$extra}
datestamp = "{$time}"
METADATA;
return $info;
}

/**
* Generate version information for `.info` files in YAML format.
*/
function _drush_pm_generate_info_yaml_metadata($version, $project_name) {
$matches = array();
$extra = '';
if (preg_match('/^((\d+)\.x)-.*/', $version, $matches) && $matches[2] >= 6) {
$extra .= "\ncore: '$matches[1]'";
}
if (!drush_get_option('no-gitprojectinfo', FALSE)) {
$extra = "\nproject: '$project_name'";
}
$time = time();
$date = date('Y-m-d');
$info = <<<METADATA
# Information added by drush on {$date}
version: '{$version}'{$extra}
datestamp: {$time}
METADATA;
return $info;
}
25 changes: 25 additions & 0 deletions tests/makeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,25 @@ function testInfoFileWritingGit() {
$this->assertContains('project = "caption_filter"', $contents);
}

/**
* Test .info file writing and the use of a git reference cache for
* git downloads.
*/
function testInfoYamlFileWritingGit() {
// Use the Drupal 8 .make file.
$config = $this->getMakefile('git-simple-8');

$options = array('no-core' => NULL);
$makefile = $this->makefile_path . DIRECTORY_SEPARATOR . $config['makefile'];
$this->drush('make', array($makefile, UNISH_SANDBOX . '/test-build'), $options);

$this->assertFileExists(UNISH_SANDBOX . '/test-build/modules/honeypot/honeypot.info.yml');
$contents = file_get_contents(UNISH_SANDBOX . '/test-build/modules/honeypot/honeypot.info.yml');
$this->assertContains('# Information added by drush on ' . date('Y-m-d'), $contents);
$this->assertContains("version: '8.x-1.18-beta1+1-dev'", $contents);
$this->assertContains("project: 'honeypot'", $contents);
}

function testMakeFileExtract() {
$this->runMakefileTest('file-extract');
}
Expand Down Expand Up @@ -444,6 +463,12 @@ function getMakefile($key) {
'md5' => '0147681209adef163a8ac2c0cff2a07e',
'options' => array('no-core' => NULL, 'no-gitinfofile' => NULL),
),
'git-simple-8' => array(
'name' => 'Simple git integration for D8',
'makefile' => 'git-simple-8.make',
'build' => TRUE,
'options' => array('no-core' => NULL),
),
'no-patch-txt' => array(
'name' => 'Test --no-patch-txt option',
'makefile' => 'patches.make',
Expand Down
5 changes: 5 additions & 0 deletions tests/makefiles/git-simple-8.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
core = 8.x
api = 2

projects[honeypot][download][branch] = "8.x-1.x"
projects[honeypot][download][revision] = "50ab794"

0 comments on commit 05d354c

Please sign in to comment.