LEFT | RIGHT |
1 using System; | 1 /* |
2 using System.Collections.Generic; | 2 Copyright 2013 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; |
3 using System.Diagnostics; | 18 using System.Diagnostics; |
4 using System.IO; | 19 using System.IO; |
5 using System.Linq; | 20 using System.Linq; |
6 using System.Text; | |
7 using System.Threading.Tasks; | |
8 | 21 |
9 using Microsoft.Build.Evaluation; | 22 using Microsoft.Build.Evaluation; |
10 | 23 |
11 using Google.Apis.Utils; | 24 using Google.Apis.Utils; |
| 25 using Google.Apis.Utils.Trace; |
12 | 26 |
13 namespace Google.Apis.Release | 27 namespace Google.Apis.Release |
14 { | 28 { |
15 static class ProjectExtenstions | 29 /// <summary> |
| 30 /// Extension methods for the Project class.· |
| 31 /// Add support in getting the short name of the project and also in replaci
ng the current library version with· |
| 32 /// the new version (<see cref="ReplaceVersion"/>). |
| 33 /// </summary> |
| 34 internal static class ProjectExtenstions |
16 { | 35 { |
17 static readonly TraceSource TraceSource = new TraceSource("Google.Apis")
; | 36 static readonly TraceSource TraceSource = new TraceSource("Google.Apis")
; |
18 | 37 |
| 38 /// <summary>Gets the name of the project.</summary> |
19 public static string GetName(this Project project) | 39 public static string GetName(this Project project) |
20 { | 40 { |
21 return project.DirectoryPath.Substring(project.DirectoryPath.LastInd
exOf('\\') + 1); | 41 return project.DirectoryPath.Substring(project.DirectoryPath.LastInd
exOf(Path.DirectorySeparatorChar) + 1); |
22 } | 42 } |
23 | 43 |
24 /// <summary> Replaces Assembly version number with the given one. </sum
mary> | 44 /// <summary>Replaces Assembly version number with the given one.</summa
ry> |
25 public static void ReplaceVersion(this Project project, string newVersio
n) | 45 public static void ReplaceVersion(this Project project, string newVersio
n) |
26 { | 46 { |
| 47 const string ASSEMBLY_INFO_PREFIX = "[assembly: AssemblyVersion(\""; |
| 48 |
27 // no need to increase version | 49 // no need to increase version |
28 if (newVersion == null) | 50 if (newVersion == null) |
29 return; | 51 return; |
30 | 52 |
31 var ASSEMBLY_INFO_PREFIX = "[assembly: AssemblyVersion(\""; | |
32 var assemblyInfoFile = System.IO.Path.Combine(project.DirectoryPath,
"Properties", "AssemblyInfo.cs"); | 53 var assemblyInfoFile = System.IO.Path.Combine(project.DirectoryPath,
"Properties", "AssemblyInfo.cs"); |
33 | 54 |
34 try | 55 try |
35 { | 56 { |
36 // read all lines and replace the current version with the given
newVersion | 57 // read all lines and replace the current version with the given
newVersion |
37 var allLines = File.ReadAllLines(assemblyInfoFile).Select( | 58 var allLines = File.ReadAllLines(assemblyInfoFile).Select( |
38 l => | 59 l => |
39 { | 60 { |
40 // if the line contains assembly information, replace ol
d version with new version | 61 // if the line contains assembly information, replace ol
d version with new version |
41 if (l.StartsWith(ASSEMBLY_INFO_PREFIX)) | 62 if (l.StartsWith(ASSEMBLY_INFO_PREFIX)) |
42 return ReplaceVersionInLine(l, ASSEMBLY_INFO_PREFIX,
newVersion); | 63 return ReplaceVersionInLine(l, ASSEMBLY_INFO_PREFIX,
newVersion); |
43 else | 64 else |
44 return l; | 65 return l; |
45 }); | 66 }); |
46 File.WriteAllLines(assemblyInfoFile, allLines); | 67 File.WriteAllLines(assemblyInfoFile, allLines); |
47 } | 68 } |
48 catch (Exception ex) | 69 catch (Exception ex) |
49 { | 70 { |
50 TraceSource.TraceEvent(TraceEventType.Error, "Exception occurred
while replacing version", ex.Message); | 71 TraceSource.TraceEvent(TraceEventType.Error, "Exception occurred
while replacing version", ex.Message); |
51 } | 72 } |
52 } | 73 } |
53 | 74 |
54 /// <summary> Returns the assembly information line with the new version
. </summary> | 75 /// <summary>Returns the assembly information line with the new version.
</summary> |
55 private static string ReplaceVersionInLine(string line, string assemblyV
ersionPrefix, string newVersion) | 76 private static string ReplaceVersionInLine(string line, string assemblyV
ersionPrefix, string newVersion) |
56 { | 77 { |
57 var startVersion = assemblyVersionPrefix.Length; | 78 var startVersion = assemblyVersionPrefix.Length; |
58 // '.*' ends the old version number (e.g 1.2.* \ 1.3.0.*) | 79 // '.*' ends the old version number (e.g 1.2.* or 1.2.0.*) |
59 var endVersion = line.IndexOf(".*", startVersion); | 80 var endVersion = line.IndexOf(".*", startVersion); |
60 var oldVersion = line.Substring(startVersion, endVersion - startVers
ion); | 81 var oldVersion = line.Substring(startVersion, endVersion - startVers
ion); |
61 return line.Replace(assemblyVersionPrefix + oldVersion, assemblyVers
ionPrefix + newVersion); | 82 return line.Replace(assemblyVersionPrefix + oldVersion, assemblyVers
ionPrefix + newVersion); |
62 } | 83 } |
63 } | 84 } |
64 } | 85 } |
LEFT | RIGHT |