@@ -13,6 +13,29 @@ namespace SourceGit.Native
1313 [ SupportedOSPlatform ( "linux" ) ]
1414 internal class Linux : OS . IBackend
1515 {
16+ class Terminal
17+ {
18+ public string FilePath { get ; set ; } = string . Empty ;
19+ public string OpenArgFormat { get ; set ; } = string . Empty ;
20+
21+ public Terminal ( string exec , string fmt )
22+ {
23+ FilePath = exec ;
24+ OpenArgFormat = fmt ;
25+ }
26+
27+ public void Open ( string dir )
28+ {
29+ Process . Start ( FilePath , string . Format ( OpenArgFormat , dir ) ) ;
30+ }
31+ }
32+
33+ public Linux ( )
34+ {
35+ _xdgOpenPath = FindExecutable ( "xdg-open" ) ;
36+ _terminal = FindTerminal ( ) ;
37+ }
38+
1639 public void SetupApp ( AppBuilder builder )
1740 {
1841 builder . With ( new FontManagerOptions ( )
@@ -26,97 +49,132 @@ public void SetupApp(AppBuilder builder)
2649
2750 public string FindGitExecutable ( )
2851 {
29- if ( File . Exists ( "/usr/bin/git" ) )
30- return "/usr/bin/git" ;
31- return string . Empty ;
52+ return FindExecutable ( "git" ) ;
3253 }
3354
3455 public List < Models . ExternalTool > FindExternalTools ( )
3556 {
3657 var finder = new Models . ExternalToolsFinder ( ) ;
37- finder . VSCode ( ( ) => "/usr/share/ code/code" ) ;
38- finder . VSCodeInsiders ( ( ) => "/usr/share/ code-insiders/code-insiders" ) ;
39- finder . Fleet ( ( ) => $ " { Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) } /JetBrains/Toolbox/apps/fleet/bin/Fleet" ) ;
40- finder . SublimeText ( ( ) => File . Exists ( "/usr/bin/ subl") ? "/usr/bin/subl" : "/usr/local/bin/subl" ) ;
58+ finder . VSCode ( ( ) => FindExecutable ( " code" ) ) ;
59+ finder . VSCodeInsiders ( ( ) => FindExecutable ( " code-insiders" ) ) ;
60+ finder . Fleet ( FindJetBrainFleet ) ;
61+ finder . SublimeText ( ( ) => FindExecutable ( " subl") ) ;
4162 return finder . Founded ;
4263 }
4364
4465 public void OpenBrowser ( string url )
4566 {
46- if ( ! File . Exists ( "/usr/bin/xdg-open" ) )
47- {
48- App . RaiseException ( "" , $ "You should install xdg-open first!") ;
49- return ;
50- }
51-
52- Process . Start ( "xdg-open" , $ "\" { url } \" ") ;
67+ if ( string . IsNullOrEmpty ( _xdgOpenPath ) )
68+ App . RaiseException ( "" , $ "Can NOT find `xdg-open` command!!!") ;
69+ else
70+ Process . Start ( _xdgOpenPath , $ "\" { url } \" ") ;
5371 }
5472
5573 public void OpenInFileManager ( string path , bool select )
5674 {
57- if ( ! File . Exists ( "/usr/bin/xdg-open" ) )
75+ if ( string . IsNullOrEmpty ( _xdgOpenPath ) )
5876 {
59- App . RaiseException ( "" , $ "You should install xdg-open first !") ;
77+ App . RaiseException ( "" , $ "Can NOT find ` xdg-open` command!! !") ;
6078 return ;
6179 }
6280
6381 if ( Directory . Exists ( path ) )
6482 {
65- Process . Start ( "xdg-open" , $ "\" { path } \" ") ;
83+ Process . Start ( _xdgOpenPath , $ "\" { path } \" ") ;
6684 }
6785 else
6886 {
6987 var dir = Path . GetDirectoryName ( path ) ;
7088 if ( Directory . Exists ( dir ) )
7189 {
72- Process . Start ( "xdg-open" , $ "\" { dir } \" ") ;
90+ Process . Start ( _xdgOpenPath , $ "\" { dir } \" ") ;
7391 }
7492 }
7593 }
7694
7795 public void OpenTerminal ( string workdir )
7896 {
7997 var dir = string . IsNullOrEmpty ( workdir ) ? "~" : workdir ;
80- if ( File . Exists ( "/usr/bin/gnome-terminal" ) )
81- {
82- Process . Start ( "/usr/bin/gnome-terminal" , $ "--working-directory=\" { dir } \" ") ;
83- }
84- else if ( File . Exists ( "/usr/bin/konsole" ) )
85- {
86- Process . Start ( "/usr/bin/konsole" , $ "--workdir \" { dir } \" ") ;
87- }
88- else if ( File . Exists ( "/usr/bin/xfce4-terminal" ) )
89- {
90- Process . Start ( "/usr/bin/xfce4-terminal" , $ "--working-directory=\" { dir } \" ") ;
91- }
92- else if ( File . Exists ( "/usr/bin/deepin-terminal" ) )
93- {
94- Process . Start ( "/usr/bin/deepin-terminal" , $ "--work-directory \" { dir } \" ") ;
95- }
96- else
97- {
98+ if ( _terminal == null )
9899 App . RaiseException ( dir , $ "Only supports gnome-terminal/konsole/xfce4-terminal/deepin-terminal!") ;
99- return ;
100- }
100+ else
101+ _terminal . Open ( dir ) ;
101102 }
102103
103104 public void OpenWithDefaultEditor ( string file )
104105 {
105- if ( ! File . Exists ( "/usr/bin/xdg-open" ) )
106+ if ( string . IsNullOrEmpty ( _xdgOpenPath ) )
106107 {
107- App . RaiseException ( "" , $ "You should install xdg-open first !") ;
108+ App . RaiseException ( "" , $ "Can NOT find ` xdg-open` command!! !") ;
108109 return ;
109110 }
110111
111- var proc = Process . Start ( "xdg-open" , $ "\" { file } \" ") ;
112+ var proc = Process . Start ( _xdgOpenPath , $ "\" { file } \" ") ;
112113 proc . WaitForExit ( ) ;
113114
114115 if ( proc . ExitCode != 0 )
115- {
116116 App . RaiseException ( "" , $ "Failed to open \" { file } \" ") ;
117- }
118117
119118 proc . Close ( ) ;
120119 }
120+
121+ private string FindExecutable ( string filename )
122+ {
123+ var pathVariable = Environment . GetEnvironmentVariable ( "PATH" ) ?? string . Empty ;
124+ var pathes = pathVariable . Split ( Path . PathSeparator , StringSplitOptions . RemoveEmptyEntries ) ;
125+ foreach ( var path in pathes )
126+ {
127+ var test = Path . Combine ( path , filename ) ;
128+ if ( File . Exists ( test ) )
129+ {
130+ return test ;
131+ }
132+ }
133+
134+ return string . Empty ;
135+ }
136+
137+ private Terminal FindTerminal ( )
138+ {
139+ var pathVariable = Environment . GetEnvironmentVariable ( "PATH" ) ?? string . Empty ;
140+ var pathes = pathVariable . Split ( Path . PathSeparator , StringSplitOptions . RemoveEmptyEntries ) ;
141+ foreach ( var path in pathes )
142+ {
143+ var test = Path . Combine ( path , "gnome-terminal" ) ;
144+ if ( File . Exists ( test ) )
145+ {
146+ return new Terminal ( test , "--working-directory=\" {0}\" " ) ;
147+ }
148+
149+ test = Path . Combine ( path , "konsole" ) ;
150+ if ( File . Exists ( test ) )
151+ {
152+ return new Terminal ( test , "--workdir \" {0}\" " ) ;
153+ }
154+
155+ test = Path . Combine ( path , "xfce4-terminal" ) ;
156+ if ( File . Exists ( test ) )
157+ {
158+ return new Terminal ( test , "--working-directory=\" {0}\" " ) ;
159+ }
160+
161+ test = Path . Combine ( path , "deepin-terminal" ) ;
162+ if ( File . Exists ( test ) )
163+ {
164+ return new Terminal ( test , "--work-directory \" {0}\" " ) ;
165+ }
166+ }
167+
168+ return null ;
169+ }
170+
171+ private string FindJetBrainFleet ( )
172+ {
173+ var path = $ "{ Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) } /JetBrains/Toolbox/apps/fleet/bin/Fleet";
174+ return File . Exists ( path ) ? path : FindExecutable ( "fleet" ) ;
175+ }
176+
177+ private string _xdgOpenPath = string . Empty ;
178+ private Terminal _terminal = null ;
121179 }
122180}
0 commit comments