|
| 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 {spawnSync} = require('node:child_process'); |
| 14 | +const fs = require('node:fs'); |
| 15 | +const path = require('node:path'); |
| 16 | + |
| 17 | +const REPO_ROOT = path.resolve(__dirname, '..'); |
| 18 | +const FBSOURCE_ROOT = path.resolve(REPO_ROOT, '../../..'); |
| 19 | +const IS_META_CHECKOUT = |
| 20 | + fs.existsSync(path.join(FBSOURCE_ROOT, 'tools', 'arcanist')) && |
| 21 | + fs.existsSync(path.join(FBSOURCE_ROOT, 'xplat', 'js')); |
| 22 | + |
| 23 | +function commandVersion(command, prefixArguments = []) { |
| 24 | + const result = spawnSync(command, [...prefixArguments, '--version'], { |
| 25 | + encoding: 'utf8', |
| 26 | + }); |
| 27 | + return { |
| 28 | + output: `${result.stdout ?? ''}\n${result.stderr ?? ''}`, |
| 29 | + status: result.status, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function findSystemDotslash() { |
| 34 | + for (const candidate of ['/usr/bin/dotslash', '/usr/local/bin/dotslash']) { |
| 35 | + if (fs.existsSync(candidate)) { |
| 36 | + return candidate; |
| 37 | + } |
| 38 | + } |
| 39 | + return null; |
| 40 | +} |
| 41 | + |
| 42 | +function findMetaTool(...relativePath) { |
| 43 | + if (!IS_META_CHECKOUT) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + const dotslash = findSystemDotslash(); |
| 47 | + const manifest = path.join(FBSOURCE_ROOT, ...relativePath); |
| 48 | + if (dotslash == null || !fs.existsSync(manifest)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + return {command: dotslash, prefixArguments: [manifest]}; |
| 52 | +} |
| 53 | + |
| 54 | +function javaMajorVersion(command) { |
| 55 | + const result = spawnSync(command, ['-version'], {encoding: 'utf8'}); |
| 56 | + const output = `${result.stdout ?? ''}\n${result.stderr ?? ''}`; |
| 57 | + const version = /version "(?:1\.)?(\d+)/.exec(output); |
| 58 | + return result.status === 0 && version != null ? Number(version[1]) : null; |
| 59 | +} |
| 60 | + |
| 61 | +function findJava(minimumVersion) { |
| 62 | + if (process.env.JAVA != null) { |
| 63 | + return javaMajorVersion(process.env.JAVA) >= minimumVersion |
| 64 | + ? process.env.JAVA |
| 65 | + : null; |
| 66 | + } |
| 67 | + |
| 68 | + const candidates = []; |
| 69 | + if (IS_META_CHECKOUT) { |
| 70 | + if (process.platform === 'darwin') { |
| 71 | + for (const version of [17, 21]) { |
| 72 | + const result = spawnSync( |
| 73 | + '/usr/libexec/java_home', |
| 74 | + ['-v', String(version)], |
| 75 | + { |
| 76 | + encoding: 'utf8', |
| 77 | + }, |
| 78 | + ); |
| 79 | + if (result.status === 0 && result.stdout.trim() !== '') { |
| 80 | + candidates.push(path.join(result.stdout.trim(), 'bin', 'java')); |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + candidates.push( |
| 85 | + '/usr/local/java-runtime/impl/17/bin/java', |
| 86 | + '/usr/local/java-runtime/17/bin/java', |
| 87 | + '/usr/local/java-runtime/impl/21/bin/java', |
| 88 | + '/usr/local/java-runtime/21/bin/java', |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + candidates.push('java'); |
| 93 | + |
| 94 | + return ( |
| 95 | + candidates.find(command => javaMajorVersion(command) >= minimumVersion) ?? |
| 96 | + null |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function warnMissingJava(language) { |
| 101 | + const instructions = IS_META_CHECKOUT |
| 102 | + ? 'Meta: repair the managed `java-runtime` installation, or set JAVA=/path/to/java. Expected Java at `/usr/local/java-runtime/impl/17/bin/java` on Linux or from `/usr/libexec/java_home -v 17` on macOS.' |
| 103 | + : 'Please install a JDK of your choice with Java 17 or newer and make sure the `java` command is in your PATH, or set JAVA=/path/to/java.'; |
| 104 | + console.warn( |
| 105 | + `warning: Skipping ${language} formatting because Java 17 or newer was not found.\n${instructions}`, |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { |
| 110 | + commandVersion, |
| 111 | + findJava, |
| 112 | + findMetaTool, |
| 113 | + IS_META_CHECKOUT, |
| 114 | + warnMissingJava, |
| 115 | +}; |
0 commit comments