8000 Merge pull request #694 from dotnet-script/specify-sdk · dotnet-script/dotnet-script@6f2fe27 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f2fe27

Browse files
authored
Merge pull request #694 from dotnet-script/specify-sdk
Add support for specifying Microsoft.NET.Sdk.Web as the sdk
2 parents cc2b665 + df23881 commit 6f2fe27

25 files changed

+288
-234
lines changed

README.md

Expand all lines: README.md
+27-13
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ Run C# scripts from the .NET CLI, define NuGet packages inline and edit/debug th
88

99
## NuGet Packages
1010

11-
| Name | Version | Framework(s) |
12-
| ------------------------------------- | ------------------------------------------------------------ | -------------------------------- |
13-
| `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` |
14-
| `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` |
15-
| `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` |
16-
| `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` |
17-
| `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` |
11+
| Name | Version | Framework(s) |
12+
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
13+
| `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` |
14+
| `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` |
15+
| `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` |
16+
| `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` |
17+
| `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` |
1818

1919
## Installing
2020

2121
### Prerequisites
2222

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

2525
### .NET Core Global Tool
2626

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

5353
### Windows
5454

55-
```powershell
56-
choco install dotnet.script
57-
```
58-
59-
We also provide a PowerShell script for installation.
55+
PowerShell script for installation.
6056

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

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

568+
## Specifying an SDK
569+
570+
Starting with `dotnet-script` 1.4.0 we can now specify the SDK to be used for a script.
571+
572+
For instance, creating a web server in a script is now as simple as the following.
573+
574+
```csharp
575+
#r "sdk:Microsoft.NET.Sdk.Web"
576+
577+
using Microsoft.AspNetCore.Builder;
578+
579+
var a = WebApplication.Create();
580+
a.MapGet("/", () => "Hello world");
581+
a.Run();
582+
```
583+
584+
> Please note the the only SDK currently supported is `Microsoft.NET.Sdk.Web`
585+
572586
## Team
573587

574588
- [Bernhard Richter](https://github.com/seesharper) ([@bernhardrichter](https://twitter.com/bernhardrichter))

build/Build.csx

-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#load "nuget:Dotnet.Build, 0.7.1"
22
#load "nuget:dotnet-steps, 0.0.1"
33
#load "nuget:github-changelog, 0.1.5"
4-
#load "Choco.csx"
54
#load "BuildContext.csx"
65

76
using System.Xml.Linq;
@@ -17,7 +16,6 @@ Step pack = () =>
1716
{
1817
CreateGitHubReleaseAsset();
1918
CreateNuGetPackages();
20-
CreateChocoPackage();
2119
CreateGlobalToolPackage();
2220
};
2321

@@ -39,19 +37,6 @@ private void CreateGitHubReleaseAsset()
3937
Zip(publishArchiveFolder, pathToGitHubReleaseAsset);
4038
}
4139

42-
43-
private void CreateChocoPackage()
44-
{
45-
if (BuildEnvironment.IsWindows)
46-
{
47-
Choco.Pack(dotnetScriptProjectFolder, publishArtifactsFolder, chocolateyArtifactsFolder);
48-
}
49-
else
50-
{
51-
Logger.Log("The choco package is only built on Windows");
52-
}
53-
}
54-
5540
private void CreateGlobalToolPackage()
5641
{
5742
using var globalToolBuildFolder = new DisposableFolder();
@@ -102,7 +87,6 @@ private async Task PublishRelease()
10287
await ReleaseManagerFor(owner, projectName, BuildEnvironment.GitHubAccessToken)
10388
.CreateRelease(Git.Default.GetLatestTag(), pathToReleaseNotes, new[] { new ZipReleaseAsset(pathToGitHubReleaseAsset) });
10489
NuGet.TryPush(nuGetArtifactsFolder);
105-
Choco.TryPush(chocolateyArtifactsFolder, BuildEnvironment.ChocolateyApiKey);
10690
}
10791
}
10892

build/Choco.csx

-92
This file was deleted.

build/Chocolatey/tools/ChocolateyInstall.ps1

-3
This file was deleted.

build/Chocolatey/tools/LICENSE.TXT

-19
This file was deleted.

build/Chocolatey/tools/VERIFICATION.TXT

-8
This file was deleted.

build/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:5.0
1+
FROM mcr.microsoft.com/dotnet/sdk:7.0
22

33
# https://www.nuget.org/packages/dotnet-script/
44
RUN dotnet tool install dotnet-script --tool-path /usr/bin

src/.vscode/tasks.json

