Exemplo n.º 1
0
        public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
        {
            if (options.NoCache)
            {
                _logger.Debug($"The script {options.File.Path} was executed with the '--no-cache' flag. Skipping cache.");
                hash = null;
                return(false);
            }

            var scriptFilesProvider = new ScriptFilesResolver();
            var allScriptFiles      = scriptFilesProvider.GetScriptFiles(options.File.Path);
            var projectFile         = new ScriptProjectProvider(_logFactory).CreateProjectFileFromScriptFiles(ScriptEnvironment.Default.TargetFramework, allScriptFiles.ToArray());

            if (!projectFile.IsCacheable)
            {
                _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.");
                hash = null;
                return(false);
            }


            IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);

            foreach (var scriptFile in allScriptFiles)
            {
                incrementalHash.AppendData(File.ReadAllBytes(scriptFile));
            }

            var configuration = options.OptimizationLevel.ToString();

            incrementalHash.AppendData(Encoding.UTF8.GetBytes(configuration));

            hash = Convert.ToBase64String(incrementalHash.GetHashAndReset());
            return(true);
        }
Exemplo n.º 2
0
        private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
        {
            var projectFolder        = FileUtils.GetPathToTempFolder(Path.GetDirectoryName(executeOptions.File.Path));
            var executionCacheFolder = Path.Combine(projectFolder, "execution-cache");
            var pathToLibrary        = Path.Combine(executionCacheFolder, "script.dll");

            if (!TryCreateHash(executeOptions, out var hash) || !TryGetHash(executionCacheFolder, out var cachedHash))
            {
                return(CreateLibrary());
            }

            if (!string.Equals(hash, cachedHash))
            {
                return(CreateLibrary());
            }

            return(pathToLibrary);

            string CreateLibrary()
            {
                var options = new PublishCommandOptions(executeOptions.File, executionCacheFolder, "script", PublishType.Library, executeOptions.OptimizationLevel, executeOptions.PackageSources, null, executeOptions.NoCache);

                new PublishCommand(_scriptConsole, _logFactory).Execute(options);
                if (hash != null)
                {
                    File.WriteAllText(Path.Combine(executionCacheFolder, "script.sha256"), hash);
                }
                return(Path.Combine(executionCacheFolder, "script.dll"));
            }
        }
Exemplo n.º 3
0
        private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
        {
            var projectFolder        = FileUtils.GetPathToScriptTempFolder(executeOptions.File.Path);
            var executionCacheFolder = Path.Combine(projectFolder, "execution-cache");
            var pathToLibrary        = Path.Combine(executionCacheFolder, "script.dll");

            if (TryCreateHash(executeOptions, out var hash) &&
                TryGetHash(executionCacheFolder, out var cachedHash) &&
                string.Equals(hash, cachedHash))
            {
                _logger.Debug($"Using cached compilation: " + pathToLibrary);
                return(pathToLibrary);
            }

            var options = new PublishCommandOptions(executeOptions.File, executionCacheFolder, "script", PublishType.Library, executeOptions.OptimizationLevel, executeOptions.PackageSources, null, executeOptions.NoCache)
            {
#if NETCOREAPP
                AssemblyLoadContext = executeOptions.AssemblyLoadContext
#endif
            };

            new PublishCommand(_scriptConsole, _logFactory).Execute(options);
            if (hash != null)
            {
                File.WriteAllText(Path.Combine(executionCacheFolder, "script.sha256"), hash);
            }
            return(Path.Combine(executionCacheFolder, "script.dll"));
        }
Exemplo n.º 4
0
        private async Task <TReturn> DownloadAndRunCode <TReturn>(ExecuteScriptCommandOptions executeOptions)
        {
            var downloader = new ScriptDownloader();
            var code       = await downloader.Download(executeOptions.File.Path);

            var options = new ExecuteCodeCommandOptions(code, Directory.GetCurrentDirectory(), executeOptions.Arguments, executeOptions.OptimizationLevel, executeOptions.NoCache, executeOptions.PackageSources);

            return(await new ExecuteCodeCommand(_scriptConsole, _logFactory).Execute <TReturn>(options));
        }
Exemplo n.º 5
0
        public async Task <TReturn> Run <TReturn, THost>(ExecuteScriptCommandOptions options)
        {
            if (options.File.IsRemote)
            {
                return(await DownloadAndRunCode <TReturn>(options));
            }

            var pathToLibrary = GetLibrary(options);

            return(await ExecuteLibrary <TReturn>(pathToLibrary, options.Arguments, options.NoCache));
        }
Exemplo n.º 6
0
        public async Task <TReturn> Run <TReturn, THost>(ExecuteScriptCommandOptions options)
        {
            if (options.File.IsRemote)
            {
                return(await DownloadAndRunCode <TReturn>(options));
            }

            var pathToLibrary = GetLibrary(options);

            var libraryOptions = new ExecuteLibraryCommandOptions(pathToLibrary, options.Arguments, options.NoCache)
            {
#if NETCOREAPP
                AssemblyLoadContext = options.AssemblyLoadContext
#endif
            };

            return(await new ExecuteLibraryCommand(_scriptConsole, _logFactory).Execute <TReturn>(libraryOptions));
        }