Beispiel #1
0
        private string CreateScriptAssembly <TReturn, THost>(ScriptContext context, string outputDirectory, string assemblyFileName)
        {
            var emitResult   = _scriptEmitter.Emit <TReturn, THost>(context);
            var assemblyPath = Path.Combine(outputDirectory, $"{assemblyFileName}.dll");

            using (var peFileStream = new FileStream(assemblyPath, FileMode.Create))
                using (emitResult.PeStream)
                {
                    emitResult.PeStream.WriteTo(peFileStream);
                }

            foreach (var reference in emitResult.DirectiveReferences)
            {
                if (reference.Display.EndsWith(".NuGet.dll"))
                {
                    continue;
                }

                var referenceFileInfo     = new FileInfo(reference.Display);
                var fullPathToReference   = Path.GetFullPath(referenceFileInfo.FullName);
                var fullPathToNewAssembly = Path.GetFullPath(Path.Combine(outputDirectory, referenceFileInfo.Name));

                if (!Equals(fullPathToReference, fullPathToNewAssembly))
                {
                    File.Copy(fullPathToReference, fullPathToNewAssembly, true);
                }
            }

            /*  The following is needed to make native assets work.
             *  During a regular "dotnet publish" we find these assets in a "runtimes" folder.
             *  We must copy these binaries up to the same folder as the script library so
             *  that they can be found during execution.
             */
            foreach (var runtimeDependency in emitResult.RuntimeDependencies)
            {
                if (!runtimeDependency.Name.Contains("microsoft.netcore", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (var nativeAsset in runtimeDependency.NativeAssets)
                    {
                        File.Copy(nativeAsset, Path.Combine(outputDirectory, Path.GetFileName(nativeAsset)), true);
                    }
                    foreach (var runtimeAssembly in runtimeDependency.Assemblies)
                    {
                        File.Copy(runtimeAssembly.Path, Path.Combine(outputDirectory, Path.GetFileName(runtimeAssembly.Path)), true);
                        var pathToRuntimeAssemblyFolder = Path.GetDirectoryName(runtimeAssembly.Path);
                        var pdbFileName = $"{Path.GetFileNameWithoutExtension(runtimeAssembly.Path)}.pdb";
                        var pathToPdb   = Path.Combine(pathToRuntimeAssemblyFolder, pdbFileName);
                        if (File.Exists(pathToPdb))
                        {
                            File.Copy(pathToPdb, Path.Combine(outputDirectory, Path.GetFileName(pathToPdb)), true);
                        }
                    }
                }
            }

            return(assemblyPath);
        }
Beispiel #2
0
        private string CreateScriptAssembly(ScriptContext context, string tempProjectDirecory)
        {
            try
            {
                var emitResult = _scriptEmitter.Emit <int>(context, AssemblyName);
                if (!emitResult.Success)
                {
                    throw new CompilationErrorException("One or more errors occurred when emitting the assembly", emitResult.Diagnostics);
                }

                var assemblyPath = Path.Combine(tempProjectDirecory, $"{AssemblyName}.dll");
                using (var peFileStream = new FileStream(assemblyPath, FileMode.Create))
                    using (emitResult.PeStream)
                    {
                        emitResult.PeStream.WriteTo(peFileStream);
                    }

                var pdbPath = Path.Combine(tempProjectDirecory, $"{AssemblyName}.pdb");
                using (var pdbFileStream = new FileStream(pdbPath, FileMode.Create))
                    using (emitResult.PdbStream)
                    {
                        emitResult.PdbStream.WriteTo(pdbFileStream);
                    }

                foreach (var reference in emitResult.DirectiveReferences)
                {
                    if (reference.Display.EndsWith(".NuGet.dll"))
                    {
                        continue;
                    }
                    var refInfo         = new FileInfo(reference.Display);
                    var newAssemblyPath = Path.Combine(tempProjectDirecory, refInfo.Name);
                    File.Copy(refInfo.FullName, newAssemblyPath, true);
                }

                return(assemblyPath);
            }
            catch (CompilationErrorException ex)
            {
                _scriptConsole.WritePrettyError(ex.Message);
                foreach (var diagnostic in ex.Diagnostics)
                {
                    _scriptConsole.WritePrettyError(diagnostic.ToString());
                }
                throw;
            }
        }
Beispiel #3
0
        private string CreateScriptAssembly <TReturn, THost>(ScriptContext context, string outputDirectory, string assemblyFileName)
        {
            try
            {
                var emitResult = _scriptEmitter.Emit <TReturn, THost>(context);
                if (!emitResult.Success)
                {
                    throw new CompilationErrorException("One or more errors occurred when emitting the assembly", emitResult.Diagnostics);
                }

                var assemblyPath = Path.Combine(outputDirectory, $"{assemblyFileName}.dll");
                using (var peFileStream = new FileStream(assemblyPath, FileMode.Create))
                    using (emitResult.PeStream)
                    {
                        emitResult.PeStream.WriteTo(peFileStream);
                    }

                foreach (var reference in emitResult.DirectiveReferences)
                {
                    if (reference.Display.EndsWith(".NuGet.dll"))
                    {
                        continue;
                    }

                    var referenceFileInfo     = new FileInfo(reference.Display);
                    var fullPathToReference   = Path.GetFullPath(referenceFileInfo.FullName);
                    var fullPathToNewAssembly = Path.GetFullPath(Path.Combine(outputDirectory, referenceFileInfo.Name));

                    if (!Equals(fullPathToReference, fullPathToNewAssembly))
                    {
                        File.Copy(fullPathToReference, fullPathToNewAssembly, true);
                    }
                }

                return(assemblyPath);
            }
            catch (CompilationErrorException ex)
            {
                _scriptConsole.WriteError(ex.Message);
                foreach (var diagnostic in ex.Diagnostics)
                {
                    _scriptConsole.WriteError(diagnostic.ToString());
                }
                throw;
            }
        }