+27-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,34 @@
66
"command": "dotnet",
77
"args": [
88
"build",
9-
"${workspaceRoot}/Dotnet.Script/Dotnet.Script.csproj",
109
"/property:GenerateFullPaths=true"
1110
],
12-
"group": {
13-
"kind": "build",
14-
"isDefault": true
11+
"options": {
12+
"cwd": "${workspaceFolder}/.."
13+
},
14+
"type": "shell",
15+
"group": "build",
16+
"presentation": {
17+
"reveal": "always"
18+
},
19+
"problemMatcher": "$msCompile"
20+
},
21+
{
22+
"label": "rebuild",
23+
"command": "dotnet",
24+
"args": [
25+
"build",
26+
"--no-incremental",
27+
"/property:GenerateFullPaths=true"
28+
],
29+
"options": {
30+
"cwd": "${workspaceFolder}/.."
31+
},
32+
"type": "shell",
33+
"group": "build",
34+
"presentation": {
35+
"reveal": "always",
36+
"clear": true
1537
},
1638
"problemMatcher": "$msCompile"
1739
},
@@ -24,7 +46,7 @@
2446
"-c",
2547
"release",
2648
"-f",
27-
"net6.0",
49+
"net7.0",
2850
"${workspaceFolder}/Dotnet.Script.Tests/DotNet.Script.Tests.csproj"
2951
],
3052
"problemMatcher": "$msCompile",

src/Dotnet.Script.Core/Dotnet.Script.Core.csproj

+4-11
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,15 @@
2424
<EmbeddedResource Include="**/*.template" />
2525
</ItemGroup>
2626
<ItemGroup>
27-
<PackageReference Include="Gapotchenko.FX" Version="2021.1.5" />
28-
<PackageReference Include="Gapotchenko.FX.Reflection.Loader" Version="2021.2.11" />
27+
<PackageReference Include="Gapotchenko.FX" Version="2022.2.7" />
28+
<PackageReference Include="Gapotchenko.FX.Reflection.Loader" Version="2022.2.7" />
2929
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.4.0-1.final" />
3030
<PackageReference Include="ReadLine" Version="2.0.1" />
31-
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
32-
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
33-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
34-
<!-- The following references are just quick fixes for issue #166 and issue #268 -->
35-
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
36-
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
37-
<!-- End of quick fixes -->
3831
<PackageReference Include="StrongNamer" Version="0.2.5" PrivateAssets="all" />
39-
4032
</ItemGroup>
4133
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
42-
<PackageReference Include="System.Text.Json" Version="6.0.6" />
34+
<PackageReference Include="System.Text.Json" Version="7.0.0" />
35+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
4336
</ItemGroup>
4437
<ItemGroup>
4538
<ProjectReference Include="..\Dotnet.Script.DependencyModel.Nuget\Dotnet.Script.DependencyModel.NuGet.csproj" />

src/Dotnet.Script.Core/ScriptCompiler.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static ScriptCompiler()
3939
});
4040
}
4141

42+
//Note: We should set this according to the SDK being used
4243
protected virtual IEnumerable<string> ImportedNamespaces => new[]
4344
{
4445
"System",
@@ -94,7 +95,7 @@ public virtual ScriptOptions CreateScriptOptions(ScriptContext context, IList<Ru
9495
{
9596
var scriptMap = runtimeDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts);
9697
var opts = ScriptOptions.Default.AddImports(ImportedNamespaces)
97-
.WithSourceResolver(new NuGetSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, context.WorkingDirectory),scriptMap))
98+
.WithSourceResolver(new NuGetSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, context.WorkingDirectory), scriptMap))
9899
.WithMetadataResolver(new NuGetMetadataReferenceResolver(ScriptMetadataResolver.Default.WithBaseDirectory(context.WorkingDirectory)))
99100
.WithEmitDebugInformation(true)
100101
.WithLanguageVersion(LanguageVersion.Preview)
@@ -200,7 +201,7 @@ AssemblyLoadContext is not ScriptAssemblyLoadContext salc ||
200201
Assembly loadedAssembly = null;
201202
if (homogenization)
202203
loadedAssembliesMap.TryGetValue(runtimeAssembly.Name.Name, out loadedAssembly);
203-
204+
204205
if (loadedAssembly == null)
205206
{
206207
_logger.Trace("Adding reference to a runtime dependency => " + runtimeAssembly);
@@ -290,7 +291,7 @@ private Assembly MapUnresolvedAssemblyToRuntimeLibrary(IDictionary<string, Runti
290291
if (assemblyName.Version == null || runtimeAssembly.Name.Version > assemblyName.Version)
291292
{
292293
loadedAssemblyMap.TryGetValue(assemblyName.Name, out var loadedAssembly);
293-
if(loadedAssembly != null)
294+
if (loadedAssembly != null)
294295
{
295296
_logger.Trace($"Redirecting {assemblyName} to already loaded {loadedAssembly.GetName().Name}");
296297
return loadedAssembly;

src/Dotnet.Script.DependencyModel.Nuget/NuGetMetadataReferenceResolver.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public NuGetMetadataReferenceResolver(MetadataReferenceResolver metadataReferenc
2121
{
2222
_metadataReferenceResolver = metadataReferenceResolver;
2323
}
24-
24+
2525
public override bool Equals(object other)
2626
{
2727
return _metadataReferenceResolver.Equals(other);
@@ -42,7 +42,7 @@ public override PortableExecutableReference ResolveMissingAssembly(MetadataRefer
4242

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

0 commit comments

Comments
 (0)
0