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 Tran 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
  • Phone/Zalo: (+84) 944 225 212
  • WhatsApp: (+84) 944 225 212
  • Line Messenger: (+84) 944 225 212
  • Email: [email protected]
  • Telegram Messenger: https:/t.me/tommytran0401

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

 

Quảng cáo việc làm

 

Thích hợp các bạn nữ mảng thợ may làm việc tại nước NGA

Đơn hàng Tuyển dụng 100 Thợ may đi Nga(đợt 1 tháng 3.2021, đợt 2 tháng 5.2021). Lương thực lãnh 800 USD, bao ăn ở, vé máy bay và visa, phí xuất cảnh(1800 USD)trả khi đi làm có lương. Bạn có thể liên hệ CÔNG TY qua Phone/Zalo: (+84) 944 225 212. Công ty sẽ tư vấn cho bạn.

Xem chi tiết: >>> https://bit.ly/3o9NOfR

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

 
Cho phép tạo autocompletes bên trong textareas for Drupal với At.js module

Cho phép tạo autocompletes bên trong textareas for Drupal với At.js module

A dev release is out for At.js, a module implementing the work of iChord which allows for autocompletes within textareas. This type of autocomplete can be seen on Github and Twitter, and now Long text fields within Drupal can have this too.

11 Sản phẩm "bèo bọt" trong quá khứ của Apple

11 Sản phẩm "bèo bọt" trong quá khứ của Apple

Nếu iPad và iPhone tạo nên sự thay đổi ngoạn mục trong lịch sử công nghệ tiêu dùng thế giới, thì một số sản phẩm xa xưa của Apple lại hoàn toàn bị lãng quên.

Create Awesome Cover Photo For Facebook Timeline Profile

Create Awesome Cover Photo For Facebook Timeline Profile

Facebook Timeline is one of the newest feature launched by Facebook recently with lots of new changes, Facebook cover photo is one of them.

BLOG POSTS

 

Wordpress Freelancer

 

Wordpress Freelancer