public void Emit(PackRoot root) { var package = _nugetDependencyResolver.FindCandidate( _libraryDescription.Identity.Name, _libraryDescription.Identity.Version); Console.WriteLine("Packing nupkg dependency {0} {1}", package.Id, package.Version); var resolver = new DefaultPackagePathResolver(root.PackagesPath); TargetPath = resolver.GetInstallPath(package.Id, package.Version); if (Directory.Exists(TargetPath)) { if (root.Overwrite) { root.Operations.Delete(TargetPath); } else { Console.WriteLine(" {0} already exists.", TargetPath); return; } } Console.WriteLine(" Target {0}", TargetPath); var targetNupkgPath = resolver.GetPackageFilePath(package.Id, package.Version); var hashPath = resolver.GetHashPath(package.Id, package.Version); using (var sourceStream = package.GetStream()) { using (var archive = new ZipArchive(sourceStream, ZipArchiveMode.Read)) { root.Operations.ExtractNupkg(archive, TargetPath); } } using (var sourceStream = package.GetStream()) { using (var targetStream = new FileStream(targetNupkgPath, FileMode.Create, FileAccess.Write, FileShare.None)) { sourceStream.CopyTo(targetStream); } sourceStream.Seek(0, SeekOrigin.Begin); var sha512Bytes = SHA512.Create().ComputeHash(sourceStream); File.WriteAllText(hashPath, Convert.ToBase64String(sha512Bytes)); } }
private void WriteGlobalJson() { var rootDirectory = ProjectResolver.ResolveRootDirectory(_project.ProjectDirectory); var projectResolver = new ProjectResolver(_project.ProjectDirectory, rootDirectory); var packagesDir = NuGetDependencyResolver.ResolveRepositoryPath(rootDirectory); var nugetDependencyResolver = new NuGetDependencyResolver(packagesDir, new EmptyFrameworkResolver()); var pathResolver = new DefaultPackagePathResolver(PackagesPath); var dependenciesObj = new JObject(); // Generate SHAs for all package dependencies foreach (var deploymentPackage in Packages) { // Use the exactly same approach in PackPackage.Emit() to // find the package actually in use var package = nugetDependencyResolver.FindCandidate( deploymentPackage.Library.Name, deploymentPackage.Library.Version); var shaFilePath = pathResolver.GetHashPath(package.Id, package.Version); var sha = File.ReadAllText(shaFilePath); var shaObj = new JObject(); shaObj.Add(new JProperty("version", package.Version.ToString())); shaObj.Add(new JProperty("sha", sha)); dependenciesObj.Add(new JProperty(package.Id, shaObj)); } // If "--no-source" is specified, project dependencies are packed to packages // So we also generate SHAs for them in this case foreach (var deploymentProject in Projects) { Runtime.Project project; if (!projectResolver.TryResolveProject(deploymentProject.Name, out project)) { throw new Exception("TODO: unable to resolve project named " + deploymentProject.Name); } var shaFilePath = pathResolver.GetHashPath(project.Name, project.Version); if (!File.Exists(shaFilePath)) { // This project is not packed to a package continue; } var sha = File.ReadAllText(shaFilePath); var shaObj = new JObject(); shaObj.Add(new JProperty("version", project.Version.ToString())); shaObj.Add(new JProperty("sha", sha)); dependenciesObj.Add(new JProperty(project.Name, shaObj)); } var rootObject = default(JObject); if (GlobalSettings.HasGlobalFile(rootDirectory)) { rootObject = JObject.Parse(File.ReadAllText(Path.Combine( rootDirectory, GlobalSettings.GlobalFileName))); } else { rootObject = new JObject(); } var applicationRoot = Path.Combine(OutputPath, PackRoot.AppRootName); rootObject["dependencies"] = dependenciesObj; rootObject["packages"] = PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(applicationRoot), PackagesPath); File.WriteAllText(Path.Combine(applicationRoot, GlobalSettings.GlobalFileName), rootObject.ToString()); }