22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . IO ;
5+ using System . Linq ;
56using System . Text . Json ;
67using System . Text . Json . Serialization ;
78
@@ -16,12 +17,14 @@ public class ExternalTool
1617 public string Executable { get ; private set ; }
1718 public string OpenCmdArgs { get ; private set ; }
1819 public Bitmap IconImage { get ; private set ; } = null ;
20+ public Func < string , string > ArgTransform { get ; private set ; }
1921
20- public ExternalTool ( string name , string icon , string executable , string openCmdArgs )
22+ public ExternalTool ( string name , string icon , string executable , string openCmdArgs , Func < string , string > argsTransform )
2123 {
2224 Name = name ;
2325 Executable = executable ;
2426 OpenCmdArgs = openCmdArgs ;
27+ ArgTransform = argsTransform ?? ( ( s ) => s ) ;
2528
2629 try
2730 {
@@ -37,11 +40,16 @@ public ExternalTool(string name, string icon, string executable, string openCmdA
3740
3841 public void Open ( string repo )
3942 {
43+ string arguments = string . Format ( OpenCmdArgs , repo ) ;
44+
45+ if ( ArgTransform != null )
46+ arguments = ArgTransform . Invoke ( arguments ) ;
47+
4048 Process . Start ( new ProcessStartInfo ( )
4149 {
4250 WorkingDirectory = repo ,
4351 FileName = Executable ,
44- Arguments = string . Format ( OpenCmdArgs , repo ) ,
52+ Arguments = arguments ,
4553 UseShellExecute = false ,
4654 } ) ;
4755 }
@@ -110,17 +118,17 @@ public ExternalToolsFinder()
110118 _customPaths = new ExternalToolPaths ( ) ;
111119 }
112120
113- public void TryAdd ( string name , string icon , string args , string key , Func < string > finder )
121+ public void TryAdd ( string name , string icon , string args , string key , Func < string > finder , Func < string , string > argsTransform = null )
114122 {
115123 if ( _customPaths . Tools . TryGetValue ( key , out var customPath ) && File . Exists ( customPath ) )
116124 {
117- Founded . Add ( new ExternalTool ( name , icon , customPath , args ) ) ;
125+ Founded . Add ( new ExternalTool ( name , icon , customPath , args , argsTransform ) ) ;
118126 }
119127 else
120128 {
121129 var path = finder ( ) ;
122130 if ( ! string . IsNullOrEmpty ( path ) && File . Exists ( path ) )
123- Founded . Add ( new ExternalTool ( name , icon , path , args ) ) ;
131+ Founded . Add ( new ExternalTool ( name , icon , path , args , argsTransform ) ) ;
124132 }
125133 }
126134
@@ -154,6 +162,25 @@ public void Zed(Func<string> platformFinder)
154162 TryAdd ( "Zed" , "zed" , "\" {0}\" " , "ZED" , platformFinder ) ;
155163 }
156164
165+ public void VisualStudio ( Func < string > platformFinder )
166+ {
167+ TryAdd ( "Visual Studio" , "vs" , "\" {0}\" " , "VISUALSTUDIO" , platformFinder , VisualStudioTryFindSolution ) ;
168+ }
169+
170+ private static string VisualStudioTryFindSolution ( string path )
171+ {
172+ try
173+ {
174+ if ( Directory . GetFiles ( path . Trim ( '\" ' ) , "*.sln" , SearchOption . AllDirectories ) . FirstOrDefault ( ) is string solutionPath )
175+ return Path . GetFullPath ( solutionPath ) ;
176+ }
177+ catch
178+ {
179+ // do nothing
180+ }
181+ return path ;
182+ }
183+
157184 public void FindJetBrainsFromToolbox ( Func < string > platformFinder )
158185 {
159186 var exclude = new List < string > { "fleet" , "dotmemory" , "dottrace" , "resharper-u" , "androidstudio" } ;
@@ -171,7 +198,8 @@ public void FindJetBrainsFromToolbox(Func<string> platformFinder)
171198 $ "{ tool . DisplayName } { tool . DisplayVersion } ",
172199 supported_icons . Contains ( tool . ProductCode ) ? $ "JetBrains/{ tool . ProductCode } " : "JetBrains/JB" ,
173200 Path . Combine ( tool . InstallLocation , tool . LaunchCommand ) ,
174- "\" {0}\" " ) ) ;
201+ "\" {0}\" " ,
202+ null ) ) ;
175203 }
176204 }
177205 }
0 commit comments