<commit_before>using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Dotnet.Script.DependencyModel.Environment;
using Dotnet.Script.DependencyModel.Logging;
using Dotnet.Script.DependencyModel.ProjectSystem;

namespace Dotnet.Script.Core.Commands
{
    public class ExecuteScriptCommand
    {
        private readonly ScriptConsole _scriptConsole;
        private readonly LogFactory _logFactory;
        private readonly Logger _logger;

        public ExecuteScriptCommand(ScriptConsole scriptConsole, LogFactory logFactory)
        {
            _scriptConsole = scriptConsole;
            _logFactory = logFactory;
            _logger = logFactory.CreateLogger<ExecuteScriptCommand>();

        }

        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);
        }

        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);
        }

        private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
        {
            var projectFolder = FileUtils.GetPathToScriptTempFolder(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");
            }
        }

        public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
        {
            if (options.NoCache)<commit_msg>Simplify cache check<commit_after>using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Dotnet.Script.DependencyModel.Environment;
using Dotnet.Script.DependencyModel.Logging;
using Dotnet.Script.DependencyModel.ProjectSystem;

namespace Dotnet.Script.Core.Commands
{
    public class ExecuteScriptCommand
    {
        private readonly ScriptConsole _scriptConsole;
        private readonly LogFactory _logFactory;
        private readonly Logger _logger;

        public ExecuteScriptCommand(ScriptConsole scriptConsole, LogFactory logFactory)
        {
            _scriptConsole = scriptConsole;
            _logFactory = logFactory;
            _logger = logFactory.CreateLogger<ExecuteScriptCommand>();

        }

        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);
        }

        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);
        }

        private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
        {
            var projectFolder = FileUtils.GetPathToScriptTempFolder(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)
                && string.Equals(hash, cachedHash))
            {
                return pathToLibrary;
            }

            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");
        }

        public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
        {
            if (options.NoCache)
{'fraction_non_alphanumeric': 0.07770216806456717, 'fraction_numerical': 0.0009495173286912486, 'mean_word_length': 2.827983040581466, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1367938', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1483, 'n_words': 312}