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

Side by Side Diff: Tools/Google.Build.Utils/Build/Project.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, 6 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
« no previous file with comments | « Tools/Google.Build.Tester/app.config ('k') | Tools/Google.Build.Utils/DirUtils.cs » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 Google.Apis.Samples.Helper;
23 using Microsoft.Build.Evaluation;
24 using Microsoft.Build.Framework;
25 using ConsoleLogger = Microsoft.Build.BuildEngine.ConsoleLogger;
26
27 namespace Google.Build.Utils.Build
28 {
29 /// <summary>
30 /// Represents a buildable project.
31 /// </summary>
32 public class Project
33 {
34 /// <summary>
35 /// The working directory in which the project file resides.
36 /// </summary>
37 public string WorkingDirectory { get; private set; }
38
39 /// <summary>
40 /// The .csproj project file.
41 /// </summary>
42 public string ProjectFile { get; private set; }
43
44 /// <summary>
45 /// The Configuration to build. Default: "Release"
46 /// </summary>
47 public string Configuration
48 {
49 get { return internalProject.GetProperty("Configuration").EvaluatedV alue; }
50 set { internalProject.SetProperty("Configuration", value); }
51 }
52
53 /// <summary>
54 /// Path to the generated binary file.
55 /// </summary>
56 public string BinaryFile
57 {
58 get { return internalProject.GetPropertyValue("TargetPath"); }
59 }
60
61 /// <summary>
62 /// The simple name of this project.
63 /// </summary>
64 public string Name
65 {
66 get { return internalProject.GetPropertyValue("ProjectName"); }
67 }
68
69 /// <summary>
70 /// Returns the paths to all generated output files.
71 /// </summary>
72 public string[] OutputFiles
73 {
74 get
75 {
76 string[] extensions = new[] { ".pdb", ".xml" };
77 string binary = BinaryFile;
78
79 // Generate the list of output files.
80 var result = new List<string>();
81 result.Add(BinaryFile);
82 result.AddRange(extensions.Select(ext => Path.ChangeExtension(bi nary, ext)).Where(File.Exists));
83 return result.ToArray();
84 }
85 }
86
87 private readonly Microsoft.Build.Evaluation.Project internalProject;
88
89 public Project(string projectFile)
90 {
91 if (!File.Exists(projectFile))
92 {
93 throw new FileNotFoundException("The specified project file cann ot be found.", projectFile);
94 }
95 ProjectFile = projectFile;
96 WorkingDirectory = Path.GetDirectoryName(projectFile);
97 internalProject = (from p in ProjectCollection.GlobalProjectCollecti on.LoadedProjects
98 where p.FullPath == Path.GetFullPath(projectFile)
99 select p).SingleOrDefault() ?? new Microsoft.Buil d.Evaluation.Project(ProjectFile);
100 Configuration = "Release";
101 }
102
103 private void Build(string target)
104 {
105 if (!internalProject.Build(target))
106 {
107 // If the build fails, re-run this build with a logger attached.
108 internalProject.Build(target, new[] { new ConsoleLogger(LoggerVe rbosity.Quiet) });
109 Process.Start(WorkingDirectory);
110 throw new ApplicationException("Build of project [" + this + ":" + target + "] failed!");
111 }
112 }
113
114 /// <summary>
115 /// Cleans the project.
116 /// </summary>
117 public void Clean()
118 {
119 Log("Cleaning ...");
120 Build("Clean");
121 }
122
123 /// <summary>
124 /// Compiles the project.
125 /// </summary>
126 public void Compile()
127 {
128 Log("Compiling ...");
129 Build("Build");
130
131 if (!File.Exists(BinaryFile))
132 {
133 throw new ApplicationException(
134 "Build was successful, but target binary file does not exist : " + BinaryFile);
135 }
136 }
137
138 /// <summary>
139 /// Copies the resulting binaries to the specified directory.
140 /// </summary>
141 public void CopyTo(string dir)
142 {
143 Log("Copying output to " + Path.GetFileName(dir) + " ...");
144
145 if (!Directory.Exists(dir))
146 {
147 Directory.CreateDirectory(dir);
148 }
149
150 // Copy all output files into the target directory
151 foreach (string file in OutputFiles)
152 {
153 string fileName = Path.GetFileName(file);
154 Log(" * " + fileName);
155 File.Copy(file, Path.Combine(dir, fileName), true);
156 }
157 }
158
159 /// <summary>
160 /// Copies the resulting binaries and dependencies to the specified dire ctory.
161 /// </summary>
162 public void CopyAllFilesTo(string dir)
163 {
164 Log("Copying output to " + Path.GetFileName(dir) + " ...");
165
166 if (!Directory.Exists(dir))
167 {
168 Directory.CreateDirectory(dir);
169 }
170
171 // Copy all output files into the target directory
172 string buildDir = Path.GetDirectoryName(BinaryFile);
173 foreach (string file in Directory.GetFiles(buildDir, "*.*", SearchOp tion.TopDirectoryOnly))
174 {
175 switch (Path.GetExtension(file).ToLower())
176 {
177 case ".dll":
178 case ".exe":
179 case ".pdb":
180 case ".xml":
181 break; // Copy all assemblies and relevant information i nto the target directory.
182
183 default:
184 continue; // Unsupported.
185 }
186
187 string fileName = Path.GetFileName(file);
188 Log(" * " + fileName);
189 File.Copy(file, Path.Combine(dir, fileName), true);
190 }
191 }
192
193 /// <summary> Cleans and Compiles the project. </summary>
194 public void RunBuildTask()
195 {
196 CommandLine.WriteAction("Running [" + Name + "] build task..");
197 Clean();
198 Compile();
199 }
200
201 public override string ToString()
202 {
203 return Path.GetFileNameWithoutExtension(ProjectFile);
204 }
205
206 private void Log(string msg)
207 {
208 CommandLine.WriteLine("{{green}} " + msg, this);
209 }
210
211 /// <summary> Replaces Assembly version numner with the given one. </sum mary>
212 public void ReplaceVersion(string newVersion)
213 {
214 // no need to increase version
215 if (newVersion == null)
216 return;
217
218 var ASSEMBLY_INFO_PREFIX = "[assembly: AssemblyVersion(\"";
219 var assemblyInfoFile = System.IO.Path.Combine(WorkingDirectory, "Pro perties", "AssemblyInfo.cs");
220
221 try
222 {
223 // read all lines and replace the current version with the given newVersion
224 var allLines = File.ReadAllLines(assemblyInfoFile).Select(
225 l =>
226 {
227 // if the line contains assembly information, replace ol d version with new version
228 if (l.StartsWith(ASSEMBLY_INFO_PREFIX))
229 return ReplaceVersionInLine(l, ASSEMBLY_INFO_PREFIX, newVersion);
230 else
231 return l;
232 });
233 File.WriteAllLines(assemblyInfoFile, allLines);
234 }
235 catch (Exception ex)
236 {
237 // TODO(peleyal): remove before commiting
238 CommandLine.WriteLine(ex.Message);
239 }
240 }
241
242 /// <summary> Returns the assembly infromation line with the new version . </summary>
243 private string ReplaceVersionInLine(string line, string assemblyVersionP refix, string newVersion)
244 {
245 var startVersion = assemblyVersionPrefix.Length;
246 // '.*' ends the old version number (e.g 1.2.* \ 1.3.0.*)
247 var endVersion = line.IndexOf(".*", startVersion);
248 var oldVersion = line.Substring(startVersion, endVersion - startVers ion);
249 return line.Replace(assemblyVersionPrefix + oldVersion, assemblyVers ionPrefix + newVersion);
250 }
251 }
252 }
OLDNEW
« no previous file with comments | « Tools/Google.Build.Tester/app.config ('k') | Tools/Google.Build.Utils/DirUtils.cs » ('j') | no next file with comments »

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