drush bp command confirms SASS
[project/boilerplate.git] / boilerplate.drush.inc
1 <?php
2 /**
3 * @file
4 * Contains functions only needed for drush integration.
5 */
6
7 /**
8 * Implementation of hook_drush_command().
9 */
10 function boilerplate_drush_command() {
11 $items = array();
12
13 $items['boilerplate'] = array(
14 'description' => 'Generate a theme based on Boilerplate with your customizations.',
15 'arguments' => array(
16 'name' => 'A name for your new theme.',
17 'machine_name' => '[optional] A machine-readable name for your theme.',
18 ),
19 'options' => array(
20 'name' => 'A name for your new theme.',
21 'machine-name' => '[a-z, 0-9] A machine-readable name for your theme.',
22 'description' => 'A description of your new theme.',
23 ),
24 'examples' => array(
25 'drush boilerplate "My theme name"' => 'Create a new theme based on Boilerplate.',
26 'drush boilerplate "My theme name" my_theme' => 'Create a new theme with a specific machine name.',
27 ),
28 'aliases' => array('bp'),
29 );
30
31 return $items;
32 }
33
34 /**
35 * Implementation of hook_drush_help().
36 */
37 function boilerplate_drush_help($section) {
38 switch ($section) {
39 case 'drush:boilerplate':
40 return dt("Generate a theme from Boilerplate with your customizations.");
41 case 'meta:boilerplate:summary':
42 return dt("Automates creating a Boilerplate based theme.");
43 }
44 }
45
46 /*
47 * Create a customized Boilerplate instance
48 * Some of this code is from zen drush integration
49 */
50 function drush_boilerplate($name = NULL, $machine_name = NULL) {
51 // Determine the theme name.
52 if (!isset($name)) {
53 $name = drush_get_option('name');
54 }
55
56 // Determine the machine name.
57 if (!isset($machine_name)) {
58 $machine_name = drush_get_option('machine-name');
59 }
60 if (!$machine_name) {
61 $machine_name = $name;
62 }
63 $machine_name = str_replace(' ', '_', strtolower($machine_name));
64 $search = array(
65 '/[^a-z0-9_]/', // Remove characters not valid in function names.
66 '/^[^a-z]+/', // Functions must begin with an alpha character.
67 );
68 $machine_name = preg_replace($search, '', $machine_name);
69
70 // Determine the path to the new subtheme by finding the path to boilerplate.
71 system_rebuild_theme_data();
72 $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT'). '/';
73 $boilerplate_path = drupal_get_path('theme', 'boilerplate');
74 if (preg_match('/profiles\//', $boilerplate_path)) {
75 $newtheme_path = 'sites/all/themes/custom/';
76 if (file_prepare_directory($newtheme_path)) {
77 $newtheme_path = 'sites/all/themes/custom/'.
78 str_replace('_', '-', $machine_name);
79 }
80 else {
81 $newtheme_path = 'sites/all/themes/'.
82 str_replace('_', '-', $machine_name);
83 }
84 }
85 else {
86 $newtheme_path = str_replace('boilerplate',
87 str_replace('_', '-', $machine_name),
88 $drupal_root.$boilerplate_path);
89 }
90 // Prefer sites/[host]/themes/custom if it exists
91 $custom = str_replace('/contrib/', '/custom/', $newtheme_path, &$count);
92 if ($count and is_dir(preg_replace('|/[^/]+$|', '', $custom))) {
93 $newtheme_path = $custom;
94 }
95
96 // Make a copy with our changes.
97 drush_op('boilerplate_copy', $boilerplate_path . '/', $newtheme_path . '/');
98
99 // Rename the .info file.
100 $newtheme_info_file = $newtheme_path . '/' . $machine_name . '.info';
101 drush_op('rename', $newtheme_path . '/boilerplate.info', $newtheme_info_file);
102
103 // Alter the contents of the .info file based on the command options.
104 $alterations = array(
105 '= Boilerplate' => '= ' . $name,
106 'version = "1.0"' => '',
107 'project = "Boilerplate"' => ''
108 );
109 if(drush_confirm('Will you be using SASS?')) {
110 $alterations['css/style.css'] = 'css/compiled/style.css';
111 }
112 if ($description = drush_get_option('description')) {
113 $alterations['Metaltoad HTML5/SASS base theme for Drupal 7'] = $description;
114 }
115 drush_op('boilerplate_file_str_replace', $newtheme_info_file, array_keys($alterations), $alterations);
116
117 // Remove the packaging script signature from the end of the info file
118 drush_op('boilerplate_file_preg_replace', $newtheme_info_file, '/\n; Information added by drupal.*\Z/s', '');
119
120 // Replace all occurrences of 'Boilerplate' with the machine name of our new theme.
121 drush_op('boilerplate_file_str_replace', $newtheme_path . '/theme-settings.php', 'boilerplate', $machine_name);
122 drush_op('boilerplate_file_str_replace', $newtheme_path . '/template.php', 'boilerplate', $machine_name);
123
124 // Notify user of the newly created theme.
125 drush_print(dt('New theme, "!name" created in: !path', array(
126 '!name' => $name,
127 '!path' => $newtheme_path,
128 )));
129 }
130
131 /**
132 * Copy a directory recursively.
133 */
134 function boilerplate_copy($source_dir, $target_dir, $ignore = '/^(\.(\.)?|boilerplate.drush.inc|CVS|\.svn|\.git|\.DS_Store|\.sass-cache)$/') {
135 if (!is_dir($source_dir)) {
136 drush_die(dt('The directory "!directory" was not found.', array('!directory' => $source_dir)));
137 }
138 $dir = opendir($source_dir);
139 @mkdir($target_dir);
140 while($file = readdir($dir)) {
141 if (!preg_match($ignore, $file)) {
142 if (is_dir($source_dir . '/' . $file)) {
143 boilerplate_copy($source_dir . '/' . $file, $target_dir . '/' . $file, $ignore);
144 }
145 else {
146 copy($source_dir . '/' . $file, $target_dir . '/' . $file);
147 }
148 }
149 }
150 closedir($dir);
151 }
152
153 /**
154 * Replace strings in a file.
155 */
156 function boilerplate_file_str_replace($file_path, $find, $replace) {
157 $file_contents = file_get_contents($file_path);
158 $file_contents = str_replace($find, $replace, $file_contents);
159 file_put_contents($file_path, $file_contents);
160 }
161 /**
162 * Replace strings in a file (with feeling)
163 */
164 function boilerplate_file_preg_replace($file_path, $pattern, $replace) {
165 $file_contents = file_get_contents($file_path);
166 $file_contents = preg_replace($pattern, $replace, $file_contents);
167 file_put_contents($file_path, $file_contents);
168 }