You are here

function drush_speedy_min in Speedy 7

Command callback to minify JS using UglifyJS.

This is a command that walks through a Drupal codebase and produces minified JavaScript files.

File

./speedy.drush.inc, line 54
Drush integration for the speedy module.

Code

function drush_speedy_min($uglifyjs) {
  if (empty($uglifyjs)) {
    drush_log(dt('The path to UglifyJS is missing.'), 'error');
    return;
  }
  $directory = DRUPAL_ROOT . '/' . drupal_get_path('module', 'speedy') . '/js/' . VERSION . '/';
  $directory_strlen = strlen(DRUPAL_ROOT . '/');

  // @todo Is there a cleaner way to create the directory?
  if (!is_dir($directory)) {
    mkdir($directory, 0775, TRUE);
  }

  // A list of files and directories to not act on.
  // @todo Make this configurable
  $blacklist = array(
    'misc/jquery.js',
    'misc/farbtastic/farbtastic.js',
    'misc/jquery.ba-bbq.js',
    'misc/jquery.cookie.js',
    'misc/jquery.form.js',
    'misc/ui',
    'sites',
  );

  // Make the blacklist full paths so all things are equal.
  foreach ($blacklist as $k => $v) {
    $blacklist[$k] = DRUPAL_ROOT . '/' . $v;
  }

  // @todo Replace file_scan_directory with PHP native call. Why does Drupal
  // reproduce a native php feature?
  $all_files = file_scan_directory(DRUPAL_ROOT, '/.js/');

  // Walk though each file and act on those not blacklisted.
  foreach ($all_files as $k => $file) {

    // Make sure the file isn't blacklisted.
    if (!_speedy_drush_blacklisted($blacklist, $file)) {

      // Minify and store the file.
      $new_path = $directory . substr($file->uri, $directory_strlen);
      $pathinfo = pathinfo($new_path);
      if (!is_dir($pathinfo['dirname'])) {
        mkdir($pathinfo['dirname'], 0775, TRUE);
      }
      $return = drush_shell_exec($uglifyjs . ' ' . $file->uri . ' > ' . $new_path);
      if ($return) {
        drush_log(dt('!filename was minified.', array(
          '!filename' => $file->filename,
        )), 'success');
      }
      else {

        // @todo Note that a file was created with the error information in it.
        drush_log(dt('There was an error when trying to minify !filename.', array(
          '!filename' => $file->filename,
        )), 'error');
      }
    }
  }
}