Projet

Général

Profil

Paste
Télécharger (4,96 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / zen / zen-internals / zen.drush.inc @ 87dbc3bf

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 zen_drush_command() {
11
  $items = array();
12

    
13
  $items['zen'] = array(
14
    'description' => 'Create a theme using Zen.',
15
    'arguments' => array(
16
      'name'         => 'A name for your theme.',
17
      'machine_name' => '[optional] A machine-readable name for your theme.',
18
    ),
19
    'options' => array(
20
      'name'         => 'A name for your theme.',
21
      'machine-name' => '[a-z, 0-9] A machine-readable name for your theme.',
22
      'path'         => 'The path where your theme will be created. Defaults to: sites/all/themes',
23
      'description'  => 'A description of your theme.',
24
      'without-rtl'  => 'Remove all RTL stylesheets.',
25
    ),
26
    'examples' => array(
27
      'drush zen "My theme name"' => 'Create a sub-theme, using the default options.',
28
      'drush zen "My theme name" my_theme' => 'Create a sub-theme with a specific machine name.',
29
      'drush zen "My theme" --without-rtl --path=sites/default/themes' => 'Create a sub-theme in the specified directory without RTL stylesheets.',
30
    ),
31
  );
32

    
33
  return $items;
34
}
35

    
36
/**
37
 * Create a Zen sub-theme using the starter kit.
38
 */
39
function drush_zen($name = NULL, $machine_name = NULL) {
40
  // Determine the theme name.
41
  if (!isset($name)) {
42
    $name = drush_get_option('name');
43
  }
44

    
45
  // Determine the machine name.
46
  if (!isset($machine_name)) {
47
    $machine_name = drush_get_option('machine-name');
48
  }
49
  if (!$machine_name) {
50
    $machine_name = $name;
51
  }
52
  $machine_name = str_replace(' ', '_', strtolower($machine_name));
53
  $search = array(
54
    '/[^a-z0-9_]/', // Remove characters not valid in function names.
55
    '/^[^a-z]+/',   // Functions must begin with an alpha character.
56
  );
57
  $machine_name = preg_replace($search, '', $machine_name);
58

    
59
  // Determine the path to the new subtheme.
60
  $subtheme_path = 'sites/all/themes';
61
  if ($path = drush_get_option('path')) {
62
    $subtheme_path = drush_trim_path($path);
63
  }
64
  $subtheme_path = drush_normalize_path(drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . $subtheme_path . '/' . $machine_name);
65

    
66
  // Make a fresh copy of the original starter kit.
67
  $starter_path = drush_normalize_path(drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . drupal_get_path('theme', 'zen') . '/STARTERKIT');
68
  if (!is_dir(dirname($subtheme_path))) {
69
    drush_die(dt('The directory "!directory" was not found.', array('!directory' => dirname($subtheme_path))));
70
  }
71
  drush_op('drush_copy_dir', $starter_path, $subtheme_path);
72

    
73
  // Rename the .info file.
74
  $subtheme_info_file = $subtheme_path . '/' . $machine_name . '.info';
75
  drush_op('rename', drush_normalize_path($subtheme_path . '/STARTERKIT.info.txt'), drush_normalize_path($subtheme_info_file));
76

    
77
  // Alter the contents of the .info file based on the command options.
78
  $alterations = array(
79
    '= Zen Sub-theme Starter Kit' => '= ' . $name,
80
  );
81
  if ($description = drush_get_option('description')) {
82
    $alterations['Read the <a href="https://drupal.org/node/873778">online docs</a> or the included README.txt on how to create a theme with Zen.'] = $description;
83
  }
84
  drush_op('zen_file_str_replace', $subtheme_info_file, array_keys($alterations), $alterations);
85

    
86
  // Replace all occurrences of 'STARTERKIT' with the machine name of our sub theme.
87
  drush_op('zen_file_str_replace', $subtheme_path . '/theme-settings.php', 'STARTERKIT', $machine_name);
88
  drush_op('zen_file_str_replace', $subtheme_path . '/template.php', 'STARTERKIT', $machine_name);
89

    
90
  // Remove all RTL stylesheets.
91
  if ($without_rtl = drush_get_option('without-rtl')) {
92
    foreach (array('layouts/_fixed', 'layouts/_responsive', 'components/_misc', '_normalize', 'styles') as $file) {
93
      // Move any sub-directory into a separate variable.
94
      list($sub_dir, $css_file) = explode('/', $file . '/');
95
      if (!$css_file) {
96
        $css_file = $sub_dir;
97
        $sub_dir = '';
98
      }
99
      else {
100
        $sub_dir .= '/';
101
      }
102
      // Remove the RTL css file.
103
      $css_file = $sub_dir . (($css_file[0] == '_') ? substr($css_file, 1) : $css_file);
104
      drush_op('unlink', drush_normalize_path($subtheme_path . '/css/' . $css_file . '-rtl.css'));
105
      drush_op('zen_file_str_replace', $subtheme_path . '/css/' . $css_file . '.css', ' /* LTR */', '');
106
      // Remove the RTL sass file.
107
      drush_op('unlink', drush_normalize_path($subtheme_path . '/sass/' . $file . '-rtl.scss'));
108
      drush_op('zen_file_str_replace', $subtheme_path . '/sass/' . $file . '.scss', ' /* LTR */', '');
109
    }
110
  }
111

    
112
  // Notify user of the newly created theme.
113
  drush_print(dt('Starter kit for "!name" created in: !path', array(
114
    '!name' => $name,
115
    '!path' => $subtheme_path,
116
  )));
117
}
118

    
119
/**
120
 * Replace strings in a file.
121
 */
122
function zen_file_str_replace($file_path, $find, $replace) {
123
  $file_path = drush_normalize_path($file_path);
124
  $file_contents = file_get_contents($file_path);
125
  $file_contents = str_replace($find, $replace, $file_contents);
126
  file_put_contents($file_path, $file_contents);
127
}