Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(853)

Delta Between Two Patch Sets: Tools/Google.Apis.NuGet.Publisher/Google.Apis.NuGet.Publisher/Program.cs

Issue 12662047: Issue 376: Generate NuGet pacakges for generated APIs (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: add comments Created 10 years, 7 months ago
Right Patch Set: David final comments Created 10 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 {
36 public const string ModeTest = "test";
37 public const string ModePublish = "publisher";
38
33 [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")]
34 public bool AllApis { get; set; } 40 public bool AllApis { get; set; }
35 41
36 [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" )]
37 public string ApiName { get; set; } 43 public string ApiName { get; set; }
38 44
39 [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")]
40 public string ApiVersion { get; set; } 46 public string ApiVersion { get; set; }
41 47
42 [Option('m', "mode", DefaultValue = "test", HelpText = "Indicate the mod e of operation {test|publisher}")] 48 [Option('m', "mode", DefaultValue = "test",
49 HelpText = "Indicate the mode of operation {" + ModeTest + "|" + Mod ePublish + "}")]
43 public string Mode { get; set; } 50 public string Mode { get; set; }
44 51
45 [Option('k', "nuget_key", 52 [Option('k', "nuget_key",
46 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'")]
47 public string NuGetApiKey { get; set; } 54 public string NuGetApiKey { get; set; }
48 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
60 [Option('d', "apis_directory",
61 HelpText = "For testing purposes only. " +
62 "It Defines the APIs directory which contains all the APIs we want t o test.")]
63 public string ApisDirectory { get; set; }
64
49 [HelpOption] 65 [HelpOption]
50 public string GetHelp() 66 public string GetHelp()
51 { 67 {
52 return HelpText.AutoBuild(this, c => HelpText.DefaultParsingErrorsHa ndler(this, c)); 68 return HelpText.AutoBuild(this, c => HelpText.DefaultParsingErrorsHa ndler(this, c));
53 } 69 }
54 } 70 }
55 71
72 /// <summary>
73 /// A Google APIs NuGet publisher which downloads the APIs latest bundles. F or each API it checks if there a NuGet
74 /// package doesn't exist for the client version and the API revision, and i f so it builds a new NuGet package and
75 /// publish it to NuGet main repository.
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>
56 class Program 86 class Program
57 { 87 {
58 static TraceSource TraceSource = new TraceSource("Google.Apis"); 88 private static TraceSource TraceSource = new TraceSource("Google.Apis");
59 89
60 /// <summary>The discovery URL to get all the public APIs.</summary> 90 /// <summary>The discovery URI to get all the public APIs.</summary>
61 const string DiscoveryApiUrl = @"https://www.googleapis.com/discovery/v1 /apis"; 91 static readonly Uri DiscoveryApiUri = new Uri(@"https://www.googleapis.c om/discovery/v1/apis");
62 92
63 /// <summary>The directory that the bundle will be downloaded to.</summa ry> 93 /// <summary>The download URI format to download a specific API format. Two parameters are expected the API·
64 const string DownloadBundleTempDirectory = @"C:\Temp\NuGetPublisher\"; 94 /// name and its version.</summary>
65
66 /// <summary>The download URI format to download a specific API format</ summary>
67 const string DownloadUriFormat = 95 const string DownloadUriFormat =
68 @"https://google-api-client-libraries.appspot.com/resources/api-libr aries/download/stable/{0}/{1}/csharp?deps=0"; 96 "https://google-api-client-libraries.appspot.com/download/library/{0 }/{1}/csharp?deps=0";
69 97
70 /// <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··
71 /// 'Microsoft.Bcl.Build.targets' file.</summary> 99 /// 'Microsoft.Bcl.Build.targets' file.</summary>
72 static readonly string TemplateDirectory; 100 readonly string TemplateDirectory;
101
102 /// <summary>The directory that the bundle will be downloaded to.</summa ry>
103 readonly string DownloadBundleTempDirectory = Path.Combine(Path.GetTempP ath(), "GoogleAPIsPublisher");
73 104
74 /// <summary>The program's options.</summary> 105 /// <summary>The program's options.</summary>
75 static Options Options { get; set; } 106 readonly Options options;
76 107
77 static Program() 108 #region Main
78 { 109
79 var execDir = Assembly.GetEntryAssembly().Location; 110 static void Main(string[] args)
111 {
112 var options = new Options();
113 if (!CommandLine.Parser.Default.ParseArguments(args, options))
114 {
115 // TODO(peleyal): add explanation which option is missing. Curre ntly the command line utility just
116 // print the whole help without mention what is missing.
117 }
118 else if (!options.AllApis && (options.ApiName == null || options.Api Version == null))
119 {
120 Console.WriteLine("Please Set '--all_apis true' or the API name and version, e.g. " +
121 "'--api_name=drive --api_version=v2'");
122 }
123 else if (!new[] { Options.ModePublish, Options.ModeTest }.Contains(o ptions.Mode))
124 {
125 Console.WriteLine("Mode should be '{0}' or '{1}'", Options.ModeT est, Options.ModePublish);
126 }
127 else
128 {
129
130 try
131 {
132 new Program(options).Run().Wait();
133 }
134 catch (AggregateException ex)
135 {
136 foreach (var inner in ex.InnerExceptions)
137 {
138 TraceSource.TraceEvent(TraceEventType.Error, "Exception was thrown. {0}", inner.Message);
139 }
140 }
141 }
142 Console.ReadKey();
143 }
144
145 #endregion
146
147 Program(Options options)
148 {
149 this.options = options;
80 // remove "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"
81 for (int i = 0; i < 4; ++i) 151 var fileInfo = new FileInfo(Assembly.GetEntryAssembly().Location);
82 { 152 TemplateDirectory = Path.Combine(fileInfo.Directory.Parent.Parent.Pa rent.FullName, "Template");
83 execDir = execDir.Remove(execDir.LastIndexOf("\\")); 153 }
84 } 154
85 TemplateDirectory = string.Format(@"{0}\Template\", execDir); 155 Task Run()
86 156 {
87 } 157 switch (options.Mode)
88 158 {
89 static void Main(string[] args) 159 case Options.ModeTest:
90 { 160 return RunAsync("TEST", TestAsync);
91 Options = new Options(); 161 case Options.ModePublish:
92 if (!CommandLine.Parser.Default.ParseArguments(args, Options)) 162 return RunAsync("PUBLISH", PublishAsync);
93 { 163 default:
94 // TODO(peleyal): add explanation which option is missing 164 throw new ArgumentException(string.Format("Mode should be {0 } or {1}",
95 Console.ReadLine(); 165 Options.ModePublish, Options.ModeTest));
96 Environment.Exit(1); 166 }
97 }
98 else if (!Options.AllApis && (Options.ApiName == null || Options.Api Version == null))
99 {
100 Console.WriteLine("Please Set '--all_apis true' or set the API n ame and version, e.g. " +
101 "'--api_name=drive --api_version=v2'");
102 Console.ReadLine();
103 Environment.Exit(1);
104 }
105 try
106 {
107 switch (Options.Mode)
108 {
109 case "test":
110 MainAsync("TEST", TestAsync).Wait();
111 break;
112 case "publisher":
113 MainAsync("PUBLISH", PublishAsync).Wait();
114 break;
115 default:
116 Console.WriteLine("Mode should be 'test' or 'publish'");
117 break;
118 }
119 }
120 catch (AggregateException ex)
121 {
122 foreach (var inner in ex.InnerExceptions)
123 {
124 TraceSource.TraceEvent(TraceEventType.Error, "Exception was thrown. {0}", inner.Message);
125 }
126 }
127
128 Console.ReadLine();
129 } 167 }
130 168
131 /// <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>
132 static async Task MainAsync(string header, Func<IEnumerable<DiscoveryDoc .Item>, Task> core) 170 async Task RunAsync(string header, Func<IEnumerable<DiscoveryItem>, Task > core)
133 { 171 {
134 TraceSource.TraceEvent(TraceEventType.Information, "=============== {0} ===============", header); 172 TraceSource.TraceEvent(TraceEventType.Information, "=============== {0} ===============", header);
135 TraceSource.TraceEvent(TraceEventType.Information, "Entering Google. Apis.Nuget.Publihser"); 173 TraceSource.TraceEvent(TraceEventType.Information, "Entering Google. Apis.Nuget.Publihser");
136 174
137 IEnumerable<DiscoveryDoc.Item> apis; 175 IEnumerable<DiscoveryItem> apis;
138 if (Options.AllApis) 176 if (options.AllApis)
139 { 177 {
140 apis = await new DiscoveryService().GetApis(DiscoveryApiUrl); 178 apis = await new DiscoveryService().GetApis(DiscoveryApiUri);
141 } 179 }
142 else 180 else
143 { 181 {
144 apis = new List<DiscoveryDoc.Item> { new DiscoveryDoc.Item 182 apis = new List<DiscoveryItem> { new DiscoveryItem
145 { 183 {
146 Name = Options.ApiName, 184 Name = options.ApiName.ToLower(),
147 Version = Options.ApiVersion 185 Version = options.ApiVersion.ToLower()
148 }}; 186 }};
149 } 187 }
150 188
151 if (Directory.Exists(DownloadBundleTempDirectory)) 189 if (Directory.Exists(DownloadBundleTempDirectory))
152 { 190 {
153 Directory.Delete(DownloadBundleTempDirectory, true); 191 Directory.Delete(DownloadBundleTempDirectory, true);
154 } 192 }
155 Directory.CreateDirectory(DownloadBundleTempDirectory); 193 Directory.CreateDirectory(DownloadBundleTempDirectory);
156 194
157 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" folder w as created", DownloadBundleTempDirectory); 195 try
158 196 {
159 await core(apis); 197 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" fold er was created",
160 198 DownloadBundleTempDirectory);
161 TraceSource.TraceEvent(TraceEventType.Information, 0, "Exiting Googl e.Apis.Nuget.Publihser"); 199
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 }
162 } 208 }
163 209
164 /// <summary>Publishes new APIs to NuGet main repository.</summary> 210 /// <summary>Publishes new APIs to NuGet main repository.</summary>
165 static async Task PublishAsync(IEnumerable<DiscoveryDoc.Item> apis) 211 async Task PublishAsync(IEnumerable<DiscoveryItem> apis)
166 { 212 {
167 // TODO(peleyal): validate NuGetApiKey 213 // TODO(peleyal): validate NuGetApiKey
168 foreach (var discoveryItem in apis) 214 foreach (var item in apis)
169 { 215 {
170 var workingDir = string.Format(@"{0}{1}-{2}\", DownloadBundleTem pDirectory, discoveryItem.Name, 216 var workingDir = Path.Combine(DownloadBundleTempDirectory,
171 discoveryItem.Version); 217 item.Name + "-" + item.Version);
172 Directory.CreateDirectory(workingDir); 218 Directory.CreateDirectory(workingDir);
173 var item = new ApiItemLogic(discoveryItem) 219
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)
174 { 227 {
175 BundleDirectory = workingDir, 228 BundleDirectory = workingDir,
176 DownloadUriFormat = DownloadUriFormat, 229 BundleUri = new Uri(bundleUri),
177 TemplateDirectory = TemplateDirectory, 230 TemplateDirectory = TemplateDirectory,
178 NuGetApiKey = Options.NuGetApiKey, 231 NuGetApiKey = options.NuGetApiKey,
179 }; 232 };
180 233
181 try 234 try
182 { 235 {
183 await item.Run(); 236 await publisher.Run();
184 } 237 }
185 catch (Exception ex) 238 catch (Exception ex)
186 { 239 {
187 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);
188 } 241 }
189 } 242 }
190 } 243 }
191 244
192 /// <summary> 245 /// <summary>
193 /// 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.
194 /// </summary> 247 /// </summary>
195 static async Task TestAsync(IEnumerable<DiscoveryDoc.Item> apis) 248 async Task TestAsync(IEnumerable<DiscoveryItem> apis)
196 { 249 {
197 foreach (var discoveryItem in apis) 250 foreach (var item in apis)
198 { 251 {
199 var item = new ApiItemLogic(discoveryItem) 252 var publisher = new NuGetApiPublisher(item)
200 { 253 {
201 TemplateDirectory = TemplateDirectory, 254 TemplateDirectory = TemplateDirectory,
202 }; 255 };
203 try 256 try
204 { 257 {
205 await item.Test(@"Z:\Shared\NuGet\"); 258 await publisher.Test(options.ApisDirectory);
206 } 259 }
207 catch (Exception ex) 260 catch (Exception ex)
208 { 261 {
209 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);
210 } 263 }
211 } 264 }
212 } 265 }
213 } 266 }
214 } 267 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b