internal static void DoRemoveFromNuGetCache(IActivityMonitor m, string packageId, SVersion version) { var dirPath = _localNuGetCache.AppendPart(packageId).AppendPart(version.ToNormalizedString()); if (FileHelper.RawDeleteLocalDirectory(m, dirPath)) { m.Info($"Removed {packageId} package in version {version} from local NuGet cache."); } }
/// <summary> /// /// </summary> /// <param name="dirPath">The dir path must exist.</param> /// <param name="name"></param> /// <param name="kind"></param> /// <returns></returns> public static NPMDep CreateNPMDepLocalFeedTarball(NormalizedPath dirPath, string name, ArtifactDependencyKind kind, SVersion version) { string fileName = $"{name.Replace( "@", "" ).Replace( "/", "-" )}-{version.ToNormalizedString()}.tgz"; return(new NPMDep(name, NPMVersionDependencyType.LocalFeedTarball, kind, $"file:{ dirPath.AppendPart( fileName ).ToString()}", version)); }
public async Task <bool> ExistAsync(IActivityMonitor m, string packageName, SVersion version) { if (RegistryUri.IsFile) { return(File.Exists(Path.Combine(RegistryUri.AbsolutePath, +'-' + version.ToNormalizedString() + ".tgz"))); } (_, bool exist) = await View(m, packageName, version); return(exist); }
/// <summary> /// Set the package version and change the project reference to package reference. /// </summary> /// <param name="p"></param> /// <param name="version"></param> /// <param name="preparePack"></param> /// <param name="packageJsonPreProcessor"></param> /// <returns></returns> public static IDisposable TemporaryReplacePackageVersion( NormalizedPath jsonPath, SVersion version) { (string content, TempFileTextModification temp) = CreateTempFileTextModification(jsonPath); JObject json = JObject.Parse(content); json["version"] = version.ToNormalizedString(); File.WriteAllText(jsonPath, json.ToString()); return(temp); }
async Task <(string, bool)> LegacyViewWithVersion(IActivityMonitor m, string packageName, SVersion version, bool abreviated) { (string body, HttpStatusCode statusCode) = await ViewRequest(m, packageName, abreviated); if (!IsSuccessStatusCode(statusCode)) { return(body, false); } // Here the request is successful so we should have valid json. JObject fullData = JObject.Parse(body); var v = fullData["versions"][version.ToNormalizedString()]; return(v?.ToString() ?? "", v != null); }
/// <summary> /// Set the package version and change the project reference to package reference. /// </summary> /// <param name="p"></param> /// <param name="version"></param> /// <param name="preparePack"></param> /// <param name="packageJsonPreProcessor"></param> /// <returns></returns> public static IDisposable TemporaryReplacePackageVersion( NPMSolution npmSolution, NormalizedPath jsonPath, SVersion version, bool preparePack, Action <JObject> packageJsonPreProcessor) { (string content, TempFileTextModification temp) = CreateTempFileTextModification(jsonPath); JObject json = JObject.Parse(content); json["version"] = version.ToNormalizedString(); if (preparePack) { json.Remove("devDependencies"); json.Remove("scripts"); string[] _dependencyPropNames = new string[] { "dependencies", "peerDependencies", "devDependencies", "bundledDependencies", "optionalDependencies", }; foreach (var dependencyPropName in _dependencyPropNames) { if (json.ContainsKey(dependencyPropName)) { JObject dependencies = (JObject)json[dependencyPropName]; foreach (KeyValuePair <string, JToken> keyValuePair in dependencies) { if (npmSolution.AllPublishedProjects.FirstOrDefault(x => x.PackageJson.Name == keyValuePair.Key) is NPMPublishedProject localProject) { dependencies[keyValuePair.Key] = new JValue("^" + version); } } } } } packageJsonPreProcessor?.Invoke(json); File.WriteAllText(jsonPath, json.ToString()); return(temp); }
public async Task <bool> AddDistTag(IActivityMonitor m, string packageName, SVersion version, string tagName) { if (RegistryUri.IsFile) { m.Warn("Dist tags are not supported on a filesystem registry."); return(true); } if (String.IsNullOrWhiteSpace(tagName)) { throw new ArgumentNullException(nameof(tagName)); } tagName = tagName.ToLowerInvariant(); packageName = WebUtility.UrlEncode(packageName); using (HttpRequestMessage req = NpmRequestMessage(m, $"/-/package/{packageName}/dist-tags/{tagName}", HttpMethod.Put)) { req.Content = new StringContent("\"" + version.ToNormalizedString() + "\""); req.Content.Headers.ContentType.MediaType = "application/json"; req.Content.Headers.ContentType.CharSet = ""; using (HttpResponseMessage response = await _httpClient.SendAsync(req)) { return(await HandleResponse(m, response)); } } }
static internal string GetPackagePath(string path, string packageId, SVersion v) { return(System.IO.Path.Combine(path, packageId + '.' + v.ToNormalizedString() + ".nupkg")); }
public static NPMDep CreateNPMDepMinVersion(string name, ArtifactDependencyKind kind, SVersion minVersion) { return(new NPMDep(name, NPMVersionDependencyType.MinVersion, kind, ">=" + minVersion.ToNormalizedString(), minVersion)); }
internal static string GetNPMPackagePath(string path, string packageId, SVersion v) { return(System.IO.Path.Combine(path, packageId.Replace("@", "").Replace('/', '-') + '-' + v.ToNormalizedString() + ".tgz")); }
/// <summary> /// Sets a package reference and returns the number of changes. /// </summary> /// <param name="m">The monitor.</param> /// <param name="frameworks"> /// Frameworks that applies to the reference. Must not be empty. /// Can be this project's <see cref="TargetFrameworks"/> to update the package reference regardless of the framework. /// </param> /// <param name="packageId">The package identifier.</param> /// <param name="version">The new version to set.</param> /// <param name="addIfNotExists">True to add the reference. By default, it is only updated.</param> /// <param name="preserveExisting">True to keep any existing version.</param> /// <param name="throwProjectDependendencies">False to not challenge ProjectReferences.</param> /// <returns>The number of changes.</returns> public int SetPackageReferenceVersion( IActivityMonitor m, CKTrait frameworks, string packageId, SVersion version, bool addIfNotExists = false, bool preserveExisting = false, bool throwProjectDependendencies = true) { if (!_dependencies.IsInitialized) { throw new InvalidOperationException("Invalid Project."); } if (frameworks.IsEmpty) { throw new ArgumentException("Must not be empty.", nameof(frameworks)); } var actualFrameworks = TargetFrameworks.Intersect(frameworks); if (actualFrameworks.IsEmpty) { throw new ArgumentException($"No {frameworks} in {TargetFrameworks}.", nameof(frameworks)); } if (throwProjectDependendencies && _dependencies.Projects.Any(p => p.TargetProject.ProjectName == packageId)) { throw new ArgumentException($"Package {packageId} is already a ProjectReference.", nameof(packageId)); } var sV = version.ToNormalizedString(); int changeCount = 0; CKTrait pFrameworks = null; foreach (var p in _dependencies.Packages.Where(p => p.PackageId == packageId && !(pFrameworks = p.Frameworks.Intersect(actualFrameworks)).IsEmpty)) { if (p.VersionLocked) { m.Warn($"The version({p.Version}) of the package {packageId} was manually locked. You need to change it manually."); continue; } actualFrameworks = actualFrameworks.Except(pFrameworks); var currentVersion = p.Version; if (currentVersion != version) { if (!preserveExisting) { var e = p.PropertyVersionElement; if (e != null) { e.Value = sV; } else { e = p.OriginElement; e.Attribute("Version").SetValue(sV); } ++changeCount; m.Trace($"Update in {ToString()}: {packageId} from {currentVersion} to {sV}."); } else { m.Trace($"Preserving existing version {currentVersion} for {packageId} in {ToString()} (skipped version is {sV})."); } } } // Handle creation if needed. if (!actualFrameworks.IsEmpty && addIfNotExists) { var firstPropertyGroup = ProjectFile.Document.Root.Element("PropertyGroup"); var pRef = new XElement("ItemGroup", new XElement("PackageReference", new XAttribute("Include", packageId), new XAttribute("Version", sV))); if (TargetFrameworks == actualFrameworks) { ++changeCount; firstPropertyGroup.AddAfterSelf(pRef); m.Trace($"Added unconditional package reference {packageId} -> {sV} for {ToString()}."); } else { foreach (var f in actualFrameworks.AtomicTraits) { ++changeCount; var withCond = new XElement(pRef); withCond.SetAttributeValue("Condition", $"'(TargetFrameWork)' == '{f}' "); firstPropertyGroup.AddAfterSelf(withCond); m.Trace($"Added conditional {f} package reference {packageId} -> {sV} for {ToString()}."); } } } if (changeCount > 0) { OnChange(m); } return(changeCount); }
/// <summary> /// Checks whether a package exists in the NuGet cache folder. /// </summary> /// <param name="m">The monitor to use.</param> /// <param name="packageId">The package identifier.</param> /// <param name="version">The package's version.</param> /// <returns>True if found, false otherwise.</returns> public static bool ExistsInNuGetCache(this IEnvLocalFeedProvider @this, IActivityMonitor m, string packageId, SVersion version) { var dirPath = _localNuGetCache.AppendPart(packageId).AppendPart(version.ToNormalizedString()); return(System.IO.Directory.Exists(dirPath)); }