7

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Stefan
  • 10,010
  • 7
  • 61
  • 117

2 Answers2

6

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

enter image description here

2. 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

enter image description here

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

Stefan
  • 10,010
  • 7
  • 61
  • 117
1

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?

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • Does it allow to save the commands with the solution? I would like to have the solution under version control. It would be great if a colleague would be able to open the solution and have the commands available without doing an additional import of settings. – Stefan Jul 27 '17 at 07:54
  • 1
    @Stefan No. You can automatically read files on solution opening, bun not add commands to Visual Commander. – Sergey Vlasov Jul 27 '17 at 11:13