欢迎访问Ningto's博客

Menu
  • 首页
  • 归档
  • 关于
  • 书签
  • 必应壁纸
  • IT聚合
  • 工具
    • 我的工具列表
    • 我的网盘
    • 必应每日壁纸API
    • Html转Markdown
    • 仙尘光标
Menu

AStyle格式化代码

最后更新 2018-07-13 10:16:33   阅读量 2519

通过Node.js脚本来调用AStyle.exe格式化代码。
src:需要格式化的代码目录
params:AStyle参数,默认使用linux风格
filterNameList:不进行格式化的目录,跳过这些目录
regSuffix:需要格式化的文件名正则

const fs = require('fs')
const path = require('path')
const { exec } = require('child_process');

/**
 * 通过配置这几个参数来更改行为
 */
const src = path.normalize(__dirname)
const params = '--style=linux --align-reference=type --align-pointer=type --pad-header --pad-comma --pad-oper --indent-preproc-block'
const filterNameList = ['3rd', 'protobuf', 'tinyxml', 'websocket', 'win32']
const regSuffix = /\.(h|cpp)/

function syncReadAllFile(dir, cb) {
  const subDir = fs.readdirSync(dir);
  for (let dirName of subDir) {
    const subPath = path.normalize(dir + '/' + dirName);
    if (fs.lstatSync(subPath).isDirectory()) {
      syncReadAllFile(subPath, cb);
    } else {
      cb(subPath)
    }
  }
}

let count = 0;
syncReadAllFile(src, function(subpath) {
  const dirname = path.dirname(subpath);
  const basename = path.basename(subpath);
  let isFilter = false;
  for (let filterName of filterNameList) {
    if (dirname.indexOf(filterName) >= 0) {
      isFilter = true;
      break;
    }
  }
  if (!isFilter && regSuffix.test(subpath)) {
    ++count;
    console.log(subpath)
    exec(path.normalize(src + '/AStyle.exe') + ' ' + subpath + ' ' + params)
  }
})

console.log('finished total:' + count)

AStyle.exe下载地址

(转载本站文章请注明作者和出处:泞途 - ningto.com)

下一篇 – 一张图展示google c++ 代码规范
上一篇 – JAVA8 十大新特性详解

  1. Windows

toningto@outlook.com

标签云

ChatGPT Javascript Design Database Shell Mobile MQ Tools Windows Qt Android Python Node.js React Go IOS Java Product Web MongoDB Others Tips C/C++ Life Mac Linux Bug Boost

推广链接

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

多谢支持,用了好几年,服务很稳定支持多设备!

其他

文章RSS

Copyright © 2016 Welcome To Ningto Blog | 鄂ICP备17003086号-2