PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/omega_tools/omega_tools.drush.inc

https://bitbucket.org/twinbit/ddayroma2011
Pascal | 141 lines | 29 code | 9 blank | 103 comment | 9 complexity | ae15af450ef3ab370f21b25da29e2a11 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * @file Drush sql commands
  4. */
  5. /**
  6. * Implementation of hook_drush_help().
  7. */
  8. function omega_tools_drush_help($section) {
  9. switch ($section) {
  10. case 'drush:omega-subtheme':
  11. return dt('Creates a valid Omega subtheme in sites/all/themes based on a single argument, the name of the subtheme');
  12. }
  13. }
  14. /**
  15. * Implementation of hook_drush_command().
  16. */
  17. function omega_tools_drush_command() {
  18. $items['omega-subtheme'] = array(
  19. 'description' => 'Creates an Omega subtheme.',
  20. 'callback' => 'drush_omega_subtheme',
  21. 'examples' => array(
  22. 'drush omega-subtheme "Subtheme Name"' => 'Create an HTML5 Omega subtheme to the default sites/all/themes folder named subtheme_name.',
  23. 'drush omega-subtheme "Subtheme Name" html5' => 'Create an HTML5 Omega subtheme to the default sites/all/themes folder named subtheme_name.',
  24. 'drush omega-subtheme "Subtheme Name" xhtml' => 'Create an XHTML Omega subtheme to the default sites/all/themes folder named subtheme_name.',
  25. 'drush omega-subtheme "Subtheme Name" --destination=example.com' => 'Create an HTML5 Omega subtheme to sites/example.com/themes folder named subtheme_name.',
  26. ),
  27. 'arguments' => array(
  28. 'name' => 'The name of the subtheme to create.',
  29. 'type' => 'The type of subtheme to create. XHTML or HTML5 (default)',
  30. ),
  31. 'options' => array(
  32. '--destination' => 'The site specific folder name in /sites where the information should be saved. /sites/all by default.',
  33. ),
  34. );
  35. return $items;
  36. }
  37. function drush_omega_subtheme($name, $type = 'html5') {
  38. if (isset($name)) {
  39. $exec = drush_omega_tools_subtheme($name, $type);
  40. // Avoid the php memory of the $output array in drush_shell_exec().
  41. if ($exec) {
  42. $return = drush_op('system', $exec);
  43. return $return;
  44. }
  45. }
  46. else {
  47. return drush_set_error(dt('You must delcare the name of the new subtheme. See drush help omega-subtheme for usage information.'));
  48. }
  49. }
  50. function drush_omega_tools_subtheme($name, $type) {
  51. $name_long = $name;
  52. // replace non-alphanumeric characters with an underscore
  53. $name = strtolower(preg_replace('/\W/', '_', $name));
  54. //clean up leading/trailing and double underscores
  55. $name = str_replace('__', '_', $name);
  56. $name = trim($name, '_');
  57. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT') .'/';
  58. $type = strtolower($type);
  59. if ($path = drush_get_option('destination')) {
  60. $site_dir = $drupal_root .'sites/'. $path;
  61. $theme_dir = $site_dir .'/themes';
  62. }
  63. else {
  64. $site_dir = $drupal_root .'sites/all';
  65. $theme_dir = $site_dir .'/themes';
  66. }
  67. switch ($type) {
  68. case 'html5':
  69. $omega_path = $drupal_root . drupal_get_path('theme', 'starterkit_omega_html5');
  70. break;
  71. case 'xhtml':
  72. $omega_path = $drupal_root . drupal_get_path('theme', 'starterkit_omega_xhtml');
  73. break;
  74. default:
  75. drush_set_error(dt('The only valid arguments for $type are HTML5 (default) or XHTML. Please modify your command and try again. See drush help omega-subtheme for usage information.'));
  76. return FALSE;
  77. break;
  78. }
  79. if (!is_dir($omega_path)) {
  80. drush_set_error(dt('Omega base theme not found. You must download the Omega theme to create a subtheme of Omega. See drush help omega-subtheme for usage information.'));
  81. return FALSE;
  82. }
  83. if (!is_dir($site_dir)) {
  84. drush_set_error(dt('The site directory: '. $site_dir .' does not exist. Please specify another, remove --destination to use /sites/all/themes. See drush help omega-subtheme for usage information.'));
  85. return FALSE;
  86. }
  87. if (!is_dir($theme_dir)) {
  88. if(!drush_op('mkdir', $theme_dir)) {
  89. drush_set_error(dt('The theme directory: '. $theme_dir .' does not exist. An attempt to automagically create this folder failed. See drush help omega-subtheme for usage information.'));
  90. return FALSE;
  91. }
  92. else {
  93. drush_log(dt('The theme directory: '. $theme_dir .' does not exist. It was created for you. I hope you don\'t mind!!'), 'ok');
  94. }
  95. }
  96. if (is_dir($site_dir .'/'. $name)) {
  97. drush_set_error(dt('The destination theme directory: '. $site_dir .' already exists. Please specify another theme name. See drush help omega-subtheme for usage information.'));
  98. return FALSE;
  99. }
  100. switch ($type) {
  101. case 'html5':
  102. // copy the starterkit directory to the new location
  103. $exec = 'rsync -r ' . $omega_path .'/* '. $theme_dir .'/'. $name;
  104. // replace instances of omega_starterkit in all php files
  105. //$exec .= '; sed -i.bak s/omega_starterkit/'. $name .'/ '. $theme_dir .'/'. $name .'/template.php';
  106. //$exec .= '; sed -i.bak s/omega_starterkit/'. $name .'/ '. $theme_dir .'/'. $name .'/theme-settings.php';
  107. // move the .info file
  108. $exec .= '; mv '. $theme_dir .'/'. $name .'/starterkit_omega_html5.info '. $theme_dir .'/'. $name .'/' . $name .'.info';
  109. // change the name of the theme in the .info file
  110. $exec .= '; sed -i.bak s/"Omega HTML5 Starterkit"/"'. $name_long .'"/ '. $theme_dir .'/'. $name .'/*.info';
  111. // remove the .bak files. I don't know how to run sed without the
  112. $exec .= '; rm -rf ' . $theme_dir .'/'. $name .'/*.bak';
  113. drush_log(dt('HTML5 Omega Subtheme: '. $name_long .' ('. $name .') created and configured for usage.'), 'ok');
  114. break;
  115. case 'xhtml':
  116. // copy the starterkit directory to the new location
  117. $exec = 'rsync -r ' . $omega_path .'/* '. $theme_dir .'/'. $name;
  118. // replace instances of omega_starterkit in all php files
  119. //$exec .= '; sed -i.bak s/omega_starterkit_xhtml/'. $name .'/ '. $theme_dir .'/'. $name .'/template.php';
  120. //$exec .= '; sed -i.bak s/omega_starterkit_xhtml/'. $name .'/ '. $theme_dir .'/'. $name .'/theme-settings.php';
  121. // move the .info file
  122. $exec .= '; mv '. $theme_dir .'/'. $name .'/starterkit_omega_xhtml.info '. $theme_dir .'/'. $name .'/' . $name .'.info';
  123. // change the name of the theme in the .info file
  124. $exec .= '; sed -i.bak s/"Omega XHTML Starter Kit"/"'. $name_long .'"/ '. $theme_dir .'/'. $name .'/*.info';
  125. // remove the .bak files. I don't know how to run sed without the
  126. $exec .= '; rm -rf ' . $theme_dir .'/'. $name .'/*.bak';
  127. drush_log(dt('XHTML Omega Subtheme: '. $name_long .' ('. $name .') created and configured for usage.'), 'ok');
  128. break;
  129. }
  130. return $exec;
  131. }