/// <summary> /// Clean all temporary files created thus far during store operations. /// </summary> public void PurgeCache() { // Whenever we look at the content of a package or extract it using the default // repository, NuGet expands the files in a global temporary folders. It needs to // be purged. OptimizedZipPackage.PurgeCache(); }
private void OnBeginShutDown() { _dteEvents.OnBeginShutdown -= OnBeginShutDown; _dteEvents = null; OptimizedZipPackage.PurgeCache(); }
protected void ProcessReferenceInformation() { Debug.WriteLine("\n--- Handling project references ---"); var optimizedZipPackages = new Dictionary <string, OptimizedZipPackage>(); Debug.WriteLine("adding referred NuBuild projects to dependencies by each targetFramework"); foreach (var prjFactory in nuBuildReferenceProjectFactories) { Debug.WriteLine(" project: {0}", (object)Path.GetFileName(prjFactory.FullPath)); foreach (var nuTarget in prjFactory.NuTargets) { Debug.WriteLine(" package: {0}", (object)Path.GetFileName(nuTarget)); OptimizedZipPackage package; if (!optimizedZipPackages.TryGetValue(nuTarget, out package)) { optimizedZipPackages.Add(nuTarget, package = new OptimizedZipPackage(nuTarget)); } var dependency = new PackageDependency(package.Id, new VersionSpec() { IsMinInclusive = true, MinVersion = package.Version, MaxVersion = limitMajorVersionOfDependencies ? package.Version.GetLimitingMajor() : null, }); foreach (var targetFramework in targetFrameworks) { Debug.WriteLine(" dependency: {0} | {1} | {2}", targetFramework, dependency.Id, dependency.VersionSpec); packagesAndDependenciesByFramework[targetFramework].Add( package.Id, new Tuple <IPackage, PackageDependency>(package, dependency)); } } } }
void WriteFileContext(string path) { string fileContent; if (!File.Exists(path)) { return; } if (Path.GetExtension(path) == Constants.PackageExtension) { var pkg = new OptimizedZipPackage(path); var ver1 = pkg.Version.ToString(); var ver2 = pkg.Version.Version.ToString(); if (ver1 == ver2 || $"{ver1}.0" == ver2) { fileContent = ver2; } else { fileContent = $"{ver1} (normalized: {ver2})"; } } else { fileContent = File.ReadAllText(path); } string shortPath = InstallContext.NormalizeMessage(path); LogService.console.Info($"{shortPath}: {fileContent}"); }
public void EnsureProjectFactoryDoesNotAddFileThatIsAlreadyInPackage() { // Setup var nugetexe = Util.GetNuGetExePath(); using (var workingDirectory = TestFileSystemUtility.CreateRandomTestFolder()) { // Arrange var projPath = Path.Combine(workingDirectory, "Assembly.csproj"); File.WriteAllText(projPath, GetProjectContent()); File.WriteAllText(Path.Combine(workingDirectory, "Assembly.nuspec"), GetNuspecContent()); File.WriteAllText(Path.Combine(workingDirectory, "Source.cs"), GetSourceFileContent()); // Act var r = CommandRunner.Run( nugetexe, workingDirectory, "pack Assembly.csproj -build", waitForExit: true); // Assert var package = new OptimizedZipPackage(Path.Combine(workingDirectory, "Assembly.1.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); Assert.Equal(0, r.Item1); Array.Sort(files); Assert.Equal(files, new[] { @"lib\net45\Assembly.dll", @"lib\net45\Assembly.xml" }); } }
void InstalledPackageIs_1_0() { var packageFile = Path.Combine(InstallContext.Instance.PackagesLocation, lastconf.PackageNames, lastconf.PackageNames + Constants.PackageExtension); var package = new OptimizedZipPackage(packageFile); Assert.AreEqual(package.Version.Version.to_string(), "1.0.0.0"); }
/// <summary> /// Installs the package locally and pushes the package onto the destination server /// Uses OptimizedPackage much like the 'nuget push' command to prevent issues caused by /// holding onto package as a stream in memory /// </summary> private void MirrorPackage(DataServicePackageWithCreated package, PackageServer destinationServer, IPackageManager tempPackageManager, LocalPackageRepository tempLocalRepo, string apiKey, int timeOut) { // Download the package locally into a temp folder. This prevents storing the package in memory // which becomes an issue with large packages. Push command uses OptimizedZipPackage and we will too string localPackagePath = String.Empty; OptimizedZipPackage localPackage = null; try { Log.AddingPackageLocally(package.ToString(), package.Created.Value.DateTime.ToString()); tempPackageManager.InstallPackage(package.Id, package.SemanticVersion, ignoreDependencies: true, allowPrereleaseVersions: true); Log.AddedPackageLocally(package.ToString()); } catch (Exception ex) { throw new SourceException(ex); } try { // Push the local package onto destination Repository var localInstallPath = tempLocalRepo.PathResolver.GetInstallPath(package); localPackagePath = Path.Combine(localInstallPath, tempLocalRepo.PathResolver.GetPackageFileName(package)); localPackage = new OptimizedZipPackage(localPackagePath); Log.PushingPackage(localPackage.ToString()); destinationServer.PushPackage(apiKey, localPackage, new FileInfo(localPackagePath).Length, timeOut, disableBuffering: false); } catch (Exception ex) { throw new DestinationException(ex); } }
public void CreatePackage(HttpContextBase context) { var request = context.Request; // Get the api key from the header var apiKey = request.Headers[ApiKeyHeader]; // Get the package from the request body // ReSharper disable once PossibleNullReferenceException var stream = request.Files.Count > 0 ? request.Files[0].InputStream : request.InputStream; // Copy the package to a temporary file var temporaryFile = Path.GetTempFileName(); using (var temporaryFileStream = File.Open(temporaryFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { stream.CopyTo(temporaryFileStream); } OptimizedZipPackage package = null; try { package = new OptimizedZipPackage(temporaryFile); } catch (Exception ex) { Console.Write("\n failed {0} - {1}", temporaryFile, ex.Message); } if (package == null) { return; } // Make sure the user can access this package if (Authenticate(context, apiKey, package.Id)) { try { _serverRepository.AddPackage(package); // Add(package); WriteStatus(context, HttpStatusCode.Created, ""); } catch (InvalidOperationException ex) { WriteStatus(context, HttpStatusCode.InternalServerError, ex.Message); } } package = null; try { File.Delete(temporaryFile); } catch (Exception) { WriteStatus(context, HttpStatusCode.InternalServerError, "Could not remove temporary upload file."); } }
private void OnBeginShutDown() { _dteEvents.OnBeginShutdown -= OnBeginShutDown; _dteEvents = null; // Clean up optimized zips used by NuGet.Core as part of the V2 Protocol OptimizedZipPackage.PurgeCache(); }
public virtual async Task <HttpResponseMessage> UploadPackage(CancellationToken token) { if (_authenticationService == null) { return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Package upload is not allowed")); } var apiKey = GetApiKeyFromHeader(); // Copy the package to a temporary file var temporaryFile = Path.GetTempFileName(); HttpResponseMessage retValue; try { using (var temporaryFileStream = File.Open(temporaryFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { if (Request.Content.IsMimeMultipartContent()) { var multipartContents = await Request.Content.ReadAsMultipartAsync(); await multipartContents.Contents.First().CopyToAsync(temporaryFileStream); } else { await Request.Content.CopyToAsync(temporaryFileStream); } } var package = new OptimizedZipPackage(temporaryFile); if (_authenticationService.IsAuthenticated(User, apiKey, package.Id)) { await _serverRepository.AddPackageAsync(package, token); retValue = Request.CreateResponse(HttpStatusCode.Created); } else { retValue = CreateStringResponse(HttpStatusCode.Forbidden, string.Format("Access denied for package '{0}'.", package.Id)); } package = null; } finally { try { File.Delete(temporaryFile); } catch (Exception) { retValue = CreateStringResponse(HttpStatusCode.InternalServerError, "Could not remove temporary upload file."); } } return(retValue); }
public void PublishPackage(string file, string apiKey) { var packageServer = new PackageServer("https://nuget.org/", "ripple"); var package = new OptimizedZipPackage(file); RippleLog.Info("Publishing " + file + " with " + apiKey); packageServer.PushPackage(apiKey, package, (int)60.Minutes().TotalMilliseconds); }
private IEnumerable <PackageIdentity> CreatePackageIdentityFromNupkgPath() { PackageIdentity identity = null; IPackage package = null; try { // Example: install-package2 https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg if (_isHttp) { PackageIdentity packageIdentity = ParsePackageIdentityFromHttpSource(Id); IPackageCacheRepository packageCache = this.Projects.FirstOrDefault().TryGetFeature <IPackageCacheRepository>(); IPackageName packageName = CoreConverters.SafeToPackageName(packageIdentity); SemanticVersion packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); Uri downloadUri = new Uri(Id); PackageDownloader downloader = new PackageDownloader(); // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } else { // Example: install-package2 c:\temp\packages\jQuery.1.10.2.nupkg string fullPath = Path.GetFullPath(Id); package = new OptimizedZipPackage(fullPath); } if (package != null) { Id = package.Id; Version = package.Version.ToString(); identity = new PackageIdentity(Id, NuGetVersion.Parse(Version)); } } catch (Exception ex) { Log(MessageLevel.Error, Resources.Cmdlet_FailToParsePackages, Id, ex.Message); } return(new List <PackageIdentity>() { identity }); }
/// <summary> /// This method requires <see cref="LockAndSuppressFileSystemWatcherAsync(CancellationToken)"/>. /// </summary> private void AddPackagesFromDropFolderWithoutLocking() { _logger.Log(LogLevel.Info, "Start adding packages from drop folder."); try { var serverPackages = new HashSet <ServerPackage>(IdAndVersionEqualityComparer.Instance); foreach (var packageFile in _fileSystem.GetFiles(_fileSystem.Root, "*.nupkg", recursive: false)) { try { // Create package var package = new OptimizedZipPackage(_fileSystem, packageFile); if (!CanPackageBeAddedWithoutLocking(package, shouldThrow: false)) { continue; } // Add the package to the file system store. var serverPackage = _serverPackageStore.Add( package, EnableDelisting); // Keep track of the the package for addition to metadata store. serverPackages.Add(serverPackage); // Remove file from drop folder _fileSystem.DeleteFile(packageFile); } catch (UnauthorizedAccessException ex) { // The file may be in use (still being copied) - ignore the error _logger.Log(LogLevel.Error, "Error adding package file {0} from drop folder: {1}", packageFile, ex.Message); } catch (IOException ex) { // The file may be in use (still being copied) - ignore the error _logger.Log(LogLevel.Error, "Error adding package file {0} from drop folder: {1}", packageFile, ex.Message); } } // Add packages to metadata store in bulk _serverPackageCache.AddRange(serverPackages, EnableDelisting); _serverPackageCache.PersistIfDirty(); _logger.Log(LogLevel.Info, "Finished adding packages from drop folder."); } finally { OptimizedZipPackage.PurgeCache(); } }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new OptimizedZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), sourceName); packageServer.PushPackage(apiKey, package, Convert.ToInt32(timeout.TotalMilliseconds)); Console.WriteLine(NuGetResources.PushCommandPackagePushed); }
public void PackCommand_ProjectReferencedByMultipleProjects() { var targetDir = ConfigurationManager.AppSettings["TargetDir"]; var nugetexe = Path.Combine(targetDir, "nuget.exe"); var workingDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); try { // Arrange Util.CreateDirectory(workingDirectory); // create test project, with the following dependency relationships: // proj1 depends on proj2, proj3 // proj2 depends on proj3 CreateTestProject(workingDirectory, "proj1", new string[] { @"..\proj2\proj2.csproj", @"..\proj3\proj3.csproj" }); CreateTestProject(workingDirectory, "proj2", new string[] { @"..\proj3\proj3.csproj" }); CreateTestProject(workingDirectory, "proj3", null); // Act var proj1Directory = Path.Combine(workingDirectory, "proj1"); var r = CommandRunner.Run( nugetexe, proj1Directory, "pack proj1.csproj -build -IncludeReferencedProjects", waitForExit: true); Assert.Equal(0, r.Item1); // Assert var package = new OptimizedZipPackage(Path.Combine(proj1Directory, "proj1.0.0.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); Array.Sort(files); Assert.Equal( files, new string[] { @"lib\net40\proj1.dll", @"lib\net40\proj2.dll", @"lib\net40\proj3.dll" }); } finally { Directory.Delete(workingDirectory, true); } }
/// <summary> /// Sets the current cache to null so it will be regenerated next time. /// </summary> public void ClearCache() { using (LockAndSuppressFileSystemWatcher()) { OptimizedZipPackage.PurgeCache(); _serverPackageStore.Clear(); _serverPackageStore.Persist(); _needsRebuild = true; _logger.Log(LogLevel.Info, "Cleared package cache."); } }
/// <summary> /// Sets the current cache to null so it will be regenerated next time. /// </summary> public async Task ClearCacheAsync(CancellationToken token) { using (await LockAndSuppressFileSystemWatcherAsync(token)) { OptimizedZipPackage.PurgeCache(); _serverPackageCache.Clear(); _serverPackageCache.Persist(); _needsRebuild = true; _logger.Log(LogLevel.Info, "Cleared package cache."); } }
/// <summary> /// CreateCache loads all packages and determines additional metadata such as the hash, IsAbsoluteLatestVersion, and IsLatestVersion. /// </summary> private ConcurrentDictionary <IPackage, DerivedPackageData> CreateCache() { ConcurrentDictionary <IPackage, DerivedPackageData> packages = new ConcurrentDictionary <IPackage, DerivedPackageData>(); ParallelOptions opts = new ParallelOptions(); opts.MaxDegreeOfParallelism = 4; ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> > absoluteLatest = new ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> >(); ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> > latest = new ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> >(); // get settings bool checkFrameworks = EnableFrameworkFiltering; bool enableDelisting = EnableDelisting; // we need to save the current context because it's stored in TLS and we're computing hashes on different threads. var context = HttpContext.Current; // load and cache all packages. // Note that we can't pass GetPackageFiles() to Parallel.ForEach() because // the file could be added/deleted from _fileSystem, and if this happens, // we'll get error "Collection was modified; enumeration operation may not execute." // So we have to materialize the IEnumerable into a list first. var packageFiles = GetPackageFiles().ToList(); Parallel.ForEach(packageFiles, opts, path => { OptimizedZipPackage zip = OpenPackage(path); Debug.Assert(zip != null, "Unable to open " + path); if (zip == null) { return; } if (enableDelisting) { // hidden packages are considered delisted zip.Listed = !File.GetAttributes(_fileSystem.GetFullPath(path)).HasFlag(FileAttributes.Hidden); } AddPackageToCache(packages, absoluteLatest, latest, checkFrameworks, context, path, zip); }); // Set additional attributes after visiting all packages foreach (var entry in absoluteLatest.Values) { entry.Item2.IsAbsoluteLatestVersion = true; } foreach (var entry in latest.Values) { entry.Item2.IsLatestVersion = true; } return(packages); }
public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.CommandExecutionTimeoutSeconds)); if (timeout.Seconds <= 0) { timeout = TimeSpan.FromMinutes(300); // Default to 5 hours if there is a zero (infinite) timeout } const bool disableBuffering = false; var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; var package = new OptimizedZipPackage(nupkgFilePath); try { packageServer.PushPackage( config.PushCommand.Key, package, new FileInfo(nupkgFilePath).Length, Convert.ToInt32(timeout.TotalMilliseconds), disableBuffering); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message)) { if (config.Sources == ApplicationParameters.ChocolateyCommunityFeedPushSource && message.Contains("already exists and cannot be modified")) { throw new ApplicationException("An error has occurred. This package version already exists on the repository and cannot be modified.{0}Package versions that are approved, rejected, or exempted cannot be modified.{0}See https://docs.chocolatey.org/en-us/community-repository/moderation/ for more information".format_with(Environment.NewLine), ex); } if (message.Contains("(406)") || message.Contains("(409)")) { // Let this fall through so the actual error message is shown when the exception is re-thrown "chocolatey".Log().Error("An error has occurred. It's possible the package version already exists on the repository or a nuspec element is invalid. See error below..."); } } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); }
// Use SharpCompress to get a list of files, and write to disk if not already there. If the files are corrupt, // we don't care. Delete them and try again. Significantly faster. private static bool New_NuGet_OptimizedZipPackage_EnsurePackageFiles(OptimizedZipPackage __instance, ref Dictionary <string, PhysicalPackageFile> ____files, ref string ____expandedFolderPath, IFileSystem ____expandedFileSystem) { if (____files != null) { return(false); } ____files = new Dictionary <string, PhysicalPackageFile>(); ____expandedFolderPath = __instance.Id + "." + __instance.Version; using (var stream = __instance.GetStream()) { using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read, true)) { foreach (var zipArchiveEntry in zipArchive.Entries) { var path = zipArchiveEntry.FullName; if (path.EndsWith(".nuspec") || path.EndsWith(".psmdcp") || path == "_rels/.rels" || path == "[Content_Types].xml") { continue; } var localPath = Path.Combine(____expandedFolderPath, path); if (!____expandedFileSystem.FileExists(localPath)) { using (var entryStream = zipArchiveEntry.Open()) { try { using (var file = ____expandedFileSystem.CreateFile(localPath)) entryStream.CopyTo(file); } catch (Exception) { } } } var physicalPackageFile = new PhysicalPackageFile { SourcePath = ____expandedFileSystem.GetFullPath(localPath), TargetPath = path }; ____files[path] = physicalPackageFile; } } } return(false); }
// POST /api/sign public async Task <HttpResponseMessage> Post([FromBody] WebHookEvent payload) { if (payload.Payload.PackageIdentifier.EndsWith(ConfigurationManager.AppSettings["Signature:PackageIdSuffix"])) { return(new HttpResponseMessage(HttpStatusCode.OK) { ReasonPhrase = "Package is already signed. " }); } string tempPath = Path.GetTempFileName(); try { // Download the package var httpClient = new HttpClient(); var packageStream = await httpClient.GetStreamAsync(payload.Payload.PackageDownloadUrl); using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write)) { packageStream.CopyTo(stream); } // Sign the package PackageSigner signer = new PackageSigner(); if (signer.SignPackage(tempPath, tempPath, System.Web.Hosting.HostingEnvironment.MapPath("~/" + ConfigurationManager.AppSettings["Signature:KeyFile"]), ConfigurationManager.AppSettings["Signature:KeyFilePassword"], payload.Payload.PackageIdentifier + ConfigurationManager.AppSettings["Signature:PackageIdSuffix"])) { var server = new PackageServer(ConfigurationManager.AppSettings["Signature:NuGetFeedUrl"], "Signature/1.0"); server.PushPackage(ConfigurationManager.AppSettings["Signature:NuGetFeedApiKey"], new OptimizedZipPackage(tempPath), new FileInfo(tempPath).Length, 60 * 1000, true); OptimizedZipPackage.PurgeCache(); return(new HttpResponseMessage(HttpStatusCode.Created) { ReasonPhrase = "Package has been signed." }); } } finally { File.Delete(tempPath); } return(new HttpResponseMessage(HttpStatusCode.OK) { ReasonPhrase = "Package is already signed." }); }
private void BuildPackage(PackageBuilder builder, bool analyzePackage, string outputPath = null) { if (!String.IsNullOrEmpty(Version)) { builder.Version = new SemanticVersion(Version); } if (_minClientVersionValue != null) { builder.MinClientVersion = _minClientVersionValue; } outputPath = outputPath ?? GetOutputPath(builder); ExcludeFiles(builder.Files); // Track if the package file was already present on disk bool isExistingPackage = File.Exists(outputPath); try { using (Stream stream = File.Create(outputPath)) { builder.Save(stream); } } catch { if (!isExistingPackage && File.Exists(outputPath)) { File.Delete(outputPath); } throw; } if (Verbosity == Verbosity.Detailed) { PrintVerbose(outputPath); } var package = new OptimizedZipPackage(outputPath); if (analyzePackage) { AnalyzePackage(package); } Console.WriteLine(LocalizedResourceManager.GetString("PackageCommandSuccess"), outputPath); }
public void EnsureProjectFactoryWorksAsExpectedWithReferenceOutputAssemblyValuesComplex() { // Setup var nugetexe = Util.GetNuGetExePath(); using (var workingDirectory = TestFileSystemUtility.CreateRandomTestFolder()) { // Setup the projects DummyProject link = new DummyProject("Link", Path.Combine(workingDirectory, "Link\\Link.csproj")); DummyProject a = new DummyProject("A", Path.Combine(workingDirectory, "A\\A.csproj")); DummyProject b = new DummyProject("B", Path.Combine(workingDirectory, "B\\B.csproj")); DummyProject c = new DummyProject("C", Path.Combine(workingDirectory, "C\\C.csproj")); DummyProject d = new DummyProject("D", Path.Combine(workingDirectory, "D\\D.csproj")); DummyProject e = new DummyProject("E", Path.Combine(workingDirectory, "E\\E.csproj")); link.AddProjectReference(a, false); link.AddProjectReference(b, true); a.AddProjectReference(c, false); c.AddProjectReference(d, true); b.AddProjectReference(e, false); link.WriteToFile(); a.WriteToFile(); b.WriteToFile(); c.WriteToFile(); d.WriteToFile(); e.WriteToFile(); // Act var r = CommandRunner.Run( nugetexe, workingDirectory, "pack Link\\Link.csproj -build -IncludeReferencedProjects -Version 1.0.0", waitForExit: true); // Assert Util.VerifyResultSuccess(r); var package = new OptimizedZipPackage(Path.Combine(workingDirectory, "Link.1.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); Assert.Equal(0, r.Item1); Array.Sort(files); Assert.Equal(files, new[] { @"lib\net45\A.dll", @"lib\net45\C.dll", @"lib\net45\Link.dll" }); } }
private void PushPackageCore(string source, string apiKey, PackageServer packageServer, string packageToPush, TimeSpan timeout) { // Push the package to the server var package = new OptimizedZipPackage(packageToPush); string sourceName = CommandLineUtility.GetSourceDisplayName(source); Console.WriteLine(LocalizedResourceManager.GetString("PushCommandPushingPackage"), package.GetFullName(), sourceName); packageServer.PushPackage( apiKey, package, new FileInfo(packageToPush).Length, Convert.ToInt32(timeout.TotalMilliseconds), DisableBuffering); Console.WriteLine(LocalizedResourceManager.GetString("PushCommandPackagePushed")); }
async Task <string> installPackageToAppDir(UpdateInfo updateInfo, ReleaseEntry release) { string tmpDir = default(string); bool shouldDeleteTmpDir = findShortTemporaryDir(out tmpDir); var fs = new PhysicalFileSystem(tmpDir); var pkg = new OptimizedZipPackage(fs, Path.Combine(updateInfo.PackageDirectory, release.Filename)); var target = getDirectoryForRelease(release.Version); // NB: This might happen if we got killed partially through applying the release if (target.Exists) { this.Log().Warn("Found partially applied release folder, killing it: " + target.FullName); await Utility.DeleteDirectory(target.FullName); } target.Create(); // Copy all of the files out of the lib/ dirs in the NuGet package // into our target App directory. // // NB: We sort this list in order to guarantee that if a Net20 // and a Net40 version of a DLL get shipped, we always end up // with the 4.0 version. this.Log().Info("Writing files to app directory: {0}", target.FullName); var toWrite = pkg.GetLibFiles().Where(x => pathIsInFrameworkProfile(x, appFrameworkVersion)) .OrderBy(x => x.Path) .ToList(); // NB: Because of the above NB, we cannot use ForEachAsync here, we // have to copy these files in-order. Once we fix assembly resolution, // we can kill both of these NBs. await Task.Run(() => toWrite.ForEach(x => copyFileToLocation(target, x))); await pkg.GetContentFiles().ForEachAsync(x => copyFileToLocation(target, x)); if (shouldDeleteTmpDir) { await Utility.DeleteDirectory(tmpDir); } return(target.FullName); }
public void PackCommand_ReferencedProjectWithDifferentTarget() { var targetDir = ConfigurationManager.AppSettings["TargetDir"]; var nugetexe = Path.Combine(targetDir, "nuget.exe"); var workingDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); try { // Arrange Util.CreateDirectory(workingDirectory); CreateTestProject(workingDirectory, "proj1", new string[] { @"..\proj2\proj2.csproj" }); CreateTestProject(workingDirectory, "proj2", null, "v3.0"); // Act var proj1Directory = Path.Combine(workingDirectory, "proj1"); var r = CommandRunner.Run( nugetexe, proj1Directory, "pack proj1.csproj -build -IncludeReferencedProjects", waitForExit: true); Assert.Equal(0, r.Item1); // Assert var package = new OptimizedZipPackage(Path.Combine(proj1Directory, "proj1.0.0.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); Array.Sort(files, StringComparer.OrdinalIgnoreCase); Assert.Equal( files, new string[] { @"lib\net40\proj1.dll", @"lib\net40\proj2.dll" }); } finally { Directory.Delete(workingDirectory, true); } }
public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.PushCommand.TimeoutInSeconds)); if (timeout.Seconds <= 0) { timeout = TimeSpan.FromMinutes(5); // Default to 5 minutes } const bool disableBuffering = false; var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; var package = new OptimizedZipPackage(nupkgFilePath); try { packageServer.PushPackage( config.PushCommand.Key, package, new FileInfo(nupkgFilePath).Length, Convert.ToInt32(timeout.TotalMilliseconds), disableBuffering); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message) && message.Contains("(500) Internal Server Error")) { throw new ApplicationException("There was an internal server error, which might mean the package already exists on a Simple OData Server.", ex); } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); }
public void TestingCtorWithFileSystemAndPackagePath() { // Arrange var ms = GetPackageStream(); var fileSystem = new MockFileSystem("x:\\"); fileSystem.AddFile("pam.nupkg", ms); // Act var ozp = new OptimizedZipPackage(fileSystem, "pam.nupkg", new MockFileSystem("y:\\")); // Assert Assert.Equal("Package", ozp.Id); Assert.Equal(new SemanticVersion("1.0"), ozp.Version); Assert.Equal("This is a test package", ozp.Description); Assert.Equal("This is a release note.", ozp.ReleaseNotes); Assert.Equal("Copyright", ozp.Copyright); Assert.Equal("dotnetjunky", ozp.Authors.First()); // Order is not gauranteed (or required) from GetFiles(), // but we rely on the order for a few of the asserts, // and it appears to not behave the same way on Mono, // so we call "order by" here to force a specific order. var files = ozp.GetFiles().OrderBy(k => k.Path).ToList(); Assert.Equal(2, files.Count); Assert.Equal(PathFixUtility.FixPath(@"content\foo"), files[0].Path); Assert.Equal(PathFixUtility.FixPath(@"lib\40\A.dll"), files[1].Path); var assemblyReferences = ozp.AssemblyReferences.ToList(); Assert.Equal(1, assemblyReferences.Count); Assert.Equal("A.dll", assemblyReferences[0].Name); Assert.Equal(new FrameworkName(".NETFramework", new Version("4.0")), assemblyReferences[0].TargetFramework); var supportedReferences = ozp.GetSupportedFrameworks().OrderBy(p => p.FullName).ToList(); Assert.Equal(3, supportedReferences.Count); Assert.Equal(new FrameworkName(".NETFramework", new Version("4.0")), supportedReferences[0]); Assert.Equal(new FrameworkName("Silverlight", new Version("5.0")), supportedReferences[1]); Assert.Equal(new FrameworkName("Windows", new Version("8.0")), supportedReferences[2]); }
/// <summary> /// Sets the current cache to null so it will be regenerated next time. /// </summary> public void ClearCache() { MonitorFileSystem(false); try { lock (_syncLock) { OptimizedZipPackage.PurgeCache(); _serverPackageStore.Clear(); _serverPackageStore.Persist(); _logger.Log(LogLevel.Info, "Cleared package cache."); } } finally { MonitorFileSystem(true); } }
public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.CommandExecutionTimeoutSeconds)); if (timeout.Seconds <= 0) { timeout = TimeSpan.FromMinutes(300); // Default to 5 hours if there is a zero (infinite) timeout } const bool disableBuffering = false; var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); packageServer.SendingRequest += (sender, e) => { if (config.Verbose) { "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); } }; var package = new OptimizedZipPackage(nupkgFilePath); try { packageServer.PushPackage( config.PushCommand.Key, package, new FileInfo(nupkgFilePath).Length, Convert.ToInt32(timeout.TotalMilliseconds), disableBuffering); } catch (InvalidOperationException ex) { var message = ex.Message; if (!string.IsNullOrWhiteSpace(message) && (message.Contains("(406)") || message.Contains("(409)"))) { throw new ApplicationException("An error has occurred. It's possible the package version already exists on the repository.", ex); } throw; } "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); }
public void EnsureProjectFactoryDoesNotAddFileThatIsAlreadyInPackage() { // Setup var targetDir = ConfigurationManager.AppSettings["TargetDir"]; var nugetexe = Path.Combine(targetDir, "nuget.exe"); var workingDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); try { // Arrange Util.CreateDirectory(workingDirectory); Util.CreateFile(workingDirectory, "Assembly.nuspec", GetNuspecContent()); Util.CreateFile(workingDirectory, "Assembly.csproj", GetProjectContent()); Util.CreateFile(workingDirectory, "Source.cs", GetSourceFileContent()); var projPath = Path.Combine(workingDirectory, "Assembly.csproj"); // Act var actual = CommandRunner.Run( nugetexe, workingDirectory, "pack Assembly.csproj -build", waitForExit: true); var package = new OptimizedZipPackage(Path.Combine(workingDirectory, "Assembly.1.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); // Assert Assert.Equal(0, actual.Item1); Array.Sort(files); Assert.Equal(files, new[] { @"lib\net45\Assembly.dll", @"lib\net45\Assembly.xml" }); } finally { // Teardown Directory.Delete(workingDirectory, true); } }
public void Execute(NewPackageAction action, IExecutionContext context, CancellationToken cancelToken) { string downloadUriStr = action.Package[Properties.PackageContent].ToString(); Uri downloadUri; if (String.IsNullOrEmpty(downloadUriStr)) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.DownloadActionHandler_NoDownloadUrl, action.PackageIdentity)); } else if (!Uri.TryCreate(downloadUriStr, UriKind.Absolute, out downloadUri)) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.DownloadActionHandler_InvalidDownloadUrl, action.PackageIdentity, downloadUriStr)); } // Get required features from the target var packageManager = action.Target.GetRequiredFeature<IPackageManager>(); var packageCache = action.Target.TryGetFeature<IPackageCacheRepository>(); // Load the package IPackage package = null; if (downloadUri.IsFile) { // To keep feature backward-compatbility. The .nupkg file on disk may have a shorter name than semantic version. // For example: TestPackage 2.0.0.0 may be saved as TestPackage.2.0.nupkg on disk. SemanticVersion originalVersion = new SemanticVersion(action.PackageIdentity.Version.ToString()); IEnumerable<SemanticVersion> possibleVersions = VersionUtility.GetPossibleVersions(originalVersion); foreach (SemanticVersion version in possibleVersions) { // Get other alternative download path string downloadPath = downloadUri.LocalPath.Replace(originalVersion.ToString(), version.ToString()); try { package = new OptimizedZipPackage(downloadPath); break; } catch (ArgumentException) { } } // Verify the version of OptimizedZipPackage is expected. if (package == null || package.Version != originalVersion) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.FileDoesNotExit, downloadUri.LocalPath)); } } else { package = GetPackage(packageCache, action.PackageIdentity, downloadUri); } NuGetTraceSources.ActionExecutor.Verbose( "download/loadedpackage", "[{0}] Loaded package.", action.PackageIdentity); // Now convert the action and use the V2 Execution logic since we // have a true V2 IPackage (either from the cache or an in-memory ZipPackage). packageManager.Logger = new ShimLogger(context); packageManager.Execute(new PackageOperation( package, NuGet.PackageAction.Install)); // Run init.ps1 if present. Init is run WITHOUT specifying a target framework. ActionHandlerHelpers.ExecutePowerShellScriptIfPresent( "init.ps1", action.Target, package, packageManager.PathResolver.GetInstallPath(package), context); }
private void PrintVerbose(string outputPath) { Console.WriteLine(); var package = new OptimizedZipPackage(outputPath); Console.WriteLine("Id: {0}", package.Id); Console.WriteLine("Version: {0}", package.Version); Console.WriteLine("Authors: {0}", String.Join(", ", package.Authors)); Console.WriteLine("Description: {0}", package.Description); if (package.LicenseUrl != null) { Console.WriteLine("License Url: {0}", package.LicenseUrl); } if (package.ProjectUrl != null) { Console.WriteLine("Project Url: {0}", package.ProjectUrl); } if (!String.IsNullOrEmpty(package.Tags)) { Console.WriteLine("Tags: {0}", package.Tags.Trim()); } if (package.DependencySets.Any()) { Console.WriteLine("Dependencies: {0}", String.Join(", ", package.DependencySets.SelectMany(d => d.Dependencies).Select(d => d.ToString()))); } else { Console.WriteLine("Dependencies: None"); } Console.WriteLine(); foreach (var file in package.GetFiles().OrderBy(p => p.Path)) { Console.WriteLine(LocalizedResourceManager.GetString("PackageCommandAddedFile"), file.Path); } Console.WriteLine(); }
public void Execute(NewPackageAction action, IExecutionContext context, CancellationToken cancelToken) { var nugetAware = action.Target.TryGetFeature<NuGetAwareProject>(); if (nugetAware != null) { // TODO: this is a hack to get the supported frameworks. Since action.Package // does not contain this info for now, we have to download the package to // get this info. var packageContent = action.Package[Properties.PackageContent].ToString(); var downloadUri = new Uri(packageContent); IPackage package; if (downloadUri.IsFile) { package = new OptimizedZipPackage(packageContent); } else { var packageCache = action.Target.GetRequiredFeature<IPackageCacheRepository>(); package = DownloadActionHandler.GetPackage( packageCache, action.PackageIdentity, downloadUri); } var frameworks = package.GetSupportedFrameworks(); var task = nugetAware.InstallPackage( action.PackageIdentity, frameworks, context, cancelToken); task.Wait(); } else { // TODO: PMC - Write Disclamer Texts // TODO: Dialog & PMC - open Readme.txt // Get the package manager and project manager from the target var packageManager = action.Target.GetRequiredFeature<IPackageManager>(); var projectManager = action.Target.GetRequiredFeature<IProjectManager>(); // Get the package from the shared repository var package = packageManager.LocalRepository.FindPackage( action.PackageIdentity.Id, CoreConverters.SafeToSemanticVersion(action.PackageIdentity.Version)); Debug.Assert(package != null); // The package had better be in the local repository!! // Ping the metrics service action.Source.RecordMetric( action.ActionType, action.PackageIdentity, action.DependentPackage, action.IsUpdate, action.Target); // Add the package to the project projectManager.Logger = new ShimLogger(context); projectManager.Project.Logger = projectManager.Logger; projectManager.Execute(new PackageOperation( package, NuGet.PackageAction.Install)); // Run install.ps1 if present ActionHandlerHelpers.ExecutePowerShellScriptIfPresent( "install.ps1", action.Target, package, packageManager.PathResolver.GetInstallPath(package), context); } }
private IEnumerable<PackageIdentity> CreatePackageIdentityFromNupkgPath() { PackageIdentity identity = null; IPackage package = null; try { // Example: install-package2 https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg if (_isHttp) { PackageIdentity packageIdentity = ParsePackageIdentityFromHttpSource(Id); IPackageCacheRepository packageCache = this.Projects.FirstOrDefault().TryGetFeature<IPackageCacheRepository>(); IPackageName packageName = CoreConverters.SafeToPackageName(packageIdentity); SemanticVersion packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); Uri downloadUri = new Uri(Id); PackageDownloader downloader = new PackageDownloader(); // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } else { // Example: install-package2 c:\temp\packages\jQuery.1.10.2.nupkg string fullPath = Path.GetFullPath(Id); package = new OptimizedZipPackage(fullPath); } if (package != null) { Id = package.Id; Version = package.Version.ToString(); identity = new PackageIdentity(Id, NuGetVersion.Parse(Version)); } } catch (Exception ex) { Log(MessageLevel.Error, Resources.Cmdlet_FailToParsePackages, Id, ex.Message); } return new List<PackageIdentity>() { identity }; }
private OptimizedZipPackage OpenPackage(string path) { OptimizedZipPackage zip = null; if (_fileSystem.FileExists(path)) { try { zip = new OptimizedZipPackage(_fileSystem, path); } catch (FileFormatException ex) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.ErrorReadingPackage, path), ex); } // Set the last modified date on the package zip.Published = _fileSystem.GetLastModified(path); } return zip; }