| Commit | Line | Data |
|---|---|---|
| 3b62ca0f | 1 | <?php |
| ce6e93de J |
2 | /** |
| 3 | * @file | |
| 4 | * Contains functions only needed for drush integration. | |
| 5 | */ | |
| 3b62ca0f J |
6 | |
| 7 | /** | |
| 8 | * Implementation of hook_drush_command(). | |
| 9 | */ | |
| 10 | function zen_drush_command() { | |
| 11 | $items = array(); | |
| 12 | ||
| 13 | $items['zen'] = array( | |
| 9b1f6b96 | 14 | 'description' => 'Create a theme using Zen.', |
| 3b62ca0f J |
15 | 'arguments' => array( |
| 16 | 'name' => 'A name for your theme.', | |
| 17 | ), | |
| 18 | 'options' => array( | |
| ce6e93de | 19 | 'machine-name' => '[a-z, 0-9] A machine-readable name for your theme.', |
| cdfd46b3 | 20 | // @TODO: Add these options: |
| 9b1f6b96 J |
21 | // 'without-rtl' => 'Whether to remove all RTL stylesheets.', |
| 22 | // 'layout' => '[fixed,fluid,960gs] Choose the page layout method.', | |
| 3b62ca0f J |
23 | ), |
| 24 | 'examples' => array( | |
| 9b1f6b96 | 25 | 'drush zen "My theme name"' => 'Create a sub-theme, using the default options.', |
| 3b62ca0f J |
26 | ), |
| 27 | ); | |
| 28 | ||
| 29 | return $items; | |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| ce6e93de | 33 | * Set up a Zen sub-theme with the starter kit. |
| 3b62ca0f J |
34 | */ |
| 35 | function drush_zen($name = 'My theme') { | |
| 36 | ||
| 227b420c | 37 | // Determine the machine name. |
| 741acfcd | 38 | $machine_name = drush_get_option('machine-name'); |
| ce6e93de | 39 | if (!$machine_name) { |
| 227b420c | 40 | $machine_name = $name; |
| ce6e93de | 41 | } |
| 227b420c J |
42 | $machine_name = str_replace(' ', '_', strtolower($machine_name)); |
| 43 | $search = array( | |
| 44 | '/[^a-z0-9_]/', // Remove characters not valid in function names. | |
| 45 | '/^[^a-z]+/', // Functions must begin with an alpha character. | |
| 46 | ); | |
| 47 | $machine_name = preg_replace($search, '', $machine_name); | |
| 3b62ca0f J |
48 | |
| 49 | $zen_path = drush_locate_root() . '/' . drupal_get_path('theme', 'zen'); | |
| 50 | ||
| 51 | // From Zen's location, we move up one directory and construct the path where | |
| 52 | // our sub theme will be created. | |
| ce6e93de J |
53 | $subtheme_path = explode('/', $zen_path); |
| 54 | array_pop($subtheme_path); | |
| 55 | $subtheme_path = implode('/', $subtheme_path) . '/' . str_replace('_', '-', $machine_name); | |
| 3b62ca0f J |
56 | |
| 57 | // Make a fresh copy of the original starter kit. | |
| ce6e93de | 58 | drush_op('zen_copy', $zen_path . '/STARTERKIT', $subtheme_path); |
| 3b62ca0f J |
59 | |
| 60 | // Rename the info file and fill in the theme name. | |
| ce6e93de J |
61 | drush_op('zen_file_str_replace', $subtheme_path . '/STARTERKIT.info.txt', '= Zen Sub-theme Starter Kit', '= ' . $name); |
| 62 | drush_op('rename', $subtheme_path . '/STARTERKIT.info.txt', $subtheme_path . '/' . $machine_name . '.info'); | |
| 3b62ca0f J |
63 | |
| 64 | // Replace all occurences of 'STARTERKIT' with the machine name of our sub theme. | |
| ce6e93de J |
65 | drush_op('zen_file_str_replace', $subtheme_path . '/theme-settings.php', 'STARTERKIT', $machine_name); |
| 66 | drush_op('zen_file_str_replace', $subtheme_path . '/template.php', 'STARTERKIT', $machine_name); | |
| 3b62ca0f J |
67 | |
| 68 | // Notify user of the newly created theme. | |
| ce6e93de J |
69 | drush_print(dt('Starter kit for "!name" created in: !path', array( |
| 70 | '!name' => $name, | |
| 71 | '!path' => $subtheme_path, | |
| 72 | ))); | |
| 3b62ca0f J |
73 | } |
| 74 | ||
| 75 | /** | |
| ce6e93de | 76 | * Copy a directory recursively. |
| 3b62ca0f | 77 | */ |
| 39fff1d4 | 78 | function zen_copy($source_dir, $target_dir, $ignore = '/^(\.(\.)?|CVS|\.svn|\.git|\.DS_Store)$/') { |
| 3b62ca0f | 79 | if (!is_dir($source_dir)) { |
| ce6e93de | 80 | drush_die(dt('The directory "!directory" was not found.', array('!directory' => $source_dir))); |
| 3b62ca0f J |
81 | } |
| 82 | $dir = opendir($source_dir); | |
| 83 | @mkdir($target_dir); | |
| 84 | while($file = readdir($dir)) { | |
| 85 | if (!preg_match($ignore, $file)) { | |
| ce6e93de J |
86 | if (is_dir($source_dir . '/' . $file)) { |
| 87 | zen_copy($source_dir . '/' . $file, $target_dir . '/' . $file, $ignore); | |
| 3b62ca0f J |
88 | } |
| 89 | else { | |
| ce6e93de | 90 | copy($source_dir . '/' . $file, $target_dir . '/' . $file); |
| 3b62ca0f J |
91 | } |
| 92 | } | |
| 93 | } | |
| 94 | closedir($dir); | |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| ce6e93de | 98 | * Replace strings in a file. |
| 3b62ca0f J |
99 | */ |
| 100 | function zen_file_str_replace($file_path, $find, $replace) { | |
| 101 | $file_contents = file_get_contents($file_path); | |
| 102 | $file_contents = str_replace($find, $replace, $file_contents); | |
| 103 | file_put_contents($file_path, $file_contents); | |
| 104 | } |