private static int Main(string[] args) { if (Console.IsOutputRedirected) { Console.OutputEncoding = Encoding.UTF8; } var app = new CommandLineApplication() { FullName = Resources.CommandFullName, Name = Resources.CommandFullName, }; new GetDocumentCommand().Configure(app); try { return(app.Execute(args)); } catch (Exception ex) { if (ex is CommandException || ex is CommandParsingException) { Reporter.WriteVerbose(ex.ToString()); } else { Reporter.WriteInformation(ex.ToString()); } Reporter.WriteError(ex.Message); return(1); } }
private static int Main(string[] args) { var app = new CommandLineApplication(throwOnUnexpectedArg: false) { FullName = Resources.CommandFullName, Name = Resources.CommandFullName, }; new InvokeCommand().Configure(app); try { return(app.Execute(args)); } catch (Exception ex) { if (ex is CommandException || ex is CommandParsingException) { Reporter.WriteVerbose(ex.ToString()); } else { Reporter.WriteInformation(ex.ToString()); } Reporter.WriteError(ex.Message); return(1); } }
public static int Run( string executable, IReadOnlyList <string> args, string workingDirectory = null, bool interceptOutput = false) { var arguments = ToArguments(args); Reporter.WriteVerbose(executable + " " + arguments); var startInfo = new ProcessStartInfo { FileName = executable, Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = interceptOutput }; if (workingDirectory != null) { startInfo.WorkingDirectory = workingDirectory; } using (var process = Process.Start(startInfo)) { if (interceptOutput) { string line; while ((line = process.StandardOutput.ReadLine()) != null) { Reporter.WriteVerbose(line); } } // Follow precedent set in Razor integration tests and ensure process events and output are complete. // https://github.com/aspnet/Razor/blob/d719920fdcc7d1db3a6f74cd5404d66fa098f057/test/Microsoft.NET.Sdk.Razor.Test/IntegrationTests/MSBuildProcessManager.cs#L91-L102 // Timeout is double how long the inside man waits for the IDocumentProcessor to wrap up. if (!process.WaitForExit((int)(TimeSpan.FromMinutes(2).TotalMilliseconds))) { process.Kill(); // Should be unreachable in almost every case. throw new TimeoutException($"Process {executable} timed out after 2 minutes."); } process.WaitForExit(); return(process.ExitCode); } }
public static int Run( string executable, IReadOnlyList <string> args, string workingDirectory = null, bool interceptOutput = false) { var arguments = ToArguments(args); Reporter.WriteVerbose(executable + " " + arguments); var startInfo = new ProcessStartInfo { FileName = executable, Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = interceptOutput }; if (workingDirectory != null) { startInfo.WorkingDirectory = workingDirectory; } var process = Process.Start(startInfo); if (interceptOutput) { string line; while ((line = process.StandardOutput.ReadLine()) != null) { Reporter.WriteVerbose(line); } } process.WaitForExit(); return(process.ExitCode); }
public static Project FromFile( string projectFile, string buildExtensionsDirectory, string framework = null, string configuration = null, string runtime = null) { if (string.IsNullOrEmpty(projectFile)) { throw new ArgumentNullException(nameof(projectFile)); } if (string.IsNullOrEmpty(buildExtensionsDirectory)) { buildExtensionsDirectory = Path.Combine(Path.GetDirectoryName(projectFile), "obj"); } IODirectory.CreateDirectory(buildExtensionsDirectory); var assembly = typeof(Project).Assembly; var targetsPath = Path.Combine( buildExtensionsDirectory, $"{Path.GetFileName(projectFile)}.{ResourceFilename}"); using (var input = assembly.GetManifestResourceStream(MSBuildResourceName)) { using (var output = File.OpenWrite(targetsPath)) { // NB: Copy always in case it changes Reporter.WriteVerbose(Resources.FormatWritingFile(targetsPath)); input.CopyTo(output); output.Flush(); } } IDictionary <string, string> metadata; var metadataPath = Path.GetTempFileName(); try { var args = new List <string> { "msbuild", "/target:WriteServiceProjectReferenceMetadata", "/verbosity:quiet", "/nologo", "/nodeReuse:false", $"/property:ServiceProjectReferenceMetadataPath={metadataPath}", projectFile, }; if (!string.IsNullOrEmpty(framework)) { args.Add($"/property:TargetFramework={framework}"); } if (!string.IsNullOrEmpty(configuration)) { args.Add($"/property:Configuration={configuration}"); } if (!string.IsNullOrEmpty(runtime)) { args.Add($"/property:RuntimeIdentifier={runtime}"); } var exitCode = Exe.Run("dotnet", args); if (exitCode != 0) { throw new CommandException(Resources.GetMetadataFailed); } metadata = File .ReadLines(metadataPath) .Select(l => l.Split(new[] { ':' }, 2)) .ToDictionary(s => s[0], s => s[1].TrimStart()); } finally { // Ignore errors about in-use files. Should still be marked for delete after process cleanup. try { File.Delete(metadataPath); } catch (UnauthorizedAccessException) { } try { File.Delete(targetsPath); } catch (UnauthorizedAccessException) { } } var project = new Project { DefaultDocumentName = metadata[nameof(DefaultDocumentName)], DefaultMethod = metadata[nameof(DefaultMethod)], DefaultService = metadata[nameof(DefaultService)], AssemblyName = metadata[nameof(AssemblyName)], Configuration = metadata[nameof(Configuration)], OutputPath = metadata[nameof(OutputPath)], Platform = metadata[nameof(Platform)], PlatformTarget = metadata[nameof(PlatformTarget)] ?? metadata[nameof(Platform)], ProjectAssetsFile = metadata[nameof(ProjectAssetsFile)], ProjectDepsFilePath = metadata[nameof(ProjectDepsFilePath)], ProjectDirectory = metadata[nameof(ProjectDirectory)], ProjectExtensionsPath = metadata[nameof(ProjectExtensionsPath)], ProjectName = metadata[nameof(ProjectName)], ProjectRuntimeConfigFilePath = metadata[nameof(ProjectRuntimeConfigFilePath)], RuntimeFrameworkVersion = metadata[nameof(RuntimeFrameworkVersion)], RuntimeIdentifier = metadata[nameof(RuntimeIdentifier)], TargetFramework = metadata[nameof(TargetFramework)], TargetFrameworkMoniker = metadata[nameof(TargetFrameworkMoniker)], TargetPath = metadata[nameof(TargetPath)], }; if (string.IsNullOrEmpty(project.OutputPath)) { throw new CommandException( Resources.FormatGetMetadataValueFailed(nameof(OutputPath), nameof(OutputPath))); } if (string.IsNullOrEmpty(project.ProjectDirectory)) { throw new CommandException( Resources.FormatGetMetadataValueFailed(nameof(ProjectDirectory), "MSBuildProjectDirectory")); } if (string.IsNullOrEmpty(project.TargetPath)) { throw new CommandException( Resources.FormatGetMetadataValueFailed(nameof(TargetPath), nameof(TargetPath))); } if (!Path.IsPathRooted(project.ProjectDirectory)) { project.OutputPath = Path.GetFullPath( Path.Combine(IODirectory.GetCurrentDirectory(), project.ProjectDirectory)); } if (!Path.IsPathRooted(project.OutputPath)) { project.OutputPath = Path.GetFullPath(Path.Combine(project.ProjectDirectory, project.OutputPath)); } if (!Path.IsPathRooted(project.ProjectExtensionsPath)) { project.ProjectExtensionsPath = Path.GetFullPath( Path.Combine(project.ProjectDirectory, project.ProjectExtensionsPath)); } if (!Path.IsPathRooted(project.TargetPath)) { project.TargetPath = Path.GetFullPath(Path.Combine(project.OutputPath, project.TargetPath)); } // Some document generation tools support non-ASP.NET Core projects. Any of the remaining properties may // thus be null empty. var configPath = $"{project.TargetPath}.config"; if (File.Exists(configPath)) { project.ConfigPath = configPath; } if (!(string.IsNullOrEmpty(project.ProjectAssetsFile) || Path.IsPathRooted(project.ProjectAssetsFile))) { project.ProjectAssetsFile = Path.GetFullPath( Path.Combine(project.ProjectDirectory, project.ProjectAssetsFile)); } if (!(string.IsNullOrEmpty(project.ProjectDepsFilePath) || Path.IsPathRooted(project.ProjectDepsFilePath))) { project.ProjectDepsFilePath = Path.GetFullPath( Path.Combine(project.ProjectDirectory, project.ProjectDepsFilePath)); } if (!(string.IsNullOrEmpty(project.ProjectRuntimeConfigFilePath) || Path.IsPathRooted(project.ProjectRuntimeConfigFilePath))) { project.ProjectRuntimeConfigFilePath = Path.GetFullPath( Path.Combine(project.OutputPath, project.ProjectRuntimeConfigFilePath)); } return(project); }