Skip to content

Commit 3c15c66

Browse files
committed
Docs and cleanup
1 parent 3b507c8 commit 3c15c66

File tree

5 files changed

+91
-42
lines changed

5 files changed

+91
-42
lines changed

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

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

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
using System;
22
using System.IO;
3-
using System.Linq;
3+
using System.Security.Cryptography;
4+
using System.Text;
45
using System.Threading.Tasks;
5-
using Dotnet.Script.DependencyModel.Environment;
66
using Dotnet.Script.DependencyModel.Logging;
77
using Dotnet.Script.DependencyModel.ProjectSystem;
8-
using Dotnet.Script.DependencyModel.Runtime;
98

109
namespace Dotnet.Script.Core.Commands
1110
{
12-
public class ExecuteScriptCommand : IFileCommand
11+
public class ExecuteScriptCommand
1312
{
1413
private readonly ScriptConsole _scriptConsole;
1514
private readonly LogFactory _logFactory;
15+
private readonly Logger _logger;
1616

1717
public ExecuteScriptCommand(ScriptConsole scriptConsole, LogFactory logFactory)
1818
{
1919
_scriptConsole = scriptConsole;
2020
_logFactory = logFactory;
21+
_logger = logFactory.CreateLogger<ExecuteScriptCommand>();
22+
2123
}
2224

2325
public async Task<TReturn> Run<TReturn, THost>(ExecuteScriptCommandOptions options)
@@ -27,7 +29,7 @@ public async Task<TReturn> Run<TReturn, THost>(ExecuteScriptCommandOptions optio
2729
return await DownloadAndRunCode<TReturn>(options);
2830
}
2931

30-
var pathToLibrary = CreateLibrary(options);
32+
var pathToLibrary = GetLibrary(options);
3133
return await ExecuteLibrary<TReturn>(pathToLibrary, options.Arguments, options.NoCache);
3234
}
3335

@@ -39,13 +41,90 @@ private async Task<TReturn> DownloadAndRunCode<TReturn>(ExecuteScriptCommandOpti
3941
return await new ExecuteCodeCommand(_scriptConsole, _logFactory).Execute<TReturn>(options);
4042
}
4143

42-
private string CreateLibrary(ExecuteScriptCommandOptions executeOptions)
44+
45+
private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
4346
{
4447
var projectFolder = FileUtils.GetPathToTempFolder(Path.GetDirectoryName(executeOptions.File.Path));
4548
var publishDirectory = Path.Combine(projectFolder, "publish");
46-
var options = new PublishCommandOptions(executeOptions.File,publishDirectory, "script", PublishType.Library,executeOptions.OptimizationLevel, null, executeOptions.NoCache);
47-
new PublishCommand(_scriptConsole, _logFactory).Execute(options);
48-
return Path.Combine(publishDirectory, "script.dll");
49+
var pathToLibrary = Path.Combine(publishDirectory, "script.dll");
50+
51+
if (!TryCreateHash(executeOptions, out var hash) || !TryGetHash(publishDirectory, out var cachedHash))
52+
{
53+
return CreateLibrary();
54+
}
55+
56+
if (!string.Equals(hash,cachedHash))
57+
{
58+
return CreateLibrary();
59+
}
60+
61+
return pathToLibrary;
62+
63+
string CreateLibrary()
64+
{
65+
var options = new PublishCommandOptions(executeOptions.File,publishDirectory, "script", PublishType.Library,executeOptions.OptimizationLevel, null, executeOptions.NoCache);
66+
new PublishCommand(_scriptConsole, _logFactory).Execute(options);
67+
if (hash != null)
68+
{
69+
File.WriteAllText(Path.Combine(publishDirectory, "script.sha256"), hash);
70+
}
71+
return Path.Combine(publishDirectory, "script.dll");
72+
}
73+
}
74+
75+
public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
76+
{
77+
if (options.NoCache)
78+
{
79+
_logger.Debug($"The script {options.File.Path} was executed with the '--nocache' flag. Skipping cache.");
80+
hash = null;
81+
return false;
82+
}
83+
84+
var projectFolder = FileUtils.GetPathToTempFolder(Path.GetDirectoryName(options.File.Path));
85+
var pathToProjectFile = Path.Combine(projectFolder, "script.csproj");
86+
var projectFile = new ProjectFile(File.ReadAllText(pathToProjectFile));
87+
if (!projectFile.IsCacheable)
88+
{
89+
_logger.Warning($"The script {options.File.Path} is not cacheable. For caching and optimal performance, ensure that the script only contains NuGet references with pinned/exact versions.");
90+
hash = null;
91+
return false;
92+
}
93+
94+
var scriptFilesProvider = new ScriptFilesResolver();
95+
var allScriptFiles = scriptFilesProvider.GetScriptFiles(options.File.Path);
96+
IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
97+
foreach (var scriptFile in allScriptFiles)
98+
{
99+
incrementalHash.AppendData(File.ReadAllBytes(scriptFile));
100+
}
101+
102+
var configuration = options.OptimizationLevel.ToString();
103+
incrementalHash.AppendData(Encoding.UTF8.GetBytes(configuration));
104+
105+
hash = Convert.ToBase64String(incrementalHash.GetHashAndReset());
106+
return true;
107+
}
108+
109+
110+
public bool TryGetHash(string cacheFolder, out string hash)
111+
{
112+
if (!Directory.Exists(cacheFolder))
113+
{
114+
hash = null;
115+
return false;
116+
}
117+
118+
var pathToHashFile = Path.Combine(cacheFolder, "script.sha256");
119+
120+
if (!File.Exists(Path.Combine(cacheFolder, "script.sha256")))
121+
{
122+
hash = null;
123+
return false;
124+
}
125+
126+
hash = File.ReadAllText(pathToHashFile);
127+
return true;
49128
}
50129

51130
private async Task<TReturn> ExecuteLibrary<TReturn>(string pathToLibrary, string[] arguments, bool noCache)

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Dotnet.Script/Extensions.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,5 @@ public static bool ValueEquals(this CommandOption option, string value, StringCo
1212
if (option == null) throw new ArgumentNullException(nameof(option));
1313
return option.HasValue() && string.Equals(option.Value(), value, comparison);
1414
}
15-
16-
public static string GetRootedPath(this string path) => Path.IsPathRooted(path) ? path : Path.Combine(Directory.GetCurrentDirectory(), path);
17-
18-
public static SourceText ToSourceText(this string absoluteFilePath)
19-
{
20-
using (var filestream = File.OpenRead(absoluteFilePath))
21-
{
22-
return SourceText.From(filestream);
23-
}
24-
}
2515
}
2616
}

src/Dotnet.Script/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ private static int Wain(string[] args)
192192

193193
var scriptFile = new ScriptFile(file.Value);
194194
var optimizationLevel = configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug;
195+
var scriptArguments = app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray();
195196

196197
if (infoOption.HasValue())
197198
{
@@ -204,13 +205,13 @@ private static int Wain(string[] args)
204205
{
205206
if (interactive.HasValue())
206207
{
207-
return await RunInteractiveWithSeed(file.Value, logFactory, app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(), packageSources.Values?.ToArray());
208+
return await RunInteractiveWithSeed(file.Value, logFactory, scriptArguments, packageSources.Values?.ToArray());
208209
}
209210

210211
var fileCommandOptions = new ExecuteScriptCommandOptions
211212
(
212213
new ScriptFile(file.Value),
213-
app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(),
214+
scriptArguments,
214215
optimizationLevel,
215216
packageSources.Values?.ToArray(),
216217
interactive.HasValue(),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy