Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Tools/Google.Build.Utils/Runner.cs

Issue 12767046: Issue 377: New build for releasing a new version (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Patch Set: david comments Created 10 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 Copyright 2011 Google Inc
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Diagnostics;
20 using System.IO;
21 using System.Linq;
22 using System.Runtime.InteropServices;
23 using System.Threading;
24 using Google.Apis.Samples.Helper;
25
26 namespace Google.Build.Utils
27 {
28 /// <summary>
29 /// Process Runner.
30 /// </summary>
31 public class Runner
32 {
33 /// <summary>
34 /// The command or application to run.
35 /// </summary>
36 public string Command { get; private set; }
37
38 /// <summary>
39 /// The arguments which are passed to the application.
40 /// </summary>
41 public string Arguments { get; private set; }
42
43 /// <summary>
44 /// The working directory of the application.
45 /// </summary>
46 public string WorkingDirectory { get; set; }
47
48 public Runner(string command, params string[] args)
49 {
50 Command = command;
51 Arguments = args.Select(arg => "\"" arg "\"").Aggregate((a, b) = > a " " b);
52 }
53
54 /// <summary>
55 /// Runs the process in a seperate shell window.
56 /// </summary>
57 public void RunShell()
58 {
59 RunInternal(setup => {}, process => {});
60 }
61 ········
62 /// <summary>
63 /// Runs the process and redirects the output into the current Console.
64 /// </summary>
65 public void Run()
66 {
67 string fileName = Path.GetFileNameWithoutExtension(Command);
68 object sync = new object();
69 RunInternal((isError, msg) =>
70 {
71 lock (sync)
72 {
73 if (isError)
74 {
75 msg = "{red}" msg;
76 }
77 CommandLine.WriteResult(fileName, msg);
78 }
79 });
80 }
81
82 /// <summary>
83 /// Runs the process and returns the output.
84 /// </summary>
85 /// <returns>All messages of the process.</returns>
86 public string[] RunAndListen()
87 {
88 var msgs = new List<string>();
89 RunInternal((isError, msg) => msgs.Add(msg));
90 return msgs.ToArray();
91 }
92
93 private void RunInternal(Action<bool,string> msg)
94 {
95 Action<bool, DataReceivedEventArgs> msgHandler = (isError, data) =>
96 {
97 if (data.Da ta != null)
98 {
99 msg(isE rror, data.Data);
100 }
101 };
102
103 RunInternal(
104 info =>
105 {
106 info.RedirectStandardError = true;
107 info.RedirectStandardOutput = true;
108 info.CreateNoWindow = true;
109 info.UseShellExecute = false;
110 }, process =>
111 {
112 process.ErrorDataReceived = (sender, data) => ms gHandler(true, data);
113 process.OutputDataReceived = (sender, data) => m sgHandler(false, data);
114 process.BeginErrorReadLine();
115 process.BeginOutputReadLine();
116 });
117 }
118
119 private void RunInternal(Action<ProcessStartInfo> setupProcess, Action<P rocess> afterStart)
120 {
121 // Setup the process.
122 var startInfo = new ProcessStartInfo(Command, Arguments);
123 startInfo.WorkingDirectory = WorkingDirectory;
124 setupProcess(startInfo);
125 ············
126 // Launch the process.
127 Process process = Process.Start(startInfo);
128 afterStart(process);
129
130 // Wait for exit.
131 process.WaitForExit();
132 Thread.Sleep(0);
133 if (process.ExitCode != 0)
134 {
135 string cmdLine = process.StartInfo.FileName " " process.Star tInfo.Arguments;
136 throw new ExternalException("The process '" cmdLine "' exite d with errors.", process.ExitCode);
137 }
138 }
139 }
140 }
OLDNEW
« no previous file with comments | « Tools/Google.Build.Utils/Properties/AssemblyInfo.cs ('k') | Tools/Google.Build.Utils/Text/Template.cs » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b