Projet

Général

Profil

Paste
Télécharger (5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / adaptivetheme / at_core / adaptivetheme.drush.inc @ 87dbc3bf

1
<?php
2
/**
3
 * @file
4
 * Contains functions only needed for drush integration.
5
 *
6
 * Usage: drush adaptivetheme "Foobar Theme" foobartheme
7
 */
8

    
9
/**
10
 * Implementation of hook_drush_command().
11
 */
12
function adaptivetheme_drush_command() {
13
  $items = array();
14

    
15
  $items['adaptivetheme'] = array(
16
    'description' => 'Create a theme using adaptivetheme.',
17
    'arguments' => array(
18
      'name'         => 'A name for your theme.',
19
      'machine_name' => '[optional] A machine-readable name for your theme.',
20
    ),
21
    'options' => array(
22
      'name'         => 'A name for your theme.',
23
      'machine-name' => '[a-z, 0-9] A machine-readable name for your theme.',
24
      'description'  => 'A description of your theme.',
25
      'without-rtl'  => 'Remove all RTL stylesheets.',
26
      // @TODO potentially add settings defaults?
27
    ),
28
    'examples' => array(
29
      'drush adaptivetheme "My theme name"' => 'Create a sub-theme, using the default options.',
30
      'drush adaptivetheme "My theme name" my_theme' => 'Create a sub-theme with a specific machine name.',
31
    ),
32
  );
33

    
34
  return $items;
35
}
36

    
37
/**
38
 * Create a adaptivetheme sub-theme using the starter kit.
39
 */
40
function drush_adaptivetheme($name = NULL, $machine_name = NULL) {
41
  // Determine the theme name.
42
  if (!isset($name)) {
43
    if (!$name = drush_get_option('name')) {
44
      drush_print(dt('Please specify a name!' . "\n" . 'e.g., drush adaptivetheme "Your Theme Name" yourthemename'));
45
      return;
46
    }
47
  }
48

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

    
63
  // Determine the path to the new subtheme by finding the path to adaptivetheme.
64
  $adaptivetheme_path = drush_locate_root() . '/' . drupal_get_path('theme', 'adaptivetheme');
65
  $subtheme_path = explode('/', $adaptivetheme_path);
66
  array_pop($subtheme_path);
67
  array_pop($subtheme_path);
68
  $subtheme_path = implode('/', $subtheme_path) . '/' . str_replace('_', '-', $machine_name);
69

    
70
  // Make a fresh copy of the original subtheme.
71
  drush_op('adaptivetheme_copy', $adaptivetheme_path . '/../' .'at_subtheme', $subtheme_path);
72

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

    
77
  // Alter the contents of the .info file based on the command options.
78
  $alterations = array(
79
    '= AT Subtheme' => '= ' . $name,
80
    'project = "adaptivetheme"' => '', // attempt to strip out the project name added by the drupal packing script
81
  );
82
  if ($description = drush_get_option('description')) {
83
    $alterations['Starter subtheme for Adaptivetheme. Copy this subtheme to get started building your own Drupal theme. For help see our <b><a href="http://adaptivethemes.com/documentation/adaptivethemes-documentation" title="Adaptivethemes.com - Rocking the hardest since 2006">documentation</a></b>. If you have a problem and need additional help please use the <b><a href="http://drupal.org/project/issues/adaptivetheme">issue queue</a></b>.'] = $description;
84
  }
85
  drush_op('adaptivetheme_file_str_replace', $subtheme_info_file, array_keys($alterations), $alterations);
86

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

    
91
  // Notify user of the newly created theme.
92
  drush_print(dt('New Subtheme for "!name" created in: !path', array(
93
    '!name' => $name,
94
    '!path' => $subtheme_path,
95
  )));
96

    
97
  //system_rebuild_theme_data(); // potentially execute this here, see http://drupal.org/node/1235942
98
}
99

    
100
/**
101
 * Copy a directory recursively.
102
 */
103
function adaptivetheme_copy($source_dir, $target_dir, $ignore = '/^(\.(\.)?|CVS|\.svn|\.git|\.DS_Store)$/') {
104
  if (!is_dir($source_dir)) {
105
    drush_die(dt('The directory "!directory" was not found.', array('!directory' => $source_dir)));
106
  }
107
  $dir = opendir($source_dir);
108
  @mkdir($target_dir);
109
  while($file = readdir($dir)) {
110
    if (!preg_match($ignore, $file)) {
111
      if (is_dir($source_dir . '/' . $file)) {
112
        adaptivetheme_copy($source_dir . '/' . $file, $target_dir . '/' . $file, $ignore);
113
      }
114
      else {
115
        copy($source_dir . '/' . $file, $target_dir . '/' . $file);
116
      }
117
    }
118
  }
119
  closedir($dir);
120
}
121

    
122
/**
123
 * Replace strings in a file.
124
 */
125
function adaptivetheme_file_str_replace($file_path, $find, $replace) {
126
  $file_contents = file_get_contents($file_path);
127
  $file_contents = str_replace($find, $replace, $file_contents);
128
  file_put_contents($file_path, $file_contents);
129
}