Ejemplo n.º 1
0
        public int AddProjectToProjectReferences(string framework, IEnumerable <string> refs)
        {
            int numberOfAddedReferences = 0;

            ProjectItemGroupElement itemGroup = ProjectRootElement.FindUniformOrCreateItemGroupWithCondition(
                ProjectItemElementType,
                framework);

            foreach (var @ref in refs.Select((r) => PathUtility.GetPathWithBackSlashes(r)))
            {
                if (ProjectRootElement.HasExistingItemWithCondition(framework, @ref))
                {
                    Reporter.Output.WriteLine(string.Format(
                                                  CommonLocalizableStrings.ProjectAlreadyHasAreference,
                                                  @ref));
                    continue;
                }

                numberOfAddedReferences++;
                itemGroup.AppendChild(ProjectRootElement.CreateItemElement(ProjectItemElementType, @ref));

                Reporter.Output.WriteLine(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @ref));
            }

            return(numberOfAddedReferences);
        }
        public static ProjectItemElement AppendItem(this ProjectItemGroupElement itemGroupElement, string itemType, string include)
        {
            ProjectItemElement itemElement = itemGroupElement.ContainingProject.CreateItemElement(itemType, include);

            itemGroupElement.AppendChild(itemElement);

            return(itemElement);
        }
Ejemplo n.º 3
0
        private void AddItemToItemGroup(ProjectItemElement item, ProjectItemGroupElement itemGroup)
        {
            var outputItem = itemGroup.ContainingProject.CreateItemElement("___TEMP___");

            outputItem.CopyFrom(item);

            itemGroup.AppendChild(outputItem);
            outputItem.AddMetadata(item.Metadata);
        }
Ejemplo n.º 4
0
        public override Task <int> Execute()
        {
            List <FileInfo> matchedProjects = ProjectUtility.GetAllMatchedProjects(_options.RootDir, new[] { ".csproj" }, _options.Include, _options.Exclude).ToList();

            Dictionary <string, FileInfo> lookup = GetProjectsLookup(matchedProjects);

            foreach (FileInfo projectFile in matchedProjects)
            {
                Project project = _projectStore.Load(projectFile);

                List <string> toReplace = project.GetItems("PackageReference")
                                          .Select(s => s.EvaluatedInclude)
                                          .Where(s => lookup.ContainsKey(s))
                                          .ToList();

                if (!toReplace.Any())
                {
                    Log.Debug("Skipping {File}, nothing to do", projectFile.FullName);
                    continue;
                }

                ProjectItemGroupElement itemGroup = project.Xml.AddItemGroup();
                itemGroup.Label = "Elephant Project";

                foreach (string packageName in toReplace)
                {
                    ProjectItemElement removePackage = project.Xml.CreateItemElement("PackageReference");
                    removePackage.Remove = packageName;

                    ProjectItemElement includeReference = project.Xml.CreateItemElement("ProjectReference");
                    includeReference.Include = lookup[packageName].FullName;

                    itemGroup.AppendChild(removePackage);
                    itemGroup.AppendChild(includeReference);
                }

                Log.Information("Altered {File}, replaced {Count:N0} packages with projects", Path.GetRelativePath(_options.RootDir.FullName, projectFile.FullName), toReplace.Count);
                project.Save();
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 5
0
        private void AddItemToItemGroup(ProjectItemElement item, ProjectItemGroupElement itemGroup)
        {
            var outputItem = itemGroup.ContainingProject.CreateItemElement("___TEMP___");

            outputItem.CopyFrom(item);

            MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: AddItemToItemGroup {{ ItemType: {outputItem.ItemType}, Condition: {outputItem.Condition}, Include: {outputItem.Include}, Exclude: {outputItem.Exclude}, Update: {outputItem.Update} }}");

            itemGroup.AppendChild(outputItem);
            outputItem.AddMetadata(item.Metadata);
        }
Ejemplo n.º 6
0
        private void AddItemToItemGroup(ProjectItemElement item, ProjectItemGroupElement itemGroup)
        {
            var outputItem = itemGroup.ContainingProject.CreateItemElement("___TEMP___");

            outputItem.CopyFrom(item);

            MigrationTrace.Instance.WriteLine(String.Format(LocalizableStrings.ItemTransformApplicatorAddItemHeader, nameof(ItemTransformApplicator), outputItem.ItemType, outputItem.Condition, outputItem.Include, outputItem.Exclude, outputItem.Update));

            itemGroup.AppendChild(outputItem);
            outputItem.AddMetadata(item.Metadata, MigrationTrace.Instance);
        }
        /// <summary>
        /// Adds an item to the current item group.
        /// </summary>
        /// <param name="itemGroup">The parent item group to add the item to.</param>
        /// <param name="itemType">The type of the item to add.</param>
        /// <param name="include">The file or wildcard to include in the list of items.</param>
        /// <param name="exclude">An optional file or wildcard to exclude from the list of items.</param>
        /// <param name="metadata">An optional <see cref="IDictionary{String,String}"/> containing metadata for the item.</param>
        /// <param name="remove">The file or wildcard to remove from the list of items.</param>
        /// <param name="update">The file or wildcard to update in the list of items.</param>
        /// <param name="condition">An optional condition to add to the item.</param>
        /// <param name="keepDuplicates">Specifies whether an item should be added to the target group if it's an exact duplicate of an existing item.</param>
        /// <param name="keepMetadata">The metadata for the source items to add to the target items.</param>
        /// <param name="label">An optional label to add to the item.</param>
        /// <returns>The current <see cref="ProjectCreator"/>.</returns>
        private ProjectCreator Item(
            ProjectItemGroupElement itemGroup,
            string itemType,
            string include,
            string exclude = null,
            IDictionary <string, string> metadata = null,
            string remove         = null,
            string update         = null,
            string condition      = null,
            string keepDuplicates = null,
            string keepMetadata   = null,
            string label          = null)
        {
            ProjectItemElement item = include == null
                ? RootElement.CreateItemElement(itemType)
                : RootElement.CreateItemElement(itemType, include);

            item.Remove = remove;
            item.Update = update;

            // Item must be added after Include, Update, or Remove is set but before metadata is added
            itemGroup.AppendChild(item);

            if (metadata != null)
            {
                foreach (KeyValuePair <string, string> metadatum in metadata.Where(i => i.Value != null))
                {
                    item.AddMetadata(metadatum.Key, metadatum.Value);
                }
            }

            item.Condition = condition;
            item.Label     = label;
            item.Exclude   = exclude;

            if (keepDuplicates != null)
            {
                item.KeepDuplicates = keepDuplicates;
            }

            if (keepMetadata != null)
            {
                item.KeepMetadata = keepMetadata;
            }

            return(this);
        }
Ejemplo n.º 8
0
        internal static int AddProjectToProjectReference(ProjectRootElement root, string framework, IEnumerable <string> refs)
        {
            int          numberOfAddedReferences = 0;
            const string ProjectItemElementType  = "ProjectReference";

            ProjectItemGroupElement itemGroup = root.FindUniformOrCreateItemGroupWithCondition(ProjectItemElementType, framework);

            foreach (var @ref in refs.Select((r) => NormalizeSlashesForMsbuild(r)))
            {
                if (root.HasExistingItemWithCondition(framework, @ref))
                {
                    Reporter.Output.WriteLine(string.Format(LocalizableStrings.ProjectAlreadyHasAreference, @ref));
                    continue;
                }

                numberOfAddedReferences++;
                itemGroup.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref));

                Reporter.Output.WriteLine(string.Format(LocalizableStrings.ReferenceAddedToTheProject, @ref));
            }

            return(numberOfAddedReferences);
        }