10000 Add support for specifying Microsoft.NET.Sdk.Web as the sdk by seesharper · Pull Request #694 · dotnet-script/dotnet-script · GitHub
[go: up one dir, main page]

Skip to content

Add support for specifying Microsoft.NET.Sdk.Web as the sdk #694

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

Merged
merged 20 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ Run C# scripts from the .NET CLI, define NuGet packages inline and edit/debug th

## NuGet Packages

| Name | Version | Framework(s) |
| ------------------------------------- | ------------------------------------------------------------ | -------------------------------- |
| `dotnet-script` (global tool) | [![Nuget](http://img.shields.io/nuget/v/dotnet-script.svg?maxAge=10800)](https://www.nuget.org/packages/dotnet-script/) | `net6.0`, `net5.0`, `netcoreapp3.1` |
| `Dotnet.Script` (CLI as Nuget) | [![Nuget](http://img.shields.io/nuget/v/dotnet.script.svg?maxAge=10800)](https://www.nuget.org/packages/dotnet.script/) | `net6.0`, `net5.0`, `netcoreapp3.1` |
| `Dotnet.Script.Core` | [![Nuget](http://img.shields.io/nuget/v/Dotnet.Script.Core.svg?maxAge=10800)](https://www.nuget.org/packages/Dotnet.Script.Core/) | `netcoreapp3.1` , `netstandard2.0` |
| `Dotnet.Script.DependencyModel` | [![Nuget](http://img.shields.io/nuget/v/Dotnet.Script.DependencyModel.svg?maxAge=10800)](https://www.nuget.org/packages/Dotnet.Script.DependencyModel/) | `netstandard2.0` |
| `Dotnet.Script.DependencyModel.Nuget` | [![Nuget](http://img.shields.io/nuget/v/Dotnet.Script.DependencyModel.Nuget.svg?maxAge=10800)](https://www.nuget.org/packages/Dotnet.Script.DependencyModel.Nuget/) | `netstandard2.0` |
| Name | Version | Framework(s) |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| `dotnet-script` (global tool) | [![Nuget](http://img.shields.io/nuget/v/dotnet-script.svg?maxAge=10800)](https://www.nuget.org/packages/dotnet-script/) | `net6.0`, `net7.0` |
| `Dotnet.Script` (CLI as Nuget) | [![Nuget](http://img.shields.io/nuget/v/dotnet.script.svg?maxAge=10800)](https://www.nuget.org/packages/dotnet.script/) | `net6.0`, `net7.0` |
| `Dotnet.Script.Core` | [![Nuget](http://img.shields.io/nuget/v/Dotnet.Script.Core.svg?maxAge=10800)](https://www.nuget.org/packages/Dotnet.Script.Core/) | `net6.0` , `netstandard2.0` |
| `Dotnet.Script.DependencyModel` | [![Nuget](http://img.shields.io/nuget/v/Dotnet.Script.DependencyModel.svg?maxAge=10800)](https://www.nuget.org/packages/Dotnet.Script.DependencyModel/) | `netstandard2.0` |
| `Dotnet.Script.DependencyModel.Nuget` | [![Nuget](http://img.shields.io/nuget/v/Dotnet.Script.DependencyModel.Nuget.svg?maxAge=10800)](https://www.nuget.org/packages/Dotnet.Script.DependencyModel.Nuget/) | `netstandard2.0` |

## Installing

### Prerequisites

The only thing we need to install is [.NET Core 3.1 or .NET 5.0 SDK](https://www.microsoft.com/net/download/core).
The only thing we need to install is [.Net 6.0 or .Net 7.0](https://www.microsoft.com/net/download/core).

### .NET Core Global Tool

Expand Down Expand Up @@ -52,11 +52,7 @@ Tool 'dotnet-script' (version '0.22.0') was successfully uninstalled.

### Windows

```powershell
choco install dotnet.script
```

We also provide a PowerShell script for installation.
PowerShell script for installation.

```powershell
(new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/dotnet-script/dotnet-script/master/install/install.ps1") | iex
Expand Down Expand Up @@ -569,6 +565,24 @@ We will also see this when working with scripts in VS Code under the problems pa

![image](https://user-images.githubusercontent.com/1034073/65727087-0e982600-e0b7-11e9-8fa0-d16331ab948a.png)

## Specifying an SDK

Starting with `dotnet-script` 1.4.0 we can now specify the SDK to be used for a script.

For instance, creating a web server in a script is now as simple as the following.

```csharp
#r "sdk:Microsoft.NET.Sdk.Web"

using Microsoft.AspNetCore.Builder;

var a = WebApplication.Create();
a.MapGet("/", () => "Hello world");
a.Run();
```

> Please note the the only SDK currently supported is `Microsoft.NET.Sdk.Web`

## Team

- [Bernhard Richter](https://github.com/seesharper) ([@bernhardrichter](https://twitter.com/bernhardrichter))
Expand Down
16 changes: 0 additions & 16 deletions build/Build.csx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#load "nuget:Dotnet.Build, 0.7.1"
#load "nuget:dotnet-steps, 0.0.1"
#load "nuget:github-changelog, 0.1.5"
#load "Choco.csx"
#load "BuildContext.csx"

using System.Xml.Linq;
Expand All @@ -17,7 +16,6 @@ Step pack = () =>
{
CreateGitHubReleaseAsset();
CreateNuGetPackages();
CreateChocoPackage();
CreateGlobalToolPackage();
};

Expand All @@ -39,19 +37,6 @@ private void CreateGitHubReleaseAsset()
Zip(publishArchiveFolder, pathToGitHubReleaseAsset);
}


private void CreateChocoPackage()
{
if (BuildEnvironment.IsWindows)
{
Choco.Pack(dotnetScriptProjectFolder, publishArtifactsFolder, chocolateyArtifactsFolder);
}
else
{
Logger.Log("The choco package is only built on Windows");
}
}

private void CreateGlobalToolPackage()
{
using var globalToolBuildFolder = new DisposableFolder();
Expand Down Expand Up @@ -102,7 +87,6 @@ private async Task PublishRelease()
await ReleaseManagerFor(owner, projectName, BuildEnvironment.GitHubAccessToken)
.CreateRelease(Git.Default.GetLatestTag(), pathToReleaseNotes, new[] { new ZipReleaseAsset(pathToGitHubReleaseAsset) });
NuGet.TryPush(nuGetArtifactsFolder);
Choco.TryPush(chocolateyArtifactsFolder, BuildEnvironment.ChocolateyApiKey);
}
}

Expand Down
92 changes: 0 additions & 92 deletions build/Choco.csx

This file was deleted.

3 changes: 0 additions & 3 deletions build/Chocolatey/tools/ChocolateyInstall.ps1

This file was deleted.

19 changes: 0 additions & 19 deletions build/Chocolatey/tools/LICENSE.TXT

This file was deleted.

8 changes: 0 additions & 8 deletions build/Chocolatey/tools/VERIFICATION.TXT

This file was deleted.

2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:5.0
FROM mcr.microsoft.com/dotnet/sdk:7.0

# https://www.nuget.org/packages/dotnet-script/
RUN dotnet tool install dotnet-script --tool-path /usr/bin
Expand Down
32 changes: 27 additions & 5 deletions src/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,34 @@
"command": "dotnet",
"args": [
"build",
"${workspaceRoot}/Dotnet.Script/Dotnet.Script.csproj",
"/property:GenerateFullPaths=true"
],
"group": {
"kind": "build",
"isDefault": true
"options": {
"cwd": "${workspaceFolder}/.."
},
"type": "shell",
"group": "build",
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
},
{
"label": "rebuild",
"command": "dotnet",
"args": [
"build",
"--no-incremental",
"/property:GenerateFullPaths=true"
],
"options": {
"cwd": "${workspaceFolder}/.."
},
"type": "shell",
"group": "build",
"presentation": {
"reveal": "always",
"clear": true
},
"problemMatcher": "$msCompile"
},
Expand All @@ -24,7 +46,7 @@
"-c",
"release",
"-f",
"net6.0",
"net7.0",
"${workspaceFolder}/Dotnet.Script.Tests/DotNet.Script.Tests.csproj"
],
"problemMatcher": "$msCompile",
Expand Down
15 changes: 4 additions & 11 deletions src/Dotnet.Script.Core/Dotnet.Script.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@
<EmbeddedResource Include="**/*.template" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Gapotchenko.FX" Version="2021.1.5" />
<PackageReference Include="Gapotchenko.FX.Reflection.Loader" Version="2021.2.11" />
<PackageReference Include="Gapotchenko.FX" Version="2022.2.7" />
<PackageReference Include="Gapotchenko.FX.Reflection.Loader" Version="2022.2.7" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.4.0-1.final" />
<PackageReference Include="ReadLine" Version="2.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<!-- The following references are just quick fixes for issue #166 and issue #268 -->
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<!-- End of quick fixes -->
<PackageReference Include="StrongNamer" Version="0.2.5" PrivateAssets="all" />

</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Text.Json" Version="6.0.6" />
<PackageReference Include="System.Text.Json" Version="7.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dotnet.Script.DependencyModel.Nuget\Dotnet.Script.DependencyModel.NuGet.csproj" />
Expand Down
7 changes: 4 additions & 3 deletions src/Dotnet.Script.Core/ScriptCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static ScriptCompiler()
});
}

//Note: We should set this according to the SDK being used
protected virtual IEnumerable<string> ImportedNamespaces => new[]
{
"System",
Expand Down Expand Up @@ -94,7 +95,7 @@ public virtual ScriptOptions CreateScriptOptions(ScriptContext context, IList<Ru
{
var scriptMap = runtimeDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts);
var opts = ScriptOptions.Default.AddImports(ImportedNamespaces)
.WithSourceResolver(new NuGetSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, context.WorkingDirectory),scriptMap))
.WithSourceResolver(new NuGetSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, context.WorkingDirectory), scriptMap))
.WithMetadataResolver(new NuGetMetadataReferenceResolver(ScriptMetadataResolver.Default.WithBaseDirectory(context.WorkingDirectory)))
.WithEmitDebugInformation(true)
.WithLanguageVersion(LanguageVersion.Preview)
Expand Down Expand Up @@ -200,7 +201,7 @@ AssemblyLoadContext is not ScriptAssemblyLoadContext salc ||
Assembly loadedAssembly = null;
if (homogenization)
loadedAssembliesMap.TryGetValue(runtimeAssembly.Name.Name, out loadedAssembly);

if (loadedAssembly == null)
{
_logger.Trace("Adding reference to a runtime dependency => " + runtimeAssembly);
Expand Down Expand Up @@ -290,7 +291,7 @@ private Assembly MapUnresolvedAssemblyToRuntimeLibrary(IDictionary<string, Runti
if (assemblyName.Version == null || runtimeAssembly.Name.Version > assemblyName.Version)
{
loadedAssemblyMap.TryGetValue(assemblyName.Name, out var loadedAssembly);
if(loadedAssembly != null)
if (loadedAssembly != null)
{
_logger.Trace($"Redirecting {assemblyName} to already loaded {loadedAssembly.GetName().Name}");
return loadedAssembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public NuGetMetadataReferenceResolver(MetadataReferenceResolver metadataReferenc
{
_metadataReferenceResolver = metadataReferenceResolver;
}

public override bool Equals(object other)
{
return _metadataReferenceResolver.Equals(other);
Expand All @@ -42,7 +42,7 @@ public override PortableExecutableReference ResolveMissingAssembly(MetadataRefer

public override ImmutableArray<PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
{
if (reference.StartsWith("nuget", StringComparison.OrdinalIgnoreCase))
if (reference.StartsWith("nuget", StringComparison.OrdinalIgnoreCase) || reference.StartsWith("sdk", StringComparison.OrdinalIgnoreCase))
{
// HACK We need to return something here to "mark" the reference as resolved.
// https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs#L838
Expand Down
Loading
0