如何在VS Code任务中拆分${fileBasename}变量?
在VS Code任务里拆分变量实现目标curl命令
VS Code任务本身没法直接用连字符拆分变量,但有两种靠谱的方法能实现你要的效果:
方法1:用VS Code内置的正则替换变量
VS Code支持对变量做正则替换,直接用这个语法就能提取出schemaName:
把任务改成这样:
{ "version": "2.0.0", "tasks": [ { "label": "上传Schema", "type": "shell", "command": "curl -X PUT http://localhost:9000/schema/${fileBasename/(.*)-schema.json/$1/} -d @${fileBasename}", "problemMatcher": [] } ] }
这里${fileBasename/(.*)-schema.json/$1/}的作用是:匹配xxx-schema.json格式的文件名,把xxx(也就是你要的schemaName)提取出来,替换到URL路径里。
方法2:用Shell的字符串处理(适合类Unix系统)
如果你的终端是bash/zsh这类,可以用shell自带的参数扩展来拆分:
任务命令改成:
{ "version": "2.0.0", "tasks": [ { "label": "上传Schema", "type": "shell", "command": "FILE=${fileBasename}; curl -X PUT http://localhost:9000/schema/${FILE%-schema.json} -d @${FILE}", "problemMatcher": [] } ] }
${FILE%-schema.json}会自动去掉文件名末尾的-schema.json,直接得到schemaName,语法简单直接。
要是需要跨Windows、Mac、Linux用,选方法1;只在Mac/Linux上用,方法2更省心。
内容的提问来源于stack exchange,提问作者HotFix
相关产品推荐
相关产品推荐

