public void CreateExecutable <TReturn, THost>(ScriptContext context, LogFactory logFactory, string runtimeIdentifier, string executableFileName = null) { if (runtimeIdentifier == null) { throw new ArgumentNullException(nameof(runtimeIdentifier)); } executableFileName = executableFileName ?? Path.GetFileNameWithoutExtension(context.FilePath); const string AssemblyName = "scriptAssembly"; var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), ScriptEnvironment.Default.TargetFramework); var renamedProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), ScriptEnvironment.Default.TargetFramework, executableFileName); var tempProjectDirectory = Path.GetDirectoryName(tempProjectPath); var scriptAssemblyPath = CreateScriptAssembly <TReturn, THost>(context, tempProjectDirectory, AssemblyName); var projectFile = new ProjectFile(File.ReadAllText(tempProjectPath)); projectFile.PackageReferences.Add(new PackageReference("Microsoft.CodeAnalysis.Scripting", ScriptingVersion)); projectFile.AssemblyReferences.Add(new AssemblyReference(scriptAssemblyPath)); projectFile.Save(renamedProjectPath); CopyProgramTemplate(tempProjectDirectory); var commandRunner = new CommandRunner(logFactory); // todo: may want to add ability to return dotnet.exe errors var exitcode = commandRunner.Execute("dotnet", $"publish \"{renamedProjectPath}\" -c Release -r {runtimeIdentifier} -o \"{context.WorkingDirectory}\" {(ScriptEnvironment.Default.TargetFramework == "netcoreapp3.0" ? "/p:PublishSingleFile=true" : "")} /p:DebugType=Embedded"); if (exitcode != 0) { throw new Exception($"dotnet publish failed with result '{exitcode}'"); } _scriptConsole.WriteSuccess($"Published {context.FilePath} (executable) to {context.WorkingDirectory}"); }
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 tempProjectDirectory = Path.GetDirectoryName(tempProjectPath); var scriptAssemblyPath = CreateScriptAssembly <TReturn, THost>(context, tempProjectDirectory, AssemblyName); var projectFile = new ProjectFile(File.ReadAllText(tempProjectPath)); projectFile.PackageReferences.Add(new PackageReference("Microsoft.CodeAnalysis.Scripting", ScriptingVersion)); projectFile.AssemblyReferences.Add(new AssemblyReference(scriptAssemblyPath)); projectFile.Save(tempProjectPath); CopyProgramTemplate(tempProjectDirectory); 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}"); }
public void CreateAssembly <TReturn, THost>(ScriptContext context, LogFactory logFactory, string assemblyFileName = null) { Directory.CreateDirectory(context.WorkingDirectory); Directory.CreateDirectory(Path.Combine(context.WorkingDirectory, "obj")); assemblyFileName = assemblyFileName ?? Path.GetFileNameWithoutExtension(context.FilePath); var scriptAssemblyPath = CreateScriptAssembly <TReturn, THost>(context, context.WorkingDirectory, assemblyFileName); var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), ScriptEnvironment.Default.TargetFramework); var tempProjectDirecory = Path.GetDirectoryName(tempProjectPath); var sourceProjectAssetsPath = Path.Combine(tempProjectDirecory, "obj", "project.assets.json"); var destinationProjectAssetsPath = Path.Combine(context.WorkingDirectory, "obj", "project.assets.json"); File.Copy(sourceProjectAssetsPath, destinationProjectAssetsPath, overwrite: true); var sourceNugetPropsPath = Path.Combine(tempProjectDirecory, "obj", "script.csproj.nuget.g.props"); var destinationNugetPropsPath = Path.Combine(context.WorkingDirectory, "obj", "script.csproj.nuget.g.props"); File.Copy(sourceNugetPropsPath, destinationNugetPropsPath, overwrite: true); // only display published if we aren't auto publishing to temp folder if (!scriptAssemblyPath.StartsWith(FileUtils.GetTempPath())) { _scriptConsole.WriteSuccess($"Published {context.FilePath} to { scriptAssemblyPath}"); } }
public void CreateAssembly <TReturn, THost>(ScriptContext context, LogFactory logFactory, string assemblyFileName = null) { Directory.CreateDirectory(context.WorkingDirectory); Directory.CreateDirectory(Path.Combine(context.WorkingDirectory, "obj")); assemblyFileName = assemblyFileName ?? Path.GetFileNameWithoutExtension(context.FilePath); CreateScriptAssembly <TReturn, THost>(context, context.WorkingDirectory, assemblyFileName); var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath)); var tempProjectDirecory = Path.GetDirectoryName(tempProjectPath); var sourceProjectAssetsPath = Path.Combine(tempProjectDirecory, "obj", "project.assets.json"); var destinationProjectAssetsPath = Path.Combine(context.WorkingDirectory, "obj", "project.assets.json"); File.Copy(sourceProjectAssetsPath, destinationProjectAssetsPath, overwrite: true); var sourceNugetPropsPath = Path.Combine(tempProjectDirecory, "obj", "script.csproj.nuget.g.props"); var destinationNugetPropsPath = Path.Combine(context.WorkingDirectory, "obj", "script.csproj.nuget.g.props"); File.Copy(sourceNugetPropsPath, destinationNugetPropsPath, overwrite: true); }