oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.56k stars 234 forks source link

//css_ref vs -r:: #373

Open d3nd3 opened 1 month ago

d3nd3 commented 1 month ago

I expect that -r:C:\somedll.dll would do the same thing as `//css_ref C:\somedll.dll

I am using this in sublime text editor. Only //css_ref works, but I dont' want to put that at the top of every file. I have tried:

dotnet cscs.dll -config:set:SearchDirs=add:

dotnet cscs.dll -config:set:DefaultArguments=add:-dir:

dotnet cscs.dll -config:set:DefaultArguments=add:-r:

Only //css_ref is making the Intellisense for that reference work! What am I doing wrong? And yes I confirmed those things with the CS-Script config output in sublime.

You can avoid referencing the most common assemblies in every script by setting them as default referenced assemblies in the CS-Script config file (ContextMenu > CS-Script > Settings > CS-Script Config). Basically I can't do this. It doesn't work for me.

OS: Windows 11 dotnet --version 8.0.205


"C:\Program Files (x86)\dotnet\sdk\7.0.409\Roslyn\bincore\csc.dll" exists after installing .NET 7 SDK , yet

AppData\Roaming\Sublime Text\Packages\User\cs-script\cs-script_v1.3.3>cscs.exe
C# Script execution engine (.NET Core). Version 4.6.2.0.
Copyright (C) 2004-2020 Oleg Shilo. www.csscript.net (github.com/oleg-shilo/cs-script)

   CLR:             7.0.19
   System:          Microsoft Windows NT 10.0.22631.0
   Architecture:    x64
   Install dir:     <not integrated>
   Script engine:   C:\Users\dendi\AppData\Roaming\Sublime Text\Packages\User\cs-script\cs-script_v1.3.3\cscs.dll
   Config file:     C:\Users\dendi\AppData\Roaming\Sublime Text\Packages\User\cs-script\cs-script_v1.3.3\css_config.xml
   Compiler engine: dotnet (C:\Program Files\dotnet\dotnet.exe)
                    WARNING: .NET 7 SDK cannot be found. It's required for `csc` and `dotnet` compiler engines.
   NuGet manager:   dotnet
   NuGet cache:     <not found>
   Custom commands: C:\ProgramData\cs-script\commands
   Global includes: C:\ProgramData\cs-script\inc

Could this warning be related? It can't find the csc.dll , yet its there, hm.

Nevermind this is because cscs.exe is x64 and cscs.dll is x86. Yet my default dotnet.exe is x64. "C:\Program Files (x86)\dotnet\dotnet.exe" cscs.dll

   AppData\Roaming\Sublime Text\Packages\User\cs-script\cs-script_v1.3.3>"C:\Program Files (x86)\dotnet\dotnet.exe" cscs.dll
C# Script execution engine (.NET Core). Version 4.6.2.0.
Copyright (C) 2004-2020 Oleg Shilo. www.csscript.net (github.com/oleg-shilo/cs-script)

   CLR:             7.0.19
   System:          Microsoft Windows NT 10.0.22631.0
   Architecture:    x86
   Install dir:     <not integrated>
   Script engine:   C:\Users\dendi\AppData\Roaming\Sublime Text\Packages\User\cs-script\cs-script_v1.3.3\cscs.dll
   Config file:     C:\Users\dendi\AppData\Roaming\Sublime Text\Packages\User\cs-script\cs-script_v1.3.3\css_config.xml
   Compiler engine: dotnet (C:\Program Files (x86)\dotnet\dotnet.exe)
   NuGet manager:   dotnet
   NuGet cache:     <not found>
   Custom commands: C:\ProgramData\cs-script\commands
   Global includes: C:\ProgramData\cs-script\inc

I replaced references of dotnet with the full path to the x86 dotnet within cs-script.py for the sublime package. I still can't get the behaviour I want.

My goal is specifically for the UnityEngine.dll. From reading the docs I understand that on windows using UnityEngine with correct include dir should be enough.

oleg-shilo commented 1 month ago

Yes indeed -r:<asm_file> has the same effect as //css_ref <asm_file>. But this effect is located within the boundaries of the script engine. And script engine is only responsible for the script execution but not for intellisense for the C# (script) in the editor.

Intellisense is implemented by a completely different product cs-syntaxer. Every CS-script plugin for and editor/ide (Notepad++, Sublime Text, VSCode) is a package containing a script engine, syntaxer and a little code to glue it all together and integrate with the host (editor).

Syntxer has no idea about what you are going to pass as CLI params when you execute the script thus it can only rely on C# code supplied to it. That's why it works with //css_ref *. The same applies to default referenced assemblies in the CS-Script config file. These are the features of script execution but not IDE integration.

Saying that, it's fair to ask if this extra info (CLI args, and config) can be supplied to the syntaxer by the "glue code". While it's theoretically possible, it is hard to implement reliably.

Only imagine this scenario. In the script file user specifies a custom config file //css_args -config:c:\my_custom_config.xml. And this config has a default ref with your assembly.

You can perfectly execute this script as the script engine knows how to deal with all this convoluted logic. But The plugin code does not.

That's why in scenarios like yours I use this approach

Script file script.cs

//css_inc unity_development 

<your business logic code>

Script with no business logic but dependencies only: unity_development.cs

//css_dir <some relevant dirs>
//css_nuget <unity package X>
//css_ref UnityEngine.dll
/css_inc my_favorite_unity_extensions.cs

Now every script for unity development will have only one extra line //css_inc unity_development that defines ALL dependencies that you need.

You can even put unity_development.cs in the special global dir for that: image

However, tbh I do not remember if syntaxer pics it or not. you will need to check it.

I you think about this, having all dependencies defined in the script makes more sense. In fact, the vast majority of the scripting systems do it this way (e.g. python imports).


Could this warning be related?

No, this warning is saying that on your system you cannot configure cs-script to use csc.exe compiler engine as the right version of SDK (v7) is not present. So you can only use slower dotnet.exe. BTW, you can update your cs-script to the latest (dotnet tool, choco, winget) and then the warning will be gone as the latest release integrates with .NET 8.

d3nd3 commented 1 month ago
Howto get CS-Script
Note, every [release](https://github.com/oleg-shilo/cs-script/releases) comes with a detailed description of how to deploy/integrate CS-Script on/with your environment or application. This covers various package distribution solutions

.NET Tools
Chocolatey
WinGet
Debian package
NuGet package

On the main entry README on github it says that, but I can't see how its true? I can't find such resource anywhere.

I am trying to update the sublime cs-script version using some command as you mention, but not sure the specifics.

Edit: Ah I see it, Under deployment drop down in the releases! Ye, I'm a bit slow. Sorry.

oleg-shilo commented 1 month ago

image

And of course, you can search cs-script in all these package/app managers.

And this is the summary:

choco install cs-script
winget install cs-script
dotnet tool install --global cs-script.cli

I recommend choco. But any will do.