Skip to content

Instantly share code, notes, and snippets.

@pfrenssen
Created July 5, 2019 14:21
Show Gist options
  • Save pfrenssen/f2a0c94c5a30da89f95880c22244edc3 to your computer and use it in GitHub Desktop.
Save pfrenssen/f2a0c94c5a30da89f95880c22244edc3 to your computer and use it in GitHub Desktop.
Provides project namespaces for Drupal module dependencies in info.yml files
<?php
$webroot = '/home/pieter/v/joinup-dev/web/';
$source_folders = [
'core/modules/' => 'drupal',
'modules/custom/' => 'joinup',
'modules/contrib/' => 'contrib',
];
// Compile a namespaced list of all modules.
$projects = [];
foreach ($source_folders as $source_folder => $project) {
$source_path = $webroot . $source_folder;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source_path));
$files = new RegexIterator($iterator, '/\.info\.yml$/i', RecursiveRegexIterator::GET_MATCH);
foreach (array_keys(iterator_to_array($files)) as $info_file_path) {
switch ($project) {
case 'contrib':
$project_name = substr($info_file_path, strlen($source_path));
$project_name = substr($project_name, 0, strpos($project_name, '/'));
break;
default:
$project_name = $project;
}
$matches = [];
preg_match('/.*\/(.*)\.info\.yml$/', $info_file_path, $matches);
$module_name = $matches[1];
$projects[$module_name] = "$project_name:$module_name";
}
}
// Loop over all info files of custom modules.
$source_path = $webroot . 'modules/custom/';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source_path));
$files = new RegexIterator($iterator, '/\.info\.yml$/i', RecursiveRegexIterator::GET_MATCH);
foreach (array_keys(iterator_to_array($files)) as $info_file_path) {
$changed = FALSE;
$data = yaml_parse_file($info_file_path);
if (!empty($data['dependencies'])) {
foreach ($data['dependencies'] as &$dependency) {
if (strpos($dependency, ':') !== FALSE) continue;
if (!array_key_exists($dependency, $projects)) {
echo "not found: $dependency when parsing info file $info_file_path";
continue;
}
$dependency = $projects[$dependency];
$changed = TRUE;
}
}
if ($changed) {
yaml_emit_file($info_file_path, $data, YAML_UTF8_ENCODING, YAML_LN_BREAK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment