VSCode 扩展位置变量 [英] VSCode extensions location variable
问题描述
在我正在编写的扩展中,我想重新定义工作区中的现有设置以指向我与扩展一起打包的脚本.例如,在 Mac 上,此脚本位于 ~/.vscode/extensions/publisher.name.version/script
中.
In an extension I am writing, I want to redefine an existing setting in the workspace to point at a script I am packaging with the extension. On a mac this script lives in ~/.vscode/extensions/publisher.name.version/script
for example.
如果我假设这是扩展所在的位置,那么在我的 activate 函数中,我可以使用
If I assume that this is where the extension lives then in my activate function I can update this value using
export async function activate(context: vscode.ExtensionContext) {
const home = process.env.HOME;
const execLocation = home + "/.vscode/extensions/publisher.name.version/script";
...
然后更新工作区设置.
但是 - 我想访问本地安装的扩展位置,以及我的扩展的 ID 和版本 - 我在 VSCode 中找不到正确的设置来执行此操作.如果有人知道正确的环境变量以便我可以访问它们,我将不胜感激.
However - I would like to access the locally installed extensions location, together with the id and version of my extension - I cannot find the correct setting in VSCode to do this. I would be very grateful if someone knew the correct environment variable so I could access them.
我知道可以使用选项 --extensionHomePath
从命令行调用代码 - 我不确定如何以编程方式访问此变量.
I know it is possible to call code from the command line with the option --extensionHomePath
- I am not sure how to access this variable programmatically.
此外,我不确定如何从 context
参数中找到版本、发布者和名称 - 显然我从 package.json
文件中知道这些,但它会是如果可能的话,很高兴能够以编程方式访问它们.
Also I am not sure how to find the version, publisher and name from the context
parameter - obviously I know these from the package.json
file but it would be nice to be able to access them programmatically if possible.
推荐答案
您可以通过 ExtensionContext
.
此方法为您提供给定相对路径(关于您的项目根目录)的资源的绝对路径.
You can get that information by the asAbsolutePath()
method of the ExtensionContext
.
This method gets you the absolute path of a resource for a given relative path (regarding your project root).
因此我建议您将代码更改为以下内容:
Therefore I suggest you to change your code to the following:
export async function activate(context: vscode.ExtensionContext) {
const execLocation = context.asAbsolutePath("script");
console.log("Absolute exec location: " + execLocation);
这篇关于VSCode 扩展位置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!