-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(workspace-tree): Future proof icon path resolution (#424)
Current resource path resolution is using the source file as the base directory. This will fail if we minify the package into single js. With this commit, we use the extension path as the base directory which is more reliable: it is also working for the current packaging, and we just have to make sure the resource layout of the minified package remains the same. An enum is provided with a test looping through all value and checking the existence of the icon files in extension.
- Loading branch information
Showing
9 changed files
with
161 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 The Bazel Authors. All rights reserved. | ||
// | ||
// 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 vscode from "vscode"; | ||
|
||
export enum IconName { | ||
ANDROID_BINARY = "android_binary", | ||
APPLE_APPLICATION = "apple_application", | ||
APPLE_EXECUTABLE_BUNDLE = "apple_executable_bundle", | ||
APPLE_FRAMEWORK = "apple_framework", | ||
BINARY = "binary", | ||
CONFIG_SETTING = "config_setting", | ||
FILEGROUP = "filegroup", | ||
GENRULE = "genrule", | ||
LIBRARY = "library", | ||
PROTO = "proto", | ||
RESOURCE_BUNDLE = "resource_bundle", | ||
TEST = "test", | ||
TEST_SUITE = "test_suite", | ||
} | ||
|
||
/** | ||
* Helper functions for getting the resource bundled inside this extension. | ||
*/ | ||
export class Resources { | ||
public static fromExtensionContext( | ||
context: vscode.ExtensionContext, | ||
): Resources { | ||
return new Resources(context.extensionPath); | ||
} | ||
|
||
/** | ||
* @param extensionPath The extension path, usually from the extension | ||
* context. | ||
*/ | ||
constructor(private readonly extensionPath: string) {} | ||
|
||
/** | ||
* Returns the icon path in string. | ||
* | ||
* @param name The icon file name. | ||
*/ | ||
public getIconPath(name: IconName): string { | ||
return path.join(this.extensionPath, "icons", `${name}.svg`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import assert = require("assert"); | ||
import { IconName, Resources } from "../src/extension/resources"; | ||
import * as fs from "fs/promises"; | ||
import * as vscode from "vscode"; | ||
|
||
describe("The resources", () => { | ||
const extension = vscode.extensions.getExtension("BazelBuild.vscode-bazel"); | ||
if (!extension) { | ||
assert.fail("Extension not found"); | ||
} | ||
const resources = new Resources(extension.extensionPath); | ||
|
||
it("provides valid icon paths", async () => { | ||
for (const name of Object.values(IconName)) { | ||
await assert.doesNotReject(async () => { | ||
await fs.stat(resources.getIconPath(name)); | ||
}, "invalid icon path"); | ||
} | ||
}); | ||
}); |