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

 
Học ngành gì trong kỷ nguyên 4.0? (3 cuộc cách mạng công nghiệp diễn ra)

Học ngành gì trong kỷ nguyên 4.0? (3 cuộc cách mạng công nghiệp diễn ra)

Kỷ nguyên 4.0 diễn ra mạnh mẽ tại nhiều nước trên thế giới như là hệ quả tất yếu của vòng xoáy phát triển của xã hội. Nó mang đến những cơ hội nhưng cũng tiềm tàng những rủi ro.

Hướng dẫn tạo nút reply facebook đơn giản 1 phút

Hướng dẫn tạo nút reply facebook đơn giản 1 phút

Nhưng chúng ta đã biết lúc trước facebook chỉ cho bật nút reply comment facebook 

Phần 1: Giới thiệu Composer in contrib

Phần 1: Giới thiệu Composer in contrib

 Add a composer.json file to your Drupal 8 module. The example below gives everything you need.

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

 

Diet con trung