Skip to content

Commit

Permalink
(cake-buildGH-3879)(cake-buildGH-3880) Scope cache per script host
Browse files Browse the repository at this point in the history
* Include script host in the cache filename
* Move hash from file to the cache filename
* fixes cake-build#3879
* fixes cake-build#3880
  • Loading branch information
devlead committed Apr 29, 2022
1 parent a5e35e5 commit c1044eb
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/Cake/Infrastructure/Scripting/RoslynScriptSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,17 @@ public void ImportNamespace(string @namespace)
public void Execute(Script script)
{
var scriptName = _settings.Script.GetFilename();
var cacheDLLFileName = $"{scriptName}.dll";
var cacheHashFileName = $"{scriptName}.hash";
var cachedAssembly = _scriptCachePath.CombineWithFilePath(cacheDLLFileName);
var hashFile = _scriptCachePath.CombineWithFilePath(cacheHashFileName);
string scriptHash = default;
FilePath cachedAssembly = _scriptCacheEnabled
? GetCachedAssemblyPath(script, scriptName)
: default;

if (_scriptCacheEnabled && _fileSystem.Exist(cachedAssembly) && !_regenerateCache)
{
_log.Verbose($"Cache enabled: Checking cache build script ({cacheDLLFileName})");
scriptHash = FastHash.GenerateHash(Encoding.UTF8.GetBytes(string.Concat(script.Lines)));
var cachedHash = _fileSystem.Exist(hashFile)
? _fileSystem.GetFile(hashFile).ReadLines(Encoding.UTF8).FirstOrDefault()
: string.Empty;
if (scriptHash.Equals(cachedHash, StringComparison.Ordinal))
{
_log.Verbose("Running cached build script...");
RunScriptAssembly(cachedAssembly.FullPath);
return;
}
else
{
_log.Verbose("Cache check failed.");
}
_log.Verbose("Running cached build script...");
RunScriptAssembly(cachedAssembly.FullPath);
return;
}

// Generate the script code.
var generator = new RoslynCodeGenerator();
var code = generator.Generate(script);
Expand Down Expand Up @@ -205,19 +193,11 @@ public void Execute(Script script)
{
_fileSystem.GetDirectory(_scriptCachePath).Create();
}
if (string.IsNullOrWhiteSpace(scriptHash))
{
scriptHash = FastHash.GenerateHash(Encoding.UTF8.GetBytes(string.Concat(script.Lines)));
}

var emitResult = compilation.Emit(cachedAssembly.FullPath);

if (emitResult.Success)
{
using (var stream = _fileSystem.GetFile(hashFile).OpenWrite())
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
writer.Write(scriptHash);
}
RunScriptAssembly(cachedAssembly.FullPath);
}
}
Expand All @@ -227,6 +207,15 @@ public void Execute(Script script)
}
}

private FilePath GetCachedAssemblyPath(Script script, FilePath scriptName)
=> _scriptCachePath.CombineWithFilePath(
string.Join(
'_',
scriptName.GetFilenameWithoutExtension().FullPath,
_host.GetType().Name,
FastHash.GenerateHash(Encoding.UTF8.GetBytes(string.Concat(script.Lines))),
".dll"));

private void RunScriptAssembly(string assemblyPath)
{
var assembly = _loader.Load(assemblyPath, false);
Expand Down

0 comments on commit c1044eb

Please sign in to comment.