Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 969
Stefan Avatar asked Jul 26 '17 08:07

Stefan


People also ask

How do I add a command in Visual Studio?

In the Solution Explorer, right-click the project node and select Add > New Item. In the Add New Item dialog, go to Visual C# > Extensibility and select Command. In the Name field at the bottom of the window, change the command file name to FirstCommand.


2 Answers

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

like image 61
Stefan Avatar answered Oct 21 '22 14:10

Stefan


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?

like image 34
Sergey Vlasov Avatar answered Oct 21 '22 12:10

Sergey Vlasov