LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with 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 | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Collections.Generic; | |
19 using System.IO; | 18 using System.IO; |
20 using System.Linq; | |
21 using System.Text; | |
22 using System.Text.RegularExpressions; | |
23 using System.Threading.Tasks; | |
24 | 19 |
25 namespace Google.Apis.Release.Wiki | 20 namespace Google.Apis.Release.Wiki |
26 { | 21 { |
27 /// <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> |
28 public static class DownloadsPageUpdater | 25 public static class DownloadsPageUpdater |
29 { | 26 { |
30 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>"; |
31 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END<
/wiki:comment>"; | 28 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END<
/wiki:comment>"; |
32 | 29 |
33 /// <summary>Updates the Wiki.</summary> | 30 /// <summary>Updates the Wiki.</summary> |
34 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) |
35 { | 32 { |
36 if (notes != null) | 33 if (string.IsNullOrEmpty(workingDirectory)) throw new ArgumentNullEx
ception("workingDirectory"); |
37 { | 34 if (string.IsNullOrEmpty(notes)) throw new ArgumentNullException("no
tes"); |
38 var file = Path.Combine(workingDirectory, "Downloads.wiki"); | 35 if (string.IsNullOrEmpty(oldVersion)) throw new ArgumentNullExceptio
n("oldVersion"); |
39 string content = File.ReadAllText(file); | 36 if (string.IsNullOrEmpty(newVersion)) throw new ArgumentNullExceptio
n("newVersion"); |
40 | 37 |
41 // update notes. Replace all text from StableStartTag to StableE
ndTag | |
42 var generatedNotes = "{{{" Environment.NewLine notes Envir
onment.NewLine "}}}"; | |
43 content = ReplacePart(content, StableStartTag, StableEndTag, gen
eratedNotes); | |
44 | 38 |
45 // update links | 39 var filePath = Path.Combine(workingDirectory, "Downloads.wiki"); |
46 content = content.Replace(oldVersion, newVersion); | 40 string content = File.ReadAllText(filePath); |
47 File.WriteAllText(file, content); | |
48 } | |
49 } | |
50 | 41 |
51 /// <summary> | 42 // update notes. Replace all text from StableStartTag to StableEndTa
g |
52 /// 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 "}}}"; |
53 /// <c>endPartTag</c>. | 44 content = ReplacePart(content, StableStartTag, StableEndTag, oldText
=> generatedNotes); |
54 /// </summary> | 45 |
55 /// <param name="source">The source content</param> | 46 // update links |
56 /// <param name="startPartTag">The start part tag</param> | 47 content = content.Replace(oldVersion, newVersion); |
57 /// <param name="endPartTag">The end part tag</param> | 48 File.WriteAllText(filePath, content); |
58 /// <param name="newText"> | |
59 /// The new text which will replace the content between <c>startPartTag<
/c> to <c>endPartTag</c>. | |
60 /// </param> | |
61 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, string newText) | |
62 { | |
63 return ReplacePart(source, startPartTag, endPartTag, old => newText)
; | |
64 } | 49 } |
65 | 50 |
66 /// <summary> | 51 /// <summary> |
67 /// 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· |
68 /// <c>startPartTag</c> to <c>endPartTag</c>. | 53 /// <c>startPartTag</c> to <c>endPartTag</c>. |
69 /// </summary> | 54 /// </summary> |
70 /// <param name="source">The source content</param> | 55 /// <param name="source">The source content</param> |
71 /// <param name="startPartTag">The start part tag</param> | 56 /// <param name="startPartTag">The start part tag</param> |
72 /// <param name="endPartTag">The end part tag</param> | 57 /// <param name="endPartTag">The end part tag</param> |
73 /// <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> |
74 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, | 59 private static string ReplacePart(string source, string startPartTag, st
ring endPartTag, |
75 Func<string, string> replaceFunc) | 60 Func<string, string> replaceFunc) |
76 { | 61 { |
| 62 // find the start tag |
77 int startIndex = source.IndexOf(startPartTag); | 63 int startIndex = source.IndexOf(startPartTag); |
78 if (startIndex < 0) | 64 if (startIndex < 0) |
79 { | 65 { |
80 throw new ArgumentException("Start Tag not found: " startPartT
ag); | 66 throw new ArgumentException("Start Tag not found: " startPartT
ag); |
81 } | 67 } |
82 startIndex = startPartTag.Length; | 68 startIndex = startPartTag.Length; |
83 | 69 |
84 // Find the end tag. | 70 // find the end tag |
85 int endIndex = source.IndexOf(endPartTag, startIndex); | 71 int endIndex = source.IndexOf(endPartTag, startIndex); |
86 if (endIndex < 0) | 72 if (endIndex < 0) |
87 { | 73 { |
88 throw new ArgumentException("End Tag not found: " endPartTag); | 74 throw new ArgumentException("End Tag not found: " endPartTag); |
89 } | 75 } |
90 | 76 |
91 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr
im('\r', '\n'); | 77 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr
im('\r', '\n'); |
92 var newText = replaceFunc(oldText); | 78 var newText = replaceFunc(oldText); |
93 | 79 |
94 // Replace the text | 80 // replace the text |
95 return source.Substring(0, startIndex) Environment.NewLine | 81 return source.Substring(0, startIndex) Environment.NewLine |
96 newText Environment.NewLine source.Substring(endIndex); | 82 newText Environment.NewLine source.Substring(endIndex); |
97 } | 83 } |
98 } | 84 } |
99 } | 85 } |
LEFT | RIGHT |