22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . IO ;
5- using System . Linq ;
65using System . Text . Json ;
76using System . Text . Json . Serialization ;
87
@@ -14,17 +13,13 @@ namespace SourceGit.Models
1413 public class ExternalTool
1514 {
1615 public string Name { get ; private set ; }
17- public string Executable { get ; private set ; }
18- public string OpenCmdArgs { get ; private set ; }
1916 public Bitmap IconImage { get ; private set ; } = null ;
20- public Func < string , string > ArgTransform { get ; private set ; }
2117
22- public ExternalTool ( string name , string icon , string executable , string openCmdArgs , Func < string , string > argsTransform )
18+ public ExternalTool ( string name , string icon , string execFile , Func < string , string > execArgsGenerator = null )
2319 {
2420 Name = name ;
25- Executable = executable ;
26- OpenCmdArgs = openCmdArgs ;
27- ArgTransform = argsTransform ?? ( ( s ) => s ) ;
21+ _execFile = execFile ;
22+ _execArgsGenerator = execArgsGenerator ?? ( repo => $ "\" { repo } \" ") ;
2823
2924 try
3025 {
@@ -40,19 +35,17 @@ public ExternalTool(string name, string icon, string executable, string openCmdA
4035
4136 public void Open ( string repo )
4237 {
43- string arguments = string . Format ( OpenCmdArgs , repo ) ;
44-
45- if ( ArgTransform != null )
46- arguments = ArgTransform . Invoke ( arguments ) ;
47-
4838 Process . Start ( new ProcessStartInfo ( )
4939 {
5040 WorkingDirectory = repo ,
51- FileName = Executable ,
52- Arguments = arguments ,
41+ FileName = _execFile ,
42+ Arguments = _execArgsGenerator . Invoke ( repo ) ,
5343 UseShellExecute = false ,
5444 } ) ;
5545 }
46+
47+ private string _execFile = string . Empty ;
48+ private Func < string , string > _execArgsGenerator = null ;
5649 }
5750
5851 public class JetBrainsState
@@ -118,67 +111,48 @@ public ExternalToolsFinder()
118111 _customPaths = new ExternalToolPaths ( ) ;
119112 }
120113
121- public void TryAdd ( string name , string icon , string args , string key , Func < string > finder , Func < string , string > argsTransform = null )
114+ public void TryAdd ( string name , string icon , Func < string > finder , Func < string , string > execArgsGenerator = null )
122115 {
123- if ( _customPaths . Tools . TryGetValue ( key , out var customPath ) && File . Exists ( customPath ) )
116+ if ( _customPaths . Tools . TryGetValue ( name , out var customPath ) && File . Exists ( customPath ) )
124117 {
125- Founded . Add ( new ExternalTool ( name , icon , customPath , args , argsTransform ) ) ;
118+ Founded . Add ( new ExternalTool ( name , icon , customPath , execArgsGenerator ) ) ;
126119 }
127120 else
128121 {
129122 var path = finder ( ) ;
130123 if ( ! string . IsNullOrEmpty ( path ) && File . Exists ( path ) )
131- Founded . Add ( new ExternalTool ( name , icon , path , args , argsTransform ) ) ;
124+ Founded . Add ( new ExternalTool ( name , icon , path , execArgsGenerator ) ) ;
132125 }
133126 }
134127
135128 public void VSCode ( Func < string > platformFinder )
136129 {
137- TryAdd ( "Visual Studio Code" , "vscode" , " \" {0} \" " , "VSCODE" , platformFinder ) ;
130+ TryAdd ( "Visual Studio Code" , "vscode" , platformFinder ) ;
138131 }
139132
140133 public void VSCodeInsiders ( Func < string > platformFinder )
141134 {
142- TryAdd ( "Visual Studio Code - Insiders" , "vscode_insiders" , " \" {0} \" " , "VSCODE_INSIDERS" , platformFinder ) ;
135+ TryAdd ( "Visual Studio Code - Insiders" , "vscode_insiders" , platformFinder ) ;
143136 }
144137
145138 public void VSCodium ( Func < string > platformFinder )
146139 {
147- TryAdd ( "VSCodium" , "codium" , " \" {0} \" " , "VSCODIUM" , platformFinder ) ;
140+ TryAdd ( "VSCodium" , "codium" , platformFinder ) ;
148141 }
149142
150143 public void Fleet ( Func < string > platformFinder )
151144 {
152- TryAdd ( "Fleet" , "fleet" , " \" {0} \" " , "FLEET" , platformFinder ) ;
145+ TryAdd ( "Fleet" , "fleet" , platformFinder ) ;
153146 }
154147
155148 public void SublimeText ( Func < string > platformFinder )
156149 {
157- TryAdd ( "Sublime Text" , "sublime_text" , " \" {0} \" " , "SUBLIME_TEXT" , platformFinder ) ;
150+ TryAdd ( "Sublime Text" , "sublime_text" , platformFinder ) ;
158151 }
159152
160153 public void Zed ( Func < string > platformFinder )
161154 {
162- TryAdd ( "Zed" , "zed" , "\" {0}\" " , "ZED" , platformFinder ) ;
163- }
164-
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 ;
155+ TryAdd ( "Zed" , "zed" , platformFinder ) ;
182156 }
183157
184158 public void FindJetBrainsFromToolbox ( Func < string > platformFinder )
@@ -197,9 +171,7 @@ public void FindJetBrainsFromToolbox(Func<string> platformFinder)
197171 Founded . Add ( new ExternalTool (
198172 $ "{ tool . DisplayName } { tool . DisplayVersion } ",
199173 supported_icons . Contains ( tool . ProductCode ) ? $ "JetBrains/{ tool . ProductCode } " : "JetBrains/JB" ,
200- Path . Combine ( tool . InstallLocation , tool . LaunchCommand ) ,
201- "\" {0}\" " ,
202- null ) ) ;
174+ Path . Combine ( tool . InstallLocation , tool . LaunchCommand ) ) ) ;
203175 }
204176 }
205177 }
0 commit comments