Skip to content

Commit 52f638f

Browse files
cortinicometa-codesync[bot]
authored andcommitted
Add Java to yarn format
Summary: Add `yarn format-java` and `yarn format-check-java` using the npm `google-java-format` package, which bundles google-java-format 1.23.0. This removes the Gradle build dependency and adds the offline package mirror and workspace lock entries. The wrapper discovers a suitable JDK when available and otherwise prints environment-specific Java 17 setup guidance before skipping Java. Changelog: [Internal] Differential Revision: D119487613
1 parent dd0b4f5 commit 52f638f

3 files changed

Lines changed: 160 additions & 2 deletions

File tree

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
"cxx-api-validate": "python -m scripts.cxx-api.parser --validate",
1515
"flow-check": "flow full-check",
1616
"flow": "flow",
17-
"format-check": "yarn format-check-javascript && yarn format-check-cpp && yarn format-check-kotlin",
17+
"format-check": "yarn format-check-javascript && yarn format-check-cpp && yarn format-check-kotlin && yarn format-check-java",
1818
"format-check-cpp": "node ./scripts/clang-format.js --check",
19+
"format-check-java": "node ./scripts/format-java.js --check",
1920
"format-check-javascript": "prettier --check \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
2021
"format-check-kotlin": "node ./scripts/format-kotlin.js --check",
21-
"format": "yarn format-javascript && yarn format-cpp && yarn format-kotlin",
22+
"format": "yarn format-javascript && yarn format-cpp && yarn format-kotlin && yarn format-java",
2223
"format-cpp": "node ./scripts/clang-format.js",
24+
"format-java": "node ./scripts/format-java.js",
2325
"format-javascript": "prettier --write \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
2426
"format-kotlin": "node ./scripts/format-kotlin.js",
2527
"featureflags": "yarn --cwd packages/react-native featureflags",
@@ -93,6 +95,7 @@
9395
"flow-eslint": "0.331.0",
9496
"flow-parser": "0.331.0",
9597
"flow-transform": "0.331.0",
98+
"google-java-format": "1.4.0",
9699
"ini": "^5.0.0",
97100
"inquirer": "^7.1.0",
98101
"jest": "^29.7.0",

scripts/format-java.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const {findJava, warnMissingJava} = require('./format-utils');
14+
const {spawnSync} = require('node:child_process');
15+
const fs = require('node:fs');
16+
const path = require('node:path');
17+
const {globSync} = require('tinyglobby');
18+
19+
const REPO_ROOT = path.resolve(__dirname, '..');
20+
const GENERATED_MARKER = Buffer.from('@' + 'generated');
21+
const MINIMUM_JAVA_VERSION = 17;
22+
const MAX_FILES_PER_PROCESS = 30;
23+
const MAX_HEADER_BYTES = 4096;
24+
const IGNORE = [
25+
'**/Pods/**',
26+
'**/build/**',
27+
'**/com/facebook/yoga/**',
28+
'**/node_modules/**',
29+
];
30+
31+
function isGenerated(file) {
32+
let fd;
33+
try {
34+
fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r');
35+
const header = Buffer.alloc(MAX_HEADER_BYTES);
36+
const bytesRead = fs.readSync(fd, header, 0, header.length, 0);
37+
return header.subarray(0, bytesRead).includes(GENERATED_MARKER);
38+
} catch (error) {
39+
const message = error instanceof Error ? error.message : String(error);
40+
throw new Error(`Unable to inspect ${file}: ${message}`, {cause: error});
41+
} finally {
42+
if (fd != null) {
43+
fs.closeSync(fd);
44+
}
45+
}
46+
}
47+
48+
function findGoogleJavaFormatJar() {
49+
const packageRoot = path.dirname(
50+
require.resolve('google-java-format/package.json'),
51+
);
52+
const jars = fs
53+
.readdirSync(path.join(packageRoot, 'lib'))
54+
.filter(file => file.endsWith('-all-deps.jar'));
55+
if (jars.length !== 1) {
56+
throw new Error(
57+
`Expected one google-java-format jar, found ${jars.length}.`,
58+
);
59+
}
60+
return path.join(packageRoot, 'lib', jars[0]);
61+
}
62+
63+
function main() {
64+
const check = process.argv[2] === '--check';
65+
const java = findJava(MINIMUM_JAVA_VERSION);
66+
if (java == null) {
67+
warnMissingJava('Java');
68+
return;
69+
}
70+
const googleJavaFormatJar = findGoogleJavaFormatJar();
71+
const files = globSync('**/*.java', {cwd: REPO_ROOT, ignore: IGNORE}).filter(
72+
file => !isGenerated(file),
73+
);
74+
75+
let exitStatus = 0;
76+
for (let i = 0; i < files.length; i += MAX_FILES_PER_PROCESS) {
77+
const result = spawnSync(
78+
java,
79+
[
80+
'--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
81+
'--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
82+
'--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
83+
'--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
84+
'--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
85+
'--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
86+
'-jar',
87+
googleJavaFormatJar,
88+
...(check ? ['--dry-run', '--set-exit-if-changed'] : ['--replace']),
89+
...files.slice(i, i + MAX_FILES_PER_PROCESS),
90+
],
91+
{cwd: REPO_ROOT, stdio: 'inherit'},
92+
);
93+
if (result.error != null) {
94+
throw result.error;
95+
}
96+
if (result.signal != null) {
97+
throw new Error(`google-java-format was terminated by ${result.signal}`);
98+
}
99+
if (result.status !== 0) {
100+
exitStatus = result.status ?? 1;
101+
}
102+
}
103+
process.exitCode = exitStatus;
104+
}
105+
106+
main();

