generate.make.inc

  1. 8.0.x commands/make/generate.make.inc
  2. 6.x commands/make/generate.make.inc
  3. 7.x commands/make/generate.make.inc
  4. 5.x commands/make/generate.make.inc
  5. master commands/make/generate.make.inc

Functions for the generate makefile command.

Functions

Namesort descending Description
drush_make_generate Drush callback; generate makefile from the current build.
_drush_generate_custom_project Create a project record for an extension not downloaded from drupal.org
_drush_generate_makefile_check_path Helper function to check for a non-default installation location.
_drush_generate_track_version Helper function to determine if a given project is to have its version tracked.
_drush_generate_validate_repo_location If the user has checked in the Drupal root, or the 'sites/all/modules' folder into a git repository, then we do not want to confuse that location with a "project".
_drush_make_generate_add_patch_files Record any patches that were applied to this project per information stored in PATCHES.txt.
_drush_make_generate_get_version_options Create the $version_options array from the --include-versions and --exclude-versions command line options.
_drush_make_generate_projects Generate the $projects makefile array for the current site.

File

commands/make/generate.make.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions for the generate makefile command.
  5. */
  6. include_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
  7. include_once drupal_get_path('module', 'system') . '/system.install';
  8. include_once 'generate.contents.make.inc';
  9. /**
  10. * Drush callback; generate makefile from the current build.
  11. */
  12. function drush_make_generate($file = NULL) {
  13. $version_options = _drush_make_generate_get_version_options();
  14. $all_extensions = drush_get_extensions();
  15. list($projects, $libraries) = _drush_make_generate_projects($all_extensions, $version_options);
  16. $core = drush_drupal_major_version() . '.x';
  17. $contents = make_generate_makefile_contents($projects, $libraries, $core);
  18. // Write or print our makefile.
  19. make_generate_print($contents, $file);
  20. }
  21. /**
  22. * Create the $version_options array from the --include-versions and
  23. * --exclude-versions command line options.
  24. */
  25. function _drush_make_generate_get_version_options() {
  26. // What projects should we pin the versions for?
  27. // Check the command-line options for details.
  28. foreach (array("include", "exclude") as $option) {
  29. $version_options[$option] = drush_get_option("$option-versions");
  30. if ($version_options[$option] !== TRUE) {
  31. $version_options[$option] = array_filter(explode(",", $version_options[$option]));
  32. }
  33. }
  34. return $version_options;
  35. }
  36. /**
  37. * Generate the $projects makefile array for the current site.
  38. */
  39. function _drush_make_generate_projects($all_extensions, $version_options) {
  40. $release_info = drush_get_engine('release_info');
  41. $projects = array();
  42. $project_libraries = array();
  43. $system_requirements = system_requirements('runtime');
  44. // Update xml expects the drupal version to be expressed as "7.x" or "8.x"
  45. // We used to check $system_requirements['drupal']['value'], but this now
  46. // contains values such as "7.10-dev".
  47. $drupal_major_version = drush_drupal_major_version() . '.x';
  48. $core_project = strtolower($system_requirements['drupal']['title']);
  49. $projects[$core_project] = array('_type' => 'core');
  50. if ($core_project != 'drupal') {
  51. $projects[$core_project]['custom_download'] = TRUE;
  52. $projects[$core_project]['type'] = 'core';
  53. }
  54. else {
  55. // Drupal core - we can determine the version if required.
  56. if (_drush_generate_track_version("drupal", $version_options)) {
  57. $projects[$core_project]["version"] = drush_drupal_version();
  58. }
  59. }
  60. $install_profile = drush_drupal_major_version() >= 7 ? drupal_get_profile() : variable_get('install_profile', '');
  61. if (!in_array($install_profile, array('default', 'standard', 'minimal', 'testing')) && $install_profile != '') {
  62. $projects[$install_profile]['type']
  63. = $projects[$install_profile]['_type'] = 'profile';
  64. $request = array(
  65. 'name' => $install_profile,
  66. 'drupal_version' => $drupal_major_version,
  67. );
  68. if (!$release_info->checkProject($request, 'profile')) {
  69. $projects[$install_profile]['custom_download'] = TRUE;
  70. }
  71. }
  72. // Iterate installed projects to build $projects array.
  73. $extensions = $all_extensions;
  74. $project_info = drush_get_projects($extensions);
  75. foreach ($project_info as $name => $project) {
  76. // Discard the extensions within this project. At the end $extensions will
  77. // contain only extensions part of custom projects (not from drupal.org or
  78. // other update service).
  79. foreach ($project['extensions'] as $ext) {
  80. unset($extensions[$ext]);
  81. }
  82. if ($name == 'drupal') {
  83. continue;
  84. }
  85. $type = $project['type'];
  86. // Discard projects with all modules disabled.
  87. if (($type == 'module') && (!$project['status'])) {
  88. continue;
  89. }
  90. $projects[$name] = array('_type' => $type);
  91. // Check the project is on drupal.org or its own update service.
  92. $request = array(
  93. 'name' => $name,
  94. 'drupal_version' => $drupal_major_version,
  95. );
  96. if (isset($project['status url'])) {
  97. $request['status url'] = $project['status url'];
  98. $projects[$name]['location'] = $project['status url'];
  99. }
  100. if (!$release_info->checkProject($request, $type)) {
  101. // It is not a project on drupal.org neither an external update service.
  102. $projects[$name]['type'] = $type;
  103. $projects[$name]['custom_download'] = TRUE;
  104. }
  105. // Add 'subdir' if the project is installed in a non-default location.
  106. if (isset($project['path'])) {
  107. $projects[$name] += _drush_generate_makefile_check_path($project);
  108. }
  109. // Add version number if this project's version is to be tracked.
  110. if (_drush_generate_track_version($name, $version_options) && $project["version"]) {
  111. $version = preg_replace("/^" . drush_get_drupal_core_compatibility() . "-/", "", $project["version"]);
  112. // Strip out MINOR+GIT_COMMIT strings for dev releases.
  113. if (substr($version, -4) == '-dev' && strpos($version, '+')) {
  114. $version = substr($version, 0, strrpos($version, '.')) . '.x-dev';
  115. }
  116. $projects[$name]['version'] = $version;
  117. }
  118. foreach ($project['extensions'] as $extension_name) {
  119. _drush_make_generate_add_patch_files($projects[$name], _drush_extension_get_path($all_extensions[$extension_name]));
  120. }
  121. }
  122. // Add a project for each unknown extension.
  123. foreach ($extensions as $name => $extension) {
  124. list($project_name, $project_data) = _drush_generate_custom_project($name, $extension, $version_options);
  125. $projects[$project_name] = $project_data;
  126. }
  127. // Add libraries.
  128. if (function_exists('libraries_get_libraries')) {
  129. $libraries = libraries_get_libraries();
  130. foreach ($libraries as $library_name => $library_path) {
  131. $path = explode('/', $library_path);
  132. $project_libraries[$library_name] = array(
  133. 'directory_name' => $path[(count($path) - 1)],
  134. 'custom_download' => TRUE,
  135. 'type' => 'library',
  136. '_type' => 'librarie', // For plural.
  137. );
  138. }
  139. }
  140. return array($projects, $project_libraries);
  141. }
  142. /**
  143. * Record any patches that were applied to this project
  144. * per information stored in PATCHES.txt.
  145. */
  146. function _drush_make_generate_add_patch_files(&$project, $location) {
  147. $patchfile = DRUPAL_ROOT . '/' . $location . '/PATCHES.txt';
  148. if (is_file($patchfile)) {
  149. foreach (file($patchfile) as $line) {
  150. if (substr($line, 0, 2) == '- ') {
  151. $project['patch'][] = trim(substr($line, 2));
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * Create a project record for an extension not downloaded from drupal.org
  158. */
  159. function _drush_generate_custom_project($name, $extension, $version_options) {
  160. $project['_type'] = drush_extension_get_type($extension);
  161. $project['type'] = drush_extension_get_type($extension);
  162. $location = drush_extension_get_path($extension);
  163. // To start off, we will presume that our custom extension is
  164. // stored in a folder named after its project, and there are
  165. // no subfolders between the .info file and the project root.
  166. $project_name = basename($location);
  167. drush_shell_cd_and_exec($location, 'git rev-parse --git-dir 2> ' . drush_bit_bucket());
  168. $output = drush_shell_exec_output();
  169. if (!empty($output)) {
  170. $git_dir = $output[0];
  171. // Find the actual base of the git repository.
  172. $repo_root = $git_dir == ".git" ? $location : dirname($git_dir);
  173. // If the repository root is at the drupal root or some parent
  174. // of the drupal root, or some other location that could not
  175. // pausibly be a project, then there is nothing we can do.
  176. // (We can't tell Drush make to download some sub-part of a repo,
  177. // can we?)
  178. if ($repo_project_name = _drush_generate_validate_repo_location($repo_root)) {
  179. $project_name = $repo_project_name;
  180. drush_shell_cd_and_exec($repo_root, 'git remote show origin');
  181. $output = drush_shell_exec_output();
  182. foreach ($output as $line) {
  183. if (strpos($line, "Fetch URL:") !== FALSE) {
  184. $url = preg_replace('/ *Fetch URL: */', '', $line);
  185. if (!empty($url)) {
  186. // We use the unconventional-looking keys
  187. // `download][type` and `download][url` so that
  188. // we can produce output that appears to be two-dimensional
  189. // arrays from a single-dimensional array.
  190. $project['download][type'] = 'git';
  191. $project['download][url'] = $url;
  192. // Fill in the branch as well.
  193. drush_shell_cd_and_exec($repo_root, 'git branch');
  194. $output = drush_shell_exec_output();
  195. foreach ($output as $line) {
  196. if ($line{0} == '*') {
  197. $branch = substr($line, 2);
  198. if ($branch != "master") {
  199. $project['download][branch'] = $branch;
  200. }
  201. }
  202. }
  203. // Put in the commit hash.
  204. drush_shell_cd_and_exec($repo_root, 'git log');
  205. $output = drush_shell_exec_output();
  206. if (substr($output[0], 0, 7) == "commit ") {
  207. $revision = substr($output[0], 7);
  208. if (_drush_generate_track_version($project_name, $version_options)) {
  209. $project['download][revision'] = $revision;
  210. }
  211. }
  212. // Add patch files, if any.
  213. _drush_make_generate_add_patch_files($project, $repo_root);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. // If we could not figure out where the extension came from, then give up and
  220. // flag it as a "custom" download.
  221. if (!isset($project['download][type'])) {
  222. $project['custom_download'] = TRUE;
  223. }
  224. return array($project_name, $project);
  225. }
  226. /**
  227. * If the user has checked in the Drupal root, or the 'sites/all/modules'
  228. * folder into a git repository, then we do not want to confuse that location
  229. * with a "project".
  230. */
  231. function _drush_generate_validate_repo_location($repo_root) {
  232. $project_name = basename($repo_root);
  233. // The Drupal root, or any folder immediately inside the Drupal
  234. // root cannot be a project location.
  235. if ((strlen(DRUPAL_ROOT) >= strlen($repo_root)) || (dirname($repo_root) == DRUPAL_ROOT)) {
  236. return NULL;
  237. }
  238. // Also exclude sites/* and sites/*/{modules,themes} and profile/* and
  239. // profile/*/{modules,themes}.
  240. return $project_name;
  241. }
  242. /**
  243. * Helper function to determine if a given project is to have its version
  244. * tracked.
  245. */
  246. function _drush_generate_track_version($project, $version_options) {
  247. // A. If --exclude-versions has been specified:
  248. // A.a. if it's a boolean, check the --include-versions option.
  249. if ($version_options["exclude"] === TRUE) {
  250. // A.a.1 if --include-versions has been specified, ensure it's an array.
  251. if (is_array($version_options["include"])) {
  252. return in_array($project, $version_options["include"]);
  253. }
  254. // A.a.2 If no include array, then we're excluding versions for ALL
  255. // projects.
  256. return FALSE;
  257. }
  258. // A.b. if --exclude-versions is an array with items, check this project is in
  259. // it: if so, then return FALSE.
  260. elseif (is_array($version_options["exclude"]) && count($version_options["exclude"])) {
  261. return !in_array($project, $version_options["exclude"]);
  262. }
  263. // B. If by now no --exclude-versions, but --include-versions is an array,
  264. // examine it for this project.
  265. if (is_array($version_options["include"]) && count($version_options["include"])) {
  266. return in_array($project, $version_options["include"]);
  267. }
  268. // If none of the above conditions match, include version number by default.
  269. return TRUE;
  270. }
  271. /**
  272. * Helper function to check for a non-default installation location.
  273. */
  274. function _drush_generate_makefile_check_path($project) {
  275. $info = array();
  276. $type = $project['type'];
  277. $path = dirname($project['path']);
  278. // Check to see if the path is in a subdir sites/all/modules or
  279. // profiles/profilename/modules
  280. if (preg_match('@^sites/[a-zA-Z0-9_]*/' . $type . 's/..*@', $path) || preg_match('@^sites/[a-zA-Z0-9_]*/' . $type . 's/..*@', $path)) {
  281. $subdir = preg_replace(array('@^[a-zA-Z0-9_]*/[a-zA-Z0-9_]*/' . $type . 's/*@', "@/$name" . '$@'), '', $path);
  282. if (!empty($subdir)) {
  283. $info['subdir'] = $subdir;
  284. }
  285. }
  286. return $info;
  287. }