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 |
| 56 [Option('l', "library_version", |
| 57 HelpText = "Define the Google.Apis library which is used to download
the bundles (e.g. 1.5.0-beta)")] |
| 58 public string GoogleApisVersion { get; set; } |
| 59 |
53 [Option('d', "apis_directory", | 60 [Option('d', "apis_directory", |
54 HelpText = "For testing purposes only. It Defines the APIs directory
which contains all the APIs we " + | 61 HelpText = "For testing purposes only. " + |
55 "want to test.")] | 62 "It Defines the APIs directory which contains all the APIs we want t
o test.")] |
56 public string ApisDirectory { get; set; } | 63 public string ApisDirectory { get; set; } |
57 | 64 |
58 [HelpOption] | 65 [HelpOption] |
59 public string GetHelp() | 66 public string GetHelp() |
60 { | 67 { |
61 return HelpText.AutoBuild(this, c => HelpText.DefaultParsingErrorsHa
ndler(this, c)); | 68 return HelpText.AutoBuild(this, c => HelpText.DefaultParsingErrorsHa
ndler(this, c)); |
62 } | 69 } |
63 } | 70 } |
64 | 71 |
65 /// <summary> | 72 /// <summary> |
66 /// 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 |
67 /// 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 |
68 /// publish it to NuGet main repository. | 75 /// publish it to NuGet main repository. |
69 /// </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> |
70 class Program | 86 class Program |
71 { | 87 { |
72 static TraceSource TraceSource = new TraceSource("Google.Apis"); | 88 private static TraceSource TraceSource = new TraceSource("Google.Apis"); |
73 | 89 |
74 /// <summary>The discovery URI to get all the public APIs.</summary> | 90 /// <summary>The discovery URI to get all the public APIs.</summary> |
75 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"); |
76 | 92 |
77 /// <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· |
78 /// name and its version.</summary> | 94 /// name and its version.</summary> |
79 const string DownloadUriFormat = | 95 const string DownloadUriFormat = |
80 "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"; |
81 "{0}/{1}/csharp?deps=0"; | |
82 | 97 |
83 /// <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·· |
84 /// 'Microsoft.Bcl.Build.targets' file.</summary> | 99 /// 'Microsoft.Bcl.Build.targets' file.</summary> |
85 readonly string TemplateDirectory; | 100 readonly string TemplateDirectory; |
86 | 101 |
87 /// <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> |
88 readonly string DownloadBundleTempDirectory = Path.Combine(Path.GetTempP
ath(), "GoogleAPIsPublisher"); | 103 readonly string DownloadBundleTempDirectory = Path.Combine(Path.GetTempP
ath(), "GoogleAPIsPublisher"); |
89 | 104 |
90 /// <summary>The program's options.</summary> | 105 /// <summary>The program's options.</summary> |
91 readonly Options options; | 106 readonly Options options; |
(...skipping 10 matching lines...) Expand all Loading... |
102 } | 117 } |
103 else if (!options.AllApis && (options.ApiName == null || options.Api
Version == null)) | 118 else if (!options.AllApis && (options.ApiName == null || options.Api
Version == null)) |
104 { | 119 { |
105 Console.WriteLine("Please Set '--all_apis true' or the API name
and version, e.g. " + | 120 Console.WriteLine("Please Set '--all_apis true' or the API name
and version, e.g. " + |
106 "'--api_name=drive --api_version=v2'"); | 121 "'--api_name=drive --api_version=v2'"); |
107 } | 122 } |
108 else if (!new[] { Options.ModePublish, Options.ModeTest }.Contains(o
ptions.Mode)) | 123 else if (!new[] { Options.ModePublish, Options.ModeTest }.Contains(o
ptions.Mode)) |
109 { | 124 { |
110 Console.WriteLine("Mode should be '{0}' or '{1}'", Options.ModeT
est, Options.ModePublish); | 125 Console.WriteLine("Mode should be '{0}' or '{1}'", Options.ModeT
est, Options.ModePublish); |
111 } | 126 } |
112 | 127 else |
113 try | 128 { |
114 { | 129 |
115 new Program(options).Run().Wait(); | 130 try |
116 } | 131 { |
117 catch (AggregateException ex) | 132 new Program(options).Run().Wait(); |
118 { | 133 } |
119 foreach (var inner in ex.InnerExceptions) | 134 catch (AggregateException ex) |
120 { | 135 { |
121 TraceSource.TraceEvent(TraceEventType.Error, "Exception was
thrown. {0}", inner.Message); | 136 foreach (var inner in ex.InnerExceptions) |
122 } | 137 { |
123 } | 138 TraceSource.TraceEvent(TraceEventType.Error, "Exception
was thrown. {0}", inner.Message); |
| 139 } |
| 140 } |
| 141 } |
| 142 Console.ReadKey(); |
124 } | 143 } |
125 | 144 |
126 #endregion | 145 #endregion |
127 | 146 |
128 Program(Options options) | 147 Program(Options options) |
129 { | 148 { |
130 this.options = options; | 149 this.options = options; |
131 // 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" |
132 var fileInfo = new FileInfo(Assembly.GetEntryAssembly().Location); | 151 var fileInfo = new FileInfo(Assembly.GetEntryAssembly().Location); |
133 TemplateDirectory = Path.Combine(fileInfo.Directory.Parent.Parent.Pa
rent.FullName, "Template"); | 152 TemplateDirectory = Path.Combine(fileInfo.Directory.Parent.Parent.Pa
rent.FullName, "Template"); |
134 } | 153 } |
135 | 154 |
136 Task Run() | 155 Task Run() |
137 { | 156 { |
138 switch (options.Mode) | 157 switch (options.Mode) |
139 { | 158 { |
140 case Options.ModeTest: | 159 case Options.ModeTest: |
141 return RunAsync("TEST", TestAsync); | 160 return RunAsync("TEST", TestAsync); |
142 case Options.ModePublish: | 161 case Options.ModePublish: |
143 return RunAsync("PUBLISH", PublishAsync); | 162 return RunAsync("PUBLISH", PublishAsync); |
144 default: | 163 default: |
145 throw new ArgumentException(string.Format("Mode should be {0
} or {1}", | 164 throw new ArgumentException(string.Format("Mode should be {0
} or {1}", |
146 Options.ModePublish, Options.ModeTest)); | 165 Options.ModePublish, Options.ModeTest)); |
147 } | 166 } |
148 } | 167 } |
149 | 168 |
150 /// <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> |
151 async Task RunAsync(string header, Func<IEnumerable<DiscoveryDoc.Item>,
Task> core) | 170 async Task RunAsync(string header, Func<IEnumerable<DiscoveryItem>, Task
> core) |
152 { | 171 { |
153 TraceSource.TraceEvent(TraceEventType.Information, "===============
{0} ===============", header); | 172 TraceSource.TraceEvent(TraceEventType.Information, "===============
{0} ===============", header); |
154 TraceSource.TraceEvent(TraceEventType.Information, "Entering Google.
Apis.Nuget.Publihser"); | 173 TraceSource.TraceEvent(TraceEventType.Information, "Entering Google.
Apis.Nuget.Publihser"); |
155 | 174 |
156 IEnumerable<DiscoveryDoc.Item> apis; | 175 IEnumerable<DiscoveryItem> apis; |
157 if (options.AllApis) | 176 if (options.AllApis) |
158 { | 177 { |
159 apis = await new DiscoveryService().GetApis(DiscoveryApiUri); | 178 apis = await new DiscoveryService().GetApis(DiscoveryApiUri); |
160 } | 179 } |
161 else | 180 else |
162 { | 181 { |
163 apis = new List<DiscoveryDoc.Item> { new DiscoveryDoc.Item | 182 apis = new List<DiscoveryItem> { new DiscoveryItem |
164 { | 183 { |
165 Name = options.ApiName, | 184 Name = options.ApiName.ToLower(), |
166 Version = options.ApiVersion | 185 Version = options.ApiVersion.ToLower() |
167 }}; | 186 }}; |
168 } | 187 } |
169 | 188 |
170 if (Directory.Exists(DownloadBundleTempDirectory)) | 189 if (Directory.Exists(DownloadBundleTempDirectory)) |
171 { | 190 { |
172 Directory.Delete(DownloadBundleTempDirectory, true); | 191 Directory.Delete(DownloadBundleTempDirectory, true); |
173 } | 192 } |
174 Directory.CreateDirectory(DownloadBundleTempDirectory); | 193 Directory.CreateDirectory(DownloadBundleTempDirectory); |
175 | 194 |
176 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" folder w
as created", | 195 try |
177 DownloadBundleTempDirectory); | 196 { |
178 | 197 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" fold
er was created", |
179 await core(apis); | 198 DownloadBundleTempDirectory); |
180 | 199 |
181 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 } |
182 } | 208 } |
183 | 209 |
184 /// <summary>Publishes new APIs to NuGet main repository.</summary> | 210 /// <summary>Publishes new APIs to NuGet main repository.</summary> |
185 async Task PublishAsync(IEnumerable<DiscoveryDoc.Item> apis) | 211 async Task PublishAsync(IEnumerable<DiscoveryItem> apis) |
186 { | 212 { |
187 // TODO(peleyal): validate NuGetApiKey | 213 // TODO(peleyal): validate NuGetApiKey |
188 foreach (var discoveryItem in apis) | 214 foreach (var item in apis) |
189 { | 215 { |
190 var workingDir = string.Format(@"{0}{1}-{2}\", DownloadBundleTem
pDirectory, discoveryItem.Name, | 216 var workingDir = Path.Combine(DownloadBundleTempDirectory, |
191 discoveryItem.Version); | 217 item.Name + "-" + item.Version); |
192 Directory.CreateDirectory(workingDir); | 218 Directory.CreateDirectory(workingDir); |
193 | 219 |
194 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) |
195 { | 227 { |
196 BundleDirectory = workingDir, | 228 BundleDirectory = workingDir, |
197 BundleUriFormat = DownloadUriFormat, | 229 BundleUri = new Uri(bundleUri), |
198 TemplateDirectory = TemplateDirectory, | 230 TemplateDirectory = TemplateDirectory, |
199 NuGetApiKey = options.NuGetApiKey, | 231 NuGetApiKey = options.NuGetApiKey, |
200 }; | 232 }; |
201 | 233 |
202 try | 234 try |
203 { | 235 { |
204 await item.Run(); | 236 await publisher.Run(); |
205 } | 237 } |
206 catch (Exception ex) | 238 catch (Exception ex) |
207 { | 239 { |
208 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); |
209 } | 241 } |
210 } | 242 } |
211 } | 243 } |
212 | 244 |
213 /// <summary> | 245 /// <summary> |
214 /// 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. |
215 /// </summary> | 247 /// </summary> |
216 async Task TestAsync(IEnumerable<DiscoveryDoc.Item> apis) | 248 async Task TestAsync(IEnumerable<DiscoveryItem> apis) |
217 { | 249 { |
218 foreach (var discoveryItem in apis) | 250 foreach (var item in apis) |
219 { | 251 { |
220 var item = new ApiItemLogic(discoveryItem) | 252 var publisher = new NuGetApiPublisher(item) |
221 { | 253 { |
222 TemplateDirectory = TemplateDirectory, | 254 TemplateDirectory = TemplateDirectory, |
223 }; | 255 }; |
224 try | 256 try |
225 { | 257 { |
226 await item.Test(options.ApisDirectory); | 258 await publisher.Test(options.ApisDirectory); |
227 } | 259 } |
228 catch (Exception ex) | 260 catch (Exception ex) |
229 { | 261 { |
230 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); |
231 } | 263 } |
232 } | 264 } |
233 } | 265 } |
234 } | 266 } |
235 } | 267 } |
LEFT | RIGHT |