|
14 | 14 | using System.Threading.Tasks; |
15 | 15 | using System.Windows.Forms; |
16 | 16 | using CSScriptIntellisense; |
| 17 | +using UltraSharp.Cecil; |
17 | 18 |
|
18 | 19 | namespace CSScriptNpp |
19 | 20 | { |
@@ -142,13 +143,127 @@ public static string NppScripts_ScriptsDir //NppScripts is another CS-Script re |
142 | 143 | } |
143 | 144 | } |
144 | 145 |
|
145 | | - public static string SystemCSScriptDir => Environment.GetEnvironmentVariable("CSSCRIPT_ROOT"); |
146 | | - public static string SystemCSSyntaxerDir => Environment.GetEnvironmentVariable("CSSYNTAXER_ROOT"); |
147 | | - public static bool IsCSSyntaxerInstalled => !SystemCSSyntaxerDir.IsEmpty(); |
148 | | - public static bool IsCSScriptInstalled => !SystemCSScriptDir.IsEmpty(); |
| 146 | + internal static string LocateChocolateySyntaxerAppDir() |
| 147 | + { |
| 148 | + // C:\ProgramData\chocolatey\lib\cs-syntaxer\tools\syntaxer.dll |
| 149 | + return LocateChocolateyAppDir("cs-syntaxer", "syntaxer.dll"); |
| 150 | + } |
| 151 | + internal static string LocateChocolateyCSScriptsAppDir() |
| 152 | + { |
| 153 | + // C:\ProgramData\chocolatey\lib\cs-script\tools\cscs.dll |
| 154 | + return LocateChocolateyAppDir("cs-script", "cscs.dll"); |
| 155 | + } |
| 156 | + |
| 157 | + internal static string LocateChocolateyAppDir(string app, string fileName) |
| 158 | + { |
| 159 | + // C:\ProgramData\chocolatey\lib\<app>\tools\<file> |
| 160 | + try |
| 161 | + { |
| 162 | + var packageDir = Environment.SpecialFolder.CommonApplicationData.GetPath() |
| 163 | + .PathJoin($@"chocolatey\lib\{app}\tools"); |
| 164 | + |
| 165 | + if (Directory.Exists(packageDir)) |
| 166 | + { |
| 167 | + if (File.Exists(packageDir.PathJoin(fileName))) |
| 168 | + return packageDir; |
| 169 | + } |
| 170 | + } |
| 171 | + catch { } |
| 172 | + return null; |
| 173 | + } |
| 174 | + |
| 175 | + internal static string LocateDotNetCSScriptsToolDir() |
| 176 | + { |
| 177 | + // C:\Users\user\.dotnet\tools |
| 178 | + // C:\Users\user\.dotnet\tools\.store\cs-script.cli\4.8.16\cs-script.cli\4.8.16\tools\net8.0\any\cscs.dll |
| 179 | + // dotnet tool install --global cs-script.cli |
| 180 | + try |
| 181 | + { |
| 182 | + var packageDir = Environment.SpecialFolder.UserProfile.GetPath() |
| 183 | + .PathJoin(@".dotnet\tools\.store\cs-script.cli"); |
| 184 | + |
| 185 | + if (Directory.Exists(packageDir)) |
| 186 | + { |
| 187 | + var installedVersionDir = Directory.GetDirectories(packageDir) |
| 188 | + .OrderByDescending(x => x) |
| 189 | + .FirstOrDefault(); |
| 190 | + |
| 191 | + if (installedVersionDir != null) |
| 192 | + { |
| 193 | + var appDir = Directory.GetDirectories( |
| 194 | + installedVersionDir.PathJoin("cs-script.cli", Path.GetFileName(installedVersionDir), "tools")) |
| 195 | + .OrderByDescending(x => x) |
| 196 | + .FirstOrDefault()? |
| 197 | + .PathJoin("any"); |
| 198 | + |
| 199 | + if (File.Exists(appDir.PathJoin("cscs.dll"))) |
| 200 | + return appDir; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + catch { } |
| 205 | + return null; |
| 206 | + } |
| 207 | + |
| 208 | + internal static string LocateWingetCSScriptAppDir() |
| 209 | + { |
| 210 | + // C:\Users\user\AppData\Local\Microsoft\WinGet\Packages |
| 211 | + // C:\Users\user\AppData\Local\Microsoft\WinGet\Packages\oleg-shilo.cs-script_Microsoft.Winget.Source_8wekyb3d8bbwe |
| 212 | + |
| 213 | + try |
| 214 | + { |
| 215 | + var uesrProfile = Environment.SpecialFolder.LocalApplicationData.GetPath(); |
| 216 | + |
| 217 | + var appDir = Directory.GetDirectories(Path.Combine(uesrProfile, @"Microsoft\WinGet\Packages"), "oleg-shilo.cs-script*") |
| 218 | + .FirstOrDefault(); |
| 219 | + |
| 220 | + if (File.Exists(appDir.PathJoin("cscs.dll"))) |
| 221 | + return appDir; |
| 222 | + } |
| 223 | + catch { } |
| 224 | + |
| 225 | + return null; |
| 226 | + } |
| 227 | + |
| 228 | + public static string SystemCSScriptDir => |
| 229 | + Environment.GetEnvironmentVariable("CSSCRIPT_ROOT") ?? |
| 230 | + LocateChocolateyCSScriptsAppDir() ?? |
| 231 | + LocateWingetCSScriptAppDir() ?? |
| 232 | + LocateDotNetCSScriptsToolDir(); |
| 233 | + public static string SystemCSSyntaxerDir => |
| 234 | + Environment.GetEnvironmentVariable("CSSYNTAXER_ROOT") ?? |
| 235 | + LocateChocolateySyntaxerAppDir(); |
| 236 | + public static bool IsCSSyntaxerInstalled => SystemCSSyntaxerDir.IsNotEmpty(); |
| 237 | + public static bool IsCSScriptInstalled => SystemCSScriptDir.IsNotEmpty(); |
| 238 | + public static bool IsCSScriptChocoInstalled => IsCSScriptInstalled && LocateChocolateyCSScriptsAppDir().IsNotEmpty(); |
| 239 | + public static bool IsCSScriptWingetInstalled => IsCSScriptInstalled && LocateWingetCSScriptAppDir().IsNotEmpty(); |
| 240 | + public static bool IsCSScriptDotnetInstalled => IsCSScriptInstalled && LocateDotNetCSScriptsToolDir().IsNotEmpty(); |
| 241 | + |
| 242 | + public static string InstallCssCmd |
| 243 | + { |
| 244 | + get |
| 245 | + { |
| 246 | + if (IsCSScriptInstalled) |
| 247 | + { |
| 248 | + if (IsCSScriptChocoInstalled) |
| 249 | + return InstallCssChocoCmd; |
| 250 | + else if (IsCSScriptWingetInstalled) |
| 251 | + return InstallCssWingetCmd; |
| 252 | + else if (IsCSScriptDotnetInstalled) |
| 253 | + return InstallCssDotnetCmd; |
| 254 | + return |
| 255 | + null; |
| 256 | + } |
| 257 | + else |
| 258 | + return InstallCssChocoCmd; |
| 259 | + } |
| 260 | + } |
| 261 | + public static string InstallCssChocoCmd => $"choco {(IsCSScriptInstalled ? "upgrade" : "install")} cs-script --y"; |
| 262 | + public static string InstallCssWingetCmd => $"winget {(IsCSScriptInstalled ? "upgrade" : "install")} cs-script"; |
| 263 | + public static string InstallCssDotnetCmd => $"dotnet tool {(IsCSScriptInstalled ? "update" : "install")} --global cs-script.cli"; |
| 264 | + |
149 | 265 |
|
150 | | - public static string InstallCssCmd = $"choco {(CSScriptHelper.IsCSScriptInstalled ? "upgrade" : "install")} cs-script --y"; |
151 | | - public static string InstallCsSyntaxerCmd = $"choco {(CSScriptHelper.IsCSSyntaxerInstalled ? "upgrade" : "install")} cs-syntaxer --y"; |
| 266 | + public static string InstallCsSyntaxerCmd = $"choco {(IsCSSyntaxerInstalled ? "upgrade" : "install")} cs-syntaxer --y"; |
152 | 267 |
|
153 | 268 | public static bool IsChocoInstalled |
154 | 269 | { |
|
0 commit comments