Exemple #1
0
        public void CreateExecutable <TReturn, THost>(ScriptContext context, LogFactory logFactory, string runtimeIdentifier)
        {
            if (runtimeIdentifier == null)
            {
                throw new ArgumentNullException(nameof(runtimeIdentifier));
            }

            const string AssemblyName = "scriptAssembly";

            var tempProjectPath     = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath));
            var tempProjectDirecory = Path.GetDirectoryName(tempProjectPath);

            var scriptAssemblyPath = CreateScriptAssembly <TReturn, THost>(context, tempProjectDirecory, AssemblyName);
            var projectFile        = new ProjectFile(File.ReadAllText(tempProjectPath));

            projectFile.AddPackageReference(new PackageReference("Microsoft.CodeAnalysis.Scripting", ScriptingVersion, PackageOrigin.ReferenceDirective));
            projectFile.AddAssemblyReference(scriptAssemblyPath);
            projectFile.Save(tempProjectPath);

            CopyProgramTemplate(tempProjectDirecory);

            var commandRunner = new CommandRunner(logFactory);
            // todo: may want to add ability to return dotnet.exe errors
            var exitcode = commandRunner.Execute("dotnet", $"publish \"{tempProjectPath}\" -c Release -r {runtimeIdentifier} -o {context.WorkingDirectory}");

            if (exitcode != 0)
            {
                throw new Exception($"dotnet publish failed with result '{exitcode}'");
            }

            _scriptConsole.WriteSuccess($"Published {context.FilePath} (executable) to {context.WorkingDirectory}");
        }
Exemple #2
0
        public void CreateExecutable(ScriptContext context, LogFactory logFactory)
        {
            var tempProjectPath     = _scriptProjectProvider.CreateProjectForScriptFile(context.FilePath);
            var tempProjectDirecory = Path.GetDirectoryName(tempProjectPath);

            var scriptAssemblyPath = CreateScriptAssembly(context, tempProjectDirecory);

            var projectFile = new ProjectFile(File.ReadAllText(tempProjectPath));

            projectFile.AddPackageReference(new PackageReference("Microsoft.CodeAnalysis.Scripting", ScriptingVersion, PackageOrigin.ReferenceDirective));
            projectFile.AddAssemblyReference(scriptAssemblyPath);
            projectFile.Save(tempProjectPath);

            CopyProgramTemplate(tempProjectDirecory);

            var runtimeIdentifier = _scriptEnvironment.RuntimeIdentifier;

            var commandRunner = new CommandRunner(logFactory);
            // todo: may want to add ability to return dotnet.exe errors
            var exitcode = commandRunner.Execute("dotnet", $"publish \"{tempProjectPath}\" -c Release -r {runtimeIdentifier} -o {context.WorkingDirectory}");

            if (exitcode != 0)
            {
                throw new Exception($"dotnet publish failed with result '{exitcode}'");
            }
        }