最新消息:20210917 已从crifan.com换到crifan.org

【已解决】VSCode中launch.json中能否使用定义变量保存通用路径

JSON crifan 678浏览 0评论
折腾:
【未解决】Win中VMWare中macOS中调试抓包项目
期间,用VSCode去调试项目,用到了launch.json
其中有需要调整不同的值,去测试不同的app:
{
    // 使用 IntelliSense 了解相关属性。
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
        },
        {
            "name": "Python: crawlerStart",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            // "args": ["-task", "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task/191107_card_TestDianPing/191107_card_TestDianPing_app.txt", "-id", "1"],。。。

            // "args": ["-task", "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task/200227_finance_NeteaseNews/200227_finance_NeteaseNews_app.txt", "-id", "1"],

            "args": [
                "-task",
。。。
                // "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task/20200601_qiche_TuHu/20200601_qiche_TuHu_app_iOS.txt",
                // "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task/20200603_xxx_BanMaAICourse/20200603_xxx_BanMaAICourse_app_iOS.txt",
                // "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task/20200604_xxx_MiJia/20200604_xxx_MiJia_app_iOS.txt",


                // "/Users/crifanli/dev/DevRoot/appcrawler/task/20200603_xxx_BanMaAICourse/20200603_xxx_BanMaAICourse_app_iOS.txt",
                "/Users/crifanli/dev/DevRoot/appcrawler/task/20200526_finance_BiYao/20200526_finance_BiYao_app_iOS.txt",


                "-id",
                "1"
            ],
        },
而其中很多前缀路径,是通用的。
希望找到方式,能定义到launch.json中,这样就不用每次切换要调试的App,路径要写很长了。
也方便替换路径。
vscode launch.json define variable
官网中有,但是是:引用固定的变量
可用的有
  • ${workspaceFolder} – the path of the folder opened in VS Code
  • * ${workspaceFolderBasename} – the name of the folder opened in VS Code without any slashes (/)
  • * ${file} – the current opened file
  • * ${relativeFile} – the current opened file relative to workspaceFolder
  • * ${relativeFileDirname} – the current opened file’s dirname relative to workspaceFolder
  • * ${fileBasename} – the current opened file’s basename
  • * ${fileBasenameNoExtension} – the current opened file’s basename with no file extension
  • * ${fileDirname} – the current opened file’s dirname
  • * ${fileExtname} – the current opened file’s extension
  • * ${cwd} – the task runner’s current working directory on startup
  • * ${lineNumber} – the current selected line number in the active file
  • * ${selectedText} – the current selected text in the active file
  • * ${execPath} – the path to the running VS Code executable
  • * ${defaultBuildTask} – the name of the default build task
不过对于此处来说,倒也是可以用得上的:
此处希望是:
最新的:/Users/crifanli/dev/DevRoot/appcrawler/task
或:
之前的:/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/task/
其实(看起来应该)都是:
${workspaceFolder}/task
后来也看到,可以引用环境变量
Environment variables
You can also reference environment variables through the ${env:Name} syntax (for example, ${env:USERNAME}).
{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "program": "${workspaceFolder}/app.js",
  "cwd": "${workspaceFolder}",
  "args": ["${env:USERNAME}"]
}
不过,也再去看看,能否自定义其他路径
node.js – How do I add environment variables to launch.json in VSCode – Stack Overflow
好像支持env?
别人说了,env已改名为:environment
-》貌似理论上可以用 environment 去:添加 环境变量
但不是我此处希望的:只是此处的launch.json中用的变量
好像是task,task.json? 中才有
  • ${file} the current opened file
  • ${fileBasename} the current opened file’s basename
  • ${fileDirname} the current opened file’s dirname
  • ${fileExtname} the current opened file’s extension
  • ${cwd} the task runner’s current working directory on startup
回去
看到更多的解释:
Many debuggers support some of the following attributes:
  • * program – executable or file to run when launching the debugger
  • * args – arguments passed to the program to debug
  • * env – environment variables (the value null can be used to “undefine” a variable)
  • * cwd – current working directory for finding dependencies and other files
  • * port – port when attaching to a running process
  • * stopOnEntry – break immediately when the program launches
  • * console – what kind of console to use, for example, internalConsole, integratedTerminal, or externalTerminal
其中:
env的话,就是:定义环境变量 用的
而不是此处,launch.json内部可以访问的
-》所以还是无法自定义变量
-》只能用前面说的,已有变量
去改为:${workspaceFolder}
                // "/Users/crifanli/dev/DevRoot/appcrawler/task/20200526_finance_BiYao/20200526_finance_BiYao_app_iOS.txt",
                "${workspaceFolder}/task/20200526_finance_BiYao/20200526_finance_BiYao_app_iOS.txt",
去试试如何
是可以的,此处task可以正常解析出路径的。
【总结】
此处VSCode的调试的配置文件launch.json中,想要把之前一个常用路径,替换成变量
此处:无法自定义额外变量,在launch.json中用。
但是该常用路径:
/Users/crifanli/dev/DevRoot/appcrawler/task/
其中前面部分
/Users/crifanli/dev/DevRoot/appcrawler
属于:当前项目根目录
所以可以借助于
VSCode中预定义的变量
  • ${workspaceFolder} – the path of the folder opened in VS Code
把原先的:
                "/Users/crifanli/dev/DevRoot/appcrawler/task/20200526_finance_BiYao/20200526_finance_BiYao_app_iOS.txt",
去替换为:
                "${workspaceFolder}/task/20200526_finance_BiYao/20200526_finance_BiYao_app_iOS.txt",
即可。
注:其他更多预定义变量,和具体含义解释,详见

转载请注明:在路上 » 【已解决】VSCode中launch.json中能否使用定义变量保存通用路径

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.186 seconds, using 23.33MB memory