Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Simple C# Programs #296

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

WeihanLi
Copy link
Contributor

@WeihanLi WeihanLi commented May 18, 2023


## Simple C# is just C# code

Simple C# Programs are not a dialect of the C# language, like the interactive dialect, CSX. Any code in a Simple C# Program can be built and run in a project-based program.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you say that Simple C# programs will not a dialect of the C# language, but then you propose that the #r "nuget:PackageId" syntax be copied from .NET Interactive.

Therefore, are you proposing that directories with a csproj allow individual .cs files to also use #r to reference packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, wondering if it's possible to treat the #r syntax as a comment when building with a project file?
We should be able to tell if the build is built with a project/solution file, right?
If yes, think then we could make it different when in different modes

@Zeioth
Copy link

Zeioth commented Aug 20, 2023

I'm interested. Please ping me in the future if you need testers or any kind of help.

@AraHaan
Copy link
Member

AraHaan commented Oct 12, 2023

These are my 2 cents on this:

  • Support this only if all of the files are *.csx.
  • All *.csx files in the same directory as well as any nested directories are assumed to be in the same compilation at runtime in dotnet run.
  • dotnet run will fallback to this only if:
    • directory does not have any csproj files.
    • no project.json files.
    • no bin or obj folders and no binary files that can be ran in the folder or any recursive directories under it (no .NET *.exe's, *.dll's).
      • if one is found, only the one that has the +x bit set is ran, on Windows it looks for the apphost (*.exe) and use's it's file name to obtain the name for the *.dll that the *.exe would normally run anyways.
  • #r reference not needed to reference other *.csx files, dotnet run would need a special way to support this one though.
  • #r "nuget:PackageId" is only required in a single *.csx file, after that it is usable in all of them.
  • The user can also add an GlobalUsings.csx file for any and all global usings they want to use for the entire thing, however it must be all done by hand and not by any special tooling.
  • the primary csx file that defines the start of the program must be named main.csx. This is to only support top level applications and top level code must be in that file. Also this file must be in the EXACT folder for which dotnet run is invoked from.
  • the application will only run with the absolute newest version of the runtime installed on the user's system. This includes the latest patch of the newest major version they have installed to avoid security issues. Also implies the newest C# language version as well that the .NET SDK supports.
  • For the frameworks required a new file is required as well for this layout: frameworks.json which lists the frameworks required at runtime:
    • Can also work with the frameworks from workloads such as maui for example as well.
    • Example:
{
    "frameworks": [
        "Microsoft.WindowsDesktop.App",
        "Microsoft.NETCore.App",
    ]
}

Notes:

  • After dotnet run builds the code it then stores the built code in a *.dll (file name is the name of the folder from which dotnet run is invoked in) to cache for future runs unless --rebuild is specified.
  • Built code is built with Ready to Run and crossgen2'd for max optimizations.

With all of this I think it would be a good compromise for easier ways of running .NET Program code in a way that is similar in nature to python, JavaScript, etc.

Overall I think the document can be summarized down to basically what I wrote above which I feel is much better and also easier to understand and break into parts for people to handle.

@AraHaan
Copy link
Member

AraHaan commented Oct 13, 2023

I just thought of this as well but an alternative to using the #r "nuget:PackageId" preprocessor on csx files:

  • a packages.json that defines the nuget packages required to build and run the scripts, along with the versions (dotnet run would convert this to the equivalent #r preprocessor for it as well expanded with the version of it too).
    • this has a benefit as well of being able to explicitly specify a specific version as well which for some might be a good idea as well.

@AraHaan
Copy link
Member

AraHaan commented Oct 13, 2023

After thinking about this more, the processing of this for dotnet run could be made on the MSBuild side in the Build target for when they use only csx + frameworks.json + packages.json files (no cs, csproj, sln, and no project.json).

And due to dotnet run invoking an implicit build by default, that means a binary would be cached that it can then run.

@WeihanLi WeihanLi changed the title Add Simple C# Programs Add Simple C# Programs[draft] Oct 30, 2023
@WeihanLi
Copy link
Contributor Author

WeihanLi commented Oct 30, 2023

Support this only if all of the files are *.csx.

Hoping for *.cs file also

All *.csx files in the same directory as well as any nested directories are assumed to be in the same compilation at runtime in dotnet run

I think this could be considered as an option, like enable default items or not in the MS Build

And I'm trying on the dotnet-exec project, trying to make it possible to simpler C# programs, while maybe not in a correct way.

Currently, I'm trying to compile the code with Roslyn in runtime and execute the compiled dll with reflection and assembly loader

@WeihanLi WeihanLi changed the title Add Simple C# Programs[draft] Add Simple C# Programs Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants