Git Product home page Git Product logo

dotnet-exec's Introduction

dotnet-exec

Package Latest Latest Preview
dotnet-execute dotnet-execute dotnet-execute Latest
ReferenceResolver ReferenceResolver ReferenceResolver Latest

default

Docker Pulls

BuiltWithDot.Net shield

中文介绍

Intro

dotnet-exec is a command-line tool for executing C# program without a project file, and you can have your custom entry point other than the Main method

Slides:

Github Action for executing without dotnet environment

Install/Update

dotnet tool

Latest stable version:

dotnet tool update -g dotnet-execute

Latest preview version:

dotnet tool update -g dotnet-execute --prerelease

Install failed? try the command below:

dotnet tool update -g dotnet-execute --prerelease --add-source https://api.nuget.org/v3/index.json --ignore-failed-sources

Container support

Execute with docker

docker run --rm weihanli/dotnet-exec:latest "1+1"
docker run --rm weihanli/dotnet-exec:latest "Guid.NewGuid()"
docker run --rm --pull=always weihanli/dotnet-exec:latest "ApplicationHelper.RuntimeInfo"

Execute with podman

podman run --rm weihanli/dotnet-exec:latest "1+1"
podman run --rm weihanli/dotnet-exec:latest "Guid.NewGuid()"
podman run --rm --pull=always weihanli/dotnet-exec:latest "ApplicationHelper.RuntimeInfo"

for the full image tag list, see https://hub.docker.com/r/weihanli/dotnet-exec/tags

Examples

Get started

Execute local file:

dotnet-exec HttpPathJsonSample.cs

Execute a local file with custom entry point:

dotnet-exec 'HttpPathJsonSample.cs' --entry MainTest

Execute remote file:

dotnet-exec https://github.com/WeihanLi/SamplesInPractice/blob/master/net7Sample/Net7Sample/ArgumentExceptionSample.cs

Execute raw code:

dotnet-exec 'Console.WriteLine(1+1);'

Execute the raw script:

dotnet-exec '1 + 1'
dotnet-exec 'Guid.NewGuid()'

References

Execute raw code with custom references:

NuGet package reference:

dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "nuget: WeihanLi.Npoi,2.4.2" -u "WeihanLi.Npoi"

Local dll reference:

dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "./out/WeihanLi.Npoi.dll" -u "WeihanLi.Npoi"

Local dll in a folder references:

dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "folder: ./out" -u "WeihanLi.Npoi"

Local project reference:

dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "project: ./WeihanLi.Npoi.csproj" -u "WeihanLi.Npoi"

Framework reference:

dotnet-exec 'WebApplication.Create().Run();' --reference 'framework:web'

Web framework reference in one option:

dotnet-exec 'WebApplication.Create().Run();' --web

Usings

Execute raw code with custom usings:

dotnet-exec 'WriteLine(1+1);' --using "static System.Console"

Execute script with custom reference:

dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump()' -r "nuget:WeihanLi.Npoi,2.4.2" -u WeihanLi.Npoi

More

Execute with additional dependencies

dotnet-exec 'typeof(LocalType).FullName.Dump();' --ad FileLocalType2.cs
dotnet-exec 'typeof(LocalType).FullName.Dump();' --addition FileLocalType2.cs

Execute with exacting references and usings from the project file

dotnet-exec 'typeof(LocalType).FullName.Dump();' --project ./Sample.csproj

Execute file with preview features:

dotnet-exec RawStringLiteral.cs --preview

Config Profile

You can customize the config you used often into a config profile to reuse it for convenience.

List the profiles had configured:

dotnet-exec profile ls

Configure a profile:

dotnet-exec profile set web -r "nuget:WeihanLi.Web.Extensions" -u 'WeihanLi.Web.Extensions' --web --wide false

Get the profile details:

dotnet-exec profile get web

Remove the profile not needed:

dotnet-exec profile rm web

Executing with specific profile config:

dotnet-exec 'WebApplication.Create().Chain(_=>_.MapRuntimeInfo()).Run();' --profile web --using 'WeihanLi.Extensions'

image

Executing with specific profile config and remove preset specific using:

dotnet-exec 'WebApplication.Create().Run();' --profile web --using '-WeihanLi.Extensions'

Acknowledgements

dotnet-exec's People

Contributors

corstiaan84 avatar weihanli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dotnet-exec's Issues

性能怎么样?支持csx吗?

