This needs a little work, but currently it will create a new subtheme within the adaptivetheme directory (although it should be created outside of it, in the standard themes dir).

This file should be called adaptivetheme.drush.inc and be placed in your ~/.drush folder for drush 4.4, anything later than 4.4 (4.x dev), this file can remain inside the adaptivetheme directory and will be available to drush.

The command to invoke it is:

drush adaptivetheme "Your Theme Name" yourthemename

adaptivetheme.drush.inc

<?php
/**
 * @file
 * Contains functions only needed for drush integration.
 */

/**
 * Implementation of hook_drush_command().
 */
function adaptivetheme_drush_command() {
  $items = array();

  $items['adaptivetheme'] = array(
    'description' => 'Create a theme using adaptivetheme.',
    'arguments' => array(
      'name'         => 'A name for your theme.',
      'machine_name' => '[optional] A machine-readable name for your theme.',
    ),
    'options' => array(
      'name'         => 'A name for your theme.',
      'machine-name' => '[a-z, 0-9] A machine-readable name for your theme.',
      'description'  => 'A description of your theme.',
      'without-rtl'  => 'Remove all RTL stylesheets.',
    ),
    'examples' => array(
      'drush adaptivetheme "My theme name"' => 'Create a sub-theme, using the default options.',
      'drush adaptivetheme "My theme name" my_theme' => 'Create a sub-theme with a specific machine name.',
    ),
  );

  return $items;
}

/**
 * Create a adaptivetheme sub-theme using the existing sub theme.
 */
