Left: | ||
Right: |
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.IO; | 18 using System.IO; |
19 | 19 |
20 namespace Google.Apis.Release.Wiki | 20 namespace Google.Apis.Release.Wiki |
21 { | 21 { |
22 /// <summary>Updates the Downloads wiki page. </summary> | 22 /// <summary> |
David waters
2013/09/19 16:22:32
please give live url to page
peleyal
2013/09/20 18:56:18
Done.
| |
23 /// Updates the Downloads wiki page (https://code.google.com/p/google-api-do tnet-client/wiki/Downloads).· | |
24 /// </summary> | |
23 public static class DownloadsPageUpdater | 25 public static class DownloadsPageUpdater |
24 { | 26 { |
25 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>"; |
26 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END< /wiki:comment>"; | 28 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END< /wiki:comment>"; |
27 | 29 |
28 /// <summary>Updates the Wiki.</summary> | 30 /// <summary>Updates the Wiki.</summary> |
29 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) |
30 { | 32 { |
David waters
2013/09/19 16:22:32
consider :
workingDirectory.ThrowIfNullOrEmpty()
peleyal
2013/09/20 18:56:18
Done.
| |
31 if (notes == null) | 33 if (string.IsNullOrEmpty(workingDirectory)) throw new ArgumentNullEx ception("workingDirectory"); |
32 { | 34 if (string.IsNullOrEmpty(notes)) throw new ArgumentNullException("no tes"); |
33 return; | 35 if (string.IsNullOrEmpty(oldVersion)) throw new ArgumentNullExceptio n("oldVersion"); |
34 } | 36 if (string.IsNullOrEmpty(newVersion)) throw new ArgumentNullExceptio n("newVersion"); |
37 | |
35 | 38 |
36 var filePath = Path.Combine(workingDirectory, "Downloads.wiki"); | 39 var filePath = Path.Combine(workingDirectory, "Downloads.wiki"); |
37 string content = File.ReadAllText(filePath); | 40 string content = File.ReadAllText(filePath); |
38 | 41 |
39 // update notes. Replace all text from StableStartTag to StableEndTa g | 42 // update notes. Replace all text from StableStartTag to StableEndTa g |
40 var generatedNotes = "{{{" + Environment.NewLine + notes + Environme nt.NewLine + "}}}"; | 43 var generatedNotes = "{{{" + Environment.NewLine + notes + Environme nt.NewLine + "}}}"; |
41 content = ReplacePart(content, StableStartTag, StableEndTag, generat edNotes); | 44 content = ReplacePart(content, StableStartTag, StableEndTag, oldText => generatedNotes); |
42 | 45 |
43 // update links | 46 // update links |
44 content = content.Replace(oldVersion, newVersion); | 47 content = content.Replace(oldVersion, newVersion); |
45 File.WriteAllText(filePath, content); | 48 File.WriteAllText(filePath, content); |
46 } | |
47 | |
48 /// <summary> | |
49 /// Returns a new string with the content of the source and the input ne w text between <c>startPartTag</c> to· | |
50 /// <c>endPartTag</c>. | |
51 /// </summary> | |
52 /// <param name="source">The source content</param> | |
53 /// <param name="startPartTag">The start part tag</param> | |
54 /// <param name="endPartTag">The end part tag</param> | |
55 /// <param name="newText"> | |
56 /// The new text which will replace the content between <c>startPartTag< /c> to <c>endPartTag</c>. | |
57 /// </param> | |
58 private static string ReplacePart(string source, string startPartTag, st ring endPartTag, string newText) | |
59 { | |
60 return ReplacePart(source, startPartTag, endPartTag, old => newText) ; | |
61 } | 49 } |
62 | 50 |
63 /// <summary> | 51 /// <summary> |
64 /// 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· |
65 /// <c>startPartTag</c> to <c>endPartTag</c>. | 53 /// <c>startPartTag</c> to <c>endPartTag</c>. |
66 /// </summary> | 54 /// </summary> |
67 /// <param name="source">The source content</param> | 55 /// <param name="source">The source content</param> |
68 /// <param name="startPartTag">The start part tag</param> | 56 /// <param name="startPartTag">The start part tag</param> |
69 /// <param name="endPartTag">The end part tag</param> | 57 /// <param name="endPartTag">The end part tag</param> |
70 /// <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> |
71 private static string ReplacePart(string source, string startPartTag, st ring endPartTag, | 59 private static string ReplacePart(string source, string startPartTag, st ring endPartTag, |
72 Func<string, string> replaceFunc) | 60 Func<string, string> replaceFunc) |
David waters
2013/09/19 16:22:32
Consider,
This feels like over-generalization we h
peleyal
2013/09/20 18:56:18
Done.
| |
73 { | 61 { |
74 // find the start tag | 62 // find the start tag |
75 int startIndex = source.IndexOf(startPartTag); | 63 int startIndex = source.IndexOf(startPartTag); |
76 if (startIndex < 0) | 64 if (startIndex < 0) |
77 { | 65 { |
78 throw new ArgumentException("Start Tag not found: " + startPartT ag); | 66 throw new ArgumentException("Start Tag not found: " + startPartT ag); |
79 } | 67 } |
80 startIndex += startPartTag.Length; | 68 startIndex += startPartTag.Length; |
81 | 69 |
82 // find the end tag | 70 // find the end tag |
83 int endIndex = source.IndexOf(endPartTag, startIndex); | 71 int endIndex = source.IndexOf(endPartTag, startIndex); |
84 if (endIndex < 0) | 72 if (endIndex < 0) |
85 { | 73 { |
86 throw new ArgumentException("End Tag not found: " + endPartTag); | 74 throw new ArgumentException("End Tag not found: " + endPartTag); |
87 } | 75 } |
88 | 76 |
89 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr im('\r', '\n'); | 77 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr im('\r', '\n'); |
90 var newText = replaceFunc(oldText); | 78 var newText = replaceFunc(oldText); |
91 | 79 |
92 // replace the text | 80 // replace the text |
93 return source.Substring(0, startIndex) + Environment.NewLine + | 81 return source.Substring(0, startIndex) + Environment.NewLine + |
94 newText + Environment.NewLine + source.Substring(endIndex); | 82 newText + Environment.NewLine + source.Substring(endIndex); |
95 } | 83 } |
96 } | 84 } |
97 } | 85 } |
LEFT | RIGHT |