LEFT | RIGHT |
(no file at all) | |
| 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.Diagnostics; |
| 19 using System.IO; |
| 20 using System.Linq; |
| 21 |
| 22 using NuGet; |
| 23 |
| 24 namespace Google.Apis.Utils |
| 25 { |
| 26 /// <summary> |
| 27 /// NuGet utilities class which support publishing to NuGet main repository,
creating a local NuGet package, etc. |
| 28 /// </summary> |
| 29 public static class NuGetUtilities |
| 30 { |
| 31 private static readonly TraceSource TraceSource = new TraceSource("Googl
e.Apis"); |
| 32 |
| 33 /// <summary>The local NuGet folder which stores local NuGet packages.</
summary> |
| 34 public static readonly string LocalNuGetPackageFolder; |
| 35 |
| 36 /// <summary>The main NuGet repository. This is used to publish a new pa
ckage.</summary> |
| 37 private static readonly IPackageRepository MainNuGetRepository = |
| 38 new PackageRepositoryFactory().CreateRepository("https://nuget.org/a
pi/v2/"); |
| 39 |
| 40 private const int NuGetPushTimeoutMills = 10000; |
| 41 |
| 42 static NuGetUtilities() |
| 43 { |
| 44 LocalNuGetPackageFolder = Path.Combine(Path.DirectorySeparatorChar.T
oString(), "LocalNuGetFeed"); |
| 45 if (!Directory.Exists(LocalNuGetPackageFolder)) |
| 46 { |
| 47 Directory.CreateDirectory(LocalNuGetPackageFolder); |
| 48 } |
| 49 } |
| 50 |
| 51 /// <summary> |
| 52 /// Creates a local nupkg file, and stores it in a local folder (<see cr
ef="LocalNuGetPackageFolder"/>). |
| 53 /// </summary> |
| 54 /// <returns>The path to the nupkg file</returns> |
| 55 public static string CreateLocalNupkgFile(string nuspecFilePath, string
directory) |
| 56 { |
| 57 var packageName = nuspecFilePath.Substring(nuspecFilePath.LastIndexO
f(Path.DirectorySeparatorChar) + 1) |
| 58 .Replace(".nuspec", ".nupkg"); |
| 59 var nupkg = Path.Combine(LocalNuGetPackageFolder, packageName); |
| 60 |
| 61 // read the nuspec metadata file· |
| 62 using (var stream = new FileStream(nuspecFilePath, FileMode.Open, Fi
leAccess.Read)) |
| 63 { |
| 64 TraceSource.TraceEvent(TraceEventType.Verbose, "Creating {0}", p
ackageName); |
| 65 // create the package and save it to disk |
| 66 var builder = new PackageBuilder(stream, directory); |
| 67 using (FileStream nupkgStream = File.Open(nupkg, FileMode.Create
)) |
| 68 { |
| 69 builder.Save(nupkgStream); |
| 70 } |
| 71 TraceSource.TraceEvent(TraceEventType.Information, "{0} was crea
ted", nupkg); |
| 72 } |
| 73 return nupkg; |
| 74 } |
| 75 |
| 76 /// <summary>Publishes to the NuGet main repository.</summary> |
| 77 public static void PublishToNuget(string packagePath, string nugetApiKey
) |
| 78 { |
| 79 TraceSource.TraceEvent(TraceEventType.Verbose, "Pushing \"{0}\" to N
uGet repository", packagePath); |
| 80 PackageServer packageServer = new PackageServer("https://nuget.org/"
, "Google.Apis"); |
| 81 try |
| 82 { |
| 83 packageServer.PushPackage(nugetApiKey, new ZipPackage(packagePat
h), NuGetPushTimeoutMills); |
| 84 TraceSource.TraceEvent(TraceEventType.Information, "\"{0}\" was
pushed successfully", packagePath); |
| 85 } |
| 86 catch (Exception ex) |
| 87 { |
| 88 TraceSource.TraceEvent(TraceEventType.Error, "Can't publish \"{0
}\" to NuGet repository. " + |
| 89 "Exception is: {1}", packagePath, ex.Message); |
| 90 } |
| 91 } |
| 92 |
| 93 /// <summary> |
| 94 /// Returns <c>true</c> if a NuGet package already exists in the main Nu
Get repository for the input name and· |
| 95 /// version. |
| 96 /// </summary> |
| 97 public static bool DoesNugetPackageExist(string packageName, string vers
ion) |
| 98 { |
| 99 IPackage package; |
| 100 return MainNuGetRepository.TryFindPackage(packageName, new SemanticV
ersion(version), out package); |
| 101 } |
| 102 } |
| 103 } |
LEFT | RIGHT |