forked from kraftwagen/kraftwagen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependencies.module.apply.kw.inc
134 lines (117 loc) · 4.96 KB
/
dependencies.module.apply.kw.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* @file
* This file contains the functions that are required to execute
* `drush kw-apply-module-dependencies`.
*/
/**
* Implements drush_COMMAND for `drush kw-apply-module-dependencies`.
*
* @param string $environment
* The name of the environment. Defaults to 'production'.
*/
function drush_kraftwagen_kw_apply_module_dependencies($environment = 'production') {
// Enable watchdog printing to show information about enabled, disabled,
// installed and uninstalled modules.
drush_set_option('watchdog', 'print');
// Get the data we need. This is a excerpt of system_rebuild_module_data with
// the environment specific dependencies logic inserted.
$modules = _system_rebuild_module_data();
ksort($modules);
system_get_files_database($modules, 'module');
system_update_files_database($modules, 'module');
// Inject enviroment specific dependencies into the dependencies.
foreach ($modules as $key => $module) {
if (!empty($module->info['env_dependencies'][$environment])) {
foreach ($module->info['env_dependencies'][$environment] as $dependency) {
$module->info['dependencies'][] = $dependency;
}
}
}
// Resolve dependencies and find out in which order to install modules.
$files = _module_build_dependencies($modules);
// Get the active profile name.
$profile = drupal_get_profile();
// If we can't gather data about the install profile, we can't do anything.
if (!isset($files[$profile])) {
return drush_set_error(dt('Could not find dependencies for profile !profile.', array('!profile' => $profile)));
}
// To find the required modules we need to clear the required flag on the
// profile, because otherwise all modules that need to be enabled are called
// required. Required is reserved for base modules for Drupal.
$files[$profile]->info['required'] = FALSE;
// Find the 'required' modules
// Noteworthy fact when debugging: the field module markes modules that are
// used by existing fields as 'required'.
$required = array();
foreach ($files as $name => $info) {
if (!empty($info->info['required']) && !isset($required[$name])) {
$required[$name] = $info->sort;
if (!empty($info->requires)) {
foreach (array_keys($info->requires) as $dependency) {
if (!isset($required[$dependency])) {
if (!isset($files[$dependency])) {
return drush_set_error(dt('Dependency !dependency of required module !module does not exist.', array('!module' => $name, '!dependency' => $dependency)));
}
$required[$dependency] = $files[$dependency]->sort;
}
}
}
}
}
arsort($required);
// Find the modules that the profile depend on (the whole dependency tree).
$dependencies = array();
foreach (array_keys($files[$profile]->requires) as $dependency) {
if (!isset($required[$dependency])) {
if (!isset($files[$dependency])) {
return drush_set_error(dt('Dependency !dependency of the install profile !profile does not exist.', array('!dependency' => $dependency, '!profile' => $profile)));
}
$dependencies[$dependency] = $files[$dependency]->sort;
}
}
arsort($dependencies);
// Enable the required modules and dependencies.
$expected = array_merge(array_keys($required), array_keys($dependencies));
$result = module_enable($expected);
if (!$result) {
return drush_set_error(dt('An unexpected error occured while trying to enable modules !modules', array('!modules', implode(', ', $expected))));
}
// Check for enabled modules that are not expected to be enabled.
$unexpected = array();
foreach (module_list(TRUE) as $module) {
if (!in_array($module, $expected) && $module != $profile) {
if (!isset($files[$module])) {
drush_log(dt('Could not find dependencies for currently enabled module !module.', array('!module' => $module)), 'error');
}
$unexpected[$module] = $files[$module]->sort;
}
}
asort($unexpected);
$unexpected = array_keys($unexpected);
// Disable the unexpected modules.
if ($unexpected) {
module_disable($unexpected);
}
// Check for modules that are still installed even though they are not
// expected.
$orphan_installs = array();
include_once DRUPAL_ROOT . '/includes/install.inc';
foreach ($files as $name => $info) {
if ($name != $profile && !in_array($name, $expected)) {
if (drupal_get_installed_schema_version($name) != SCHEMA_UNINSTALLED) {
$orphan_installs[$name] = $info->sort;
}
}
}
asort($orphan_installs);
$orphan_installs = array_keys($orphan_installs);
// Remove the hanging installs.
if ($orphan_installs) {
$result = drupal_uninstall_modules($orphan_installs, FALSE);
if (!$result) {
return drush_set_error(dt('An unexpected error occured while trying to uninstall modules !modules', array('!modules', implode(', ', $orphan_installs))));
}
}
drush_log(dt('Finished enabling required modules and uninstalling orphan modules.'), 'ok');
}