Ejemplo n.º 1
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"));
            }
        }
Ejemplo n.º 2
0
        public void Execute(PublishCommandOptions options)
        {
            var absoluteFilePath = options.File.Path;

            // if a publish directory has been specified, then it is used directly, otherwise:
            // -- for EXE {current dir}/publish/{runtime ID}
            // -- for DLL {current dir}/publish
            var publishDirectory = options.OutputDirectory ??
                                   (options.PublishType == PublishType.Library ? Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish") : Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish", options.RuntimeIdentifier));

            var absolutePublishDirectory = publishDirectory.GetRootedPath();
            var compiler      = GetScriptCompiler(!options.NoCache, _logFactory);
            var scriptEmitter = new ScriptEmitter(_scriptConsole, compiler);
            var publisher     = new ScriptPublisher(_logFactory, scriptEmitter);
            var code          = absoluteFilePath.ToSourceText();
            var context       = new ScriptContext(code, absolutePublishDirectory, Enumerable.Empty <string>(), absoluteFilePath, options.OptimizationLevel, packageSources: options.PackageSources);

            if (options.PublishType == PublishType.Library)
            {
                publisher.CreateAssembly <int, CommandLineScriptGlobals>(context, _logFactory, options.LibraryName);
            }
            else
            {
                publisher.CreateExecutable <int, CommandLineScriptGlobals>(context, _logFactory, options.RuntimeIdentifier);
            }
        }
Ejemplo 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"));
        }