private string Intern(string text) => stringTable.Intern(text);
Exemple #2
0
        public void AnalyzeResolveAssemblyReference(Task rar)
        {
            stringTable = rar.GetNearestParent <Build>()?.StringTable;

            currentUsedLocations.Clear();

            var results    = rar.FindChild <Folder>(c => c.Name == Strings.Results);
            var parameters = rar.FindChild <Folder>(c => c.Name == Strings.Parameters);

            TotalRARDuration += rar.Duration;

            IList <string> searchPaths = null;

            if (parameters != null)
            {
                var searchPathsNode = parameters.FindChild <NamedNode>(c => c.Name == Strings.SearchPaths);
                if (searchPathsNode != null)
                {
                    searchPaths = searchPathsNode.Children.Select(c => c.ToString()).ToArray();
                }
            }

            if (results != null)
            {
                results.SortChildren();

                foreach (var reference in results.Children.OfType <Parameter>())
                {
                    const string ResolvedFilePathIs   = "Resolved file path is \"";
                    string       resolvedFilePath     = null;
                    var          resolvedFilePathNode = reference.FindChild <Item>(i => i.ToString().StartsWith(ResolvedFilePathIs, StringComparison.Ordinal));
                    if (resolvedFilePathNode != null)
                    {
                        var text = resolvedFilePathNode.ToString();
                        resolvedFilePath = text.Substring(ResolvedFilePathIs.Length, text.Length - ResolvedFilePathIs.Length - 2);
                    }

                    const string ReferenceFoundAt = "Reference found at search path location \"";
                    var          foundAtLocation  = reference.FindChild <Item>(i => i.ToString().StartsWith(ReferenceFoundAt, StringComparison.Ordinal));
                    if (foundAtLocation != null)
                    {
                        var text     = foundAtLocation.ToString();
                        var location = text.Substring(ReferenceFoundAt.Length, text.Length - ReferenceFoundAt.Length - 2);

                        // filter out the case where the assembly is resolved from the AssemblyFiles parameter
                        // In this case the location matches the resolved file path.
                        if (resolvedFilePath == null || resolvedFilePath != location)
                        {
                            UsedLocations.Add(location);
                            currentUsedLocations.Add(location);
                        }
                    }

                    var thisReferenceName = ParseReferenceName(reference.Name);

                    if (reference.Name.StartsWith("Dependency ", StringComparison.Ordinal) || reference.Name.StartsWith("Unified Dependency ", StringComparison.Ordinal))
                    {
                        bool foundNotCopyLocalBecauseMetadata = false;
                        var  requiredBy          = new List <Item>();
                        Item notCopyLocalMessage = null;

                        foreach (var message in reference.Children.OfType <Item>())
                        {
                            string text = message.Text;
                            if (text.StartsWith("Required by \""))
                            {
                                requiredBy.Add(message);
                            }
                            else if (text == @"This reference is not ""CopyLocal"" because at least one source item had ""Private"" set to ""false"" and no source items had ""Private"" set to ""true"".")
                            {
                                foundNotCopyLocalBecauseMetadata = true;
                                notCopyLocalMessage = message;
                            }
                        }

                        if (foundNotCopyLocalBecauseMetadata)
                        {
                            var assemblies = rar.FindChild <Folder>(Strings.Parameters)?.FindChild <Parameter>(Strings.Assemblies);
                            if (assemblies != null)
                            {
                                var dictionary = assemblies.Children
                                                 .OfType <Item>()
                                                 .GroupBy(i => i.Text, StringComparer.OrdinalIgnoreCase)
                                                 .ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase);

                                foreach (var sourceItem in requiredBy)
                                {
                                    int    prefixLength  = "Required by \"".Length;
                                    string text          = sourceItem.Text;
                                    var    referenceName = text.Substring(prefixLength, text.Length - prefixLength - 2);
                                    Item   foundSourceItem;
                                    if (dictionary.TryGetValue(referenceName, out foundSourceItem))
                                    {
                                        foreach (var metadata in foundSourceItem.Children.OfType <Metadata>())
                                        {
                                            if (metadata.Name == "Private")
                                            {
                                                sourceItem.AddChild(new Metadata()
                                                {
                                                    Name = metadata.Name, Value = metadata.Value
                                                });
                                                if (notCopyLocalMessage != null)
                                                {
                                                    var message = $"{foundSourceItem} has {metadata.Name} set to {metadata.Value}";
                                                    message = stringTable?.Intern(message);
                                                    notCopyLocalMessage.AddChild(new Message
                                                    {
                                                        Text = message
                                                    });
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (searchPaths != null)
            {
                foreach (var searchPath in searchPaths)
                {
                    if (currentUsedLocations.Contains(searchPath))
                    {
                        var usedLocations = rar.GetOrCreateNodeWithName <Folder>(Strings.UsedLocations);
                        usedLocations.AddChild(new Item {
                            Text = searchPath
                        });
                        UnusedLocations.Remove(searchPath);
                    }
                    else
                    {
                        var unusedLocations = rar.GetOrCreateNodeWithName <Folder>(Strings.UnusedLocations);
                        unusedLocations.AddChild(new Item {
                            Text = searchPath
                        });
                        if (!UsedLocations.Contains(searchPath))
                        {
                            UnusedLocations.Add(searchPath);
                        }
                        else
                        {
                            UnusedLocations.Remove(searchPath);
                        }
                    }
                }
            }
        }
        private void CalculateTargetGraph(Project project)
        {
            ProjectInstance projectInstance;

            if (!_projectToProjectInstanceMap.TryGetValue(project, out projectInstance))
            {
                // if for some reason we weren't able to fish out the project instance from MSBuild,
                // just add all orphans directly to the project
                var unparented = project.GetUnparentedTargets();
                foreach (var orphan in unparented)
                {
                    project.TryAddTarget(orphan);
                }

                return;
            }

            var targetGraph = new TargetGraph(projectInstance);

            IEnumerable <Target> unparentedTargets = null;

            while ((unparentedTargets = project.GetUnparentedTargets()).Any())
            {
                foreach (var unparentedTarget in unparentedTargets)
                {
                    var parents = targetGraph.GetDependents(unparentedTarget.Name);
                    if (parents != null && parents.Any())
                    {
                        foreach (var parent in parents)
                        {
                            var parentNode = project.GetOrAddTargetByName(parent);
                            if (parentNode != null && (parentNode.Id != -1 || parentNode.HasChildren))
                            {
                                parentNode.TryAddTarget(unparentedTarget);
                                break;
                            }
                        }
                    }

                    project.TryAddTarget(unparentedTarget);
                }
            }

            project.VisitAllChildren <Target>(t =>
            {
                if (t.Project == project)
                {
                    var dependencies = targetGraph.GetDependencies(t.Name);
                    if (dependencies != null && dependencies.Any())
                    {
                        t.DependsOnTargets = stringTable.Intern(string.Join(",", dependencies));
                    }
                }
            });
        }
        public void Process(BuildMessageEventArgs args)
        {
            if (args == null)
            {
                return;
            }

            var message = args.Message;

            if (message == null)
            {
                return;
            }

            // Task Input / Outputs
            if (message.StartsWith(TaskParameterMessagePrefix))
            {
                var task      = GetTask(args);
                var folder    = task.GetOrCreateNodeWithName <Folder>("Parameters");
                var parameter = ItemGroupParser.ParsePropertyOrItemList(message, TaskParameterMessagePrefix, stringTable);
                folder.AddChild(parameter);
            }
            else if (message.StartsWith(OutputItemsMessagePrefix))
            {
                var task      = GetTask(args);
                var folder    = task.GetOrCreateNodeWithName <Folder>("OutputItems");
                var parameter = ItemGroupParser.ParsePropertyOrItemList(message, OutputItemsMessagePrefix, stringTable);
                folder.AddChild(parameter);
            }
            else if (message.StartsWith(OutputPropertyMessagePrefix))
            {
                var task      = GetTask(args);
                var folder    = task.GetOrCreateNodeWithName <Folder>("OutputProperties");
                var parameter = ItemGroupParser.ParsePropertyOrItemList(message, OutputPropertyMessagePrefix, stringTable);
                folder.AddChild(parameter);
            }

            // Item / Property groups
            else if (message.StartsWith(PropertyGroupMessagePrefix))
            {
                AddPropertyGroup(args, PropertyGroupMessagePrefix);
            }
            else if (message.StartsWith(ItemGroupIncludeMessagePrefix))
            {
                AddItemGroup(args, ItemGroupIncludeMessagePrefix, new AddItem());
            }
            else if (message.StartsWith(ItemGroupRemoveMessagePrefix))
            {
                AddItemGroup(args, ItemGroupRemoveMessagePrefix, new RemoveItem());
            }
            else
            {
                // This was command line arguments for task
                var taskArgs = args as TaskCommandLineEventArgs;
                if (taskArgs != null)
                {
                    AddCommandLine(taskArgs);
                    return;
                }

                // A task from assembly message (parses out the task name and assembly path).
                const string taskAssemblyPattern = "Using \"(?<task>.+)\" task from (assembly|the task factory) \"(?<assembly>.+)\"\\.";
                var          match = Regex.Match(message, taskAssemblyPattern);
                if (match.Success)
                {
                    construction.SetTaskAssembly(
                        stringTable.Intern(match.Groups["task"].Value),
                        stringTable.Intern(match.Groups["assembly"].Value));
                }
                else
                {
                    // Just the generic log message or something we currently don't handle in the object model.
                    AddMessage(args, message);
                }
            }
        }
        /// <summary>
        /// Parses a log output string to a list of Items (e.g. ItemGroup with metadata or property string).
        /// </summary>
        /// <param name="message">The message output from the logger.</param>
        /// <param name="prefix">The prefix parsed out (e.g. 'Output Item(s): '.).</param>
        /// <param name="name">Out: The name of the list.</param>
        /// <returns>List of items within the list and all metadata.</returns>
        public static object ParsePropertyOrItemList(string message, string prefix, StringCache stringTable)
        {
            if (!Utilities.ContainsLineBreak(message))
            {
                var nameValue = Utilities.ParseNameValue(message, trimFromStart: prefix.Length);
                var property  = new Property
                {
                    Name  = stringTable.Intern(nameValue.Key),
                    Value = stringTable.Intern(nameValue.Value)
                };
                return(property);
            }

            message = message.Replace("\r\n", "\n");
            message = message.Replace('\r', '\n');
            var lines = message.Split('\n');

            var parameter = new Parameter();

            if (lines[0].Length > prefix.Length)
            {
                // we have a weird case of multi-line value
                var nameValue = Utilities.ParseNameValue(lines[0].Substring(prefix.Length));

                parameter.Name = stringTable.Intern(nameValue.Key);

                parameter.AddChild(new Item
                {
                    Text = stringTable.Intern(nameValue.Value.Replace("\r", ""))
                });

                for (int i = 1; i < lines.Length; i++)
                {
                    parameter.AddChild(new Item
                    {
                        Text = stringTable.Intern(lines[i].Replace("\r", ""))
                    });
                }

                return(parameter);
            }

            Item     currentItem     = null;
            Property currentProperty = null;

            foreach (var line in lines)
            {
                var numberOfLeadingSpaces = Utilities.GetNumberOfLeadingSpaces(line);
                switch (numberOfLeadingSpaces)
                {
                case 4:
                    if (line.EndsWith("=", StringComparison.Ordinal))
                    {
                        parameter.Name = stringTable.Intern(line.Substring(4, line.Length - 5));
                    }
                    break;

                case 8:
                    if (line.IndexOf('=') != -1)
                    {
                        var kvp = Utilities.ParseNameValue(line.Substring(8));
                        currentProperty = new Property
                        {
                            Name  = stringTable.Intern(kvp.Key),
                            Value = stringTable.Intern(kvp.Value)
                        };
                        parameter.AddChild(currentProperty);
                        currentItem = null;
                    }
                    else
                    {
                        currentItem = new Item
                        {
                            Text = stringTable.Intern(line.Substring(8))
                        };
                        parameter.AddChild(currentItem);
                        currentProperty = null;
                    }
                    break;

                case 16:
                    var currentLine = line.Substring(16);
                    if (currentItem != null)
                    {
                        if (!currentLine.Contains("="))
                        {
                            // must be a continuation of the metadata value from the previous line
                            if (currentItem.HasChildren)
                            {
                                var metadata = currentItem.Children[currentItem.Children.Count - 1] as Metadata;
                                if (metadata != null)
                                {
                                    metadata.Value = stringTable.Intern((metadata.Value ?? "") + currentLine);
                                }
                            }
                        }
                        else
                        {
                            var nameValue = Utilities.ParseNameValue(currentLine);
                            var metadata  = new Metadata
                            {
                                Name  = stringTable.Intern(nameValue.Key),
                                Value = stringTable.Intern(nameValue.Value)
                            };
                            currentItem.AddChild(metadata);
                        }
                    }
                    break;

                default:
                    if (numberOfLeadingSpaces == 0 && line == prefix)
                    {
                        continue;
                    }

                    // must be a continuation of a multi-line value
                    if (currentProperty != null)
                    {
                        currentProperty.Value += "\n" + line;
                    }
                    else if (currentItem != null && currentItem.HasChildren)
                    {
                        var metadata = currentItem.Children[currentItem.Children.Count - 1] as Metadata;
                        if (metadata != null)
                        {
                            metadata.Value = (metadata.Value ?? "") + line;
                        }
                    }
                    break;
                }
            }

            return(parameter);
        }
 private string GetCurrentValue()
 {
     return(stringTable.Intern(reader.Value));
 }
        public void Process(BuildMessageEventArgs args)
        {
            if (args == null)
            {
                return;
            }

            var message = args.Message;

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            switch (message[0])
            {
            case 'A':
                if (message.StartsWith(ItemGroupIncludeMessagePrefix))
                {
                    AddItemGroup(args, ItemGroupIncludeMessagePrefix, new AddItem());
                    return;
                }
                break;

            case 'O':
                if (message.StartsWith(OutputItemsMessagePrefix))
                {
                    var task      = GetTask(args);
                    var folder    = task.GetOrCreateNodeWithName <Folder>("OutputItems");
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, OutputItemsMessagePrefix, stringTable);
                    folder.AddChild(parameter);
                    return;
                }

                if (message.StartsWith(OutputPropertyMessagePrefix))
                {
                    var task      = GetTask(args);
                    var folder    = task.GetOrCreateNodeWithName <Folder>("OutputProperties");
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, OutputPropertyMessagePrefix, stringTable);
                    folder.AddChild(parameter);
                    return;
                }
                break;

            case 'R':
                if (message.StartsWith(ItemGroupRemoveMessagePrefix))
                {
                    AddItemGroup(args, ItemGroupRemoveMessagePrefix, new RemoveItem());
                    return;
                }
                break;

            case 'S':
                if (message.StartsWith(PropertyGroupMessagePrefix))
                {
                    AddPropertyGroup(args, PropertyGroupMessagePrefix);
                    return;
                }
                break;

            case 'T':
                if (message.StartsWith(TaskParameterMessagePrefix))
                {
                    var task      = GetTask(args);
                    var folder    = task.GetOrCreateNodeWithName <Folder>("Parameters");
                    var parameter = ItemGroupParser.ParsePropertyOrItemList(message, TaskParameterMessagePrefix, stringTable);
                    folder.AddChild(parameter);
                    return;
                }
                break;

            case 'U':
                // A task from assembly message (parses out the task name and assembly path).
                var match = usingTaskRegex.Match(message);
                if (match.Success)
                {
                    construction.SetTaskAssembly(
                        stringTable.Intern(match.Groups["task"].Value),
                        stringTable.Intern(match.Groups["assembly"].Value));
                    return;
                }

                break;

            default:
                break;
            }

            if (args is TaskCommandLineEventArgs taskArgs)
            {
                AddCommandLine(taskArgs);
                return;
            }

            // Just the generic log message or something we currently don't handle in the object model.
            AddMessage(args, message);
        }
        public static void HandleThereWasAConflict(Parameter parameter, string message, StringCache stringTable)
        {
            var      numberOfLeadingSpaces = TextUtilities.GetNumberOfLeadingSpaces(message);
            TreeNode node = parameter;

            switch (numberOfLeadingSpaces)
            {
            case 0:
                parameter.AddChild(new Item()
                {
                    Text = stringTable.Intern(message)
                });
                break;

            case 4:
                node = parameter.LastChild as TreeNode ?? node;
                Add(node, message, 4);
                break;

            case 6:
            {
                if (parameter.LastChild is TreeNode item)
                {
                    node = item;
                    if (item.LastChild is TreeNode item2)
                    {
                        node = item2;
                    }
                }
                Add(node, message, 6);
            }
            break;

            case 8:
            {
                if (parameter.LastChild is TreeNode item)
                {
                    node = item;
                    if (item.LastChild is TreeNode item2)
                    {
                        node = item2;
                        if (item2.LastChild is TreeNode item3)
                        {
                            node = item3;
                        }
                    }
                }
                Add(node, message, 8);
            }
            break;

            default:
                Add(node, message, 0);
                break;
            }

            void Add(TreeNode parent, string text, int spaces)
            {
                text = text.Substring(spaces);
                parent.AddChild(new Item
                {
                    Text = stringTable.Intern(text)
                });
            }
        }
Exemple #9
0
 private string ReadTextContent(XElement element)
 {
     return(stringTable.Intern(element.Value));
 }