Skip to content

Commit e00716f

Browse files
committed
- Added integration with Winget and Dotnet.Tools
1 parent 582dcad commit e00716f

File tree

3 files changed

+124
-6
lines changed

3 files changed

+124
-6
lines changed

src/CSScriptNpp/CSScriptNpp/CSScriptHelper.cs

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Threading.Tasks;
1515
using System.Windows.Forms;
1616
using CSScriptIntellisense;
17+
using UltraSharp.Cecil;
1718

1819
namespace CSScriptNpp
1920
{
@@ -142,13 +143,127 @@ public static string NppScripts_ScriptsDir //NppScripts is another CS-Script re
142143
}
143144
}
144145

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+
149265

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";
152267

153268
public static bool IsChocoInstalled
154269
{

src/CSScriptNpp/Dialogs/ConfigForm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public ConfigForm(Config data)
5353
customSyntaxerExe.Text = CSScriptHelper.SystemCSSyntaxerDir.PathJoin("syntaxer.dll");
5454

5555
cssInstallCmd.Text = CSScriptHelper.InstallCssCmd;
56+
deployCSScript.Text = CSScriptHelper.IsCSScriptInstalled ? "Update" : "Install";
5657
cssyntaxerInstallCmd.Text = CSScriptHelper.InstallCsSyntaxerCmd;
58+
deploySyntaxer.Text = CSScriptHelper.IsCSSyntaxerInstalled ? "Update" : "Install";
5759

5860
customLocationBtn_CheckedChanged(null, null);
5961
}

src/CSScriptNpp/Interop/UnmanagedExports.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void InitPlugin()
4949
try
5050
{
5151
// Debug.Assert(false);
52+
5253
Intellisense.EnsureIntellisenseIntegration();
5354

5455
CSScriptNpp.Plugin.CommandMenuInit(); //this will also call CSScriptIntellisense.Plugin.CommandMenuInit

0 commit comments

Comments
 (0)