66 using System . Linq ;
77 using System . Reflection ;
88 using System . Text ;
9+ using System . Text . RegularExpressions ;
910 using Microsoft . Win32 ;
1011
1112 internal class Visualizer : IDisposable
@@ -15,76 +16,113 @@ internal class Visualizer : IDisposable
1516 private readonly Action < string > _logger ;
1617 private readonly Version _version ;
1718 private readonly string _vsixManifest ;
19+ private RegistryKey _registryKey ;
20+ private string _vsExePath ;
21+ private string _vsSetupArgument ;
22+ private string _installPath ;
23+ private string _vsixManifestPath ;
24+
25+ public Visualizer (
26+ Action < string > logger ,
27+ FileVersionInfo version ,
28+ string vsixManifest ,
29+ string resourceName )
30+ : this ( logger , new Version ( version . FileVersion ) , vsixManifest , resourceName , GetVsVersionNumber ( resourceName ) )
31+ {
32+ }
33+
34+ #region Setup
1835
19- public Visualizer ( Action < string > logger , FileVersionInfo version , string vsixManifest )
20- : this ( logger , new Version ( version . FileVersion ) , vsixManifest )
36+ private static readonly Regex _versionNumberMatcher =
37+ new Regex ( @"Vs(?<VersionNumber>[\d]+)\.dll$" , RegexOptions . IgnoreCase ) ;
38+
39+ private static int GetVsVersionNumber ( string resourceName )
2140 {
41+ var matchValue = _versionNumberMatcher
42+ . Match ( resourceName )
43+ . Groups [ "VersionNumber" ]
44+ . Value ;
45+
46+ return int . Parse ( matchValue ) ;
2247 }
2348
24- private Visualizer ( Action < string > logger , Version version , string vsixManifest )
49+ #endregion
50+
51+ private Visualizer (
52+ Action < string > logger ,
53+ Version version ,
54+ string vsixManifest ,
55+ string resourceName ,
56+ int vsVersionNumber )
2557 {
2658 _logger = logger ;
2759 _version = version ;
2860 _vsixManifest = vsixManifest ;
61+ ResourceName = resourceName ;
62+ VsVersionNumber = vsVersionNumber ;
2963 }
3064
31- public int VsVersionNumber { get ; set ; }
65+ public int VsVersionNumber { get ; }
3266
3367 public string VsFullVersionNumber => VsVersionNumber + ".0" ;
3468
35- public string ResourceName { get ; set ; }
36-
37- public RegistryKey RegistryKey { get ; set ; }
69+ public string ResourceName { get ; }
3870
39- public string VsInstallDirectory { get ; set ; }
71+ public string GetResourceFileName ( )
72+ {
73+ var resourceAssemblyNameLength = ( typeof ( Visualizer ) . Namespace ? . Length + 1 ) . GetValueOrDefault ( ) ;
74+ var resourceFileName = ResourceName . Substring ( resourceAssemblyNameLength ) ;
4075
41- public string InstallPath { get ; set ; }
76+ return resourceFileName ;
77+ }
4278
43- public string VsixManifestPath { get ; set ; }
79+ public string VsInstallDirectory { get ; private set ; }
4480
45- public string VsExePath { get ; set ; }
81+ public void SetInstallPath ( string pathToVisualizers )
82+ => _installPath = Path . Combine ( pathToVisualizers , GetResourceFileName ( ) ) ;
4683
47- public string VsSetupArgument { get ; set ; }
84+ public void SetVsixManifestPath ( string pathToExtensions )
85+ => _vsixManifestPath = Path . Combine ( pathToExtensions , "extension.vsixmanifest" ) ;
4886
4987 public void Install ( )
5088 {
5189 // ReSharper disable once AssignNullToNotNullAttribute
52- if ( ! Directory . Exists ( Path . GetDirectoryName ( InstallPath ) ) )
90+ if ( ! Directory . Exists ( Path . GetDirectoryName ( _installPath ) ) )
5391 {
54- Log ( "Skipping as directory does not exist: " + InstallPath ) ;
92+ Log ( "Skipping as directory does not exist: " + _installPath ) ;
5593 return ;
5694 }
5795
5896 using ( var resourceStream = _thisAssembly . GetManifestResourceStream ( ResourceName ) )
59- using ( var visualizerFileStream = File . OpenWrite ( InstallPath ) )
97+ using ( var visualizerFileStream = File . OpenWrite ( _installPath ) )
6098 {
61- Log ( "Writing visualizer to " + InstallPath ) ;
99+ Log ( "Writing visualizer to " + _installPath ) ;
62100 // ReSharper disable once PossibleNullReferenceException
63101 resourceStream . CopyTo ( visualizerFileStream ) ;
64102 }
65103
66- var manifestDirectory = Path . GetDirectoryName ( VsixManifestPath ) ;
104+ var manifestDirectory = Path . GetDirectoryName ( _vsixManifestPath ) ;
67105
68- Log ( "Writing manifest to " + VsixManifestPath ) ;
106+ Log ( "Writing manifest to " + _vsixManifestPath ) ;
69107 // ReSharper disable once AssignNullToNotNullAttribute
70108 Directory . CreateDirectory ( manifestDirectory ) ;
71- File . WriteAllText ( VsixManifestPath , _vsixManifest , Encoding . ASCII ) ;
109+ File . WriteAllText ( _vsixManifestPath , _vsixManifest , Encoding . ASCII ) ;
72110
73111 ResetVsExtensions ( ) ;
74112 }
75113
76114 public DirectoryInfo GetVsixManifestDirectory ( )
77115 {
78116 // ReSharper disable once AssignNullToNotNullAttribute
79- return new DirectoryInfo ( Path . GetDirectoryName ( VsixManifestPath ) ) ;
117+ return new DirectoryInfo ( Path . GetDirectoryName ( _vsixManifestPath ) ) ;
80118 }
81119
82120 public void Uninstall ( )
83121 {
84122 DeletePreviousManifests ( ) ;
85123
86124 // ReSharper disable PossibleNullReferenceException
87- if ( File . Exists ( VsixManifestPath ) )
125+ if ( File . Exists ( _vsixManifestPath ) )
88126 {
89127 var vsixManifestDirectory = GetVsixManifestDirectory ( ) ;
90128 var productDirectory = vsixManifestDirectory . Parent ;
@@ -109,9 +147,9 @@ public void Uninstall()
109147 }
110148 // ReSharper restore PossibleNullReferenceException
111149
112- if ( File . Exists ( InstallPath ) )
150+ if ( File . Exists ( _installPath ) )
113151 {
114- File . Delete ( InstallPath ) ;
152+ File . Delete ( _installPath ) ;
115153 }
116154 }
117155
@@ -140,13 +178,13 @@ private void DeletePreviousManifests()
140178
141179 private void ResetVsExtensions ( )
142180 {
143- if ( VsExePath == null )
181+ if ( _vsExePath == null )
144182 {
145183 return ;
146184 }
147185
148- Log ( "Updating VS extension records using " + VsExePath ) ;
149- using ( Process . Start ( VsExePath , VsSetupArgument ) ) { }
186+ Log ( "Updating VS extension records using " + _vsExePath ) ;
187+ using ( Process . Start ( _vsExePath , _vsSetupArgument ) ) { }
150188 }
151189
152190 public void PopulateVsSetupData ( )
@@ -158,23 +196,21 @@ public void PopulateVsSetupData()
158196 return ;
159197 }
160198
161- VsExePath = pathToDevEnv ;
162- VsSetupArgument = RegistryKey ? . GetValue ( "SetupCommandLine" ) as string ?? "/setup" ;
199+ _vsExePath = pathToDevEnv ;
200+ _vsSetupArgument = _registryKey ? . GetValue ( "SetupCommandLine" ) as string ?? "/setup" ;
163201 }
164202
165203 public Visualizer With ( RegistryKey registryKey , string vsInstallPath )
166204 {
167- return new Visualizer ( _logger , _version , _vsixManifest )
205+ return new Visualizer ( _logger , _version , _vsixManifest , ResourceName , VsVersionNumber )
168206 {
169- VsVersionNumber = VsVersionNumber ,
170- ResourceName = ResourceName ,
171- RegistryKey = registryKey ,
207+ _registryKey = registryKey ,
172208 VsInstallDirectory = vsInstallPath
173209 } ;
174210 }
175211
176212 private void Log ( string message ) => _logger . Invoke ( message ) ;
177213
178- public void Dispose ( ) => RegistryKey ? . Dispose ( ) ;
214+ public void Dispose ( ) => _registryKey ? . Dispose ( ) ;
179215 }
180216}
0 commit comments