Пример #1
0
        public void Remove(FilePath fromFilePath, PackageId packageId)
        {
            SerializableLocalToolsManifest serializableLocalToolsManifest =
                DeserializeLocalToolsManifest(fromFilePath);

            List <ToolManifestPackage> toolManifestPackages =
                GetToolManifestPackageFromOneManifestFile(
                    serializableLocalToolsManifest,
                    fromFilePath,
                    fromFilePath.GetDirectoryPath());

            if (!toolManifestPackages.Any(t => t.PackageId.Equals(packageId)))
            {
                throw new ToolManifestException(string.Format(
                                                    LocalizableStrings.CannotFindPackageIdInManifest, packageId));
            }

            if (serializableLocalToolsManifest.Tools == null)
            {
                throw new InvalidOperationException(
                          $"Invalid state {nameof(serializableLocalToolsManifest)} if out of sync with {nameof(toolManifestPackages)}. " +
                          $"{nameof(serializableLocalToolsManifest)} cannot be null when " +
                          $"the package id can be found in {nameof(toolManifestPackages)}.");
            }

            serializableLocalToolsManifest.Tools = serializableLocalToolsManifest.Tools
                                                   .Where(pair => !pair.Key.Equals(packageId.ToString(), StringComparison.Ordinal))
                                                   .ToDictionary(pair => pair.Key, pair => pair.Value);

            _fileSystem.File.WriteAllText(
                fromFilePath.Value,
                serializableLocalToolsManifest.ToJson());
        }
Пример #2
0
        public void Edit(
            FilePath manifest,
            PackageId packageId,
            NuGetVersion newNuGetVersion,
            ToolCommandName[] newToolCommandNames)
        {
            SerializableLocalToolsManifest deserializedManifest =
                DeserializeLocalToolsManifest(manifest);

            List <ToolManifestPackage> toolManifestPackages =
                GetToolManifestPackageFromOneManifestFile(deserializedManifest, manifest, manifest.GetDirectoryPath());

            var existing = toolManifestPackages.Where(t => t.PackageId.Equals(packageId)).ToArray();

            if (existing.Any())
            {
                var existingPackage = existing.Single();

                if (existingPackage.PackageId.Equals(packageId))
                {
                    var toEdit = deserializedManifest.Tools.Single(t => new PackageId(t.PackageId).Equals(packageId));

                    toEdit.Version  = newNuGetVersion.ToNormalizedString();
                    toEdit.Commands = newToolCommandNames.Select(c => c.Value).ToArray();
                }
            }
            else
            {
                throw new ArgumentException($"Manifest {manifest.Value} does not contain package id '{packageId}'.");
            }

            _fileSystem.File.WriteAllText(manifest.Value, deserializedManifest.ToJson());
        }
Пример #3
0
        public void Add(
            FilePath to,
            PackageId packageId,
            NuGetVersion nuGetVersion,
            ToolCommandName[] toolCommandNames)
        {
            SerializableLocalToolsManifest deserializedManifest =
                DeserializeLocalToolsManifest(to);

            List <ToolManifestPackage> toolManifestPackages =
                GetToolManifestPackageFromOneManifestFile(deserializedManifest, to, to.GetDirectoryPath());

            var existing = toolManifestPackages.Where(t => t.PackageId.Equals(packageId)).ToArray();

            if (existing.Any())
            {
                var existingPackage = existing.Single();

                if (existingPackage.PackageId.Equals(packageId) &&
                    existingPackage.Version == nuGetVersion &&
                    CommandNamesEqual(existingPackage.CommandNames, toolCommandNames))
                {
                    return;
                }

                throw new ToolManifestException(string.Format(
                                                    LocalizableStrings.ManifestPackageIdCollision,
                                                    existingPackage.Version.ToNormalizedString(),
                                                    existingPackage.PackageId.ToString(),
                                                    to.Value,
                                                    nuGetVersion.ToNormalizedString()));
            }

            if (deserializedManifest.Tools == null)
            {
                deserializedManifest.Tools = new Dictionary <string, SerializableLocalToolSinglePackage>();
            }

            deserializedManifest.Tools.Add(
                packageId.ToString(),
                new SerializableLocalToolSinglePackage
            {
                Version  = nuGetVersion.ToNormalizedString(),
                Commands = toolCommandNames.Select(c => c.Value).ToArray()
            });

            _fileSystem.File.WriteAllText(to.Value, deserializedManifest.ToJson());
        }