generate.contents.make.inc

  1. 8.0.x commands/make/generate.contents.make.inc
  2. 7.x commands/make/generate.contents.make.inc
  3. master commands/make/generate.contents.make.inc

Functions for the generate makefile command.

Functions

Namesort descending Description
make_generate_from_makefile Write a makefile based on data parsed from a previous makefile.
make_generate_print Print the generated makefile to the terminal, or write it to a file.
_drush_make_generate_defaults
_drush_make_generate_lines Utility function to generate the line or lines for a key/value pair in the make file.
_drush_make_generate_makefile_body
_drush_make_generate_makefile_contents Generate the actual contents of the .make file.
_make_generate_array_filter Helper function to recursively remove empty values from an array (but not '0'!).

File

commands/make/generate.contents.make.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions for the generate makefile command.
  5. */
  6. use Drush\Log\LogLevel;
  7. /**
  8. * Generate the actual contents of the .make file.
  9. */
  10. function _drush_make_generate_makefile_contents($projects, $libraries = array(), $core_version = NULL, $defaults = array()) {
  11. if (is_null($core_version)) {
  12. $core_version = drush_get_drupal_core_compatibility();
  13. }
  14. $header = array();
  15. $header[] = '; This file was auto-generated by drush make';
  16. $header['core'] = $core_version;
  17. $header['api'] = MAKE_API;
  18. $header[] = '';
  19. if (!empty($defaults)) {
  20. _drush_make_generate_defaults($defaults, $header);
  21. $header[] = '';
  22. }
  23. $header[] = '; Core';
  24. return _drush_make_generate_makefile_body($projects, $header) . _drush_make_generate_makefile_body($libraries);
  25. }
  26. function _drush_make_generate_makefile_body($projects, $output = array()) {
  27. $custom = FALSE;
  28. $previous_type = 'core';
  29. if (isset($projects)) {
  30. foreach ($projects as $name => $project) {
  31. $type = (isset($project['type']) && ($project['type'] == 'library')) ? 'libraries' : 'projects';
  32. if ($previous_type != $project['_type']) {
  33. $previous_type = $project['_type'];
  34. $output[] = '; ' . ucfirst($previous_type) . 's';
  35. }
  36. unset($project['_type']);
  37. if (!$project && is_string($name)) {
  38. $output[] = $type . '[] = "' . $name . '"';
  39. continue;
  40. }
  41. $base = $type . '[' . $name . ']';
  42. if (isset($project['custom_download'])) {
  43. $custom = TRUE;
  44. $output[] = '; Please fill the following out. Type may be one of get, git, bzr or svn,';
  45. $output[] = '; and url is the url of the download.';
  46. $output[$base . '[download][type]'] = '""';
  47. $output[$base . '[download][url]'] = '""';
  48. unset($project['custom_download']);
  49. }
  50. $output = array_merge($output, _drush_make_generate_lines($base, $project));
  51. $output[] = '';
  52. }
  53. }
  54. $string = '';
  55. foreach ($output as $k => $v) {
  56. if (!is_numeric($k)) {
  57. $string .= $k . ' = ' . $v;
  58. }
  59. else {
  60. $string .= $v;
  61. }
  62. $string .= "\n";
  63. }
  64. if ($custom) {
  65. drush_log(dt('Some of the properties in your makefile will have to be manually edited. Please do that now.'), LogLevel::WARNING);
  66. }
  67. return $string;
  68. }
  69. /**
  70. * Write a makefile based on data parsed from a previous makefile.
  71. *
  72. * @param $file
  73. * The path to the file to write our generated makefile to, or TRUE to
  74. * print to the terminal.
  75. * @param $makefile
  76. * A makefile on which to base our generated one.
  77. */
  78. function make_generate_from_makefile($file, $makefile) {
  79. $info = make_parse_info_file($makefile);
  80. $projects = drush_get_option('DRUSH_MAKE_PROJECTS', FALSE);
  81. if ($projects === FALSE) {
  82. $projects = make_prepare_projects(FALSE, $info);
  83. if (isset($projects['contrib'])) {
  84. $projects = array_merge($projects['core'], $projects['contrib']);
  85. }
  86. }
  87. $defaults = $info['defaults'];
  88. $core = current($projects);
  89. $core = $core['core'];
  90. foreach ($projects as $name => $project) {
  91. // If a specific revision was requested, do not set the version.
  92. if (!isset($project['revision'])) {
  93. $projects[$name]['version'] = $project['download']['full_version'];
  94. if ($project['type'] != 'core' && strpos($projects[$name]['version'], $project['core']) === 0) {
  95. $projects[$name]['version'] = substr($projects[$name]['version'], strlen($project['core'] . '-'));
  96. }
  97. }
  98. else {
  99. unset($projects[$name]['version']);
  100. }
  101. $projects[$name]['_type'] = $project['type'];
  102. // Don't clutter the makefile with defaults
  103. if (is_array($defaults)) {
  104. foreach ($defaults as $type => $defs) {
  105. if ($type == 'projects') {
  106. foreach ($defs as $key => $value) {
  107. if (isset($project[$key]) && $project[$key] == $value) {
  108. unset($projects[$name][$key]);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. if ($project['name'] == $name) {
  115. unset($projects[$name]['name']);
  116. }
  117. if ($project['type'] == 'module' && !isset($info[$name]['type'])) {
  118. unset($projects[$name]['type']); // Module is the default
  119. }
  120. if (!(isset($project['download']['type'])) || ($project['download']['type'] == 'pm')) {
  121. unset($projects[$name]['download']); // PM is the default
  122. }
  123. $ignore = array('build_path', 'contrib_destination', 'core', 'location', 'make_directory', 'l10n_url', 'download_type');
  124. foreach ($ignore as $key) {
  125. unset($projects[$name][$key]);
  126. }
  127. // Remove empty entries (e.g. 'directory_name')
  128. $projects[$name] = _make_generate_array_filter($projects[$name]);
  129. }
  130. $libraries = drush_get_option('DRUSH_MAKE_LIBRARIES', FALSE);
  131. if ($libraries === FALSE) {
  132. $libraries = $info['libraries'];
  133. }
  134. if (is_array($libraries)) {
  135. foreach ($libraries as $name => $library) {
  136. $libraries[$name]['type'] = 'library';
  137. $libraries[$name]['_type'] = 'librarie';
  138. }
  139. }
  140. $contents = _drush_make_generate_makefile_contents($projects, $libraries, $core, $defaults);
  141. // Write or print our makefile.
  142. $file = $file !== TRUE ? $file : NULL;
  143. make_generate_print($contents, $file);
  144. }
  145. /**
  146. * Helper function to recursively remove empty values from an array (but not
  147. * '0'!).
  148. */
  149. function _make_generate_array_filter($haystack) {
  150. foreach ($haystack as $key => $value) {
  151. if (is_array($value)) {
  152. $haystack[$key] = _make_generate_array_filter($haystack[$key]);
  153. }
  154. if (empty($value) && $value !== '0') {
  155. unset($haystack[$key]);
  156. }
  157. }
  158. return $haystack;
  159. }
  160. /**
  161. * Print the generated makefile to the terminal, or write it to a file.
  162. *
  163. * @param $contents
  164. * The formatted contents of a makefile.
  165. * @param $file
  166. * (optional) The path to write the makefile.
  167. */
  168. function make_generate_print($contents, $file = NULL) {
  169. if (!$file) {
  170. drush_print($contents);
  171. }
  172. elseif (file_put_contents($file, $contents)) {
  173. drush_log(dt("Wrote .make file @file", array('@file' => $file)), LogLevel::OK);
  174. }
  175. else {
  176. drush_make_error('FILE_ERROR', dt("Unable to write .make file !file", array('!file' => $file)));
  177. }
  178. }
  179. /**
  180. * Utility function to generate the line or lines for a key/value pair in the
  181. * make file.
  182. *
  183. * @param $base
  184. * The base for the configuration lines. Values will be appended to it as
  185. * [$key] = $value, or if value is an array itself it will expand into as many
  186. * lines as required.
  187. * @param $values
  188. * May be a single value or an array.
  189. * @return
  190. * An array of strings that represent lines for the make file.
  191. */
  192. function _drush_make_generate_lines($base, $values) {
  193. $output = array();
  194. if (is_array($values)) {
  195. foreach ($values as $key => $value) {
  196. $newbase = $base . '[' . $key . ']';
  197. $output = array_merge($output, _drush_make_generate_lines($newbase, $value));
  198. }
  199. }
  200. else {
  201. $output[$base] = '"' . $values . '"';
  202. }
  203. return $output;
  204. }
  205. function _drush_make_generate_defaults($defaults, &$output = array()) {
  206. $output[] = '; Defaults';
  207. foreach ($defaults as $name => $project) {
  208. $type = 'defaults';
  209. if (!$project && is_string($name)) {
  210. $output[] = $type . '[] = "' . $name . '"';
  211. continue;
  212. }
  213. $base = $type . '[' . $name . ']';
  214. $output = array_merge($output, _drush_make_generate_lines($base, $project));
  215. }
  216. }