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_makefile_contents Generate makefile contents in the appropriate format.
make_generate_makefile_contents_ini Generate makefile contents in (legacy) INI format.
make_generate_makefile_contents_yaml Generate makefile contents in YAML format.
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'!).
_make_generate_array_filter_key Helper function to recursively remove elements matching a specific key from an array.

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. if (!$info = make_parse_info_file($makefile)) {
  80. return drush_set_error('MAKE_GENERATE_FAILED_PARSE', dt('Failed to parse makefile :makefile.', array(':makefile' => $makefile)));
  81. }
  82. $projects = drush_get_option('DRUSH_MAKE_PROJECTS', FALSE);
  83. if ($projects === FALSE) {
  84. $projects = make_prepare_projects(FALSE, $info);
  85. if (isset($projects['contrib'])) {
  86. $projects = array_merge($projects['core'], $projects['contrib']);
  87. }
  88. }
  89. $defaults = $info['defaults'];
  90. $core = current($projects);
  91. $core = $core['core'];
  92. foreach ($projects as $name => $project) {
  93. // If a specific revision was requested, do not set the version.
  94. if (!isset($project['revision'])) {
  95. $projects[$name]['version'] = $project['download']['full_version'];
  96. if ($project['type'] != 'core' && strpos($projects[$name]['version'], $project['core']) === 0) {
  97. $projects[$name]['version'] = substr($projects[$name]['version'], strlen($project['core'] . '-'));
  98. }
  99. }
  100. else {
  101. unset($projects[$name]['version']);
  102. }
  103. $projects[$name]['_type'] = $project['type'];
  104. // Don't clutter the makefile with defaults
  105. if (is_array($defaults)) {
  106. foreach ($defaults as $type => $defs) {
  107. if ($type == 'projects') {
  108. foreach ($defs as $key => $value) {
  109. if (isset($project[$key]) && $project[$key] == $value) {
  110. unset($projects[$name][$key]);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. if ($project['name'] == $name) {
  117. unset($projects[$name]['name']);
  118. }
  119. if ($project['type'] == 'module' && !isset($info[$name]['type'])) {
  120. unset($projects[$name]['type']); // Module is the default
  121. }
  122. if (!(isset($project['download']['type'])) || ($project['download']['type'] == 'pm')) {
  123. unset($projects[$name]['download']); // PM is the default
  124. }
  125. $ignore = array('build_path', 'contrib_destination', 'core', 'location', 'make_directory', 'l10n_url', 'download_type');
  126. foreach ($ignore as $key) {
  127. unset($projects[$name][$key]);
  128. }
  129. // Remove empty entries (e.g. 'directory_name')
  130. $projects[$name] = _make_generate_array_filter($projects[$name]);
  131. }
  132. $libraries = drush_get_option('DRUSH_MAKE_LIBRARIES', FALSE);
  133. if ($libraries === FALSE) {
  134. $libraries = $info['libraries'];
  135. }
  136. if (is_array($libraries)) {
  137. foreach ($libraries as $name => $library) {
  138. $libraries[$name]['type'] = 'library';
  139. $libraries[$name]['_type'] = 'librarie';
  140. }
  141. }
  142. $contents = make_generate_makefile_contents($projects, $libraries, $core, $defaults);
  143. // Write or print our makefile.
  144. $file = $file !== TRUE ? $file : NULL;
  145. make_generate_print($contents, $file);
  146. }
  147. /**
  148. * Generate makefile contents in the appropriate format.
  149. */
  150. function make_generate_makefile_contents($projects, $libraries, $core, $defaults) {
  151. $format = drush_get_option('format', 'yaml');
  152. $func = "make_generate_makefile_contents_$format";
  153. if (function_exists($func)) {
  154. $contents = call_user_func($func, $projects, $libraries, $core, $defaults);
  155. }
  156. else {
  157. return drush_set_error('MAKE_UNKNOWN_OUTPUT_FORMAT', dt('Generating makefiles in the :format output format is not yet supported. Implement :func() to add such support.', array(':format' => $format, ':func' => $func)));
  158. }
  159. return $contents;
  160. }
  161. /**
  162. * Generate makefile contents in (legacy) INI format.
  163. */
  164. function make_generate_makefile_contents_ini($projects, $libraries, $core, $defaults) {
  165. return _drush_make_generate_makefile_contents($projects, $libraries, $core, $defaults);
  166. }
  167. /**
  168. * Generate makefile contents in YAML format.
  169. */
  170. function make_generate_makefile_contents_yaml($projects, $libraries, $core, $defaults) {
  171. $info = array(
  172. 'core' => $core,
  173. 'api' => MAKE_API,
  174. 'defaults' => $defaults,
  175. 'projects' => $projects,
  176. 'libraries' => $libraries,
  177. );
  178. $info = _make_generate_array_filter($info);
  179. $info = _make_generate_array_filter_key('_type', $info);
  180. $dumper = drush_load_engine('outputformat', 'yaml');
  181. $yaml = $dumper->format($info, array());
  182. return $yaml;
  183. }
  184. /**
  185. * Helper function to recursively remove empty values from an array (but not
  186. * '0'!).
  187. */
  188. function _make_generate_array_filter($haystack) {
  189. foreach ($haystack as $key => $value) {
  190. if (is_array($value)) {
  191. $haystack[$key] = _make_generate_array_filter($haystack[$key]);
  192. }
  193. if (empty($value) && $value !== '0') {
  194. unset($haystack[$key]);
  195. }
  196. }
  197. return $haystack;
  198. }
  199. /**
  200. * Helper function to recursively remove elements matching a specific key from an array.
  201. */
  202. function _make_generate_array_filter_key($needle, $haystack) {
  203. foreach ($haystack as $key => $value) {
  204. if ($key === $needle) {
  205. unset($haystack[$key]);
  206. }
  207. elseif (is_array($value)) {
  208. $haystack[$key] = _make_generate_array_filter_key($needle, $haystack[$key]);
  209. }
  210. }
  211. return $haystack;
  212. }
  213. /**
  214. * Print the generated makefile to the terminal, or write it to a file.
  215. *
  216. * @param $contents
  217. * The formatted contents of a makefile.
  218. * @param $file
  219. * (optional) The path to write the makefile.
  220. */
  221. function make_generate_print($contents, $file = NULL) {
  222. if (!$file) {
  223. drush_print($contents);
  224. }
  225. elseif (file_put_contents($file, $contents)) {
  226. drush_log(dt("Wrote .make file @file", array('@file' => $file)), LogLevel::OK);
  227. }
  228. else {
  229. make_error('FILE_ERROR', dt("Unable to write .make file !file", array('!file' => $file)));
  230. }
  231. }
  232. /**
  233. * Utility function to generate the line or lines for a key/value pair in the
  234. * make file.
  235. *
  236. * @param $base
  237. * The base for the configuration lines. Values will be appended to it as
  238. * [$key] = $value, or if value is an array itself it will expand into as many
  239. * lines as required.
  240. * @param $values
  241. * May be a single value or an array.
  242. * @return
  243. * An array of strings that represent lines for the make file.
  244. */
  245. function _drush_make_generate_lines($base, $values) {
  246. $output = array();
  247. if (is_array($values)) {
  248. foreach ($values as $key => $value) {
  249. $newbase = $base . '[' . $key . ']';
  250. $output = array_merge($output, _drush_make_generate_lines($newbase, $value));
  251. }
  252. }
  253. else {
  254. $output[$base] = '"' . $values . '"';
  255. }
  256. return $output;
  257. }
  258. function _drush_make_generate_defaults($defaults, &$output = array()) {
  259. $output[] = '; Defaults';
  260. foreach ($defaults as $name => $project) {
  261. $type = 'defaults';
  262. if (!$project && is_string($name)) {
  263. $output[] = $type . '[] = "' . $name . '"';
  264. continue;
  265. }
  266. $base = $type . '[' . $name . ']';
  267. $output = array_merge($output, _drush_make_generate_lines($base, $project));
  268. }
  269. }