public ConsoleExecute(string commandPath, string arguments) { var resultLock = new object(); var result = new List <OutputItem>(); var process = new Process { StartInfo = { FileName = commandPath, Arguments = arguments, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, } }; var onOutput = new DataReceivedEventHandler(new Action <object, DataReceivedEventArgs>((sender, args) => { if (args != null && args.Data != null) { lock (resultLock) { result.Add(OutputItem.Standard(args.Data)); } } })); var onError = new DataReceivedEventHandler(new Action <object, DataReceivedEventArgs>((sender, args) => { if (args != null && args.Data != null) { lock (resultLock) { result.Add(new OutputItem(args.Data, OutputCategory.Error)); } } })); process.OutputDataReceived += onOutput; process.ErrorDataReceived += onError; process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.WaitForExit(); process.OutputDataReceived -= onOutput; process.ErrorDataReceived -= onError; this.OutputItems = result; this.ExitCode = process.ExitCode; }
public static IEnumerable <OutputItem> Create(CommandLineOptions commandLineOptions, SourceFile sourceFile) { var tempPath = Path.Combine(commandLineOptions.TempPath, Guid.NewGuid().ToString("D")); Directory.CreateDirectory(tempPath); var result = new List <OutputItem>(); var sourceFiles = sourceFile.GetAll().ToList(); var id = sourceFile.SourceConfigurationOptions.Single(x => x.OptionType == SourceConfigurationOptionType.Id).Value; var nuspecFilename = Path.Combine(tempPath, id + ".nuspec"); var version = sourceFile.SourceConfigurationOptions.Single(x => x.OptionType == SourceConfigurationOptionType.Version).Value; var nupkgFilename = Path.Combine(commandLineOptions.OutputPath, id + "." + version + ".nupkg"); if (!File.Exists(nupkgFilename) || new FileInfo(nupkgFilename).LastWriteTimeUtc <= sourceFile.LastWriteTimeUtc()) { var contentPath = sourceFile.SourceConfigurationOptions .SingleOrDefault(x => x.OptionType == SourceConfigurationOptionType.ContentPath); if (contentPath != null) { var fileElements = new List <XElement>(); sourceFiles.ForEach(x => { var tempFile = Path.Combine(tempPath, Path.GetFileName(x.RelativePath)); File.WriteAllLines(tempFile, x.Lines); fileElements.Add( new XElement( NuspecNamespace + "file", new XAttribute("src", tempFile), new XAttribute( "target", Path.Combine( new[] { "content", contentPath.Value, }.Where(o => o != null).ToArray())))); }); var optionElementArray = OptionsToElements(sourceFile.SourceConfigurationOptions).ToArray(); var document = new XDocument( new XDeclaration("1.0", "UTF-8", null), new XElement(NuspecNamespace + "package", new XElement(NuspecNamespace + "metadata", optionElementArray), new XElement(NuspecNamespace + "files", fileElements.ToArray()))); using (var stream = new FileStream(nuspecFilename, FileMode.Create)) using (var writer = new StreamWriter(stream, Encoding.UTF8)) { document.Save(writer); } if (commandLineOptions.Verbose) { result.AddRange(File.ReadAllLines(nuspecFilename).Select(OutputItem.Standard)); } // launch nuspec creator var nugetExe = commandLineOptions.NugetPath; if (!string.IsNullOrEmpty(nugetExe)) { string arguments = string.Format( CultureInfo.InvariantCulture, "pack \"{0}\" -OutputDirectory \"{1}\" -Verbosity detailed -BasePath \"{2}\"", nuspecFilename, commandLineOptions.OutputPath, sourceFile.BasePath); result.Add(OutputItem.Standard(arguments)); var nuspecProcess = new ConsoleExecute(nugetExe, arguments); if (nuspecProcess.ExitCode != 0 && !commandLineOptions.Verbose) { result.AddRange(File.ReadAllLines(nuspecFilename).Select(OutputItem.Standard)); } } } } Directory.Delete(tempPath, true); return(result); }