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

 
BMW 640i GT 2018 chỉ 2 năm bán lỗ hơn 2 tỉ đồng

BMW 640i GT 2018 chỉ 2 năm bán lỗ hơn 2 tỉ đồng

Chiếc BMW 640i Gran Turismo 2018 về Việt Nam thời điểm cuối tháng 5.2018 theo diện nhập khẩu không chính hãng.

Loa dock “hầm hố” của Yamaha dành cho iOS

Loa dock “hầm hố” của Yamaha dành cho iOS

Mới đây, hãng Yamaha đã công bố mẫu loa dock di động mới nhất của mình ra thị trường với tên gọi Yamaha PDX-11. Đây là mẫu dock được thiết kế cho iPod và iPhone của Apple.

Timeline Facebook thử nghiệm bố trí mới của ảnh cover

Timeline Facebook thử nghiệm bố trí mới của ảnh cover

Có vẻ như sắp tới giao diện Timeline của Facebook lại có chút thay đổi.

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

 

Diet con trung