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

Unified Diff: Tools/Google.Apis.Release/ProjectExtenstions.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 11 years, 5 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Tools/Google.Apis.Release/Program.cs ('k') | Tools/Google.Apis.Release/Properties/AssemblyInfo.cs » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Tools/Google.Apis.Release/ProjectExtenstions.cs
===================================================================
new file mode 100644
--- /dev/null
+++ b/Tools/Google.Apis.Release/ProjectExtenstions.cs
@@ -0,0 +1,85 @@
+/*
+Copyright 2013 Google Inc
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+
+using Microsoft.Build.Evaluation;
+
+using Google.Apis.Utils;
+using Google.Apis.Utils.Trace;
+
+namespace Google.Apis.Release
+{
+ /// <summary>
+ /// Extension methods for the Project class.
+ /// Add support in getting the short name of the project and also in replacing the current library version with
+ /// the new version (<see cref="ReplaceVersion"/>).
+ /// </summary>
+ internal static class ProjectExtenstions
+ {
+ static readonly TraceSource TraceSource = new TraceSource("Google.Apis");
+
+ /// <summary>Gets the name of the project.</summary>
+ public static string GetName(this Project project)
+ {
+ return project.DirectoryPath.Substring(project.DirectoryPath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
+ }
+
+ /// <summary>Replaces Assembly version number with the given one.</summary>
+ public static void ReplaceVersion(this Project project, string newVersion)
+ {
+ const string ASSEMBLY_INFO_PREFIX = "[assembly: AssemblyVersion(\"";
+
+ // no need to increase version
+ if (newVersion == null)
+ return;
+
+ var assemblyInfoFile = System.IO.Path.Combine(project.DirectoryPath, "Properties", "AssemblyInfo.cs");
+
+ try
+ {
+ // read all lines and replace the current version with the given newVersion
+ var allLines = File.ReadAllLines(assemblyInfoFile).Select(
+ l =>
+ {
+ // if the line contains assembly information, replace old version with new version
+ if (l.StartsWith(ASSEMBLY_INFO_PREFIX))
+ return ReplaceVersionInLine(l, ASSEMBLY_INFO_PREFIX, newVersion);
+ else
+ return l;
+ });
+ File.WriteAllLines(assemblyInfoFile, allLines);
+ }
+ catch (Exception ex)
+ {
+ TraceSource.TraceEvent(TraceEventType.Error, "Exception occurred while replacing version", ex.Message);
+ }
+ }
+
+ /// <summary>Returns the assembly information line with the new version. </summary>
+ private static string ReplaceVersionInLine(string line, string assemblyVersionPrefix, string newVersion)
+ {
+ var startVersion = assemblyVersionPrefix.Length;
+ // '.*' ends the old version number (e.g 1.2.* or 1.2.0.*)
+ var endVersion = line.IndexOf(".*", startVersion);
+ var oldVersion = line.Substring(startVersion, endVersion - startVersion);
+ return line.Replace(assemblyVersionPrefix + oldVersion, assemblyVersionPrefix + newVersion);
+ }
+ }
+}
« no previous file with comments | « Tools/Google.Apis.Release/Program.cs ('k') | Tools/Google.Apis.Release/Properties/AssemblyInfo.cs » ('j') | no next file with comments »

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