项目代码统计

今天接到一个需求,需要统计整个项目的代码量,为了系统且清晰的统计到整体的代码量,我还是做了些工作的。

于是这篇文章便诞生了。

Tasking

要做代码量统计,大致需要以下工作

  1. 批量clone group下所有仓库日常开发,我们本地并不会clone下所有的仓库代码,因此还是有此需求,批量Clone用ghorg
  2. 循环执行统计脚本,统计工具用cloc
  3. 合并到一个CSV文件

贴出代码

具体如何操作,看这里

  1. clone repository

    1
    ghorg clone groupname --concurrency=50 --protocol=ssh --scm=gitlab --base-url=https://gitlab.com --token=CP-SevCew54pjq_JnuDb
    • ghorg安装参考官网即可
    • 上述token即gitlab账户的access token,必填
  2. 执行统计报告生成

    • cloc安装npm i cloc
    • 我将循环遍历仓库,执行统计并合并最终报告的脚本做了下封装,这里贴出来,不感兴趣实现的,直接CV使用即可。
    • 脚本中4个变量需要根据实际情况,进行配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
       const fs = require('fs');
    const { execSync } = require('child_process');

    // 项目代码
    const rootDir = '';

    // CLOC Shell路径
    const clocCommand = '/Users/qhe/.nvm/versions/node/v10.16.0/bin/cloc';

    // 报告生成临时路径
    const reportFiles = '';

    // 总报告名称
    const reportFile = '';

    const deleteFolderRecursive = function (path) {
    if (fs.existsSync(path)) {
    fs.readdirSync(path).forEach(function (file, index) {
    var curPath = path + '/' + file;
    if (fs.lstatSync(curPath).isDirectory()) {
    deleteFolderRecursive(curPath);
    } else {
    fs.unlinkSync(curPath);
    }
    });
    fs.rmdirSync(path);
    }
    };

    /**
    * @description
    * 遍历生成每个仓库代码报告
    */
    function createReport() {
    return new Promise((resolve) => {
    fs.readdir(rootDir, (_0, files) => {
    files.forEach((file) => {
    if (fs.statSync(`${rootDir}/${file}`).isFile()) {
    return;
    }
    execSync(
    `${clocCommand} ${rootDir}/${file} --csv --out=out/${file}.csv`,
    (error, stdout, stderr) => {
    if (error) {
    console.log(`error: ${error.message}`);
    return;
    }
    if (stderr) {
    console.log(`stderr: ${stderr}`);
    return;
    }
    console.log(`stdout: ${stdout}`);
    }
    );
    });
    resolve();
    });
    });
    }

    /**
    * @description
    * 合并每个仓库的报告到一个CSV文件中
    */
    function mergeReport() {
    let mergedReport = '';
    fs.readdir(reportFiles, (_0, files) => {
    files.forEach((file) => {
    if (file === '.DS_Store') {
    return;
    }
    const contents = fs.readFileSync(`${reportFiles}/${file}`, {
    encoding: 'utf8',
    });
    mergedReport += `${file.replace(/\.csv/, '')},,,,,\n` + contents;
    });
    deleteFolderRecursive(reportFiles);
    fs.writeFile(reportFile, mergedReport, function (err) {
    if (err) {
    console.log(err);
    }
    });
    });
    }

    createReport().then(() => mergeReport());

结果

  • 利用Excel筛选,即可操作这样完整的数据。比如利用筛选,我快速了解到项目组共34个仓库,代码行数1340244,不得不说还是挺多的。。。。当然因为项目中存在部分vendor资源,即水分。
  • 发现cloc进行统计存在一个问题,比如文件夹下直接统计N个项目的代码量与如上循环遍历统计的代码量之和存在较大出入。我认为具体仓库统计的是对的。

写到最后

如上,操作已经脚本化,类似需求以后无非2句命令即可。妈妈再也不担心如何做项目代码统计了。

参考文档