blob: 555571a10c4dd94c96bf493c3841058020a60bb3 [file]
/**
* @license
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from "path";
import * as fs from "fs";
export function fail(message: string): never {
console.error(message);
process.exit(1);
}
// Bazel params may be surrounded with quotes
function removeSurrondedQuotes(str: string): string {
return str.startsWith("'") && str.endsWith("'") ?
str.slice(1, -1) : str;
}
function resolveExecrootPath(filePath: string): string {
if (path.isAbsolute(filePath)) return filePath;
const bazelBindir = process.env["BAZEL_BINDIR"];
if (bazelBindir && process.cwd().endsWith(bazelBindir)) {
const execroot = process.cwd().slice(0, process.cwd().length - bazelBindir.length);
return path.join(execroot, filePath);
}
return path.join(process.cwd(), filePath);
}
export function readMultilineParamFile(filePath: string): string[] {
return fs.readFileSync(resolveExecrootPath(filePath), {encoding: 'utf-8'})
.split(/\r?\n/)
.filter(f => f.length > 0)
.map(removeSurrondedQuotes)
.map(resolveExecrootPath);
}