Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
},
// Node
{
files: ['**/.*rc.(c){j,t}s', '**/*.config.?(c){j,t}s'],
files: ['**/.*rc.(c){j,t}s', '**/*.config.?(c){j,t}s', 'scripts/**/*.js'],
env: {
node: true,
},
Expand Down
5 changes: 2 additions & 3 deletions javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"scripts": {
"benchmark": "just benchmark --config just.config.cjs",
"build": "just build --config just.config.cjs",
"clang-format": "just clang-format --config just.config.cjs",
"clang-format:fix": "just clang-format --fix --config just.config.cjs",
"clang-format": "yarn --cwd .. format-check-cpp",
"clang-format:fix": "yarn --cwd .. format-cpp",
"clean": "just clean --config just.config.cjs",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
Expand All @@ -43,7 +43,6 @@
"@types/which": "^3.0.0",
"@yogalayout/cmake-bin": "3.28.0-1",
"babel-register-esm": "^1.2.5",
"clang-format": "^1.8.0",
"glob": "^10.4.2",
"jest": "^29.3.1",
"just-scripts": "^2.1.0",
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
"version": "0.0.0",
"private": true,
"scripts": {
"format": "yarn format-javascript && yarn format-cpp && yarn format-kotlin && yarn format-python",
"format-check": "yarn format-check-javascript && yarn format-check-cpp && yarn format-check-kotlin && yarn format-check-python",
"format-check-cpp": "node ./scripts/clang-format.js --check",
"format-check-javascript": "prettier --check \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
"format-check-kotlin": "node ./scripts/format-kotlin.js --check",
"format-check-python": "node ./scripts/format-python.js --check",
"format-cpp": "node ./scripts/clang-format.js",
"format-javascript": "prettier --write \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
"format-kotlin": "node ./scripts/format-kotlin.js",
"format-python": "node ./scripts/format-python.js",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"tsc": "yarn workspaces run tsc",
Expand All @@ -21,6 +31,9 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.1.7",
"eslint-plugin-prettier": "^5.5.6",
"fb-dotslash": "0.5.8",
"glob": "^10.4.2",
"ktfmt": "0.59.0",
"prettier": "3.9.4",
"typescript": "5.0.4"
},
Expand Down
52 changes: 52 additions & 0 deletions scripts/clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env dotslash

// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

{
"name": "clang-format 21.1.2",
"platforms": {
"linux-aarch64": {
"size": 1725525,
"hash": "sha256",
"digest": "f66d2bcf98df1373df6ab4544a2b881e9816985b606e1144e4c77dc8ac87b826",
"providers": [{"type": "http", "url": "https://files.pythonhosted.org/packages/c5/83/61fadfa8d62a288d778e0a1ad2f73b01abca64574ee34c5d6d078e0821da/clang_format-21.1.2-py2.py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl"}],
"format": "zip",
"path": "clang_format/data/bin/clang-format"
},
"linux-x86_64": {
"size": 1805060,
"hash": "sha256",
"digest": "6d7caf74fe89154258ddfd63984c98ffe902ef98f013ac517178fc44d72861ff",
"providers": [{"type": "http", "url": "https://files.pythonhosted.org/packages/fb/ac/3c04772acc0257f5730e83adb542b2603c1a62d1315010ab593a980af404/clang_format-21.1.2-py2.py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl"}],
"format": "zip",
"path": "clang_format/data/bin/clang-format"
},
"macos-aarch64": {
"size": 1458874,
"hash": "sha256",
"digest": "fc034652dee24583633177d800bc9deebcc9c65eb7ab53b25bbd0fbd443392a9",
"providers": [{"type": "http", "url": "https://files.pythonhosted.org/packages/ba/90/b8230efcff90a8543da3fb7fc09d7077afebaba019eceb1686d4db94cac3/clang_format-21.1.2-py2.py3-none-macosx_11_0_arm64.whl"}],
"format": "zip",
"path": "clang_format/data/bin/clang-format"
},
"macos-x86_64": {
"size": 1440163,
"hash": "sha256",
"digest": "00498efb43d60d7ac4195362009a79936d26145a9a90cdfa7a6013a62ab3c40c",
"providers": [{"type": "http", "url": "https://files.pythonhosted.org/packages/7d/97/0bb5a6866dfb5f55f7e6ca79466cb0b0081fccbc9f57887949ff23b5c38a/clang_format-21.1.2-py2.py3-none-macosx_10_9_x86_64.whl"}],
"format": "zip",
"path": "clang_format/data/bin/clang-format"
},
"windows-x86_64": {
"size": 1426244,
"hash": "sha256",
"digest": "c98e195a50c0fa40bb058449511b1b681ca7ad553579aa32425f0cfeca8d81ce",
"providers": [{"type": "http", "url": "https://files.pythonhosted.org/packages/fe/0d/3b9c6a41a9eed2d45431d91c0e8608da315cd44d0c24c517bfb686db4b6b/clang_format-21.1.2-py2.py3-none-win_amd64.whl"}],
"format": "zip",
"path": "clang_format/data/bin/clang-format.exe"
}
}
}
93 changes: 93 additions & 0 deletions scripts/clang-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @format
*/

'use strict';

const {findMetaTool, REPO_ROOT} = require('./format-utils');
const {spawnSync} = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const {globSync} = require('glob');

const OSS_CLANG_FORMAT_DOTSLASH = path.join(__dirname, 'clang-format');
const GENERATED_MARKER = Buffer.from('@' + 'generated');
const MAX_HEADER_BYTES = 4096;
const MAX_FILES_PER_PROCESS = 30;
const SOURCE_GLOB = '**/*.{c,cc,cpp,cu,cuh,cxx,h,hh,hpp,hxx,m,mm,proto,tcc}';
const IGNORE = [
'**/.cxx/**',
'**/build/**',
'**/node_modules/**',
'**/Pods/**',
'lib/**',
];

function findClangFormat() {
if (process.env.CLANG_FORMAT != null && process.env.CLANG_FORMAT !== '') {
return {command: process.env.CLANG_FORMAT, prefixArguments: []};
}
return (
findMetaTool('tools', 'third-party', 'clang-format', 'clang-format') ?? {
command: require('fb-dotslash'),
prefixArguments: [OSS_CLANG_FORMAT_DOTSLASH],
}
);
}

function isGenerated(file) {
let fd;
try {
fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r');
const header = Buffer.alloc(MAX_HEADER_BYTES);
const bytesRead = fs.readSync(fd, header, 0, header.length, 0);
return header.subarray(0, bytesRead).includes(GENERATED_MARKER);
} catch (error) {
console.warn(`Unable to inspect ${file}: ${String(error)}`);
return false;
} finally {
if (fd != null) {
fs.closeSync(fd);
}
}
}

function main() {
const check = process.argv.includes('--check');
const files = globSync(SOURCE_GLOB, {
cwd: REPO_ROOT,
ignore: IGNORE,
}).filter(file => !isGenerated(file));
const clangFormat = findClangFormat();
let exitStatus = 0;

for (let index = 0; index < files.length; index += MAX_FILES_PER_PROCESS) {
const result = spawnSync(
clangFormat.command,
[
...clangFormat.prefixArguments,
...(check ? ['--dry-run', '--Werror'] : ['-i']),
...files.slice(index, index + MAX_FILES_PER_PROCESS),
],
{cwd: REPO_ROOT, stdio: 'inherit'},
);
if (result.error != null) {
throw result.error;
}
if (result.signal != null) {
throw new Error(`clang-format was terminated by ${result.signal}`);
}
if (result.status !== 0) {
exitStatus = result.status ?? 1;
}
}
process.exitCode = exitStatus;
}

main();
88 changes: 88 additions & 0 deletions scripts/format-kotlin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @format
*/

'use strict';

const {findJava, REPO_ROOT} = require('./format-utils');
const {spawnSync} = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const {globSync} = require('glob');

const GENERATED_MARKER = Buffer.from('@' + 'generated');
const MAX_HEADER_BYTES = 4096;
const MAX_FILES_PER_PROCESS = 100;

function isGenerated(file) {
let fd;
try {
fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r');
const header = Buffer.alloc(MAX_HEADER_BYTES);
const bytesRead = fs.readSync(fd, header, 0, header.length, 0);
return header.subarray(0, bytesRead).includes(GENERATED_MARKER);
} catch (error) {
console.warn(`Unable to inspect ${file}: ${String(error)}`);
return false;
} finally {
if (fd != null) {
fs.closeSync(fd);
}
}
}

function main() {
const check = process.argv.includes('--check');
const ktfmt =
process.env.KTFMT != null && process.env.KTFMT !== ''
? {command: process.env.KTFMT, prefixArguments: []}
: (() => {
const java = findJava(17);
if (java == null) {
throw new Error(
'Kotlin formatting requires Java 17 or newer. Install a JDK or set JAVA=/path/to/java.',
);
}
const jar =
process.env.KTFMT_JAR != null && process.env.KTFMT_JAR !== ''
? process.env.KTFMT_JAR
: require.resolve('ktfmt/lib/ktfmt.jar');
return {command: java, prefixArguments: ['-jar', jar]};
})();
const files = globSync('**/*.{kt,kts}', {
cwd: REPO_ROOT,
ignore: ['**/.gradle/**', '**/build/**', '**/node_modules/**'],
}).filter(file => !isGenerated(file));
let exitStatus = 0;

for (let index = 0; index < files.length; index += MAX_FILES_PER_PROCESS) {
const result = spawnSync(
ktfmt.command,
[
...ktfmt.prefixArguments,
'--do-not-remove-unused-imports',
...(check ? ['--dry-run', '--set-exit-if-changed'] : []),
...files.slice(index, index + MAX_FILES_PER_PROCESS),
],
{cwd: REPO_ROOT, stdio: 'inherit'},
);
if (result.error != null) {
throw result.error;
}
if (result.signal != null) {
throw new Error(`ktfmt was terminated by ${result.signal}`);
}
if (result.status !== 0) {
exitStatus = result.status ?? 1;
}
}
process.exitCode = exitStatus;
}

main();
Loading
Loading