Ejemplo n.º 1
0
        /// <summary>
        /// Add a scoped registries.
        /// </summary>
        /// <param name="registries">Registries to add to the manifest.</para>
        /// <returns>true if the registries are added to the manifest, false otherwise.</returns>
        internal bool AddRegistries(IEnumerable <UnityPackageManagerRegistry> registries)
        {
            List <object> scopedRegistries;

            try {
                scopedRegistries = ScopedRegistries;
            } catch (ParseException exception) {
                Logger.Log(String.Format("{0}  Unable to add registries:\n",
                                         exception.ToString(),
                                         UnityPackageManagerRegistry.ToString(registries)),
                           level: LogLevel.Error);
                return(false);
            }
            if (scopedRegistries == null)
            {
                scopedRegistries = new List <object>();
                manifestDict[MANIFEST_SCOPED_REGISTRIES_KEY] = scopedRegistries;
            }
            RemoveRegistries(registries, displayWarning: false);
            foreach (var registry in registries)
            {
                scopedRegistries.Add(new Dictionary <string, object>()
                {
                    { MANIFEST_REGISTRY_NAME_KEY, registry.Name },
                    { MANIFEST_REGISTRY_URL_KEY, registry.Url },
                    { MANIFEST_REGISTRY_SCOPES_KEY, registry.Scopes }
                });
            }
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Remove all scoped registries in the given list.
        /// </summary>
        /// <para, name="registries">A list of scoped registry to be removed</para>
        /// <param name="displayWarning">Whether to display a warning if specified registries were not
        /// found.</param>
        /// <returns>true if the registries could be removed, false otherwise.</returns>
        internal bool RemoveRegistries(IEnumerable <UnityPackageManagerRegistry> registries,
                                       bool displayWarning = true)
        {
            List <object> scopedRegistries = null;

            try {
                scopedRegistries = ScopedRegistries;
            } catch (ParseException exception) {
                Logger.Log(String.Format("{0}  Unable to remove registries:\n", exception.ToString(),
                                         UnityPackageManagerRegistry.ToString(registries)),
                           level: LogLevel.Error);
                return(false);
            }
            int removed               = 0;
            int numberOfRegistries    = 0;
            var scopedRegistriesByUrl = UnityPackageManagerRegistries;

            foreach (var registry in registries)
            {
                numberOfRegistries++;
                List <UnityPackageManagerRegistry> existingRegistries;
                if (scopedRegistriesByUrl.TryGetValue(registry.Url, out existingRegistries))
                {
                    int remaining = existingRegistries.Count;
                    foreach (var existingRegistry in existingRegistries)
                    {
                        if (scopedRegistries.Remove(existingRegistry.CustomData))
                        {
                            remaining--;
                        }
                        else
                        {
                            Logger.Log(String.Format("Failed to remove registry '{0}' from '{1}'",
                                                     existingRegistry, MANIFEST_FILE_PATH),
                                       level: LogLevel.Error);
                        }
                    }
                    if (remaining == 0)
                    {
                        removed++;
                    }
                }
            }
            if (displayWarning)
            {
                Logger.Log(String.Format("Removed {0}/{1} registries from '{2}'",
                                         removed, numberOfRegistries, MANIFEST_FILE_PATH),
                           level: removed == numberOfRegistries ? LogLevel.Verbose : LogLevel.Warning);
            }
            return(removed == numberOfRegistries);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Apply registry changes to the projects manifest.
        /// </summary>
        /// <param name="manifestModifier">Object that modifies the project's manifest.</param>
        /// <param name="availableRegistries">Registries that are available in the
        /// configuration.</param>
        /// <param name="manifestRegistries">Registries that are present in the manifest.</param>
        /// <param name="selectedRegistryUrls">URLs of selected registries, these should be items in
        /// availableRegistries.</param>
        /// <param name="addRegistries">Whether to add selected registries to the manifest.</param>
        /// <param name="removeRegistries">Whether to remove unselected registries from the
        /// manifest.</param>
        /// <param name="invertSelection">If false, adds the selected registries and removes the
        /// unselected registries.  If true, removes the selected registries and adds the unselected
        /// registries.</param>
        /// <returns>true if successful, false otherwise.</returns>
        private static bool SyncRegistriesToManifest(
            PackageManifestModifier manifestModifier,
            Dictionary <string, UnityPackageManagerRegistry> availableRegistries,
            Dictionary <string, List <UnityPackageManagerRegistry> > manifestRegistries,
            HashSet <string> selectedRegistryUrls,
            bool addRegistries    = true,
            bool removeRegistries = true,
            bool invertSelection  = false)
        {
            // Build a list of registries to add to and remove from the manifest.
            var registriesToAdd    = new List <UnityPackageManagerRegistry>();
            var registriesToRemove = new List <UnityPackageManagerRegistry>();

            foreach (var availableRegistry in availableRegistries.Values)
            {
                var  url        = availableRegistry.Url;
                bool isSelected = selectedRegistryUrls.Contains(url);
                if (invertSelection)
                {
                    isSelected = !isSelected;
                }

                bool currentlyInManifest = manifestRegistries.ContainsKey(url);

                if (isSelected)
                {
                    if (addRegistries && !currentlyInManifest)
                    {
                        registriesToAdd.Add(availableRegistry);
                    }
                }
                else
                {
                    if (removeRegistries && currentlyInManifest)
                    {
                        registriesToRemove.Add(availableRegistry);
                    }
                }
            }

            bool manifestModified = false;

            if (registriesToAdd.Count > 0)
            {
                manifestModifier.AddRegistries(registriesToAdd);
                manifestModified = true;
            }
            if (registriesToRemove.Count > 0)
            {
                manifestModifier.RemoveRegistries(registriesToRemove);
                manifestModified = true;
            }

            bool successful = true;

            if (manifestModified)
            {
                successful = manifestModifier.WriteManifest();
                if (successful)
                {
                    if (registriesToAdd.Count > 0)
                    {
                        logger.Log(String.Format(
                                       "Added registries to {0}:\n{1}",
                                       PackageManifestModifier.MANIFEST_FILE_PATH,
                                       UnityPackageManagerRegistry.ToString(registriesToAdd)));
                    }
                    if (registriesToRemove.Count > 0)
                    {
                        logger.Log(String.Format(
                                       "Removed registries from {0}:\n{1}",
                                       PackageManifestModifier.MANIFEST_FILE_PATH,
                                       UnityPackageManagerRegistry.ToString(registriesToRemove)));
                    }
                    analytics.Report(
                        "registry_manifest/write/success",
                        new KeyValuePair <string, string>[] {
                        new KeyValuePair <string, string>("added", registriesToAdd.Count.ToString()),
                        new KeyValuePair <string, string>("removed",
                                                          registriesToRemove.Count.ToString())
                    },
                        "Project Manifest Modified");
                }
                else
                {
                    analytics.Report("registry_manifest/write/failed", "Project Manifest Write Failed");
                }
            }
            return(successful);
        }