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. Replace all text from StableStartTag to StableE
ndTag | |
26 var generatedNotes = "{{{" Environment.NewLine notes Envir
onment.NewLine "}}}"; | |
27 content = ReplacePart(content, StableStartTag, StableEndTag, gen
eratedNotes); | |
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); | |
32 } | |
33 } | |
34 | 41 |
35 /// <summary> | 42 // update notes. Replace all text from StableStartTag to StableEndTa
g |
36 /// Returns a new string with the content of the source and the input ne
w text between <c>startPartTag</c> to· | 43 var generatedNotes = "{{{" Environment.NewLine notes Environme
nt.NewLine "}}}"; |
37 /// <c>endPartTag</c>. | 44 content = ReplacePart(content, StableStartTag, StableEndTag, oldText
=> generatedNotes); |
38 /// </summary> | 45 |
39 /// <param name="source">The source content</param> | 46 // update links |
40 /// <param name="startPartTag">The start part tag</param> | 47 content = content.Replace(oldVersion, newVersion); |
41 /// <param name="endPartTag">The end part tag</param> | 48 File.WriteAllText(filePath, content); |
42 /// <param name="newText"> | |
43 /// The new text which will replace the content between <c>startPartTag<
/c> to <c>endPartTag</c>. | |
44 /// </param> | |
45 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, string newText) | |
46 { | |
47 return ReplacePart(source, startPartTag, endPartTag, old => newText)
; | |
48 } | 49 } |
49 | 50 |
50 /// <summary> | 51 /// <summary> |
51 /// Returns a new string with the content of the source and a new text b
ased on <c>replaceFunc</c>between· | 52 /// Returns a new string with the content of the source and a new text b
ased on <c>replaceFunc</c>between· |
52 /// <c>startPartTag</c> to <c>endPartTag</c>. | 53 /// <c>startPartTag</c> to <c>endPartTag</c>. |
53 /// </summary> | 54 /// </summary> |
54 /// <param name="source">The source content</param> | 55 /// <param name="source">The source content</param> |
55 /// <param name="startPartTag">The start part tag</param> | 56 /// <param name="startPartTag">The start part tag</param> |
56 /// <param name="endPartTag">The end part tag</param> | 57 /// <param name="endPartTag">The end part tag</param> |
57 /// <param name="replaceFunc">The replacement function from the old text
to new text</param> | 58 /// <param name="replaceFunc">The replacement function from the old text
to new text</param> |
58 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, | 59 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, |
59 Func<string, string> replaceFunc) | 60 Func<string, string> replaceFunc) |
60 { | 61 { |
| 62 // find the start tag |
61 int startIndex = source.IndexOf(startPartTag); | 63 int startIndex = source.IndexOf(startPartTag); |
62 if (startIndex < 0) | 64 if (startIndex < 0) |
63 { | 65 { |
64 throw new ArgumentException("Start Tag not found: " startPartT
ag); | 66 throw new ArgumentException("Start Tag not found: " startPartT
ag); |
65 } | 67 } |
66 startIndex = startPartTag.Length; | 68 startIndex = startPartTag.Length; |
67 | 69 |
68 // Find the end tag. | 70 // find the end tag |
69 int endIndex = source.IndexOf(endPartTag, startIndex); | 71 int endIndex = source.IndexOf(endPartTag, startIndex); |
70 if (endIndex < 0) | 72 if (endIndex < 0) |
71 { | 73 { |
72 throw new ArgumentException("End Tag not found: " endPartTag); | 74 throw new ArgumentException("End Tag not found: " endPartTag); |
73 } | 75 } |
74 | 76 |
75 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr
im('\r', '\n'); | 77 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr
im('\r', '\n'); |
76 var newText = replaceFunc(oldText); | 78 var newText = replaceFunc(oldText); |
77 | 79 |
78 // Replace the text | 80 // replace the text |
79 return source.Substring(0, startIndex) Environment.NewLine | 81 return source.Substring(0, startIndex) Environment.NewLine |
80 newText Environment.NewLine source.Substring(endIndex); | 82 newText Environment.NewLine source.Substring(endIndex); |
81 } | 83 } |
82 } | 84 } |
83 } | 85 } |
LEFT | RIGHT |