Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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; | |
18 using System.Diagnostics; | |
19 using System.IO; | |
20 using System.Linq; | |
21 | |
22 using Microsoft.Build.Evaluation; | |
23 | |
24 using Google.Apis.Utils; | |
25 | |
26 namespace Google.Apis.Release | |
27 { | |
28 /// <summary>Project extensions.</summary> | |
David waters
2013/09/19 16:22:32
Better doc please.
maybe just
Extension methods f
peleyal
2013/09/20 18:56:18
Done.
| |
29 static class ProjectExtenstions | |
David waters
2013/09/19 16:22:32
can this class be internal? ( I never remember wha
peleyal
2013/09/20 18:56:18
http://msdn.microsoft.com/en-us/library/ms173121.a
| |
30 { | |
31 static readonly TraceSource TraceSource = new TraceSource("Google.Apis") ; | |
32 | |
33 /// <summary>Gets the name of the project.</summary> | |
34 public static string GetName(this Project project) | |
35 { | |
36 return project.DirectoryPath.Substring(project.DirectoryPath.LastInd exOf(Path.DirectorySeparatorChar) + 1); | |
37 } | |
38 | |
39 /// <summary>Replaces Assembly version number with the given one.</summa ry> | |
40 public static void ReplaceVersion(this Project project, string newVersio n) | |
41 { | |
42 const string ASSEMBLY_INFO_PREFIX = "[assembly: AssemblyVersion(\""; | |
43 | |
44 // no need to increase version | |
45 if (newVersion == null) | |
46 return; | |
47 | |
48 var assemblyInfoFile = System.IO.Path.Combine(project.DirectoryPath, "Properties", "AssemblyInfo.cs"); | |
49 | |
50 try | |
51 { | |
52 // read all lines and replace the current version with the given newVersion | |
53 var allLines = File.ReadAllLines(assemblyInfoFile).Select( | |
54 l => | |
55 { | |
56 // if the line contains assembly information, replace ol d version with new version | |
57 if (l.StartsWith(ASSEMBLY_INFO_PREFIX)) | |
58 return ReplaceVersionInLine(l, ASSEMBLY_INFO_PREFIX, newVersion); | |
59 else | |
60 return l; | |
61 }); | |
62 File.WriteAllLines(assemblyInfoFile, allLines); | |
63 } | |
64 catch (Exception ex) | |
65 { | |
66 TraceSource.TraceEvent(TraceEventType.Error, "Exception occurred while replacing version", ex.Message); | |
67 } | |
68 } | |
69 | |
70 /// <summary>Returns the assembly information line with the new version. </summary> | |
71 private static string ReplaceVersionInLine(string line, string assemblyV ersionPrefix, string newVersion) | |
72 { | |
73 var startVersion = assemblyVersionPrefix.Length; | |
74 // '.*' ends the old version number (e.g 1.2.* or 1.2.0.*) | |
75 var endVersion = line.IndexOf(".*", startVersion); | |
76 var oldVersion = line.Substring(startVersion, endVersion - startVers ion); | |
77 return line.Replace(assemblyVersionPrefix + oldVersion, assemblyVers ionPrefix + newVersion); | |
78 } | |
79 } | |
80 } | |
OLD | NEW |