我没看到介绍说支持csx,而一般C#脚本都是运行这个文件的,我之前使用的是dotnet-script,但这个工具的最大问题是性能不好,执行脚本时要卡住很久,不知道你这个工具有没有避免这类问题,至少让它的脚本流畅度能达到python吧

common used snippets

Add support for common used snippets

for example:

dotnet-exec 'Guid.NewGuid()'
dotnet-exec snip set new-guid `Guid.NewGuid()`

dotnet-exec snip new-guid

unexpected compile error

dotnet-exec "https://github.com/WeihanLi/SamplesInPractice/blob/56dda58920fa9921dad50fde4a8333581541cbd2/BalabalaSample/CorrelationIdSample.cs" --project "https://github.com/WeihanLi/SamplesInPractice/blob/56dda58920fa9921dad50fde4a8333581541cbd2/BalabalaSample/BalabalaSample.csproj" --wide false --debug
fail: dotnet-exec[0]
      Compile error:
      (24,40): error CS1929: 'ILoggingBuilder' does not contain a definition for 'AddSerilog' and the best extension method overload 'SerilogLoggerExtensions.AddSerilog(ILoggerFactory, ILogger?, bool)' requires a receiver of type 'Microsoft.Extensions.Logging.ILoggerFactory'

Version:
0.14.0+60527bb786298d0a812968d5129b802c38bf12ef

privileged operation issue

dotnet-exec 'File.CreateSymbolicLink(@"C:\Users\weihan.li\.kube\config", @"C:\Users\weihan.li\.kube\paralus-nonprod.yaml")'
fail: dotnet-exec[0]
      Execute code exception
      System.IO.IOException: A required privilege is not held by the client. : 'C:\Users\weihan.li\.kube\config'
         at Interop.Kernel32.CreateSymbolicLink(String symlinkFileName, String targetFileName, Boolean isDirectory)
         at System.IO.File.CreateSymbolicLink(String path, String pathToTarget)
         at Submission#1.<<Initialize>>d__0.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Scripting.Script`1.RunSubmissionsAsync(ScriptExecutionState executionState, ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, Func`2 catchExceptionOpt, CancellationToken cancellationToken)
         at Microsoft.CodeAnalysis.Scripting.ScriptStateTaskExtensions.CastAsync[S,T](Task`1 task)
         at Exec.Services.CSharpScriptCompilerExecutor.Execute(CompileResult compileResult, ExecOptions options) in /_/src/dotnet-exec/Services/CSharpScriptingExecutor.cs:line 49
         at Exec.CommandHandler.Execute(ExecOptions options) in /_/src/dotnet-exec/CommandHandler.cs:line 92

Support private nuget source

Unhandled exception when using the advanced compiler

image

I am running a basic script containing nothing but a Console.WriteLine, the program works fine using the default compiler, but as soon as I run it using the advanced compiler, it breaks and gives me an unhandled exception. So is the case with using -a.

`$(RootNamespace)` caused `__GlobalUsings` error

When we use the --project to exact the global usings and references from project file, it would fail when we're using <Using Include="$(RootNamespace)" /> in our project file

  <ItemGroup>
    <Using Include="$(RootNamespace)" />
  </ItemGroup>

Maybe we could ignore these usings if we could not find a property to replace it in project file

project refrence seems not work

hi , the project is very good!

but i can not try for a project refrence, steps here:

mkdir -p test/alib 
cd test/alib

dotnet new classlib

cd ..

create a test.cs in test folder:

alib.Class1 inst = new();
Console.WriteLine($"inst = {inst}");

and then :

dotnet-exec .\test.cs --project .\alib\alib.csproj
fail: dotnet-exec[0]
      Compile error:
      CS0246-Error-(1,1): error CS0246: The type or namespace name 'alib' could not be found (are you missing a using directive or an assembly reference?)

anything that i had missed ?

The type initializer for 'Exec.Services.ConfigProfileManager' threw an exception

Unhandled exception. System.TypeInitializationException: The type initializer for 'Exec.Services.ConfigProfileManager' threw an exception.
 ---> System.IO.DirectoryNotFoundException: Could not find a part of the path '/root/.dotnet/tools/dotnet-exec/profiles'.
   at System.IO.FileSystem.CreateDirectory(String fullPath, UnixFileMode unixCreateMode)
   at System.IO.Directory.CreateDirectory(String path)
   at Exec.Services.ConfigProfileManager..cctor() in /_/src/dotnet-exec/Services/ConfigProfileManager.cs:line 23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.