yarn.lock

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,11 @@ async-limiter@~1.0.0:
29122912
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
29132913
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==
29142914

2915+
async@^3.2.4:
2916+
version "3.2.6"
2917+
resolved "https://registry.yarnpkg.com/async/-/async-3.2.6.tgz#1b0728e14929d51b85b449b7f06e27c1145e38ce"
2918+
integrity sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==
2919+
29152920
author-regex@^1.0.0:
29162921
version "1.0.0"
29172922
resolved "https://registry.yarnpkg.com/author-regex/-/author-regex-1.0.0.tgz#d08885be6b9bbf9439fe087c76287245f0a81450"
@@ -3146,6 +3151,13 @@ brace-expansion@^1.1.7:
31463151
balanced-match "^1.0.0"
31473152
concat-map "0.0.1"
31483153

3154+
brace-expansion@^2.0.1:
3155+
version "2.1.4"
3156+
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.1.4.tgz#589dab11c0018d0366be64cd8bf12c8dbecc8326"
3157+
integrity sha512-hGfVzPxthbf3+2yjg/RBs60cB0FhqBS/zvdV/4wn4/BmN0bNMMHPc4V/BbFieqf1TKAGGAHnY4eSjajCl0f2Xg==
3158+
dependencies:
3159+
balanced-match "^1.0.0"
3160+
31493161
brace-expansion@^2.0.2:
31503162
version "2.0.2"
31513163
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.2.tgz#54fc53237a613d854c7bd37463aad17df87214e7"
@@ -5041,6 +5053,17 @@ glob@^7.0.0, glob@^7.1.3, glob@^7.1.4:
50415053
once "^1.3.0"
50425054
path-is-absolute "^1.0.0"
50435055

5056+
glob@^8.1.0:
5057+
version "8.1.0"
5058+
resolved "https://registry.yarnpkg.com/glob/-/glob-8.1.0.tgz#d388f656593ef708ee3e34640fdfb99a9fd1c33e"
5059+
integrity sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==
5060+
dependencies:
5061+
fs.realpath "^1.0.0"
5062+
inflight "^1.0.4"
5063+
inherits "2"
5064+
minimatch "^5.0.1"
5065+
once "^1.3.0"
5066+
50445067
globals@^11.1.0:
50455068
version "11.12.0"
50465069
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
@@ -5085,6 +5108,15 @@ globby@^11.1.0:
50855108
merge2 "^1.4.1"
50865109
slash "^3.0.0"
50875110

5111+
google-java-format@1.4.0:
5112+
version "1.4.0"
5113+
resolved "https://registry.yarnpkg.com/google-java-format/-/google-java-format-1.4.0.tgz#d944b7a20f0a3729318b5c120931178843ed9a82"
5114+
integrity sha512-TlO1nUogUW6PonVL4xZCciVoJcjB2Nxo/dEY5M8GC5TJnQi6A4XeqJoiOot/To20350wPup9aQfP3Ia6GR+vEg==
5115+
dependencies:
5116+
async "^3.2.4"
5117+
glob "^8.1.0"
5118+
resolve "^1.22.8"
5119+
50885120
gopd@^1.0.1, gopd@^1.2.0:
50895121
version "1.2.0"
50905122
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.2.0.tgz#89f56b8217bdbc8802bd299df6d7f1081d7e51a1"
@@ -7220,6 +7252,13 @@ minimatch@^10.0.1, minimatch@^10.2.2:
72207252
dependencies:
72217253
brace-expansion "^5.0.5"
72227254

7255+
minimatch@^5.0.1:
7256+
version "5.1.9"
7257+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.9.tgz#1293ef15db0098b394540e8f9f744f9fda8dee4b"
7258+
integrity sha512-7o1wEA2RyMP7Iu7GNba9vc0RWWGACJOCZBJX2GJWip0ikV+wcOsgVuY9uE8CPiyQhkGFSlhuSkZPavN7u1c2Fw==
7259+
dependencies:
7260+
brace-expansion "^2.0.1"
7261+
72237262
minimatch@^9.0.4:
72247263
version "9.0.9"
72257264
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.9.tgz#9b0cb9fcb78087f6fd7eababe2511c4d3d60574e"
@@ -8080,6 +8119,16 @@ resolve@^1.1.6, resolve@^1.14.2, resolve@^1.20.0, resolve@~1.22.1, resolve@~1.22
80808119
path-parse "^1.0.7"
80818120
supports-preserve-symlinks-flag "^1.0.0"
80828121

8122+
resolve@^1.22.8:
8123+
version "1.22.12"
8124+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.12.tgz#f5b2a680897c69c238a13cd16b15671f8b73549f"
8125+
integrity sha512-TyeJ1zif53BPfHootBGwPRYT1RUt6oGWsaQr8UyZW/eAm9bKoijtvruSDEmZHm92CwS9nj7/fWttqPCgzep8CA==
8126+
dependencies:
8127+
es-errors "^1.3.0"
8128+
is-core-module "^2.16.1"
8129+
path-parse "^1.0.7"
8130+
supports-preserve-symlinks-flag "^1.0.0"
8131+
80838132
resolve@^2.0.0-next.5:
80848133
version "2.0.0-next.5"
80858134
resolved "https://registry.yarnpkg.com/resolve/-/resolve-2.0.0-next.5.tgz#6b0ec3107e671e52b68cd068ef327173b90dc03c"

0 commit comments

Comments
 (0)