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