|
| 1 | +// Copyright 2024 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import * as vscode from "vscode"; |
| 16 | +import { |
| 17 | + BazelQuery, |
| 18 | + BazelWorkspaceInfo, |
| 19 | + labelFromQueriedToAbsolute, |
| 20 | +} from "../bazel"; |
| 21 | +import { |
| 22 | + getDefaultBazelExecutablePath, |
| 23 | + getDefaultQueryExpression, |
| 24 | +} from "../extension/configuration"; |
| 25 | +import { blaze_query } from "../protos"; |
| 26 | +import { BazelInfo } from "../bazel/bazel_info"; |
| 27 | + |
| 28 | +/** |
| 29 | + * Bazel querier for workspace tree. |
| 30 | + * |
| 31 | + * The interface defined here is to specifying the operation required for a |
| 32 | + * workspace tree instead of all bazel query syntax and options supported. |
| 33 | + * |
| 34 | + * The function named with queryXxx are all for querying bazel informations. |
| 35 | + */ |
| 36 | +export interface IBazelQuerier { |
| 37 | + /** |
| 38 | + * Queries bazel workspace path by given vscode workspace folder. |
| 39 | + * |
| 40 | + * @param workspaceInfo the Bazel workspace info. |
| 41 | + * @returns package name queries in absolute apparent paths. |
| 42 | + */ |
| 43 | + queryWorkspace( |
| 44 | + workspaceFolder: vscode.WorkspaceFolder, |
| 45 | + ): Thenable<BazelWorkspaceInfo | undefined>; |
| 46 | + |
| 47 | + /** |
| 48 | + * Queries all Bazel packages in a workspace folder. |
| 49 | + * |
| 50 | + * @param workspaceInfo the Bazel workspace info. |
| 51 | + * @returns package name queries in absolute apparent paths. |
| 52 | + */ |
| 53 | + queryPackages(workspaceInfo: BazelWorkspaceInfo): Thenable<string[]>; |
| 54 | + |
| 55 | + /** |
| 56 | + * Queries all children targets of a Bazel package. |
| 57 | + * |
| 58 | + * @param workspaceInfo the Bazel workspace info. |
| 59 | + * @param packagePath the Bazel package path. Could be either in absolute label or |
| 60 | + * relative to the opening vscode workspace in `workspaceInfo`. |
| 61 | + */ |
| 62 | + queryChildrenTargets( |
| 63 | + workspaceInfo: BazelWorkspaceInfo, |
| 64 | + packagePath: string, |
| 65 | + ): Thenable<blaze_query.IQueryResult>; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Calling Bazel process for the queries. |
| 70 | + */ |
| 71 | +export class ProcessBazelQuerier implements IBazelQuerier { |
| 72 | + async queryWorkspace( |
| 73 | + workspaceFolder: vscode.WorkspaceFolder, |
| 74 | + ): Promise<BazelWorkspaceInfo | undefined> { |
| 75 | + try { |
| 76 | + const bazelWorkspacePath = await new BazelInfo( |
| 77 | + getDefaultBazelExecutablePath(), |
| 78 | + workspaceFolder.uri.fsPath, |
| 79 | + ).getOne("workspace"); |
| 80 | + return new BazelWorkspaceInfo(bazelWorkspacePath, workspaceFolder); |
| 81 | + } catch { |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + async queryPackages(workspaceInfo: BazelWorkspaceInfo): Promise<string[]> { |
| 87 | + const packages = await new BazelQuery( |
| 88 | + getDefaultBazelExecutablePath(), |
| 89 | + workspaceInfo.workspaceFolder.uri.fsPath, |
| 90 | + ).queryPackages(getDefaultQueryExpression()); |
| 91 | + return packages.map(labelFromQueriedToAbsolute); |
| 92 | + } |
| 93 | + |
| 94 | + queryChildrenTargets( |
| 95 | + workspaceInfo: BazelWorkspaceInfo, |
| 96 | + packagePath: string, |
| 97 | + ): Promise<blaze_query.IQueryResult> { |
| 98 | + // Getting all rules without files, thus using :all instead of :*. |
| 99 | + const query = `${packagePath}:all`; |
| 100 | + return new BazelQuery( |
| 101 | + getDefaultBazelExecutablePath(), |
| 102 | + workspaceInfo.workspaceFolder.uri.fsPath, |
| 103 | + ).queryTargets(query, { |
| 104 | + ignoresErrors: true, |
| 105 | + sortByRuleName: true, |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
0 commit comments