Application of Shell in code refactoring

Source: Internet
Author: User
Tags echo b echo d
Coderefactoring is sometimes boring. string replacement and other operations are not only boring, but also prone to errors. Fortunately, some tools are available. take PHP as an example, such as Rephactor, scisr and so on, but the ready-made tools often mean that they are not flexible enough, so today I want to talk about the application of Shell in code refactoring SyntaxHighli

Code refactoring is sometimes boring. string replacement and other operations are not only boring, but also prone to errors. Fortunately, some tools are available, taking PHP as an example, such as Rephactor, scisr and so on, but the ready-made tools often mean that they are not flexible enough. so today I want to talk about the application of Shell in code refactoring.

Let's start with a simple one. if we want to replace all foo_bar in the PHP file with fooBar, we can:

Method 1: Use Sed:

Shell> find/path-name "*. php" | xargs sed s/foo_bar/fooBar/g
 


Method 2: Use AWK:

Shell> find/path-name "*. php" | xargs awk {gsub (/foo_bar/, "fooBar"); print ;}
 


Note: For simplicity, I printed the result directly to the terminal screen. I will explain how to save it later.

Next, let's talk about the complexity: For a PHP project, you must call a method named "includeClass" before using the class. now you can use the method of automatic loading of classes, therefore, we need to delete the hardcoded includeClass call. for the sake of appearance, if the line below includeClass is empty, it will also be deleted together, taking into account the case insensitive factor.

Code example before reconstruction:

01 02 includeClass (...);
03 echo;
04
05 echo B;
06 includeClass (...);
07 includeClass (...);
08
09
10 echo c;
11
12 echo d;
13 includeClass (...);
14
15
16 echo e;
17?>
 


Code example after reconstruction:

01 02 echo;
03
04 echo B;
05
06 echo c;
07
08 echo d;
09
10 echo e;
11?>
 


Before doing this, we need to get a thorough understanding of the general situation:

Shell> grep-I-ri includeClass/path | more
 


The parameters of the grep command are not easy to remember at first glance, but as long as you follow the method I mentioned, you will never forget that the preceding parameters are regarded as English, and the following parameters are considered as pinyin. For more information about the parameters, see man.

Method 1: Use Sed to write script. sh:

#! /Bin/sh

For PHP in $ @; do
/Bin/sed-I
/IncludeClass/I {
H
D
}

/^ $ /{
X
/IncludeClass/Id
X
}

H
$ PHP
Done
 


Note: Due to space limitations, it is easier to write regular expressions.

The disadvantage of Sed is that the code is less readable and the advantage is that the code is shorter. In addition, the built-in "-I" option can be saved directly, which is one of the reasons I like Sed.

Method 2: Use AWK to write script. sh:

#! /Bin/sh

For PHP in $ @; do
TMP = $ (mktemp)

/Bin/awk
BEGIN {
IGNORECASE = 1
}

/IncludeClass /{
Previous = $0
Next
}

/^ $ /{
If (previous ~ /IncludeClass /){
Previous = $0
Next
}
}

{
Previous = $0
Print
}
$ PHP> $ TMP

/Bin/cp-f $ TMP $ PHP
/Bin/rm-f $ TMP
Done
 


Note: Due to space limitations, it is easier to write regular expressions.

The disadvantage of AWK is that the code is long and the advantage is that the code is readable. In addition, the program generates a unique temporary file for saving.

Note: it is sometimes inappropriate to directly overwrite the original file. after all, you may not have to worry about using SVN because even if the original file is overwritten, you can also run the "svn diff" command before submission to check whether it is correct or not. even if it is submitted, it can be restored to the previous version.

What if I call the script. sh script? Here is a general example:

Shell> find/path-name "*. php" | xargs/path/to/script. sh
 


It is suitable for simple tasks to be written with Sed. for complex tasks, it is best to write with AWK. practice is the best way to learn. for details, refer to Sed One Line and AWK One Line.

Note: the Sed and AWK used in this article refer to the GNU version.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.