美文网首页
统计项目里某一语言的代码行数

统计项目里某一语言的代码行数

作者: 夜空中最亮的星_6c64 | 来源:发表于2018-10-18 18:27 被阅读0次

    1,递归遍历文件目录。
    保存:在需要递归的目录里放sh文件。
    运行时:bash filename.sh

    #!/bin/bash  
      
    # $1是运行脚本时,输入的第一个参数,这里指的是使用者希望搜索的目录  
    # 下面的代码是对目录进行判断,如果为空则使用脚本所在的目录;否则,搜索用户输入的目录  
    if [[ -z "$1" ]] || [[ ! -d "$1" ]]; then  
        echo "The directory is empty or not exist!"  
        echo "It will use the current directory."  
        nowdir=$(pwd)  
    else  
        nowdir=$(cd $1; pwd)  
    fi  
    echo "$nowdir"  
     
    #INF=$(echo -en "\n\b") 
    
    # 递归函数的实现  
    function SearchCfile()  
    {  
            cd $1  
        for dirname in *
            do  
                if [[ -d "$dirname" ]];then  
                    #echo "dir:$dirname"
                #mv "$dirname" `echo "$dirname" | tr ' |  |(|)' '_'`
                #echo "dir:$dirname"
                        cd $dirname
                        #SearchCfile 这里有bug,跳转到根目录了  
                        #这里把当前的目录作为参数输入  
                        SearchCfile $(pwd)  
                        cd .. 
                else 
                    echo "file:$dirname"
                    if [ "${dirname##*.}" = "gz" ]; then
                        cloc ./*.tar.gz
                    fi;
                fi;  
         done;  
    }  
      
    # 调用上述递归调用函数  
    SearchCfile $nowdir 
    

    2,统计代码行数:安利cloc工具。
    运行:bash filename.sh>output.txt
    统计结果将会保存至txt文件中。
    3,根据自己的需求,统计某一种语言的代码行数。

    #!/bin/bash
    cat output.txt | while read line
    do
        if [[ $line =~ ^Verilog-SystemVerilog ]];then
            echo ${line##* }
        fi;
    done
    

    运行:bash filename.sh>result.txt
    统计结果将会每一项目的代码行数保存至txt文件中。
    4,java中对代码行数求和即可。

    package codeCount;
    
    import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
    import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
    
    import java.io.*;
    
    import static java.awt.SystemColor.text;
    
    public class getCount {
        public static int readTxtFile(File fileName)throws Exception {
            int rs=0;
            //String result=null;
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            try {
                fileReader = new FileReader(fileName);
                bufferedReader = new BufferedReader(fileReader);
                try {
                    String read = null;
                    while ((read = bufferedReader.readLine()) != null) {
                        //result=result+read+"\r\n";
                        //System.out.println(read);
                        rs += Integer.parseInt(read);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (fileReader != null) {
                    fileReader.close();
                }
            }
            //775419
            //System.out.println("代码总行数:"+rs);
            //return result;
            return rs;
        }
        public static void main(String[] args) throws Exception {
            File vhdl=new File("/home/hadoop/IdeaProjects/count_Log_Mail/src/main/java/codeCount/vhdl.txt");
            File verilog=new File("/home/hadoop/IdeaProjects/count_Log_Mail/src/main/java/codeCount/Verilog.txt");
            File vhdl_verilog=new File("/home/hadoop/IdeaProjects/count_Log_Mail/src/main/java/codeCount/vhdl_verilog.txt");
    
            int vhdlCount = readTxtFile(vhdl);
            System.out.println("vhdl代码总行数:"+vhdlCount);
            int verilogCount = readTxtFile(verilog);
            System.out.println("verilog代码总行数:"+verilogCount);
            int vhdl_verilogCount = readTxtFile(vhdl_verilog);
            System.out.println("vhdl_verilog代码总行数:"+vhdl_verilogCount);
            //vhdl代码总行数:775419
            //verilog代码总行数:519133
            //vhdl_verilog代码总行数:1636965
    
    
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:统计项目里某一语言的代码行数

          本文链接:https://www.haomeiwen.com/subject/oumszftx.html