function drush_adaptivetheme($name = NULL, $machine_name = NULL) {
  // Determine the theme name.
  if (!isset($name)) {
    $name = drush_get_option('name');
  }

  // Determine the machine name.
  if (!isset($machine_name)) {
    $machine_name = drush_get_option('machine-name');
  }
  if (!$machine_name) {
    $machine_name = $name;
  }
  $machine_name = str_replace(' ', '_', strtolower($machine_name));
  $search = array(
    '/[^a-z0-9_]/', // Remove characters not valid in function names.
    '/^[^a-z]+/',   // Functions must begin with an alpha character.
  );
  $machine_name = preg_replace($search, '', $machine_name);

  // Determine the path to the new subtheme by finding the path to adaptivetheme_subtheme.
  $adaptivetheme_subtheme_path = drush_locate_root() . '/' . drupal_get_path('theme', 'adaptivetheme_subtheme');
  $subtheme_path = explode('/', $adaptivetheme_subtheme_path);
  array_pop($subtheme_path);
  $subtheme_path = implode('/', $subtheme_path) . '/' . str_replace('_', '-', $machine_name);

  // Make a fresh copy of the original starter kit.
  drush_op('adaptivetheme_copy', $adaptivetheme_subtheme_path . '/', $subtheme_path);

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

  // Alter the contents of the .info file based on the command options.
  $alterations = array(
    '= AT Subtheme' => '= ' . $name,
  );
  if ($description = drush_get_option('description')) {
    $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/base-theme">documentation</a></b> and <b><a href="http://vimeo.com/channels/61157">video tutorials</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;
  }
  drush_op('adaptivetheme_file_str_replace', $subtheme_info_file, array_keys($alterations), $alterations);

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

  // Remove all RTL stylesheets.
  if ($without_rtl = drush_get_option('without-rtl')) {
    foreach (array('forms', 'html-reset', 'layout-fixed', 'layout-liquid', 'navigation', 'pages', 'tabs') as $file) {
      // Remove the RTL stylesheet.
      drush_op('unlink', $subtheme_path . '/css/' . $file . '-rtl.css');
      drush_op('adaptivetheme_file_str_replace', $subtheme_path . '/css/' . $file . '.css', ' /* LTR */', '');
    }
  }

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

/**
 * Copy a directory recursively.
 */
function adaptivetheme_copy($source_dir, $target_dir, $ignore = '/^(\.(\.)?|CVS|\.svn|\.git|\.DS_Store)$/') {
  if (!is_dir($source_dir)) {
    drush_die(dt('The directory "!directory" was not found.', array('!directory' => $source_dir)));
  }
  $dir = opendir($source_dir);
  @mkdir($target_dir);
  while($file = readdir($dir)) {
    if (!preg_match($ignore, $file)) {
      if (is_dir($source_dir . '/' . $file)) {
        adaptivetheme_copy($source_dir . '/' . $file, $target_dir . '/' . $file, $ignore);
      }
      else {
        copy($source_dir . '/' . $file, $target_dir . '/' . $file);
      }
    }
  }
  closedir($dir);
}

/**
 * Replace strings in a file.
 */
function adaptivetheme_file_str_replace($file_path, $find, $replace) {
  $file_contents = file_get_contents($file_path);
  $file_contents = str_replace($find, $replace, $file_contents);
  file_put_contents($file_path, $file_contents);
}

This is an adaptation of John Albin's zen.drush.inc in the 7.x-5.x release

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

KrisBulman’s picture

Issue summary: View changes

corrected typo, added filename & code block for drush command

KrisBulman’s picture

Issue summary: View changes

added credit to John Albin

Jeff Burnz’s picture

Subscribe, cool, I'll give this a go, be good if we can get it to generate the subtheme in the themes directory rather than inside the actual theme folder.

KrisBulman’s picture

The line that reads:

  $subtheme_path = implode('/', $subtheme_path) . '/' . str_replace('_', '-', $machine_name);

could actually be:

  $subtheme_path = implode('/', $subtheme_path) . '/../' . str_replace('_', '-', $machine_name);

and it will output the theme outside of the adaptive theme directory, BUT it also changes the successful print message to:

drupal-7.2/sites/all/themes/adaptivetheme/../themename

there's likely a better solution.

Stan Turyn’s picture

Found strange behaviour:

if I use
drush adaptivetheme "Test1" test1-test2

it will create a directory named "test1test2" (without dash)

However if I use
drush adaptivetheme "Test2" test1_test2

it will create a directory named "test1-test2" (now with dash, even though I used underscore)

agileadam’s picture

Component: Subtheme » CSS/HTML
FileSize
5.14 KB

Thanks so much @KrisBulman!
Regarding the directory... why not just array_pop($subtheme_path) twice? (though there's probably a cleaner way to do this with some array_xyz function).

Also, you may want to make sure a "name" value is present. drush_get_option('name') wasn't returning anything on my system.

function drush_adaptivetheme($name = NULL, $machine_name = NULL) {
  // Determine the theme name.
  if (!isset($name)) {
    if (!$name = drush_get_option('name')) {
      drush_print(dt('Please specify a name!' . "\n" . 'e.g., drush adaptivetheme "Your Theme Name" yourthemename'));
      return;
    }
  }

Also, @Stan, you should use an _ instead of - in your machine name. In the file I've attached I updated the options description for this field.

I've attached the version I'm currently using.

Again,
Thanks -- this will save me lots of time!

Jeff Burnz’s picture

Version: 7.x-1.2 » 7.x-3.x-dev
Category: feature » task

I would love if this was updated to work with the latest version, bumping for visibility and changing to task, I would really love this to be in the theme in 3.x

KrisBulman’s picture

Status: Active » Needs review
FileSize
5.76 KB
  • Removed RTL code
  • Updated paths
  • Updated naming documentation as stated above
  • Applied changes from #4
KrisBulman’s picture

Disregard last patch RTL code now gone for real :P

Jeff Burnz’s picture

I'm stuck on a machine that is giving me 7 kinds of weirdness with Drush and I can't get this to work, but I can't get hardly any Drush commands to work so hey...

I committed this to DEV so others could test this out, if we can get someone to feedback that its working I will call this FIXED and close the issue and release stable version of the theme.

I can test it properly next week some time, but would really like to get this into bed sooner, so I can release a stable version, there is only one bug holding that up right now and I fixed it today.

Stan Turyn’s picture

It's working but with a minor issue along the way. Here's how it went:

$ drush dl adaptivetheme && drush en -y adaptivetheme
Project adaptivetheme (7.x-3.x-dev) downloaded to /home/admin-local/www/unidim3/sites/all/themes/adaptivetheme.
Project adaptivetheme contains 3 themes: adaptivetheme, adaptivetheme_subtheme, adaptivetheme_admin.
The following extensions will be enabled: adaptivetheme
Do you really want to continue? (y/n): y
adaptivetheme was enabled successfully.

$ drush adaptivetheme "UniDim3" unidim3
Command adaptivetheme needs the following module(s) enabled to run: adaptivetheme.     [error]
The drush command 'adaptivetheme UniDim3 unidim3' could not be executed.

$ drush core-cron && drush cc all
Cron run successful.
'all' cache was cleared in /home/admin-local/www/unidim3#default

$ drush adaptivetheme "UniDim3" unidim3
New Subtheme for "UniDim3" created in: /home/admin-local/www/unidim3/sites/all/themes/unidim3

We get an error first but it clears after running cron/clearing caches (not sure which one - force of habit of running these 2 together).

Jeff Burnz’s picture

Looks good, shouldn't Drupal CC all after enabling (enabling anything)? I will check that, I always thought it did. Wonder if this is bug with Drush and theme enabling?

brunodbo’s picture

In my tests, the drush command behaved differently depending on whether I executed the command from within the site's folder or outside (using a drush alias in both cases). When outside the site's folder (docroot), I had to clear the cache first; when inside the folder, the command worked right after downloading and enabling adaptivetheme.

From what I could tell, drush doesn't do a cache clear after enabling modules or themes.

I took a look at how Omega creates subthemes using drush. I noticed they call system_rebuild_theme_data() in their drush extension (see http://drupalcode.org/project/omega_tools.git/blob_plain/refs/heads/7.x-...), which could maybe solve the problem described in #9.

Jeff Burnz’s picture

Could system_rebuild_theme_data() be executed at the end of drush_adaptivetheme()?

KrisBulman’s picture

Can anyone confirm adding system_rebuild_theme_data(); after drush_adaptivetheme works? I am unable to replicate the problem locally, in, or out of the adaptivetheme folder using drush 5.4

function drush_adaptivetheme($name = NULL, $machine_name = NULL) {
  system_rebuild_theme_data();
paskainos’s picture

FYI: I experienced this issue with AT 7.x-3.1 and Drush 5.4.

paskainos’s picture

More specifically, as mentioned by Stan Shevchuk in comment #9, running

drush dl adaptivetheme
drush adaptivetheme "My Theme" my_theme

Results in the following

The drush command 'adaptivetheme My Theme my_theme' could not be executed.

Clearing the cache (drush cc all) and reissuing the adaptivetheme subtheme command successfully creates the subtheme.

KrisBulman’s picture

@paskainos as asked in #13 does modifying the drush script to the following produce the same results, or resolve the issue?

function drush_adaptivetheme($name = NULL, $machine_name = NULL) {
  system_rebuild_theme_data();
paskainos’s picture

No Kris, adding system_rebuild_theme_data(); doesn't fix it. However, here's a strange 'anomaly' and perhaps a clue: I created a test install, mod'ed the adaptivetheme.drush.inc file, and tried to create an AT sub-theme using drush, which failed. Then I removed system_rebuild_theme_data(); from the beginning of function drush_adaptivetheme and uncommented it at the end of the function (where it's already included, along with reference to this thread) which also failed. Then I accidentally cleared the cache (drush cc all) in the test site directory - doh! I then removed all traces of the installation (i.e. settings.php, etc.) and ran the install again, and this time without mod'ing adaptivetheme.drush.inc running drush adaptivetheme test worked! I then created another test site ('test2' in a separate directory) and repeated the process, and again, running drush adaptivetheme test worked without mod'ing adaptivetheme.drush.inc. Since the test sites are installed in a 'dev' directory of a working Drupal install (i.e. dev.[workingsite].com/test and dev.[workingsite].com/test2) I repeated the process (clean install, test the script) in another directory (i.e. not *.[workingsite].com/*), which failed as expected. Hopefully that all makes sense. So my question is; why would clearing the cache in the site(s) installed in a subdirectory of a working Drupal install alter script run in another subdirectory install? Again, hopefully that makes sense. I'm investigating, but with no answers yet...

P2790’s picture

After enabling my subtheme everything is fine, when I tell it to be the default theme I get wsod

I used the drush command:

drush adaptivetheme "Your Theme Name" yourthemename

KrisBulman’s picture

Status: Needs review » Needs work

Potential WSOD's are slightly concerning, setting this to needs work

P2790’s picture

I had a check and created a sub theme manually and everything worked fine, so not certain exactly what is going wrong with the drush command.

sin’s picture

Can confirm #15, but the message was:

drush: The directory "_path_goes_here_//../at_subtheme" was not found.
Drush command terminated abnormally due to an unrecoverable error.

drush cc all helped, thank you!

madhattertech’s picture

I had the same issue and after running "drush core-cron && drush cc all" my theme was created

Jeff Burnz’s picture

Status: Needs work » Closed (fixed)

Please see this thread: #1997614: drush adaptivetheme THEMENAME creates subtheme with base theme missing in info file

I think we should close this, I have personally never had a WSOD with this and its a standard Drush procedure that when you add new commands you really have to drush cc all to get them to kick in. I found this many times.

If we have more issues lets open new ones, rather than continuing on, this was really just about adding the functionality, for which I am most grateful since I now use it all the time!

Jeff Burnz’s picture

Issue summary: View changes

removed echo