features.drush.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. /**
  3. * @file
  4. * Features module drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. *
  9. * @return
  10. * An associative array describing your command(s).
  11. *
  12. * @see drush_parse_command()
  13. */
  14. function features_drush_command() {
  15. $items = array();
  16. // If Features is enabled display the configured default export path,
  17. // otherwise just show the default.
  18. if (defined('FEATURES_DEFAULT_EXPORT_PATH')) {
  19. $path = variable_get('features_default_export_path', FEATURES_DEFAULT_EXPORT_PATH);
  20. }
  21. else {
  22. $path = 'sites/all/modules';
  23. }
  24. $items['features-list'] = array(
  25. 'description' => "List all the available features for your site.",
  26. 'options' => array(
  27. 'status' => "Feature status, can be 'enabled', 'disabled' or 'all'",
  28. ),
  29. 'drupal dependencies' => array('features'),
  30. 'aliases' => array('fl', 'features'),
  31. );
  32. $items['features-export'] = array(
  33. 'description' => "Export a feature from your site into a module.",
  34. 'arguments' => array(
  35. 'feature' => 'Feature name to export.',
  36. 'components' => 'Patterns of components to include, see features-components for the format of patterns.'
  37. ),
  38. 'options' => array(
  39. 'destination' => "Destination path (from Drupal root) of the exported feature. Defaults to '" . $path . "'.",
  40. 'version-set' => "Specify a version number for the feature.",
  41. 'version-increment' => "Increment the feature's version number.",
  42. 'ignore-conflicts' => "Ignore conflicts and export all components.",
  43. ),
  44. 'drupal dependencies' => array('features'),
  45. 'aliases' => array('fe'),
  46. );
  47. $items['features-add'] = array(
  48. 'description' => "Add a component to a feature module. (DEPRECATED: use features-export)",
  49. 'arguments' => array(
  50. 'feature' => 'Feature name to add to.',
  51. 'components' => 'List of components to add.',
  52. ),
  53. 'options' => array(
  54. 'version-set' => "Specify a version number for the feature.",
  55. 'version-increment' => "Increment the feature's version number.",
  56. ),
  57. 'drupal dependencies' => array('features'),
  58. 'aliases' => array('fa'),
  59. );
  60. $items['features-components'] = array(
  61. 'description' => 'List features components.',
  62. 'arguments' => array(
  63. 'patterns' => 'The features components type to list. Omit this argument to list all components.',
  64. ),
  65. 'options' => array(
  66. 'exported' => array(
  67. 'description' => 'Show only components that have been exported.',
  68. ),
  69. 'not-exported' => array(
  70. 'description' => 'Show only components that have not been exported.',
  71. ),
  72. 'info-style' => array(
  73. 'description' => 'Export components in format suitable for using in an info file.',
  74. ),
  75. ),
  76. 'aliases' => array('fc'),
  77. );
  78. $items['features-update'] = array(
  79. 'description' => "Update a feature module on your site.",
  80. 'arguments' => array(
  81. 'feature' => 'A space delimited list of features.',
  82. ),
  83. 'options' => array(
  84. 'version-set' => "Specify a version number for the feature.",
  85. 'version-increment' => "Increment the feature's version number.",
  86. ),
  87. 'drupal dependencies' => array('features'),
  88. 'aliases' => array('fu'),
  89. );
  90. $items['features-update-all'] = array(
  91. 'description' => "Update all feature modules on your site.",
  92. 'arguments' => array(
  93. 'feature_exclude' => 'A space-delimited list of features to exclude from being updated.',
  94. ),
  95. 'drupal dependencies' => array('features'),
  96. 'aliases' => array('fu-all', 'fua'),
  97. );
  98. $items['features-revert'] = array(
  99. 'description' => "Revert a feature module on your site.",
  100. 'arguments' => array(
  101. 'feature' => 'A space delimited list of features or feature.component pairs.',
  102. ),
  103. 'options' => array(
  104. 'force' => "Force revert even if Features assumes components' state are default.",
  105. ),
  106. 'examples' => array(
  107. 'drush fr foo.node foo.taxonomy bar' => 'Revert node and taxonomy components of feature "foo", but only if they are overriden. Revert all overriden components of feature "bar".',
  108. 'drush fr foo.node foo.taxonomy bar --force' => 'Revert node and taxonomy components of feature "foo". Revert all components of feature "bar".',
  109. ),
  110. 'drupal dependencies' => array('features'),
  111. 'aliases' => array('fr'),
  112. );
  113. $items['features-revert-all'] = array(
  114. 'description' => "Revert all enabled feature module on your site.",
  115. 'arguments' => array(
  116. 'feature_exclude' => 'A space-delimited list of features to exclude from being reverted.',
  117. ),
  118. 'options' => array(
  119. 'force' => "Force revert even if Features assumes components' state are default.",
  120. ),
  121. 'drupal dependencies' => array('features'),
  122. 'aliases' => array('fr-all', 'fra'),
  123. );
  124. $items['features-diff'] = array(
  125. 'description' => "Show the difference between the default and overridden state of a feature.",
  126. 'arguments' => array(
  127. 'feature' => 'The feature in question.',
  128. ),
  129. 'options' => array(
  130. 'ctypes' => 'Comma separated list of component types to limit the output to. Defaults to all types.',
  131. 'lines' => 'Generate diffs with <n> lines of context instead of the usual two.',
  132. ),
  133. 'drupal dependencies' => array('features', 'diff'),
  134. 'aliases' => array('fd'),
  135. );
  136. $items['features-diff-all'] = array(
  137. 'description' => "Show the code difference for all enabled features not in their default state.",
  138. 'arguments' => array(
  139. 'feature_exclude' => 'A space-delimited list of features to exclude from being reverted.',
  140. ),
  141. 'options' => array(
  142. 'force' => "Bypass the confirmations. This is useful if you want to output all of the diffs to a log file.",
  143. ),
  144. 'drupal dependencies' => array('features', 'diff'),
  145. 'aliases' => array('fda'),
  146. );
  147. return $items;
  148. }
  149. /**
  150. * Implements hook_drush_help().
  151. */
  152. function features_drush_help($section) {
  153. // If Features is enabled display the configured default export path,
  154. // otherwise just show the default.
  155. if (defined('FEATURES_DEFAULT_EXPORT_PATH')) {
  156. $path = variable_get('features_default_export_path', FEATURES_DEFAULT_EXPORT_PATH);
  157. }
  158. else {
  159. $path = 'sites/all/modules';
  160. }
  161. switch ($section) {
  162. case 'drush:features':
  163. return dt("List all the available features for your site.");
  164. case 'drush:features-export':
  165. return dt("Export a feature from your site into a module. If called with no arguments, display a list of available components. If called with a single argument, attempt to create a feature including the given component with the same name. The option '--destination=foo' may be used to specify the path (from Drupal root) where the feature should be created. The default destination is '@path'. The option '--version-set=foo' may be used to specify a version number for the feature or the option '--version-increment' may also to increment the feature's version number.", array('@path' => $path));
  166. case 'drush:features-components':
  167. return dt("List feature components matching patterns. The listing may be limited to exported/not-exported components.
  168. A component pattern consists of a source, a colon and a component. Both source and component may be a full name (as in \"dependencies\"), a shorthand (for instance \"dep\") or a pattern (like \"%denci%\").
  169. Shorthands are unique shortenings of a name. They will only match if exactly one option contains the shorthand. So in a standard installation, \"dep\" will work for dependencies, but \"user\" wont, as it matches both user_permission and user_role.
  170. Patterns uses * or % for matching multiple sources/components. Unlike shorthands, patterns must match the whole name, so \"field:%article%\" should be used to select all fields containing \"article\" (which could both be those on the node type article, as well as those fields named article). * and % are equivalent, but the latter doesn't have to be escaped in UNIX shells.
  171. Lastly, a pattern without a colon is interpreted as having \":%\" appended, for easy listing of all components of a source.
  172. ");
  173. case 'drush:features-update':
  174. return dt("Update a feature module on your site. The option '--version-set=foo' may be used to specify a version number for the feature or the option '--version-increment' may also to increment the feature's version number.");
  175. case 'drush:features-update-all':
  176. return dt("Update all feature modules on your site.");
  177. case 'drush:features-revert':
  178. return dt("Revert a feature module on your site.");
  179. case 'drush:features-revert-all':
  180. return dt("Revert all enabled feature module on your site.");
  181. case 'drush:features-diff':
  182. return dt("Show a diff of a feature module.");
  183. case 'drush:features-add':
  184. return dt("Add a component to a feature module. The option '--version-set=foo' may be used to specify a version number for the feature or the option '--version-increment' may also to increment the feature's version number.");
  185. }
  186. }
  187. /**
  188. * Get a list of all feature modules.
  189. */
  190. function drush_features_list() {
  191. $status = drush_get_option('status') ? drush_get_option('status') : 'all';
  192. if (!in_array($status, array('enabled', 'disabled', 'all'))) {
  193. return drush_set_error('', dt('!status is not valid', array('!status' => $status)));
  194. }
  195. module_load_include('inc', 'features', 'features.export');
  196. $rows = array(array(dt('Name'), dt('Feature'), dt('Status'), dt('Version'), dt('State')));
  197. // Sort the Features list before compiling the output.
  198. $features = features_get_features(NULL, TRUE);
  199. ksort($features);
  200. foreach ($features as $k => $m) {
  201. switch (features_get_storage($m->name)) {
  202. case FEATURES_DEFAULT:
  203. case FEATURES_REBUILDABLE:
  204. $storage = '';
  205. break;
  206. case FEATURES_OVERRIDDEN:
  207. $storage = dt('Overridden');
  208. break;
  209. case FEATURES_NEEDS_REVIEW:
  210. $storage = dt('Needs review');
  211. break;
  212. }
  213. if (
  214. ($m->status == 0 && ($status == 'all' || $status == 'disabled')) ||
  215. ($m->status == 1 && ($status == 'all' || $status == 'enabled'))
  216. ) {
  217. $rows[] = array(
  218. $m->info['name'],
  219. $m->name,
  220. $m->status ? dt('Enabled') : dt('Disabled'),
  221. $m->info['version'],
  222. $storage
  223. );
  224. }
  225. }
  226. drush_print_table($rows, TRUE);
  227. }
  228. /**
  229. * List components, with pattern matching.
  230. */
  231. function drush_features_components() {
  232. $args = func_get_args();
  233. $components = _drush_features_component_list();
  234. ksort($components);
  235. // If no args supplied, prompt with a list.
  236. if (empty($args)) {
  237. $types = array_keys($components);
  238. array_unshift($types, 'all');
  239. $choice = drush_choice($types, 'Enter a number to choose which component type to list.');
  240. if ($choice === FALSE) {
  241. return;
  242. }
  243. $args = ($choice == 0) ? array('*') : array($types[$choice]);
  244. }
  245. $options = array(
  246. 'provided by' => TRUE,
  247. );
  248. if (drush_get_option(array('exported', 'e'), NULL)) {
  249. $options['not exported'] = FALSE;
  250. }
  251. elseif (drush_get_option(array('not-exported', 'o'), NULL)) {
  252. $options['exported'] = FALSE;
  253. }
  254. if (drush_get_option(array('info-style', 'is'), NULL)) {
  255. $options['info style'] = TRUE;
  256. }
  257. $filtered_components = _drush_features_component_filter($components, $args, $options);
  258. if ($filtered_components){
  259. _drush_features_component_print($filtered_components, $options);
  260. }
  261. }
  262. /**
  263. * Returns a listing of all known components, indexed by source.
  264. */
  265. function _drush_features_component_list() {
  266. $components = array();
  267. foreach (features_get_feature_components() as $source => $info) {
  268. if ($options = features_invoke($source, 'features_export_options')) {
  269. foreach ($options as $name => $title) {
  270. $components[$source][$name] = $title;
  271. }
  272. }
  273. }
  274. return $components;
  275. }
  276. /**
  277. * Filters components by patterns.
  278. */
  279. function _drush_features_component_filter($all_components, $patterns = array(), $options = array()) {
  280. $options += array(
  281. 'exported' => TRUE,
  282. 'not exported' => TRUE,
  283. 'provided by' => FALSE,
  284. );
  285. $pool = array();
  286. // Maps exported components to feature modules.
  287. $components_map = features_get_component_map();
  288. // First filter on exported state.
  289. foreach ($all_components as $source => $components) {
  290. foreach ($components as $name => $title) {
  291. $exported = !empty($components_map[$source][$name]);
  292. if ($exported) {
  293. if ($options['exported']) {
  294. $pool[$source][$name] = $title;
  295. }
  296. }
  297. else {
  298. if ($options['not exported']) {
  299. $pool[$source][$name] = $title;
  300. }
  301. }
  302. }
  303. }
  304. $state_string = '';
  305. if (!$options['exported']) {
  306. $state_string = 'unexported';
  307. }
  308. elseif (!$options['not exported']) {
  309. $state_string = 'exported';
  310. }
  311. $selected = array();
  312. foreach ($patterns as $pattern) {
  313. // Rewrite * to %. Let users use both as wildcard.
  314. $pattern = strtr($pattern, array('*' => '%'));
  315. $sources = array();
  316. $source_pattern = strtok($pattern, ':');
  317. $component_pattern = strtok(':');
  318. // If source is empty, use a pattern.
  319. if ($source_pattern == '') {
  320. $source_pattern = '%';
  321. }
  322. if ($component_pattern == '') {
  323. $component_pattern = '%';
  324. }
  325. $preg_source_pattern = strtr(preg_quote($source_pattern, '/'), array('%' => '.*'));
  326. $preg_component_pattern = strtr(preg_quote($component_pattern, '/'), array('%' => '.*'));
  327. /*
  328. * If it isn't a pattern, but a simple string, we don't anchor the
  329. * pattern, this allows for abbreviating. Else, we do, as this seems more
  330. * natural for patterns.
  331. */
  332. if (strpos($source_pattern, '%') !== FALSE) {
  333. $preg_source_pattern = '^' . $preg_source_pattern . '$';
  334. }
  335. if (strpos($component_pattern, '%') !== FALSE) {
  336. $preg_component_pattern = '^' . $preg_component_pattern . '$';
  337. }
  338. $matches = array();
  339. // Find the sources.
  340. $all_sources = array_keys($pool);
  341. $matches = preg_grep('/' . $preg_source_pattern . '/', $all_sources);
  342. if (sizeof($matches) > 0) {
  343. // If we have multiple matches and the source string wasn't a
  344. // pattern, check if one of the matches is equal to the pattern, and
  345. // use that, or error out.
  346. if (sizeof($matches) > 1 and $preg_source_pattern[0] != '^') {
  347. if (in_array($source_pattern, $matches)) {
  348. $matches = array($source_pattern);
  349. }
  350. else {
  351. return drush_set_error('', dt('Ambiguous source "!source", matches !matches', array('!source' => $source_pattern, '!matches' => join(', ', $matches))));
  352. }
  353. }
  354. // Loose the indexes preg_grep preserved.
  355. $sources = array_values($matches);
  356. }
  357. else {
  358. return drush_set_error('', dt('No !state sources match "!source"', array('!state' => $state_string, '!source' => $source_pattern)));
  359. }
  360. // Now find the components.
  361. foreach ($sources as $source) {
  362. // Find the components.
  363. $all_components = array_keys($pool[$source]);
  364. // See if there's any matches.
  365. $matches = preg_grep('/' . $preg_component_pattern . '/', $all_components);
  366. if (sizeof($matches) > 0) {
  367. // If we have multiple matches and the components string wasn't a
  368. // pattern, check if one of the matches is equal to the pattern, and
  369. // use that, or error out.
  370. if (sizeof($matches) > 1 and $preg_component_pattern[0] != '^') {
  371. if (in_array($component_pattern, $matches)) {
  372. $matches = array($component_pattern);
  373. }
  374. else {
  375. return drush_set_error('', dt('Ambiguous component "!component", matches !matches', array('!component' => $component_pattern, '!matches' => join(', ', $matches))));
  376. }
  377. }
  378. if (empty($selected[$source])) {
  379. $selected[$source] = array();
  380. }
  381. $selected[$source] += array_intersect_key($pool[$source], array_flip($matches));
  382. }
  383. else {
  384. // No matches. If the source was a pattern, just carry on, else
  385. // error out. Allows for patterns like :*field*
  386. if ($preg_source_pattern[0] != '^') {
  387. return drush_set_error('', dt('No !state !source components match "!component"', array('!state' => $state_string, '!component' => $component_pattern, '!source' => $source)));
  388. }
  389. }
  390. }
  391. }
  392. // Lastly, provide feature module information on the selected components, if
  393. // requested.
  394. $provided_by = array();
  395. if ($options['provided by'] && $options['exported'] ) {
  396. foreach ($selected as $source => $components) {
  397. foreach ($components as $name => $title) {
  398. $exported = !empty($components_map[$source][$name]);
  399. if ($exported) {
  400. $provided_by[$source . ':' . $name] = join(', ', $components_map[$source][$name]);
  401. }
  402. }
  403. }
  404. }
  405. return array(
  406. 'components' => $selected,
  407. 'sources' => $provided_by,
  408. );
  409. }
  410. /**
  411. * Prints a list of filtered components.
  412. */
  413. function _drush_features_component_print($filtered_components, $options = array()) {
  414. $rows = array(array(dt('Available sources')));
  415. foreach ($filtered_components['components'] as $source => $components) {
  416. foreach ($components as $name => $value) {
  417. if (!empty($options['info style'])) {
  418. // Output as .info file style.
  419. $row = array('features[' . $source . '][] = "' . $name . '"');
  420. }
  421. else {
  422. $row = array($source .':'. $name);
  423. }
  424. if (isset($filtered_components['sources'][$source .':'. $name])) {
  425. $row[] = dt('Provided by') . ': ' . $filtered_components['sources'][$source .':'. $name];
  426. }
  427. $rows[] = $row;
  428. }
  429. }
  430. drush_print_table($rows, TRUE);
  431. }
  432. /**
  433. * Add a component to a features module, or create a new module with
  434. * the selected components.
  435. */
  436. function drush_features_export() {
  437. if ($args = func_get_args()) {
  438. $module = array_shift($args);
  439. if (empty($args)) {
  440. return drush_set_error('', 'No components supplied.');
  441. }
  442. $components = _drush_features_component_list();
  443. $options = array();
  444. if (!drush_get_option('ignore-conflicts', FALSE)) {
  445. $options['exported'] = FALSE;
  446. }
  447. $filtered_components = _drush_features_component_filter($components, $args, $options);
  448. $items = $filtered_components['components'];
  449. if (empty($items)) {
  450. return drush_set_error('', 'No components to add.');
  451. }
  452. $items = array_map('array_keys', $items);
  453. if (($feature = features_load_feature($module, TRUE)) && module_exists($module)) {
  454. module_load_include('inc', 'features', 'features.export');
  455. _features_populate($items, $feature->info, $feature->name);
  456. _drush_features_export($feature->info, $feature->name, dirname($feature->filename));
  457. }
  458. elseif ($feature) {
  459. _features_drush_set_error($module, 'FEATURES_FEATURE_NOT_ENABLED');
  460. }
  461. else {
  462. // Same logic as in _drush_features_export. Should be refactored.
  463. $destination = drush_get_option(array('destination'), variable_get('features_default_export_path', FEATURES_DEFAULT_EXPORT_PATH));
  464. $directory = isset($directory) ? $directory : $destination . '/' . $module;
  465. drush_print(dt('Will create a new module in !dir', array('!dir' => $directory)));
  466. if (!drush_confirm(dt('Do you really want to continue?'))) {
  467. drush_die('Aborting.');
  468. }
  469. $export = _drush_features_generate_export($items, $module);
  470. _features_populate($items, $export[info], $export[name]);
  471. _drush_features_export($export['info'], $module, $directory);
  472. }
  473. }
  474. else {
  475. return drush_set_error('', 'No feature name given.');
  476. }
  477. }
  478. /**
  479. * Add a component to a features module
  480. * the selected components.
  481. *
  482. * This is DEPRECATED, but keeping it around for a bit longer for user migration
  483. */
  484. function drush_features_add() {
  485. drush_print(dt('features-add is DEPRECATED.'));
  486. drush_print(dt('Calling features-export instead.'));
  487. drush_features_export();
  488. }
  489. /**
  490. * Update an existing feature module.
  491. */
  492. function drush_features_update() {
  493. if ($args = func_get_args()) {
  494. foreach ($args as $module) {
  495. if (($feature = features_load_feature($module, TRUE)) && module_exists($module)) {
  496. _drush_features_export($feature->info, $feature->name, dirname($feature->filename));
  497. }
  498. elseif ($feature) {
  499. _features_drush_set_error($module, 'FEATURES_FEATURE_NOT_ENABLED');
  500. }
  501. else {
  502. _features_drush_set_error($module);
  503. }
  504. }
  505. }
  506. else {
  507. // By default just show contexts that are available.
  508. $rows = array(array(dt('Available features')));
  509. foreach (features_get_features(NULL, TRUE) as $name => $info) {
  510. $rows[] = array($name);
  511. }
  512. drush_print_table($rows, TRUE);
  513. }
  514. }
  515. /**
  516. * Update all enabled features. Optionally pass in a list of features to
  517. * exclude from being updated.
  518. */
  519. function drush_features_update_all() {
  520. $features_to_update = array();
  521. $features_to_exclude = func_get_args();
  522. foreach (features_get_features() as $module) {
  523. if ($module->status && !in_array($module->name, $features_to_exclude)) {
  524. $features_to_update[] = $module->name;
  525. }
  526. }
  527. $dt_args = array('!modules' => implode(', ', $features_to_update));
  528. drush_print(dt('The following modules will be updated: !modules', $dt_args));
  529. if (!drush_confirm(dt('Do you really want to continue?'))) {
  530. return drush_user_abort('Aborting.');
  531. }
  532. // If we got here, set affirmative to TRUE, so that the user doesn't have to
  533. // confirm each and every feature. Start off by storing the current value,
  534. // so we can set it back afteward.
  535. $skip_confirmation = drush_get_context('DRUSH_AFFIRMATIVE');
  536. drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
  537. // Now update all the features.
  538. drush_invoke('features-update', $features_to_update);
  539. // Now set it back as it was, in case other commands are called after this.
  540. drush_set_context('DRUSH_AFFIRMATIVE', $skip_confirmation);
  541. }
  542. /**
  543. * Write a module to the site dir.
  544. *
  545. * @param $info
  546. * The feature info associative array.
  547. * @param $module_name
  548. * Optional. The name for the exported module.
  549. */
  550. function _drush_features_export($info, $module_name = NULL, $directory = NULL) {
  551. $root = drush_get_option(array('r', 'root'), drush_locate_root());
  552. $skip_confirmation = drush_get_context('DRUSH_AFFIRMATIVE');
  553. if ($root) {
  554. $destination = drush_get_option(array('destination'), variable_get('features_default_export_path', FEATURES_DEFAULT_EXPORT_PATH));
  555. $directory = isset($directory) ? $directory : $destination . '/' . $module_name;
  556. if (is_dir($directory)) {
  557. // If we aren't skipping confirmation and the directory already exists,
  558. // prompt the user. This message most make sense for but fe and fu.
  559. if (!$skip_confirmation && !drush_confirm(dt('Module located at !dir will be updated. Do you want to continue?', array('!dir' => $directory)))) {
  560. drush_die('Aborting.');
  561. }
  562. }
  563. else {
  564. drush_op('mkdir', $directory);
  565. }
  566. if (is_dir($directory)) {
  567. drupal_flush_all_caches();
  568. $export = _drush_features_generate_export($info, $module_name);
  569. $files = features_export_render($export, $module_name, TRUE);
  570. foreach ($files as $extension => $file_contents) {
  571. if (!in_array($extension, array('module', 'info'))) {
  572. $extension .= '.inc';
  573. }
  574. drush_op('file_put_contents', "{$directory}/{$module_name}.$extension", $file_contents);
  575. }
  576. drush_log(dt("Created module: !module in !directory", array('!module' => $module_name, '!directory' => $directory)), 'ok');
  577. }
  578. else {
  579. drush_die(dt('Couldn\'t create directory !directory', array('!directory' => $directory)));
  580. }
  581. }
  582. else {
  583. drush_die(dt('Couldn\'t locate site root'));
  584. }
  585. }
  586. /**
  587. * Helper function for _drush_feature_export.
  588. *
  589. * @param $info
  590. * The feature info associative array.
  591. * @param $module_name
  592. * Optional. The name for the exported module.
  593. */
  594. function _drush_features_generate_export(&$info, &$module_name) {
  595. module_load_include('inc', 'features', 'features.export');
  596. $export = features_populate($info, $module_name);
  597. if (!features_load_feature($module_name)) {
  598. $export['name'] = $module_name;
  599. }
  600. // Set the feature version if the --version-set or --version-increment option is passed.
  601. if ($version = drush_get_option(array('version-set'))) {
  602. preg_match('/^(?P<core>\d+\.x)-(?P<major>\d+)\.(?P<patch>\d+)-?(?P<extra>\w+)?$/', $version, $matches);
  603. if (!isset($matches['core'], $matches['major'])) {
  604. drush_die(dt('Please enter a valid version with core and major version number. Example: !example', array('!example' => '7.x-1.0')));
  605. }
  606. $export['version'] = $version;
  607. }
  608. elseif ($version = drush_get_option(array('version-increment'))) {
  609. // Determine current version and increment it.
  610. $export_load = features_export_prepare($export, $module_name);
  611. $version = $export_load['version'];
  612. $version_explode = explode('.', $version);
  613. $version_minor = array_pop($version_explode);
  614. // Increment minor version number if numeric or not a dev release.
  615. if (is_numeric($version_minor) || strpos($version_minor, 'dev') !== (strlen($version_minor) - 3)) {
  616. // Check for prefixed versions (alpha, beta, rc).
  617. if (ctype_digit($version_minor)) {
  618. ++$version_minor;
  619. }
  620. else {
  621. // Split version number parts.
  622. $pattern = '/([0-9]-[a-z]+([0-9])+)/';
  623. $matches = array();
  624. preg_match($pattern, $version_minor, $matches);
  625. $number = array_pop($matches);
  626. ++$number;
  627. $pattern = '/[0-9]+$/';
  628. $version_minor = preg_replace($pattern, $number, $version_minor);
  629. }
  630. }
  631. array_push($version_explode, $version_minor);
  632. // Rebuild version string.
  633. $version = implode('.', $version_explode);
  634. $export['version'] = $version;
  635. }
  636. return $export;
  637. }
  638. /**
  639. * Revert a feature to it's code definition.
  640. * Optionally accept a list of components to revert.
  641. */
  642. function drush_features_revert() {
  643. if ($args = func_get_args()) {
  644. module_load_include('inc', 'features', 'features.export');
  645. features_include();
  646. // Determine if revert should be forced.
  647. $force = drush_get_option('force');
  648. // Determine if -y was supplied. If so, we can filter out needless output
  649. // from this command.
  650. $skip_confirmation = drush_get_context('DRUSH_AFFIRMATIVE');
  651. // Parse list of arguments.
  652. $modules = array();
  653. foreach ($args as $arg) {
  654. $arg = explode('.', $arg);
  655. $module = array_shift($arg);
  656. $component = array_shift($arg);
  657. if (isset($module)) {
  658. if (empty($component)) {
  659. // If we received just a feature name, this means that we need all of it's components.
  660. $modules[$module] = TRUE;
  661. }
  662. elseif ($modules[$module] !== TRUE) {
  663. if (!isset($modules[$module])) {
  664. $modules[$module] = array();
  665. }
  666. $modules[$module][] = $component;
  667. }
  668. }
  669. }
  670. // Process modules.
  671. foreach ($modules as $module => $components_needed) {
  672. $dt_args['@module'] = $module;
  673. if (($feature = features_load_feature($module, TRUE)) && module_exists($module)) {
  674. $components = array();
  675. // Forcefully revert all components of a feature.
  676. if ($force) {
  677. foreach (array_keys($feature->info['features']) as $component) {
  678. if (features_hook($component, 'features_revert')) {
  679. $components[] = $component;
  680. }
  681. }
  682. }
  683. // Only revert components that are detected to be Overridden/Needs review/rebuildable.
  684. else {
  685. $states = features_get_component_states(array($feature->name), FALSE);
  686. foreach ($states[$feature->name] as $component => $state) {
  687. $revertable_states = array(FEATURES_OVERRIDDEN, FEATURES_NEEDS_REVIEW, FEATURES_REBUILDABLE);
  688. if (in_array($state, $revertable_states) && features_hook($component, 'features_revert')) {
  689. $components[] = $component;
  690. }
  691. }
  692. }
  693. if (!empty($components_needed) && is_array($components_needed)) {
  694. $components = array_intersect($components, $components_needed);
  695. }
  696. if (empty($components)) {
  697. drush_log(dt('Current state already matches defaults, aborting.'), 'ok');
  698. }
  699. else {
  700. foreach ($components as $component) {
  701. $dt_args['@component'] = $component;
  702. $confirmation_message = 'Do you really want to revert @module.@component?';
  703. if ($skip_confirmation || drush_confirm(dt($confirmation_message, $dt_args))) {
  704. if (features_feature_is_locked($module, $component)) {
  705. drush_log(dt('Skipping locked @module.@component.', $dt_args), 'ok');
  706. }
  707. else {
  708. features_revert(array($module => array($component)));
  709. drush_log(dt('Reverted @module.@component.', $dt_args), 'ok');
  710. }
  711. }
  712. else {
  713. drush_log(dt('Skipping @module.@component.', $dt_args), 'ok');
  714. }
  715. }
  716. }
  717. }
  718. elseif ($feature) {
  719. _features_drush_set_error($module, 'FEATURES_FEATURE_NOT_ENABLED');
  720. }
  721. else {
  722. _features_drush_set_error($module);
  723. }
  724. }
  725. }
  726. else {
  727. drush_features_list();
  728. return;
  729. }
  730. }
  731. /**
  732. * Revert all enabled features to their definitions in code.
  733. *
  734. * @param ...
  735. * (Optional) A list of features to exclude from being reverted.
  736. */
  737. function drush_features_revert_all() {
  738. module_load_include('inc', 'features', 'features.export');
  739. $force = drush_get_option('force');
  740. $features_to_exclude = func_get_args();
  741. $features_to_revert = array();
  742. foreach (features_get_features(NULL, TRUE) as $module) {
  743. if ($module->status && !in_array($module->name, $features_to_exclude)) {
  744. // If forced, add module regardless of status.
  745. if ($force) {
  746. $features_to_revert[] = $module->name;
  747. }
  748. else {
  749. switch (features_get_storage($module->name)) {
  750. case FEATURES_OVERRIDDEN:
  751. case FEATURES_NEEDS_REVIEW:
  752. case FEATURES_REBUILDABLE:
  753. $features_to_revert[] = $module->name;
  754. break;
  755. }
  756. }
  757. }
  758. }
  759. if ($features_to_revert) {
  760. $dt_args = array('!modules' => implode(', ', $features_to_revert));
  761. drush_print(dt('The following modules will be reverted: !modules', $dt_args));
  762. // Confirm that the user would like to continue. If not, simply abort.
  763. if (!drush_confirm(dt('Do you really want to continue?'))) {
  764. return drush_user_abort('Aborting.');
  765. }
  766. drush_invoke('features-revert', $features_to_revert);
  767. }
  768. else {
  769. drush_log(dt('Current state already matches defaults, aborting.'), 'ok');
  770. }
  771. }
  772. /**
  773. * Show the diff of a feature module.
  774. */
  775. function drush_features_diff() {
  776. if (!$args = func_get_args()) {
  777. drush_features_list();
  778. return;
  779. }
  780. $module = $args[0];
  781. $filter_ctypes = drush_get_option("ctypes");
  782. if ($filter_ctypes) {
  783. $filter_ctypes = explode(',', $filter_ctypes);
  784. }
  785. $feature = features_load_feature($module);
  786. if (!module_exists($module)) {
  787. drush_log(dt('No such feature is enabled: ' . $module), 'error');
  788. return;
  789. }
  790. module_load_include('inc', 'features', 'features.export');
  791. $overrides = features_detect_overrides($feature);
  792. if (empty($overrides)) {
  793. drush_log(dt('Feature is in its default state. No diff needed.'), 'ok');
  794. return;
  795. }
  796. module_load_include('inc', 'diff', 'diff.engine');
  797. if (!class_exists('DiffFormatter')) {
  798. if (drush_confirm(dt('It seems that the Diff module is not available. Would you like to download and enable it?'))) {
  799. // Download it if it's not already here.
  800. $project_info = drush_get_projects();
  801. if (empty($project_info['diff']) && !drush_invoke('dl', array('diff'))) {
  802. return drush_set_error(dt('Diff module could not be downloaded.'));
  803. }
  804. if (!drush_invoke('en', array('diff'))) {
  805. return drush_set_error(dt('Diff module could not be enabled.'));
  806. }
  807. }
  808. else {
  809. return drush_set_error(dt('Diff module is not enabled.'));
  810. }
  811. // If we're still here, now we can include the diff.engine again.
  812. module_load_include('inc', 'diff', 'diff.engine');
  813. }
  814. $lines = (int) drush_get_option('lines');
  815. $lines = $lines > 0 ? $lines : 2;
  816. $formatter = new DiffFormatter();
  817. $formatter->leading_context_lines = $lines;
  818. $formatter->trailing_context_lines = $lines;
  819. $formatter->show_header = FALSE;
  820. if (drush_get_context('DRUSH_NOCOLOR')) {
  821. $red = $green = "%s";
  822. }
  823. else {
  824. $red = "\033[31;40m\033[1m%s\033[0m";
  825. $green = "\033[0;32;40m\033[1m%s\033[0m";
  826. }
  827. // Print key for colors
  828. drush_print(dt('Legend: '));
  829. drush_print(sprintf($red, dt('Code: drush features-revert will remove the overrides.')));
  830. drush_print(sprintf($green, dt('Overrides: drush features-update will update the exported feature with the displayed overrides')));
  831. drush_print();
  832. if ($filter_ctypes) {
  833. $overrides = array_intersect_key($overrides, array_flip($filter_ctypes));
  834. }
  835. foreach ($overrides as $component => $items) {
  836. $diff = new Diff(explode("\n", $items['default']), explode("\n", $items['normal']));
  837. drush_print();
  838. drush_print(dt("Component type: !component", array('!component' => $component)));
  839. $rows = explode("\n", $formatter->format($diff));
  840. foreach ($rows as $row) {
  841. if (strpos($row, '>') === 0) {
  842. drush_print(sprintf($green, $row));
  843. }
  844. elseif (strpos($row, '<') === 0) {
  845. drush_print(sprintf($red, $row));
  846. }
  847. else {
  848. drush_print($row);
  849. }
  850. }
  851. }
  852. }
  853. /**
  854. * Diff all enabled features that are not in their default state.
  855. *
  856. * @param ...
  857. * (Optional) A list of features to exclude from being reverted.
  858. */
  859. function drush_features_diff_all() {
  860. module_load_include('inc', 'features', 'features.export');
  861. $features_to_exclude = func_get_args();
  862. $features_to_revert = array();
  863. foreach (features_get_features(NULL, TRUE) as $module) {
  864. if ($module->status && !in_array($module->name, $features_to_exclude)) {
  865. switch (features_get_storage($module->name)) {
  866. case FEATURES_OVERRIDDEN:
  867. case FEATURES_NEEDS_REVIEW:
  868. case FEATURES_REBUILDABLE:
  869. $features_to_diff[] = $module->name;
  870. break;
  871. }
  872. }
  873. }
  874. if ($features_to_diff) {
  875. // Check if the user wants to apply the force option.
  876. $force = drush_get_option('force');
  877. if($force) {
  878. foreach ($features_to_diff as $module) {
  879. drush_print(dt('Diff for !module:', array('!module' => $module)));
  880. drush_invoke_process(drush_sitealias_get_record('@self'), 'features-diff', array($module));
  881. }
  882. }
  883. else {
  884. drush_print(dt('A diff will be performed for the following modules: !modules',
  885. array('!modules' => implode(', ', $features_to_diff))
  886. ));
  887. if (drush_confirm(dt('Do you want to continue?'))) {
  888. foreach ($features_to_diff as $module) {
  889. if (drush_confirm(dt('Diff !module?', array('!module' => $module)))) {
  890. drush_invoke_process(drush_sitealias_get_record('@self'), 'features-diff', array($module));
  891. }
  892. }
  893. }
  894. else {
  895. return drush_user_abort('Aborting.');
  896. }
  897. }
  898. }
  899. }
  900. /**
  901. * Helper function to call drush_set_error().
  902. *
  903. * @param $feature
  904. * The string name of the feature.
  905. * @param $error
  906. * A text string identifying the type of error.
  907. * @return
  908. * FALSE. See drush_set_error().
  909. */
  910. function _features_drush_set_error($feature, $error = '') {
  911. $args = array('!feature' => $feature);
  912. switch ($error) {
  913. case 'FEATURES_FEATURE_NOT_ENABLED':
  914. $message = 'The feature !feature is not enabled.';
  915. break;
  916. case 'FEATURES_COMPONENT_NOT_FOUND':
  917. $message = 'The given component !feature could not be found.';
  918. break;
  919. default:
  920. $error = 'FEATURES_FEATURE_NOT_FOUND';
  921. $message = 'The feature !feature could not be found.';
  922. }
  923. return drush_set_error($error, dt($message, $args));
  924. }