How to execute custom file specific command / task in Visual Studio?

。_饼干妹妹 提交于 2019-12-01 07:01:31

问题


I would like to be able to define custom commands/task/macro for a VisualStudio solution. Then I would like to execute that command for a file that is selected in the Solution Explorer.

There are several possibilities to execute the command that would be fine for me:

a) Right click on the file in the Solution Explorer and choose the command from the context menu (my favorite)

b) Select a file in the Solution Explorer. Then click on a button on a tool bar. The command would then somehow retrieve the selected file from the Solution Explorer.

c) Select a file in the Solution Explorer. Then start a task from the Task Runner Explorer. The executed task would somehow retrive the selected file from the 'Solution Explorer'

I tried to use the VisualStudio extension VsCommandBuddy. However, it does not support file specific commands, see

https://github.com/PaulHuizer/VsCommandBuddy/issues/21

I also tried to use a Grunt or Gulp task that can be started from the Task Runner Explorer. However, I don't know how I can pass/access the file that is currently selected in the Solution Explorer.

https://blogs.msdn.microsoft.com/webdev/2016/01/06/task-runners-in-visual-studio-2015/

=> Is there a VisualStudio extension that easily allows to define custom commands for files?

=> How can I pass/access the file that is selected in the SolutionExplorer in script files (e.g. Gulp, Grunt, Webpack)?

=> Any other comfortable work flow that you would recommend?

It would be possible to write my own VisualStudio extension. But I guess that someone else already knows a solution for this.


回答1:


Create an external tool command:

  • Tools=>External Tools=>Add
  • Use cmd.exe as Command
  • Use /c as first entry for `Arguments' and then specify the command line command you want to execute, e.g. grunt
  • Use the available variables, e.g. $(SolutionDir), $(ItemPath) (=file path), ... to customize your external command

Add external command as entry to the context menu of the solution explorer

  • Tools=>Customize=>Commands

  • Select Context menu: Project and Solution Context Menu | Item

  • Add Command...=>Tools=>External Command 1

Run command

Use the new context menu entry for items in the solution explorer

Stop command

In order to cancel/stop an external command, you can also use the context menu of the solution explorer. If the original name of the command is "Test with Karma", the title will be modified to "(Stop) Test with Karma" as long as the command is running. =>Select that entry to stop the command.

Export settings

Unfortunately those settings can not be stored with the Solution (?). It is however possible to export those settings. Then a colleague can import them:

  • Tools=> Import and export Settings...=>Next

  • General Settings=> External Tools List and

  • General Settings=> Menu and Command Bar Customizations




回答2:


You can use my Visual Commander extension to define a custom command/macro. On how to get the currently selected file in Solution Explorer see In a VS 2015 extension, how can I get the selected object in the Solution Explorer?



来源:https://stackoverflow.com/questions/45321194/how-to-execute-custom-file-specific-command-task-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!