Skip to content

Commit 14fd632

Browse files
committed
added SDK flag
1 parent e422222 commit 14fd632

File tree

12 files changed

+57
-40
lines changed

12 files changed

+57
-40
lines changed

src/Dotnet.Script.Core/Commands/ExecuteScriptCommand.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
5757
return pathToLibrary;
5858
}
5959

60-
var options = new PublishCommandOptions(executeOptions.File, executionCacheFolder, "script", PublishType.Library, executeOptions.OptimizationLevel, executeOptions.PackageSources, null, executeOptions.NoCache);
60+
var options = new PublishCommandOptions(executeOptions.File, executionCacheFolder, "script", PublishType.Library, executeOptions.OptimizationLevel, executeOptions.PackageSources, null, executeOptions.NoCache, executeOptions.SDK);
6161
new PublishCommand(_scriptConsole, _logFactory).Execute(options);
6262
if (hash != null)
6363
{
@@ -77,7 +77,7 @@ public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
7777

7878
var scriptFilesProvider = new ScriptFilesResolver();
7979
var allScriptFiles = scriptFilesProvider.GetScriptFiles(options.File.Path);
80-
var projectFile = new ScriptProjectProvider(_logFactory).CreateProjectFileFromScriptFiles(ScriptEnvironment.Default.TargetFramework, allScriptFiles.ToArray());
80+
var projectFile = new ScriptProjectProvider(_logFactory).CreateProjectFileFromScriptFiles(ScriptEnvironment.Default.TargetFramework, allScriptFiles.ToArray(), options.SDK);
8181

8282
if (!projectFile.IsCacheable)
8383
{
@@ -93,6 +93,11 @@ public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
9393
incrementalHash.AppendData(File.ReadAllBytes(scriptFile));
9494
}
9595

96+
if (!string.IsNullOrWhiteSpace(options.SDK))
97+
{
98+
incrementalHash.AppendData(Encoding.UTF8.GetBytes(options.SDK));
99+
}
100+
96101
var configuration = options.OptimizationLevel.ToString();
97102
incrementalHash.AppendData(Encoding.UTF8.GetBytes(configuration));
98103

src/Dotnet.Script.Core/Commands/ExecuteScriptCommandOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ namespace Dotnet.Script.Core.Commands
44
{
55
public class ExecuteScriptCommandOptions
66
{
7-
public ExecuteScriptCommandOptions(ScriptFile file, string[] arguments, OptimizationLevel optimizationLevel, string[] packageSources, bool isInteractive ,bool noCache)
7+
public ExecuteScriptCommandOptions(ScriptFile file, string[] arguments, OptimizationLevel optimizationLevel, string[] packageSources, bool isInteractive ,bool noCache, string sdk)
88
{
99
File = file;
1010
Arguments = arguments;
1111
OptimizationLevel = optimizationLevel;
1212
PackageSources = packageSources;
1313
IsInteractive = isInteractive;
1414
NoCache = noCache;
15+
SDK = sdk;
1516
}
1617

1718
public ScriptFile File { get; }
@@ -20,5 +21,6 @@ public ExecuteScriptCommandOptions(ScriptFile file, string[] arguments, Optimiza
2021
public string[] PackageSources { get; }
2122
public bool IsInteractive { get; }
2223
public bool NoCache { get; }
24+
public string SDK { get; }
2325
}
2426
}

src/Dotnet.Script.Core/Commands/PublishCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Execute(PublishCommandOptions options)
3232
var scriptEmitter = new ScriptEmitter(_scriptConsole, compiler);
3333
var publisher = new ScriptPublisher(_logFactory, scriptEmitter);
3434
var code = absoluteFilePath.ToSourceText();
35-
var context = new ScriptContext(code, absolutePublishDirectory, Enumerable.Empty<string>(), absoluteFilePath, options.OptimizationLevel, packageSources: options.PackageSources);
35+
var context = new ScriptContext(code, absolutePublishDirectory, Enumerable.Empty<string>(), absoluteFilePath, options.OptimizationLevel, packageSources: options.PackageSources, sdk: options.SDK);
3636

3737
if (options.PublishType == PublishType.Library)
3838
{

src/Dotnet.Script.Core/Commands/PublishCommandOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Dotnet.Script.Core.Commands
55
{
66
public class PublishCommandOptions
77
{
8-
public PublishCommandOptions(ScriptFile file, string outputDirectory, string libraryName, PublishType publishType, OptimizationLevel optimizationLevel, string[] packageSources, string runtimeIdentifier, bool noCache)
8+
public PublishCommandOptions(ScriptFile file, string outputDirectory, string libraryName, PublishType publishType, OptimizationLevel optimizationLevel, string[] packageSources, string runtimeIdentifier, bool noCache, string sdk)
99
{
1010
File = file;
1111
OutputDirectory = outputDirectory;
@@ -15,6 +15,7 @@ public PublishCommandOptions(ScriptFile file, string outputDirectory, string lib
1515
PackageSources = packageSources;
1616
RuntimeIdentifier = runtimeIdentifier ?? ScriptEnvironment.Default.RuntimeIdentifier;
1717
NoCache = noCache;
18+
SDK = sdk;
1819
}
1920

2021
public ScriptFile File { get; }
@@ -25,6 +26,7 @@ public PublishCommandOptions(ScriptFile file, string outputDirectory, string lib
2526
public string[] PackageSources { get; }
2627
public string RuntimeIdentifier { get; }
2728
public bool NoCache { get; }
29+
public string SDK { get; }
2830
}
2931

3032
public enum PublishType

src/Dotnet.Script.Core/ScriptCompiler.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,10 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
160160
return new ScriptCompilationContext<TReturn>(script, context.Code, loader, scriptOptions, runtimeDependencies, nonSuppressedDiagnostics);
161161
}
162162

163-
private RuntimeDependency[] GetRuntimeDependencies(ScriptContext context)
164-
{
165-
if (context.ScriptMode == ScriptMode.Script)
166-
{
167-
return RuntimeDependencyResolver.GetDependencies(context.FilePath, context.PackageSources).ToArray();
168-
}
169-
else
170-
{
171-
return RuntimeDependencyResolver.GetDependenciesForCode(context.WorkingDirectory, context.ScriptMode, context.PackageSources, context.Code.ToString()).ToArray();
172-
}
173-
}
163+
private RuntimeDependency[] GetRuntimeDependencies(ScriptContext context)
164+
=> context.ScriptMode == ScriptMode.Script
165+
? RuntimeDependencyResolver.GetDependencies(context.FilePath, context.PackageSources, context.SDK).ToArray()
166+
: RuntimeDependencyResolver.GetDependenciesForCode(context.WorkingDirectory, context.ScriptMode, context.PackageSources, context.Code.ToString()).ToArray();
174167

175168
private ScriptOptions AddScriptReferences(ScriptOptions scriptOptions, Dictionary<string, Assembly> loadedAssembliesMap, Dictionary<string, RuntimeAssembly> scriptDependenciesMap)
176169
{

src/Dotnet.Script.Core/ScriptContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Dotnet.Script.Core
1010
{
1111
public class ScriptContext
1212
{
13-
public ScriptContext(SourceText code, string workingDirectory, IEnumerable<string> args, string filePath = null, OptimizationLevel optimizationLevel = OptimizationLevel.Debug, ScriptMode scriptMode = ScriptMode.Script, string[] packageSources = null)
13+
public ScriptContext(SourceText code, string workingDirectory, IEnumerable<string> args, string filePath = null, OptimizationLevel optimizationLevel = OptimizationLevel.Debug, ScriptMode scriptMode = ScriptMode.Script, string[] packageSources = null, string sdk = null)
1414
{
1515
Code = code;
1616
WorkingDirectory = workingDirectory;
@@ -19,6 +19,7 @@ public ScriptContext(SourceText code, string workingDirectory, IEnumerable<strin
1919
OptimizationLevel = optimizationLevel;
2020
ScriptMode = filePath != null ? ScriptMode.Script : scriptMode;
2121
PackageSources = packageSources ?? Array.Empty<string>();
22+
SDK = sdk;
2223
}
2324

2425
public SourceText Code { get; }
@@ -34,5 +35,6 @@ public ScriptContext(SourceText code, string workingDirectory, IEnumerable<strin
3435
public ScriptMode ScriptMode { get; }
3536

3637
public string[] PackageSources { get; }
38+
public string SDK { get; }
3739
}
3840
}

src/Dotnet.Script.DependencyModel/ProjectSystem/ProjectFile.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@ public ProjectFile(string xmlContent)
6161
/// </summary>
6262
public string TargetFramework { get; set; } = ScriptEnvironment.Default.TargetFramework;
6363

64+
/// <summary>
65+
/// Gets or sets the SDK for this <see cref="ProjectFile"/>.
66+
/// </summary>
67+
public string SDK { get; set; } = "Microsoft.NET.Sdk";
68+
6469
public void Save(string pathToProjectFile)
6570
{
6671
var projectFileDocument = XDocument.Parse(ReadTemplate("csproj.template"));
72+
projectFileDocument.Root.Add(new XAttribute("Sdk", SDK));
6773
var itemGroupElement = projectFileDocument.Descendants("ItemGroup").Single();
6874
foreach (var packageReference in PackageReferences)
6975
{
@@ -83,19 +89,15 @@ public void Save(string pathToProjectFile)
8389
var targetFrameworkElement = projectFileDocument.Descendants("TargetFramework").Single();
8490
targetFrameworkElement.Value = TargetFramework;
8591

86-
using (var fileStream = new FileStream(pathToProjectFile, FileMode.Create, FileAccess.Write))
87-
{
88-
projectFileDocument.Save(fileStream);
89-
}
92+
using var fileStream = new FileStream(pathToProjectFile, FileMode.Create, FileAccess.Write);
93+
projectFileDocument.Save(fileStream);
9094
}
9195

9296
private static string ReadTemplate(string name)
9397
{
9498
var resourceStream = typeof(ProjectFile).GetTypeInfo().Assembly.GetManifestResourceStream($"Dotnet.Script.DependencyModel.ProjectSystem.{name}");
95-
using (var streamReader = new StreamReader(resourceStream))
96-
{
97-
return streamReader.ReadToEnd();
98-
}
99+
using var streamReader = new StreamReader(resourceStream);
100+
return streamReader.ReadToEnd();
99101
}
100102

101103
/// <inheritdoc/>
@@ -105,7 +107,8 @@ public bool Equals(ProjectFile other)
105107
if (ReferenceEquals(this, other)) return true;
106108
return PackageReferences.SequenceEqual(other.PackageReferences)
107109
&& AssemblyReferences.SequenceEqual(other.AssemblyReferences)
108-
&& TargetFramework.Equals(other.TargetFramework);
110+
&& TargetFramework.Equals(other.TargetFramework)
111+
&& SDK.Equals(other.SDK);
109112
}
110113

111114
/// <inheritdoc/>

src/Dotnet.Script.DependencyModel/ProjectSystem/ScriptProjectProvider.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Dotnet.Script.DependencyModel.Environment;
77
using Dotnet.Script.DependencyModel.Logging;
88
using Dotnet.Script.DependencyModel.Process;
9+
using Newtonsoft.Json;
910

1011
namespace Dotnet.Script.DependencyModel.ProjectSystem
1112
{
@@ -79,7 +80,7 @@ public ProjectFileInfo CreateProject(string targetDirectory, string defaultTarge
7980
return CreateProject(targetDirectory, Directory.GetFiles(targetDirectory, "*.csx", SearchOption.AllDirectories), defaultTargetFramework, enableNuGetScriptReferences);
8081
}
8182

82-
public ProjectFileInfo CreateProject(string targetDirectory, IEnumerable<string> scriptFiles, string defaultTargetFramework = "net46", bool enableNuGetScriptReferences = false)
83+
public ProjectFileInfo CreateProject(string targetDirectory, IEnumerable<string> scriptFiles, string defaultTargetFramework = "net46", bool enableNuGetScriptReferences = false, string sdk = null)
8384
{
8485
if (scriptFiles == null || !scriptFiles.Any())
8586
{
@@ -94,19 +95,19 @@ public ProjectFileInfo CreateProject(string targetDirectory, IEnumerable<string>
9495

9596
_logger.Debug($"Creating project file for *.csx files found in {targetDirectory} using {defaultTargetFramework} as the default framework.");
9697

97-
return SaveProjectFileFromScriptFiles(targetDirectory, defaultTargetFramework, scriptFiles.ToArray());
98+
return SaveProjectFileFromScriptFiles(targetDirectory, defaultTargetFramework, scriptFiles.ToArray(), sdk);
9899
}
99100

100-
public ProjectFileInfo CreateProjectForScriptFile(string scriptFile)
101+
public ProjectFileInfo CreateProjectForScriptFile(string scriptFile, string sdk)
101102
{
102103
_logger.Debug($"Creating project file for {scriptFile}");
103104
var scriptFiles = _scriptFilesResolver.GetScriptFiles(scriptFile);
104-
return SaveProjectFileFromScriptFiles(Path.GetDirectoryName(scriptFile), _scriptEnvironment.TargetFramework, scriptFiles.ToArray());
105+
return SaveProjectFileFromScriptFiles(Path.GetDirectoryName(scriptFile), _scriptEnvironment.TargetFramework, scriptFiles.ToArray(), sdk);
105106
}
106107

107-
private ProjectFileInfo SaveProjectFileFromScriptFiles(string targetDirectory, string defaultTargetFramework, string[] csxFiles)
108+
private ProjectFileInfo SaveProjectFileFromScriptFiles(string targetDirectory, string defaultTargetFramework, string[] csxFiles, string sdk)
108109
{
109-
ProjectFile projectFile = CreateProjectFileFromScriptFiles(defaultTargetFramework, csxFiles);
110+
ProjectFile projectFile = CreateProjectFileFromScriptFiles(defaultTargetFramework, csxFiles, sdk);
110111

111112
var pathToProjectFile = GetPathToProjectFile(targetDirectory, defaultTargetFramework);
112113
projectFile.Save(pathToProjectFile);
@@ -116,7 +117,7 @@ private ProjectFileInfo SaveProjectFileFromScriptFiles(string targetDirectory, s
116117
return new ProjectFileInfo(pathToProjectFile, NuGetUtilities.GetNearestConfigPath(targetDirectory));
117118
}
118119

119-
public ProjectFile CreateProjectFileFromScriptFiles(string defaultTargetFramework, string[] csxFiles)
120+
public ProjectFile CreateProjectFileFromScriptFiles(string defaultTargetFramework, string[] csxFiles, string sdk)
120121
{
121122
var parseresult = _scriptParser.ParseFromFiles(csxFiles);
122123

@@ -128,6 +129,11 @@ public ProjectFile CreateProjectFileFromScriptFiles(string defaultTargetFramewor
128129
}
129130

130131
projectFile.TargetFramework = defaultTargetFramework;
132+
if (!string.IsNullOrWhiteSpace(sdk))
133+
{
134+
projectFile.SDK = sdk;
135+
}
136+
131137
return projectFile;
132138
}
133139

src/Dotnet.Script.DependencyModel/ProjectSystem/csproj.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project>
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
</PropertyGroup>
77
<ItemGroup>
8+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
89
</ItemGroup>
910
<Target Name="RecordReferencePaths" AfterTargets="AfterResolveReferences">
1011
<WriteLinesToFile File="$(OutputPath)/ReferencePaths.txt" Lines="@(ReferencePath)" />

src/Dotnet.Script.DependencyModel/Runtime/RuntimeDependencyResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ private static IRestorer CreateRestorer(LogFactory logFactory, bool useRestoreCa
4141
}
4242
}
4343

44-
public IEnumerable<RuntimeDependency> GetDependencies(string scriptFile, string[] packageSources)
44+
public IEnumerable<RuntimeDependency> GetDependencies(string scriptFile, string[] packageSources, string sdk)
4545
{
46-
var projectFileInfo = _scriptProjectProvider.CreateProjectForScriptFile(scriptFile);
46+
var projectFileInfo = _scriptProjectProvider.CreateProjectForScriptFile(scriptFile, sdk);
4747
_restorer.Restore(projectFileInfo, packageSources);
4848
var pathToAssetsFile = Path.Combine(Path.GetDirectoryName(projectFileInfo.Path), "obj", "project.assets.json");
4949
return GetDependenciesInternal(pathToAssetsFile);

src/Dotnet.Script.Tests/ScriptPackagesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void ShouldGetScriptFilesFromScriptPackage()
105105
var resolver = CreateRuntimeDependencyResolver();
106106
var fixture = GetFullPathToTestFixture("ScriptPackage/WithMainCsx");
107107
var csxFiles = Directory.GetFiles(fixture, "*.csx");
108-
var dependencies = resolver.GetDependencies(csxFiles.First(), Array.Empty<string>());
108+
var dependencies = resolver.GetDependencies(csxFiles.First(), Array.Empty<string>(), null);
109109
var scriptFiles = dependencies.Single(d => d.Name == "ScriptPackageWithMainCsx").Scripts;
110110
Assert.NotEmpty(scriptFiles);
111111
}
@@ -117,7 +117,7 @@ private static string GetFullPathToTestFixture(string path)
117117
}
118118

119119

120-
private RuntimeDependencyResolver CreateRuntimeDependencyResolver()
120+
private static RuntimeDependencyResolver CreateRuntimeDependencyResolver()
121121
{
122122
var resolver = new RuntimeDependencyResolver(TestOutputHelper.CreateTestLogFactory(), useRestoreCache: false);
123123
return resolver;

src/Dotnet.Script/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ private static int Wain(string[] args)
6767
var verbosity = app.Option("--verbosity", " Set the verbosity level of the command. Allowed values are t[trace], d[ebug], i[nfo], w[arning], e[rror], and c[ritical].", CommandOptionType.SingleValue);
6868
var nocache = app.Option("--no-cache", "Disable caching (Restore and Dll cache)", CommandOptionType.NoValue);
6969
var infoOption = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);
70+
var sdk = app.Option("--sdk <sdk>", "Project SDK to use. Default is \"Microsoft.NET.Sdk\"", CommandOptionType.SingleValue);
7071

7172
var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
7273
var argsAfterDoubleHyphen = args.SkipWhile(a => a != "--").Skip(1).ToArray();
@@ -181,7 +182,8 @@ private static int Wain(string[] args)
181182
commandConfig.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug,
182183
packageSources.Values?.ToArray(),
183184
runtime.Value() ?? ScriptEnvironment.Default.RuntimeIdentifier,
184-
nocache.HasValue()
185+
nocache.HasValue(),
186+
sdk.Value()
185187
);
186188

187189
var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
@@ -244,7 +246,8 @@ private static int Wain(string[] args)
244246
optimizationLevel,
245247
packageSources.Values?.ToArray(),
246248
interactive.HasValue(),
247-
nocache.HasValue()
249+
nocache.HasValue(),
250+
sdk.Value()
248251
);
249252

250253
var fileCommand = new ExecuteScriptCommand(ScriptConsole.Default, logFactory);

0 commit comments

Comments
 (0)