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.IO; | 18 using System.IO; |
4 using System.Linq; | |
5 using System.Text; | |
6 using System.Text.RegularExpressions; | |
7 using System.Threading.Tasks; | |
8 | 19 |
9 namespace Google.Apis.Release.Wiki | 20 namespace Google.Apis.Release.Wiki |
10 { | 21 { |
11 /// <summary>Updates the Downloads wiki page. </summary> | 22 /// <summary> |
| 23 /// Updates the Downloads wiki page (https://code.google.com/p/google-api-do
tnet-client/wiki/Downloads).· |
| 24 /// </summary> |
12 public static class DownloadsPageUpdater | 25 public static class DownloadsPageUpdater |
13 { | 26 { |
14 private const string StableStartTag = "<wiki:comment>GENERATED_STABLE_BE
GIN</wiki:comment>"; | 27 private const string StableStartTag = "<wiki:comment>GENERATED_STABLE_BE
GIN</wiki:comment>"; |
15 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END<
/wiki:comment>"; | 28 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END<
/wiki:comment>"; |
16 | 29 |
17 /// <summary>Updates the Wiki.</summary> | 30 /// <summary>Updates the Wiki.</summary> |
18 public static void UpdateWiki(string workingDirectory, string notes, str
ing oldVersion, string newVersion) | 31 public static void UpdateWiki(string workingDirectory, string notes, str
ing oldVersion, string newVersion) |
19 { | 32 { |
20 if (notes != null) | 33 if (string.IsNullOrEmpty(workingDirectory)) throw new ArgumentNullEx
ception("workingDirectory"); |
21 { | 34 if (string.IsNullOrEmpty(notes)) throw new ArgumentNullException("no
tes"); |
22 var file = Path.Combine(workingDirectory, "Downloads.wiki"); | 35 if (string.IsNullOrEmpty(oldVersion)) throw new ArgumentNullExceptio
n("oldVersion"); |
23 string content = File.ReadAllText(file); | 36 if (string.IsNullOrEmpty(newVersion)) throw new ArgumentNullExceptio
n("newVersion"); |
24 | 37 |
25 // update notes | |
26 var generatedNotes = "{{{" Environment.NewLine notes Envir
onment.NewLine "}}}"; | |
27 content = ReplaceInText(content, StableStartTag, StableEndTag, g
eneratedNotes); | |
28 | 38 |
29 // update links | 39 var filePath = Path.Combine(workingDirectory, "Downloads.wiki"); |
30 content = content.Replace(oldVersion, newVersion); | 40 string content = File.ReadAllText(filePath); |
31 File.WriteAllText(file, content); | 41 |
32 } | 42 // update notes. Replace all text from StableStartTag to StableEndTa
g |
| 43 var generatedNotes = "{{{" Environment.NewLine notes Environme
nt.NewLine "}}}"; |
| 44 content = ReplacePart(content, StableStartTag, StableEndTag, oldText
=> generatedNotes); |
| 45 |
| 46 // update links |
| 47 content = content.Replace(oldVersion, newVersion); |
| 48 File.WriteAllText(filePath, content); |
33 } | 49 } |
34 | 50 |
35 private static string ReplaceInText(string source, string startTag, stri
ng endTag, string newText) | 51 /// <summary> |
36 { | 52 /// Returns a new string with the content of the source and a new text b
ased on <c>replaceFunc</c>between· |
37 return ReplaceInText(source, startTag, endTag, old => newText); | 53 /// <c>startPartTag</c> to <c>endPartTag</c>. |
38 } | 54 /// </summary> |
39 | 55 /// <param name="source">The source content</param> |
40 private static string ReplaceInText(string source, string startTag, stri
ng endTag, | 56 /// <param name="startPartTag">The start part tag</param> |
| 57 /// <param name="endPartTag">The end part tag</param> |
| 58 /// <param name="replaceFunc">The replacement function from the old text
to new text</param> |
| 59 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, |
41 Func<string, string> replaceFunc) | 60 Func<string, string> replaceFunc) |
42 { | 61 { |
43 int start = source.IndexOf(startTag); | 62 // find the start tag |
44 if (start < 0) | 63 int startIndex = source.IndexOf(startPartTag); |
| 64 if (startIndex < 0) |
45 { | 65 { |
46 throw new ArgumentException("Start Tag not found: " startTag); | 66 throw new ArgumentException("Start Tag not found: " startPartT
ag); |
47 } | 67 } |
48 start = startTag.Length; | 68 startIndex = startPartTag.Length; |
49 | 69 |
50 // Find the end tag. | 70 // find the end tag |
51 int end = source.IndexOf(endTag, start); | 71 int endIndex = source.IndexOf(endPartTag, startIndex); |
52 if (end < 0) | 72 if (endIndex < 0) |
53 { | 73 { |
54 throw new ArgumentException("End Tag not found: " endTag); | 74 throw new ArgumentException("End Tag not found: " endPartTag); |
55 } | 75 } |
56 | 76 |
57 var oldText = source.Substring(start, end - start).Trim('\r', '\n'); | 77 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr
im('\r', '\n'); |
58 var newText = replaceFunc(oldText); | 78 var newText = replaceFunc(oldText); |
59 // Replace the text | 79 |
60 return source.Substring(0, start) Environment.NewLine | 80 // replace the text |
61 newText Environment.NewLine source.Substring(end); | 81 return source.Substring(0, startIndex) Environment.NewLine |
| 82 newText Environment.NewLine source.Substring(endIndex); |
62 } | 83 } |
63 } | 84 } |
64 } | 85 } |
LEFT | RIGHT |