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.Collections.Generic; | 18 using System.Collections.Generic; |
19 using System.Diagnostics; | 19 using System.Diagnostics; |
20 using System.IO; | 20 using System.IO; |
21 using System.Linq; | 21 using System.Linq; |
22 using System.Reflection; | 22 using System.Reflection; |
23 using System.Threading.Tasks; | 23 using System.Threading.Tasks; |
24 | 24 |
25 using CommandLine; | 25 using CommandLine; |
26 using CommandLine.Text; | 26 using CommandLine.Text; |
27 | 27 |
28 using Google.Apis.NuGet.Publisher.Discovery; | |
29 using Google.Apis.Utils; | |
30 | |
28 namespace Google.Apis.NuGet.Publisher | 31 namespace Google.Apis.NuGet.Publisher |
29 { | 32 { |
30 /// <summary>The options class which contains the different options to this utility.</summary> | 33 /// <summary>The options class which contains the different options to this utility.</summary> |
31 class Options | 34 class Options |
32 { | 35 { |
33 public const string ModeTest = "test"; | 36 public const string ModeTest = "test"; |
34 public const string ModePublish = "publisher"; | 37 public const string ModePublish = "publisher"; |
35 | 38 |
36 [Option('a', "all_apis", HelpText = "Define if NuGet publisher works on all Google APIs")] | 39 [Option('a', "all_apis", HelpText = "Define if NuGet publisher works on all Google APIs")] |
37 public bool AllApis { get; set; } | 40 public bool AllApis { get; set; } |
38 | 41 |
39 [Option('n', "api_name", HelpText = "Define the specific API to work on" )] | 42 [Option('n', "api_name", HelpText = "Define the specific API to work on" )] |
40 public string ApiName { get; set; } | 43 public string ApiName { get; set; } |
41 | 44 |
42 [Option('v', "api_version", HelpText = "Define the specific API version to work on")] | 45 [Option('v', "api_version", HelpText = "Define the specific API version to work on")] |
43 public string ApiVersion { get; set; } | 46 public string ApiVersion { get; set; } |
44 | 47 |
45 [Option('m', "mode", DefaultValue = "test", | 48 [Option('m', "mode", DefaultValue = "test", |
46 HelpText = "Indicate the mode of operation {" + ModeTest + "|" + Mod ePublish + "}")] | 49 HelpText = "Indicate the mode of operation {" + ModeTest + "|" + Mod ePublish + "}")] |
47 public string Mode { get; set; } | 50 public string Mode { get; set; } |
48 | 51 |
49 [Option('k', "nuget_key", | 52 [Option('k', "nuget_key", |
50 HelpText = @"Define the NuGet API key. If empty the publisher works local on 'C:\LocalNuGetFeed'")] | 53 HelpText = @"Define the NuGet API key. If empty the publisher works local on 'C:\LocalNuGetFeed'")] |
51 public string NuGetApiKey { get; set; } | 54 public string NuGetApiKey { get; set; } |
52 | 55 |
53 [Option('l', "language_version", | 56 [Option('l', "library_version", |
54 HelpText = @"Define the specific language version we will use to dow nload the bundles")] | 57 HelpText = "Define the Google.Apis library which is used to download the bundles (e.g. 1.5.0-beta)")] |
David waters
2013/08/16 10:18:36
I need a bit more help in this comment. Are the bu
peleyal
2013/08/16 13:57:43
Done.
| |
55 public string LanguageVersion { get; set; } | 58 public string GoogleApisVersion { get; set; } |
56 | 59 |
57 [Option('d', "apis_directory", | 60 [Option('d', "apis_directory", |
58 HelpText = "For testing purposes only. It Defines the APIs directory which contains all the APIs we " + | 61 HelpText = "For testing purposes only. " + |
59 "want to test.")] | 62 "It Defines the APIs directory which contains all the APIs we want t o test.")] |
60 public string ApisDirectory { get; set; } | 63 public string ApisDirectory { get; set; } |
61 | 64 |
62 [HelpOption] | 65 [HelpOption] |
63 public string GetHelp() | 66 public string GetHelp() |
64 { | 67 { |
65 return HelpText.AutoBuild(this, c => HelpText.DefaultParsingErrorsHa ndler(this, c)); | 68 return HelpText.AutoBuild(this, c => HelpText.DefaultParsingErrorsHa ndler(this, c)); |
66 } | 69 } |
67 } | 70 } |
68 | 71 |
69 /// <summary> | 72 /// <summary> |
70 /// A Google APIs NuGet publisher which downloads the APIs latest bundles. F or each API it checks if there a NuGet | 73 /// A Google APIs NuGet publisher which downloads the APIs latest bundles. F or each API it checks if there a NuGet |
71 /// package doesn't exist for the client version and the API revision, and i f so it builds a new NuGet package and | 74 /// package doesn't exist for the client version and the API revision, and i f so it builds a new NuGet package and |
72 /// publish it to NuGet main repository. | 75 /// publish it to NuGet main repository. |
73 /// </summary> | 76 /// </summary> |
77 /// <remarks> | |
78 /// The Google APIs NuGet publisher uses a local folder "C:\LocalNuGetFeed" to store all the NuGet packages | |
79 /// locally. | |
80 /// Notice also the different between bundle and package: | |
81 /// A bundle is the zip file that contains the sources, and it's is download ed from | |
82 /// "https://google-api-client-libraries.appspot.com/" | |
83 /// A package is the NuGet package we use to build a solution. We can store NuGet packages locally (e.g. in | |
84 /// "C:\LocalNuGetFeed") or use the main NuGet repository at "https://nuget. org/api/v2/" | |
85 /// </remarks> | |
74 class Program | 86 class Program |
75 { | 87 { |
76 static TraceSource TraceSource = new TraceSource("Google.Apis"); | 88 private static TraceSource TraceSource = new TraceSource("Google.Apis"); |
77 | 89 |
78 /// <summary>The discovery URI to get all the public APIs.</summary> | 90 /// <summary>The discovery URI to get all the public APIs.</summary> |
79 const string DiscoveryApiUri = @"https://www.googleapis.com/discovery/v1 /apis"; | 91 static readonly Uri DiscoveryApiUri = new Uri(@"https://www.googleapis.c om/discovery/v1/apis"); |
80 | 92 |
81 /// <summary>The download URI format to download a specific API format. Two parameters are expected the API· | 93 /// <summary>The download URI format to download a specific API format. Two parameters are expected the API· |
82 /// name and its version.</summary> | 94 /// name and its version.</summary> |
83 const string DownloadUriFormat = | 95 const string DownloadUriFormat = |
84 "https://google-api-client-libraries.appspot.com/resources/api-libra ries/download/stable/" + | 96 "https://google-api-client-libraries.appspot.com/download/library/{0 }/{1}/csharp?deps=0"; |
85 "{0}/{1}/csharp?deps=0"; | |
86 | 97 |
87 /// <summary>The template directory which contains the '.nuget' director y and the necessary·· | 98 /// <summary>The template directory which contains the '.nuget' director y and the necessary·· |
88 /// 'Microsoft.Bcl.Build.targets' file.</summary> | 99 /// 'Microsoft.Bcl.Build.targets' file.</summary> |
89 readonly string TemplateDirectory; | 100 readonly string TemplateDirectory; |
90 | 101 |
91 /// <summary>The directory that the bundle will be downloaded to.</summa ry> | 102 /// <summary>The directory that the bundle will be downloaded to.</summa ry> |
92 readonly string DownloadBundleTempDirectory = Path.Combine(Path.GetTempP ath(), "GoogleAPIsPublisher"); | 103 readonly string DownloadBundleTempDirectory = Path.Combine(Path.GetTempP ath(), "GoogleAPIsPublisher"); |
93 | 104 |
94 /// <summary>The program's options.</summary> | 105 /// <summary>The program's options.</summary> |
95 readonly Options options; | 106 readonly Options options; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 } | 140 } |
130 } | 141 } |
131 Console.ReadKey(); | 142 Console.ReadKey(); |
132 } | 143 } |
133 | 144 |
134 #endregion | 145 #endregion |
135 | 146 |
136 Program(Options options) | 147 Program(Options options) |
137 { | 148 { |
138 this.options = options; | 149 this.options = options; |
139 // reoove "Google.Apis.NuGet.Publisher\\bin\\{Debug|Release}\\Google .Apis.NuGet.Publisher.exe" | 150 // remove "Google.Apis.NuGet.Publisher\\bin\\{Debug|Release}\\Google .Apis.NuGet.Publisher.exe" |
David waters
2013/08/16 10:18:36
typo
peleyal
2013/08/16 13:57:43
reoove is the new remove :)
On 2013/08/16 10:18:36
| |
140 var fileInfo = new FileInfo(Assembly.GetEntryAssembly().Location); | 151 var fileInfo = new FileInfo(Assembly.GetEntryAssembly().Location); |
141 TemplateDirectory = Path.Combine(fileInfo.Directory.Parent.Parent.Pa rent.FullName, "Template"); | 152 TemplateDirectory = Path.Combine(fileInfo.Directory.Parent.Parent.Pa rent.FullName, "Template"); |
142 } | 153 } |
143 | 154 |
144 Task Run() | 155 Task Run() |
145 { | 156 { |
146 switch (options.Mode) | 157 switch (options.Mode) |
147 { | 158 { |
148 case Options.ModeTest: | 159 case Options.ModeTest: |
149 return RunAsync("TEST", TestAsync); | 160 return RunAsync("TEST", TestAsync); |
150 case Options.ModePublish: | 161 case Options.ModePublish: |
151 return RunAsync("PUBLISH", PublishAsync); | 162 return RunAsync("PUBLISH", PublishAsync); |
152 default: | 163 default: |
153 throw new ArgumentException(string.Format("Mode should be {0 } or {1}", | 164 throw new ArgumentException(string.Format("Mode should be {0 } or {1}", |
154 Options.ModePublish, Options.ModeTest)); | 165 Options.ModePublish, Options.ModeTest)); |
155 } | 166 } |
156 } | 167 } |
157 | 168 |
158 /// <summary>The main wrapper to run, gets a function to run which conta ins the main logic.</summary> | 169 /// <summary>The main wrapper to run, gets a function to run which conta ins the main logic.</summary> |
159 async Task RunAsync(string header, Func<IEnumerable<DiscoveryDoc.Item>, Task> core) | 170 async Task RunAsync(string header, Func<IEnumerable<DiscoveryItem>, Task > core) |
160 { | 171 { |
161 TraceSource.TraceEvent(TraceEventType.Information, "=============== {0} ===============", header); | 172 TraceSource.TraceEvent(TraceEventType.Information, "=============== {0} ===============", header); |
162 TraceSource.TraceEvent(TraceEventType.Information, "Entering Google. Apis.Nuget.Publihser"); | 173 TraceSource.TraceEvent(TraceEventType.Information, "Entering Google. Apis.Nuget.Publihser"); |
163 | 174 |
164 IEnumerable<DiscoveryDoc.Item> apis; | 175 IEnumerable<DiscoveryItem> apis; |
165 if (options.AllApis) | 176 if (options.AllApis) |
166 { | 177 { |
167 apis = await new DiscoveryService().GetApis(DiscoveryApiUri); | 178 apis = await new DiscoveryService().GetApis(DiscoveryApiUri); |
168 } | 179 } |
169 else | 180 else |
170 { | 181 { |
171 apis = new List<DiscoveryDoc.Item> { new DiscoveryDoc.Item | 182 apis = new List<DiscoveryItem> { new DiscoveryItem |
172 { | 183 { |
173 Name = options.ApiName, | 184 Name = options.ApiName.ToLower(), |
174 Version = options.ApiVersion | 185 Version = options.ApiVersion.ToLower() |
175 }}; | 186 }}; |
176 } | 187 } |
177 | 188 |
178 if (Directory.Exists(DownloadBundleTempDirectory)) | 189 if (Directory.Exists(DownloadBundleTempDirectory)) |
179 { | 190 { |
180 Directory.Delete(DownloadBundleTempDirectory, true); | 191 Directory.Delete(DownloadBundleTempDirectory, true); |
181 } | 192 } |
182 Directory.CreateDirectory(DownloadBundleTempDirectory); | 193 Directory.CreateDirectory(DownloadBundleTempDirectory); |
183 | 194 |
184 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" folder w as created", | 195 try |
185 DownloadBundleTempDirectory); | 196 { |
186 | 197 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" fold er was created", |
187 await core(apis); | 198 DownloadBundleTempDirectory); |
188 | 199 |
189 TraceSource.TraceEvent(TraceEventType.Information, 0, "Exiting Googl e.Apis.Nuget.Publihser"); | 200 await core(apis); |
201 | |
202 TraceSource.TraceEvent(TraceEventType.Information, 0, "Exiting G oogle.Apis.Nuget.Publihser"); | |
203 } | |
204 finally | |
205 { | |
206 Directory.Delete(DownloadBundleTempDirectory, true); | |
207 } | |
190 } | 208 } |
191 | 209 |
192 /// <summary>Publishes new APIs to NuGet main repository.</summary> | 210 /// <summary>Publishes new APIs to NuGet main repository.</summary> |
193 async Task PublishAsync(IEnumerable<DiscoveryDoc.Item> apis) | 211 async Task PublishAsync(IEnumerable<DiscoveryItem> apis) |
194 { | 212 { |
195 // TODO(peleyal): validate NuGetApiKey | 213 // TODO(peleyal): validate NuGetApiKey |
196 foreach (var discoveryItem in apis) | 214 foreach (var item in apis) |
197 { | 215 { |
198 var workingDir = Path.Combine(DownloadBundleTempDirectory, | 216 var workingDir = Path.Combine(DownloadBundleTempDirectory, |
199 discoveryItem.Name + "-" + discoveryItem.Version); | 217 item.Name + "-" + item.Version); |
200 Directory.CreateDirectory(workingDir); | 218 Directory.CreateDirectory(workingDir); |
201 | 219 |
202 var item = new ApiItemLogic(discoveryItem) | 220 var bundleUri = string.Format(DownloadUriFormat, item.Name, item .Version); |
221 if (!string.IsNullOrEmpty(options.GoogleApisVersion)) | |
222 { | |
223 bundleUri = bundleUri + "&lv=" + options.GoogleApisVersion; | |
224 } | |
225 | |
226 var publisher = new NuGetApiPublisher(item) | |
203 { | 227 { |
204 BundleDirectory = workingDir, | 228 BundleDirectory = workingDir, |
205 BundleUriFormat = DownloadUriFormat + | 229 BundleUri = new Uri(bundleUri), |
206 (!string.IsNullOrEmpty(options.LanguageVersion) ? "& lv=" + options.LanguageVersion : ""), | |
207 TemplateDirectory = TemplateDirectory, | 230 TemplateDirectory = TemplateDirectory, |
208 NuGetApiKey = options.NuGetApiKey, | 231 NuGetApiKey = options.NuGetApiKey, |
209 }; | 232 }; |
210 | 233 |
211 try | 234 try |
212 { | 235 { |
213 await item.Run(); | 236 await publisher.Run(); |
214 } | 237 } |
215 catch (Exception ex) | 238 catch (Exception ex) |
216 { | 239 { |
217 TraceSource.TraceEvent(TraceEventType.Error, "{0}\t Exceptio n [{1}] occurred", item, ex.Message); | 240 TraceSource.TraceEvent(TraceEventType.Error, "{0}\t Exceptio n [{1}] occurred", item, ex.Message); |
218 } | 241 } |
219 } | 242 } |
220 } | 243 } |
221 | 244 |
222 /// <summary> | 245 /// <summary> |
223 /// Tests the main logic behave as expected. <see cref="ApiItemLogic.Tes t"/> for more details. | 246 /// Tests the main logic behave as expected. <see cref="NuGetApiPublishe r.Test"/> for more details. |
224 /// </summary> | 247 /// </summary> |
225 async Task TestAsync(IEnumerable<DiscoveryDoc.Item> apis) | 248 async Task TestAsync(IEnumerable<DiscoveryItem> apis) |
226 { | 249 { |
227 foreach (var discoveryItem in apis) | 250 foreach (var item in apis) |
228 { | 251 { |
229 var item = new ApiItemLogic(discoveryItem) | 252 var publisher = new NuGetApiPublisher(item) |
230 { | 253 { |
231 TemplateDirectory = TemplateDirectory, | 254 TemplateDirectory = TemplateDirectory, |
232 }; | 255 }; |
233 try | 256 try |
234 { | 257 { |
235 await item.Test(options.ApisDirectory); | 258 await publisher.Test(options.ApisDirectory); |
236 } | 259 } |
237 catch (Exception ex) | 260 catch (Exception ex) |
238 { | 261 { |
239 TraceSource.TraceEvent(TraceEventType.Error, "{0}\t Exceptio n [{1}] occurred", item, ex.Message); | 262 TraceSource.TraceEvent(TraceEventType.Error, "{0}\t Exceptio n [{1}] occurred", item, ex.Message); |
240 } | 263 } |
241 } | 264 } |
242 } | 265 } |
243 } | 266 } |
244 } | 267 } |
LEFT | RIGHT |