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.IO; |
| 19 |
| 20 namespace Google.Apis.Release.Wiki |
| 21 { |
| 22 /// <summary> |
| 23 /// Updates the Downloads wiki page (https://code.google.com/p/google-api-do
tnet-client/wiki/Downloads).· |
| 24 /// </summary> |
| 25 public static class DownloadsPageUpdater |
| 26 { |
| 27 private const string StableStartTag = "<wiki:comment>GENERATED_STABLE_BE
GIN</wiki:comment>"; |
| 28 private const string StableEndTag = "<wiki:comment>GENERATED_STABLE_END<
/wiki:comment>"; |
| 29 |
| 30 /// <summary>Updates the Wiki.</summary> |
| 31 public static void UpdateWiki(string workingDirectory, string notes, str
ing oldVersion, string newVersion) |
| 32 { |
| 33 if (string.IsNullOrEmpty(workingDirectory)) throw new ArgumentNullEx
ception("workingDirectory"); |
| 34 if (string.IsNullOrEmpty(notes)) throw new ArgumentNullException("no
tes"); |
| 35 if (string.IsNullOrEmpty(oldVersion)) throw new ArgumentNullExceptio
n("oldVersion"); |
| 36 if (string.IsNullOrEmpty(newVersion)) throw new ArgumentNullExceptio
n("newVersion"); |
| 37 |
| 38 |
| 39 var filePath = Path.Combine(workingDirectory, "Downloads.wiki"); |
| 40 string content = File.ReadAllText(filePath); |
| 41 |
| 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); |
| 49 } |
| 50 |
| 51 /// <summary> |
| 52 /// Returns a new string with the content of the source and a new text b
ased on <c>replaceFunc</c>between· |
| 53 /// <c>startPartTag</c> to <c>endPartTag</c>. |
| 54 /// </summary> |
| 55 /// <param name="source">The source content</param> |
| 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, |
| 60 Func<string, string> replaceFunc) |
| 61 { |
| 62 // find the start tag |
| 63 int startIndex = source.IndexOf(startPartTag); |
| 64 if (startIndex < 0) |
| 65 { |
| 66 throw new ArgumentException("Start Tag not found: " + startPartT
ag); |
| 67 } |
| 68 startIndex += startPartTag.Length; |
| 69 |
| 70 // find the end tag |
| 71 int endIndex = source.IndexOf(endPartTag, startIndex); |
| 72 if (endIndex < 0) |
| 73 { |
| 74 throw new ArgumentException("End Tag not found: " + endPartTag); |
| 75 } |
| 76 |
| 77 var oldText = source.Substring(startIndex, endIndex - startIndex).Tr
im('\r', '\n'); |
| 78 var newText = replaceFunc(oldText); |
| 79 |
| 80 // replace the text |
| 81 return source.Substring(0, startIndex) + Environment.NewLine + |
| 82 newText + Environment.NewLine + source.Substring(endIndex); |
| 83 } |
| 84 } |
| 85 } |
OLD | NEW |