1。将astyle运行路径加到系统PATH变量中
2。添加变量ARTISTIC_STYLE_OPTIONS
ASTYLE_HOME=~/tools/astyle
ARTISTIC_STYLE_OPTIONS=${ASTYLE_HOME}/conf.opt
PATH=${PATH}:${ASTYLE_HOME}/build/gcc/bin:${ASTYLE_HOME}/scripts
export ASTYLE_HOME ARTISTIC_STYLE_OPTIONS PATH
其中,conf.opt是配置astyle参数的文件,存放位置任意,只要变量名一样就行。
${ASTYLE_HOME}/scripts不是必须加到PATH中,此目录下存放的是SHELL脚本,作用:在任何目录下运行此脚本,可以format要格式化的代码。
下面是conf.opt和astyle_format.sh 文件内容
# use linux as Predefined Style Options
style=linux
# This option will allow the comments to be indented with the code
indent-col1-comments
# Insert space padding around operators.
pad-oper
# Add brackets to unbracketed one line conditional statements
add-brackets
# Force use of the specified line end style
lineend=linux
# Indent a C/C++, C#, or Java file.
mode=java
# Converts tabs into spaces in the non-indentation part of the line.
# The number of spaces inserted will maintain the spacing of the tab.
convert-tabs
# Indent using 4 spaces per indent
indent=spaces=4
# delete empty lines within a function or method.
# delete-empty-lines
# insert space padding after paren headers only
pad-header
unpad-paren
add-brackets
#!/bin/bash
#---------------------------------------------------------#
# the script is used to format code
# 1. give code name to format
# can give multi one time
# 2. give directory to format all code file recursive
# it have two arguments:first is the directory to format
# second is the directory to save the backup code files
#---------------------------------------------------------#
# astyle_home=/opt/corp/projects/accessoam/3GPP/cu_nbi_thirdparties/astyle
# astyle_bin=${astyle_home}/bin
function help(){
echo "the script is used to format code.";
echo "usage:astyle_format.sh Util.java";
echo " astyle_format.sh Util.java Log.java ...";
echo " astyle_format.sh /home/ldyang/src /home/ldyang/bak";
echo
exit;
}
function validate(){
if [ "$#" -eq 0 ]; then
help
fi
if [ "$#" -eq 2 ]; then
if [ -d "$1" -a ! -d "$2" ]; then
echo "error arguments.";
help
fi
if [ ! -d "$1" -a -d "$2" ]; then
echo "error arguments.";
help
fi
if [ ! -d "$1" -a ! -d "$2" ]; then
if [ ! -f "$1" -o ! -f "$2" ];then
echo "file not exist.";
help
fi
fi
else
for arg
do
if [ ! -f $arg ];then
echo "file not exist.";
help
fi
done
fi
}
#--------- function clean backup files------------------------------#
function clean(){
indir=$1
outdir=$2
sub=${outdir:0-1}
if [ ! "$sub" = '/' ];then
outdir=$outdir'/';
fi
fileext=".orig"
# USE ONE OF THE FOLLOWING TO COPY OR MOVE THE FILES
#copyfiles=cp
copyfiles=mv
# display the variables
echo
echo "/"$copyfiles/" Artistic Style backup files"
echo "FROM $indir"
echo "TO $outdir"
echo
# validate input directory
if [ ! -d "$indir" ] ; then
echo "Input directory does not exist!"
echo
exit
fi
if [ "$indir" == "$outdir" ]; then
echo "Input and output directories are the same!"
echo
exit
fi
# optional statement to run Artistic Style
# astyle -R "%indir%/*.cpp" "%indir%/*.h"
# if [ $? -ne 0 ] ; then read -sn1 -p "Error executing astyle!"; fi
# variables for fle processing
#echo "processing files..."
fileHasSpaces=false # a file or directory name has spaces
extLength=${#fileext} # length of the file extension
# loop thru the input directory to find the .orig files
# then move the .orig file to the new directory
for file in `find "$indir" -type f -name "*$fileext" ` ; do
# allow spaces in file names for Cygwiin on Windows
# test if this is a continuation line and build $infile
if [ $fileHasSpaces = true ] ; then
infile+=' '$file
else
infile=$file
fi
# test end of string for no file extension
if [ ! "${infile:(-$extLength)}" = $fileext ] ; then
fileHasSpaces=true
continue
fi
fileHasSpaces=false
# echo $infile
# replace input file directory with output directory
# ${string/substring/replacement}
outfile=${infile/$indir/$outdir}
# echo $outfile
# strip filename from back of the output file
# ${string%substring} - /* strips from last '/' to end
outpath=${outfile%/*}
# echo $outpath
# strip filename from back of the output file
# ${string%substring} - /* strips from last '/' to end
outpath=${outfile%/*}
# echo $outpath
# create output directory
[ ! -d "$outpath" ] && mkdir -p "$outpath"
# copy or move the files
$copyfiles -f "$infile" "$outpath"
if [ $? -ne 0 ] ; then
read -sn1 -p "press any key to continue!"
echo
fi
# echo for every 100 files processed
let count++
let mod=count%100
[ $mod -eq 0 ] && echo $count files processed
done
echo $count files processed
echo
}
#---------end function clean backup files------------------------------#
function format_single(){
while [ "$1" ]; do
/opt/corp/projects/accessoam/3GPP/cu_nbi_thirdparties/astyle/bin/astyle "$1"
shift
done
}
function ergodic(){
arg1=$1;
len=${#arg1}
sub=${arg1:0-1}
if [ "$sub" = '/' ];then
arg1=${arg1:0:(len-1)}
fi
for file in `ls $arg1`
do
if [ -d $arg1"/"$file ]
then
ergodic $arg1"/"$file
else
local path=$arg1"/"$file
local name=$file
if [ "${name##*.}" = "java" ]
then
/opt/corp/projects/accessoam/3GPP/cu_nbi_thirdparties/astyle/bin/astyle -r
$path
clean $arg1"/" "$2"
fi
fi
done
}
function format_rec(){
codedir="$1";
bakdir="$2";
ergodic "$codedir" "$bakdir"
}
# call function validate
validate $@;
if [ "$#" -eq 2 ] ; then
if [ -d "$1" ] ; then
if [ -d "$2" ] ; then
format_rec "$1" "$2"
fi
fi
if [ -f "$1" -a -f "$2" ] ;then
format_single "$1" "$2";
fi
fi
if [ "$#" -ne 2 ]; then
if [ -f "$1" ] ;then
format_single $@;
fi
else
validate $@;
fi
shell脚本说明:
首先我SHELL只会一点,所以这个脚本写的比较别扭。然后就是clean函数里面的脚本来自于artistic style网站自带的scripts shell脚本。