Viết module cho Drush command để xoá files như INSTALL.txt, UPGRADE.txt...

Viết module cho Drush command để xoá files như INSTALL.txt, UPGRADE.txt...

While learning to use Drush I was amazed at how easy it made updating the Drupal core and community modules. The problem however, is that after the core update, I'm left with all the unwanted, non-critical files such as INSTALL.txt, UPGRADE.txt, and README.txt in my root folder. At the time of writing this post, there is a module called Drush Cleanup that does exactly what I'm looking for, though unfortunately there is no Drupal 7 version. Therefore, I decided to make my own, using small snippets from the module. Obviously it goes without saying that you should use this custom module at your own risk, that being said, it works fine for me. Download file is at the bottom, install as a module, enable it, and give it a try -  would love to know your thoughts.

The code

The code below basically creates one Drush command, that takes an option to decided what files should be removed. The default option is basic and this removes the annoying .txt files and the install.php file. The full option also removes update.php, xmlrpc.php, and web.config which is preferred by myself, as I don't believe they're needed (obviously I'll restore the update.php file when I'm doing updates).

<?php
/**
* Implements hook_drush_command().
*
* In this hook, you specify which commands your
* drush module makes available, what it does and
* description.
*
* @see drush_parse_command() for a list of recognized keys.
*
* @return
*   An associative array describing your command(s).
*/
function cleansillyfiles_drush_command() {
  $items = array();

  // the key in the $items array is the name of the command.
  $items['dl-silly-files'] = array(
    'aliases' => array('dsf'),
    'description' => dt('Deletes non-critical files from the Drupal root folder. Add --type=full if you want to delete update.php, xmlrpc.php, and web.config files too.'),
    'options' => array(
      'type' => 'Type of cleaning required. "basic" is the default and will clean common unwanted files, "full" will also clean update.php, xmlrpc.php, and web.config',
    ),
  );
  return $items;
}

/**
* Implements hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help <name-of-your-command>'
*
* @param
*   A string with the help section (prepend with 'drush:')
*
* @return
*   A string with the help text for your command.
*/
function cleansillyfiles_drush_help($section) {
  switch ($section) {
    case 'drush:dl-silly-files':
      return dt("Deletes non-critical files from the Drupal root folder. Add --type=full if you want to delete update.php, xmlrpc.php, and web.config files too.");
  }
}

/**
* Drush command callback.
*
*/
function drush_cleansillyfiles_dl_silly_files() {
  $limit = drush_get_option('type', 'basic');
  _cleansillyfiles_clean_files(_cleansillyfiles_files_to_clean($limit));
}

/**
* Declare what files to clean.
*
* @return
*   An array describing files to clean.
*/
function _cleansillyfiles_files_to_clean($limit) {
  $file = array();

  $file[] = 'CHANGELOG.txt';
  $file[] = 'COPYRIGHT.txt';
  $file[] = 'INSTALL.mysql.txt';
  $file[] = 'INSTALL.pgsql.txt';
  $file[] = 'INSTALL.sqlite.txt';
  $file[] = 'install.php';
  $file[] = 'INSTALL.txt';
  $file[] = 'LICENSE.txt';
  $file[] = 'MAINTAINERS.txt';
  $file[] = 'UPGRADE.txt';
  $file[] = 'README.txt';

  if ($limit == 'full') {
    $file[] = 'xmlrpc.php';
    $file[] = 'update.php';
    $file[] = 'web.config';
  }
  return $file;
}

/**
* Returns array of files to delete.
*/
function _cleansillyfiles_clean_files($files) {
    $files_deleted = 0;
    foreach ($files as $file) {
        // Check if path is a file.
    if (drush_delete_dir($file)) {
          $files_deleted++;
          // If the delete was successfull we print out a message.
          drush_print(dt('!filename was cleaned out.', array('!filename' => $file)));
    }
      else {
          if (file_exists($file)) {
              // Notify the user there was a problem deleting the file, which is usually due to permissions.
            drush_log(dt('There was a problem trying to delete: !filename (check file permissions)', array('!filename' => $file)), 'error');
        }
          else {
              // Notify the user the file didn't exist.
              drush_log(dt('Missing: !filename', array('!filename' => $file)), 'notice');
          }
      }
    }
    // If no files needed to be cleaned out we let the user know.
    if ($files_deleted == 0) {
        drush_print(dt("No files were cleaned out.\n"), 'ok');
    }
    // Clean up.
    unset($files_deleted);
}
?>
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Window, Window 7, Mac iOS 6, Mac OS, MacBook Air, MacBook Pro

Hướng dẫn thiết lập mạng riêng ảo (VPN) của riêng bạn trên Windows và Mac

Mạng riêng ảo (hay Virtual Private Network) là một mạng thường dành riêng để kết nối các máy tính trong nhà hay trong một tổ chức với nhau thông qua Internet

Xôn xao vụ bán đấu giá tài liệu thành lập Apple

Xôn xao vụ bán đấu giá tài liệu thành lập Apple

Trang Bloomberg vừa đưa tin nhà tổ chức bán đấu giá Sotheby’s (Mỹ) đang chuẩn bị đem ra đấu giá bản tài liệu gốc dài ba trang về quyết định thành lập công ty máy tính Apple do bộ ba Steve Jobs, Steve Wozniak và Ron Wayne

Hướng dẫn build a Blog trong Drupal 8: Using Views

Hướng dẫn build a Blog trong Drupal 8: Using Views

Just when you thought Drupal 8 couldn't get more powerful; I give to you Views in core

Công ty diệt chuột T&C

 

Diet con trung