1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . ComponentModel . Composition . Hosting ;
4
2
using System . IO ;
5
3
using System . Linq ;
6
- using System . Text ;
7
- using System . Threading . Tasks ;
8
4
using ServerRegistrationManager . Actions ;
9
5
using ServerRegistrationManager . OutputService ;
10
- using SharpShell ;
11
6
using SharpShell . Diagnostics ;
7
+ using SharpShell . Helpers ;
12
8
using SharpShell . ServerRegistration ;
13
9
14
10
namespace ServerRegistrationManager
@@ -55,8 +51,8 @@ public void Run(string[] args)
55
51
56
52
// Get the verb, target and parameters.
57
53
var verb = args [ 0 ] ;
58
- var target = args . Length > 1 ? args [ 1 ] : ( string ) null ; // TODO tidy this up.
59
- var parameters = args . Skip ( 1 ) ;
54
+ var target = args . Length > 1 ? args [ 1 ] : null ; // TODO tidy this up.
55
+ var parameters = args . Skip ( 1 ) . ToArray ( ) ;
60
56
61
57
//Allow user to override registrationType with -os32 or -os64
62
58
if ( parameters . Any ( p => p . Equals ( ParameterOS32 , StringComparison . InvariantCultureIgnoreCase ) ) )
@@ -68,7 +64,6 @@ public void Run(string[] args)
68
64
registrationType = RegistrationType . OS64Bit ;
69
65
}
70
66
71
-
72
67
// Based on the verb, perform the action.
73
68
if ( verb == VerbInstall )
74
69
InstallServer ( target , registrationType , parameters . Any ( p => p == ParameterCodebase ) ) ;
@@ -97,41 +92,18 @@ private void InstallServer(string path, RegistrationType registrationType, bool
97
92
return ;
98
93
}
99
94
100
- // Try and load the server types.
101
- IEnumerable < ISharpShellServer > serverTypes = null ;
102
- try
103
- {
104
- serverTypes = LoadServerTypes ( path ) ;
105
- }
106
- catch ( Exception e )
107
- {
108
- outputService . WriteError ( "An unhandled exception occured when loading the SharpShell" ) ;
109
- outputService . WriteError ( "Server Types from the specified assembly. Is it a SharpShell" ) ;
110
- outputService . WriteError ( "Server Assembly?" ) ;
111
- System . Diagnostics . Trace . Write ( e ) ;
112
- Logging . Error ( "An unhandled exception occured when loading a SharpShell server." , e ) ;
113
- }
95
+ var regasm = new RegAsm ( ) ;
96
+ var success = registrationType == RegistrationType . OS32Bit ? regasm . Register32 ( path , codeBase ) : regasm . Register64 ( path , codeBase ) ;
114
97
115
- // Install each server type.
116
- foreach ( var serverType in serverTypes )
98
+ if ( success )
117
99
{
118
- // Inform the user we're going to install the server.
119
- outputService . WriteMessage ( "Preparing to install (" + registrationType + "): " + serverType . DisplayName , true ) ;
120
-
121
- // Install the server.
122
- try
123
- {
124
- SharpShell . ServerRegistration . ServerRegistrationManager . InstallServer ( serverType , registrationType , codeBase ) ;
125
- SharpShell . ServerRegistration . ServerRegistrationManager . RegisterServer ( serverType , registrationType ) ;
126
- }
127
- catch ( Exception e )
128
- {
129
- outputService . WriteError ( "Failed to install and register the server." ) ;
130
- Logging . Error ( "An unhandled exception occured installing and registering the server " + serverType . DisplayName , e ) ;
131
- continue ;
132
- }
133
-
134
- outputService . WriteSuccess ( " " + serverType . DisplayName + " installed and registered." , true ) ;
100
+ outputService . WriteSuccess ( $ " { path } installed and registered.", true ) ;
101
+ outputService . WriteMessage ( regasm . StandardOutput ) ;
102
+ }
103
+ else
104
+ {
105
+ outputService . WriteError ( $ " { path } failed to register.", true ) ;
106
+ outputService . WriteError ( regasm . StandardError ) ;
135
107
}
136
108
}
137
109
@@ -142,42 +114,18 @@ private void InstallServer(string path, RegistrationType registrationType, bool
142
114
/// <param name="registrationType">Type of the registration.</param>
143
115
private void UninstallServer ( string path , RegistrationType registrationType )
144
116
{
145
- // Try and load the server types.
146
- IEnumerable < ISharpShellServer > serverTypes = null ;
147
- try
148
- {
149
- serverTypes = LoadServerTypes ( path ) ;
150
- }
151
- catch ( Exception e )
152
- {
153
- outputService . WriteError ( "An unhandled exception occured when loading the SharpShell" ) ;
154
- outputService . WriteError ( "Server Types from the specified assembly. Is it a SharpShell" ) ;
155
- outputService . WriteError ( "Server Assembly?" ) ;
156
- System . Diagnostics . Trace . Write ( e ) ;
157
- Logging . Error ( "An unhandled exception occured when loading a SharpShell server." , e ) ;
158
- }
117
+ var regasm = new RegAsm ( ) ;
118
+ var success = registrationType == RegistrationType . OS32Bit ? regasm . Unregister32 ( path ) : regasm . Unregister64 ( path ) ;
159
119
160
- // Install each server type.
161
- foreach ( var serverType in serverTypes )
120
+ if ( success )
121
+ {
122
+ outputService . WriteSuccess ( $ " { path } uninstalled.", true ) ;
123
+ outputService . WriteMessage ( regasm . StandardOutput ) ;
124
+ }
125
+ else
162
126
{
163
- // Inform the user we're going to install the server.
164
- Console . WriteLine ( "Preparing to uninstall (" + registrationType + "): " + serverType . DisplayName , true ) ;
165
-
166
- // Install the server.
167
- try
168
- {
169
-
170
- SharpShell . ServerRegistration . ServerRegistrationManager . UnregisterServer ( serverType , registrationType ) ;
171
- SharpShell . ServerRegistration . ServerRegistrationManager . UninstallServer ( serverType , registrationType ) ;
172
- }
173
- catch ( Exception e )
174
- {
175
- outputService . WriteError ( "Failed to unregister and uninstall the server." ) ;
176
- Logging . Error ( "An unhandled exception occured un registering and uninstalling the server " + serverType . DisplayName , e ) ;
177
- continue ;
178
- }
179
-
180
- outputService . WriteSuccess ( " " + serverType . DisplayName + " unregistered and uninstalled." , true ) ;
127
+ outputService . WriteError ( $ " { path } failed to uninstall.", true ) ;
128
+ outputService . WriteError ( regasm . StandardError ) ;
181
129
}
182
130
}
183
131
@@ -192,22 +140,7 @@ private void ShowWelcome()
192
140
outputService . WriteMessage ( "========================================" ) ;
193
141
outputService . WriteMessage ( "" ) ;
194
142
}
195
-
196
- /// <summary>
197
- /// Loads the server types from an assembly.
198
- /// </summary>
199
- /// <param name="assemblyPath">The assembly path.</param>
200
- /// <returns>The SharpShell Server types from the assembly.</returns>
201
- private static IEnumerable < ISharpShellServer > LoadServerTypes ( string assemblyPath )
202
- {
203
- // Create an assembly catalog for the assembly and a container from it.
204
- var catalog = new AssemblyCatalog ( Path . GetFullPath ( assemblyPath ) ) ;
205
- var container = new CompositionContainer ( catalog ) ;
206
-
207
- // Get all exports of type ISharpShellServer.
208
- return container . GetExports < ISharpShellServer > ( ) . Select ( st => st . Value ) ;
209
- }
210
-
143
+
211
144
private const string VerbHelp = @"help" ;
212
145
private const string VerbInstall = @"install" ;
213
146
private const string VerbUninstall = @"uninstall" ;
@@ -216,7 +149,7 @@ private static IEnumerable<ISharpShellServer> LoadServerTypes(string assemblyPat
216
149
217
150
private const string ParameterCodebase = @"-codebase" ;
218
151
219
- private const String ParameterOS32 = @"-os32" ;
220
- private const String ParameterOS64 = @"-os64" ;
152
+ private const string ParameterOS32 = @"-os32" ;
153
+ private const string ParameterOS64 = @"-os64" ;
221
154
}
222
155
}
0 commit comments