file uikit.drush.inc

Drush integration of UIkit.

File

uikit.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration of UIkit.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function uikit_drush_command() {
  10. $commands = [];
  11. $commands['uikit-starterkit'] = [
  12. 'description' => 'Create a UIkit sub-theme.',
  13. 'arguments' => [
  14. 'machine_name' => '[optional] A machine-readable name for your theme.',
  15. 'name' => 'A human-readable name for your theme.',
  16. ],
  17. 'aliases' => ['uikit-sk'],
  18. 'options' => [
  19. 'name' => 'A human-readable name for your theme.',
  20. 'machine-name' => '[a-z, 0-9, _] A machine-readable name for your theme.',
  21. 'path' => 'The path where your theme will be created. Defaults to: themes',
  22. 'description' => 'A description of your theme.',
  23. ],
  24. 'examples' => [
  25. 'drush uikit-sk "Amazing name"' => 'Create a sub-theme, using the default options.',
  26. 'drush uikit-sk zomg_amazing "Amazing name"' => 'Create a sub-theme with a specific machine name.',
  27. 'drush uikit-sk "Amazing name" --path=sites/default/themes --description="So amazing."' => 'Create a sub-theme in the specified directory with a custom description.',
  28. ],
  29. ];
  30. return $commands;
  31. }
  32. /**
  33. * Create a UIkit sub-theme using the starter kit.
  34. */
  35. function drush_uikit_starterkit($machine_name = NULL, $name = NULL) {
  36. // Determine the theme name and machine name.
  37. if (!isset($name)) {
  38. // If only given one argument, it is the $name.
  39. if (isset($machine_name)) {
  40. $name = $machine_name;
  41. unset($machine_name);
  42. }
  43. else {
  44. $name = drush_get_option('name');
  45. }
  46. if (!isset($machine_name)) {
  47. $machine_name = drush_get_option('machine-name');
  48. }
  49. }
  50. if (!$name) {
  51. if ($machine_name) {
  52. $name = $machine_name;
  53. }
  54. else {
  55. if (FALSE) {
  56. // Allow localize.drupal.org to pick up the string to translate.
  57. t('The name of the theme was not specified.');
  58. }
  59. return drush_set_error('UIKIT_SUBTHEME_NAME_NOT_SPECIFIED', dt('The name of the theme was not specified.'));
  60. }
  61. }
  62. if (!$machine_name) {
  63. $machine_name = $name;
  64. }
  65. // Clean up the machine name.
  66. $machine_name = str_replace(' ', '_', strtolower($machine_name));
  67. $search = [
  68. '/[^a-z0-9_]/', // Remove characters not valid in function names.
  69. '/^[^a-z]+/', // Functions must begin with an alpha character.
  70. ];
  71. $machine_name = preg_replace($search, '', $machine_name);
  72. // Determine the path to the new sub-theme.
  73. $subtheme_path = file_exists(DRUPAL_ROOT . '/themes/contrib') ? 'themes/custom' : 'themes';
  74. if ($path = drush_get_option('path')) {
  75. $subtheme_path = drush_trim_path($path);
  76. }
  77. $subtheme_path = drush_normalize_path(drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . $subtheme_path . '/' . $machine_name);
  78. // Ensure the destination directory exists.
  79. if (!is_dir(dirname($subtheme_path))) {
  80. if (FALSE) {
  81. // Allow localize.drupal.org to pick up the string to translate.
  82. t('The directory "!directory" was not found.', ['!directory' => dirname($subtheme_path)]);
  83. }
  84. return drush_set_error('UIKIT_DESTINATION_NOT_FOUND', dt('The directory "!directory" was not found.', ['!directory' => dirname($subtheme_path)]));
  85. }
  86. // Ensure the STARTERKIT directory exists.
  87. $starterkit_path = drush_normalize_path(drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . drupal_get_path('theme', 'uikit') . '/STARTERKIT');
  88. if (!is_dir($starterkit_path)) {
  89. if (FALSE) {
  90. // Allow localize.drupal.org to pick up the string to translate.
  91. t('The STARTERKIT directory was not found in "!directory"', ['!directory' => dirname($starterkit_path)]);
  92. }
  93. return drush_set_error('UIKIT_STARTERKIT_NOT_FOUND', dt('The STARTERKIT directory was not found in "!directory"', ['!directory' => dirname($starterkit_path)]));
  94. }
  95. // Make a fresh copy of the original starter kit.
  96. drush_op('drush_copy_dir', $starterkit_path, $subtheme_path);
  97. // Rename the STARTERKIT files.
  98. $subtheme_info_file = $subtheme_path . '/' . $machine_name . '.info.yml';
  99. $subtheme_libraries_file = $subtheme_path . '/' . $machine_name . '.libraries.yml';
  100. $subtheme_theme_file = $subtheme_path . '/' . $machine_name . '.theme';
  101. $subtheme_css_base_file = $subtheme_path . '/css/' . $machine_name . '.base.css';
  102. $subtheme_css_component_file = $subtheme_path . '/css/' . $machine_name . '.component.css';
  103. $subtheme_css_layout_file = $subtheme_path . '/css/' . $machine_name . '.layout.css';
  104. $subtheme_css_theme_file = $subtheme_path . '/css/' . $machine_name . '.theme.css';
  105. $subtheme_js_theme_file = $subtheme_path . '/js/' . $machine_name . '.theme.js';
  106. $subtheme_settings_file = $subtheme_path . '/config/install/' . $machine_name . '.settings.yml';
  107. $subtheme_block_account_menu_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_account_menu.yml';
  108. $subtheme_block_account_menu_offcanvas_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_account_menu_offcanvas.yml';
  109. $subtheme_block_branding_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_branding.yml';
  110. $subtheme_block_breadcrumbs_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_breadcrumbs.yml';
  111. $subtheme_block_content_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_content.yml';
  112. $subtheme_block_footer_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_footer.yml';
  113. $subtheme_block_help_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_help.yml';
  114. $subtheme_block_local_actions_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_local_actions.yml';
  115. $subtheme_block_local_tasks_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_local_tasks.yml';
  116. $subtheme_block_main_menu_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_main_menu.yml';
  117. $subtheme_block_main_menu_offcanvas_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_main_menu_offcanvas.yml';
  118. $subtheme_block_messages_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_messages.yml';
  119. $subtheme_block_page_title_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_page_title.yml';
  120. $subtheme_block_powered_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_powered.yml';
  121. $subtheme_block_search_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_search.yml';
  122. $subtheme_block_tools_file = $subtheme_path . '/config/optional/block.block.' . $machine_name . '_tools.yml';
  123. $subtheme_block_schema_file = $subtheme_path . '/config/schema/' . $machine_name . '.schema.yml';
  124. drush_op('rename', drush_normalize_path($subtheme_path . '/STARTERKIT.info.ymltmp'), drush_normalize_path($subtheme_info_file));
  125. drush_op('rename', drush_normalize_path($subtheme_path . '/STARTERKIT.libraries.yml'), drush_normalize_path($subtheme_libraries_file));
  126. drush_op('rename', drush_normalize_path($subtheme_path . '/STARTERKIT.theme'), drush_normalize_path($subtheme_theme_file));
  127. drush_op('rename', drush_normalize_path($subtheme_path . '/css/STARTERKIT.base.css'), drush_normalize_path($subtheme_css_base_file));
  128. drush_op('rename', drush_normalize_path($subtheme_path . '/css/STARTERKIT.component.css'), drush_normalize_path($subtheme_css_component_file));
  129. drush_op('rename', drush_normalize_path($subtheme_path . '/css/STARTERKIT.layout.css'), drush_normalize_path($subtheme_css_layout_file));
  130. drush_op('rename', drush_normalize_path($subtheme_path . '/css/STARTERKIT.theme.css'), drush_normalize_path($subtheme_css_theme_file));
  131. drush_op('rename', drush_normalize_path($subtheme_path . '/js/STARTERKIT.theme.js'), drush_normalize_path($subtheme_js_theme_file));
  132. drush_op('rename', drush_normalize_path($subtheme_path . '/config/install/STARTERKIT.settings.yml'), drush_normalize_path($subtheme_settings_file));
  133. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_account_menu.yml'), drush_normalize_path($subtheme_block_account_menu_file));
  134. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_account_menu_offcanvas.yml'), drush_normalize_path($subtheme_block_account_menu_offcanvas_file));
  135. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_branding.yml'), drush_normalize_path($subtheme_block_branding_file));
  136. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_breadcrumbs.yml'), drush_normalize_path($subtheme_block_breadcrumbs_file));
  137. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_content.yml'), drush_normalize_path($subtheme_block_content_file));
  138. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_footer.yml'), drush_normalize_path($subtheme_block_footer_file));
  139. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_help.yml'), drush_normalize_path($subtheme_block_help_file));
  140. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_local_actions.yml'), drush_normalize_path($subtheme_block_local_actions_file));
  141. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_local_tasks.yml'), drush_normalize_path($subtheme_block_local_tasks_file));
  142. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_main_menu.yml'), drush_normalize_path($subtheme_block_main_menu_file));
  143. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_main_menu_offcanvas.yml'), drush_normalize_path($subtheme_block_main_menu_offcanvas_file));
  144. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_messages.yml'), drush_normalize_path($subtheme_block_messages_file));
  145. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_page_title.yml'), drush_normalize_path($subtheme_block_page_title_file));
  146. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_powered.yml'), drush_normalize_path($subtheme_block_powered_file));
  147. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_search.yml'), drush_normalize_path($subtheme_block_search_file));
  148. drush_op('rename', drush_normalize_path($subtheme_path . '/config/optional/block.block.STARTERKIT_tools.yml'), drush_normalize_path($subtheme_block_tools_file));
  149. drush_op('rename', drush_normalize_path($subtheme_path . '/config/schema/STARTERKIT.schema.yml'), drush_normalize_path($subtheme_block_schema_file));
  150. // Alter the contents of the .info.yml file based on the command options.
  151. $alterations = [
  152. 'name: STARTERKIT' => "name: '" . $name . "'",
  153. 'STARTERKIT/global-styling' => $machine_name . '/global-styling',
  154. ];
  155. if ($description = drush_get_option('description')) {
  156. $alterations['UIkit Sub-theme'] = $description;
  157. }
  158. drush_op('uikit_file_str_replace', $subtheme_info_file, array_keys($alterations), $alterations);
  159. drush_op('uikit_file_str_replace', $subtheme_info_file, 'STARTERKIT', $machine_name);
  160. // Replace all occurrences of 'STARTERKIT' with the machine name of our sub theme.
  161. drush_op('uikit_file_str_replace', $subtheme_path . '/README.txt', 'STARTERKIT', $name);
  162. drush_op('uikit_file_str_replace', $subtheme_libraries_file, 'STARTERKIT', $machine_name);
  163. drush_op('uikit_file_str_replace', $subtheme_theme_file, 'STARTERKIT', $machine_name);
  164. drush_op('uikit_file_str_replace', $subtheme_js_theme_file, 'Drupal.behaviors.STARTERKIT', str_replace(' ', '', 'Drupal.behaviors.' . $name));
  165. drush_op('uikit_file_str_replace', $subtheme_js_theme_file, 'STARTERKIT', $machine_name);
  166. drush_op('uikit_file_str_replace', $subtheme_path . '/theme-settings.php', 'STARTERKIT', $machine_name);
  167. drush_op('uikit_file_str_replace', $subtheme_path . '/includes/alter.inc', 'STARTERKIT', $machine_name);
  168. drush_op('uikit_file_str_replace', $subtheme_path . '/includes/preprocess.inc', 'STARTERKIT', $machine_name);
  169. drush_op('uikit_file_str_replace', $subtheme_path . '/includes/process.inc', 'STARTERKIT', $machine_name);
  170. drush_op('uikit_file_str_replace', $subtheme_path . '/includes/theme.inc', 'STARTERKIT', $machine_name);
  171. drush_op('uikit_file_str_replace', $subtheme_block_account_menu_file, 'STARTERKIT', $machine_name);
  172. drush_op('uikit_file_str_replace', $subtheme_block_account_menu_offcanvas_file, 'STARTERKIT', $machine_name);
  173. drush_op('uikit_file_str_replace', $subtheme_block_branding_file, 'STARTERKIT', $machine_name);
  174. drush_op('uikit_file_str_replace', $subtheme_block_breadcrumbs_file, 'STARTERKIT', $machine_name);
  175. drush_op('uikit_file_str_replace', $subtheme_block_content_file, 'STARTERKIT', $machine_name);
  176. drush_op('uikit_file_str_replace', $subtheme_block_footer_file, 'STARTERKIT', $machine_name);
  177. drush_op('uikit_file_str_replace', $subtheme_block_help_file, 'STARTERKIT', $machine_name);
  178. drush_op('uikit_file_str_replace', $subtheme_block_local_actions_file, 'STARTERKIT', $machine_name);
  179. drush_op('uikit_file_str_replace', $subtheme_block_local_tasks_file, 'STARTERKIT', $machine_name);
  180. drush_op('uikit_file_str_replace', $subtheme_block_main_menu_file, 'STARTERKIT', $machine_name);
  181. drush_op('uikit_file_str_replace', $subtheme_block_main_menu_offcanvas_file, 'STARTERKIT', $machine_name);
  182. drush_op('uikit_file_str_replace', $subtheme_block_messages_file, 'STARTERKIT', $machine_name);
  183. drush_op('uikit_file_str_replace', $subtheme_block_page_title_file, 'STARTERKIT', $machine_name);
  184. drush_op('uikit_file_str_replace', $subtheme_block_powered_file, 'STARTERKIT', $machine_name);
  185. drush_op('uikit_file_str_replace', $subtheme_block_search_file, 'STARTERKIT', $machine_name);
  186. drush_op('uikit_file_str_replace', $subtheme_block_tools_file, 'STARTERKIT', $machine_name);
  187. drush_op('uikit_file_str_replace', $subtheme_block_schema_file, 'STARTERKIT theme', $name . ' theme');
  188. drush_op('uikit_file_str_replace', $subtheme_block_schema_file, 'STARTERKIT.settings', $machine_name . '.settings');
  189. drush_op('uikit_file_str_replace', $subtheme_block_schema_file, 'STARTERKIT settings', $name . ' settings');
  190. // Notify user of the newly created theme.
  191. if (FALSE) {
  192. // Allow localize.drupal.org to pick up the string to translate.
  193. t('Starterkit for "!name" created in: !path', ['!name' => $name, '!path' => $subtheme_path]);
  194. }
  195. drush_print(dt('Starterkit for "!name" created in: !path', [
  196. '!path' => $subtheme_path,
  197. '!name' => $name,
  198. ]));
  199. }
  200. /**
  201. * Replace strings in a file.
  202. */
  203. function uikit_file_str_replace($file_path, $find, $replace) {
  204. $file_path = drush_normalize_path($file_path);
  205. $file_contents = file_get_contents($file_path);
  206. $file_contents = str_replace($find, $replace, $file_contents);
  207. file_put_contents($file_path, $file_contents);
  208. }

Functions

Name Description
drush_uikit_starterkitCreate a UIkit sub-theme using the starter kit.
uikit_drush_commandImplements hook_drush_command().
uikit_file_str_replaceReplace strings in a file.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Log in