private static int GetSourceIndex(OperationParserContext ctx, string packageBaseAddress)
        {
            var matchedSources = ctx.PackageBaseAddressToSources[packageBaseAddress];

            if (matchedSources.Count > 1)
            {
                Console.WriteLine("  WARNING: There are multiple resources with package base address:");
                Console.WriteLine("  " + packageBaseAddress);
                Console.WriteLine("  URL to operation mapping is therefore ambiguous.");
            }

            // Arbitrarily pick the first source.
            return(ctx.SourceToIndex[matchedSources[0]]);
        }
        public static OperationInfo Parse(OperationParserContext ctx, StartRequest request)
        {
            if (request.Method != "GET")
            {
                return(Unknown(request));
            }

            var uri = new Uri(request.Url, UriKind.Absolute);
            IReadOnlyList <KeyValuePair <string, Uri> > pairs;

            if (TryParsePackageBaseAddressIndex(uri, out var packageBaseAddressIndex) &&
                ctx.PackageBaseAddressToPairs.TryGetValue(packageBaseAddressIndex.packageBaseAddress, out pairs))
            {
                return(new OperationInfo(
                           new OperationWithId(
                               GetSourceIndex(ctx, packageBaseAddressIndex.packageBaseAddress),
                               OperationType.PackageBaseAddressIndex,
                               packageBaseAddressIndex.id),
                           request,
                           pairs));
            }

            if (TryParsePackageBaseAddressNupkg(uri, out var packageBaseAddressNupkg) &&
                ctx.PackageBaseAddressToPairs.TryGetValue(packageBaseAddressNupkg.packageBaseAddress, out pairs))
            {
                return(new OperationInfo(
                           new OperationWithIdVersion(
                               GetSourceIndex(ctx, packageBaseAddressNupkg.packageBaseAddress),
                               OperationType.PackageBaseAddressNupkg,
                               packageBaseAddressNupkg.id,
                               packageBaseAddressNupkg.version),
                           request,
                           pairs));
            }

            return(Unknown(request));
        }
Beispiel #3
0
        public static async Task <OperationGraph> ToOperationGraphAsync(RequestGraph graph, IReadOnlyList <string> sources)
        {
            // Parse the request graph nodes.
            var uniqueRequests         = graph.Nodes.Select(x => x.StartRequest).Distinct();
            var operationParserContext = await OperationParserContext.CreateAsync(sources);

            var parsedOperations = uniqueRequests
                                   .Select(r => OperationParser.Parse(operationParserContext, r))
                                   .ToList();

            var unknownOperations = parsedOperations
                                    .Where(x => x.Operation == null)
                                    .OrderBy(x => x.Request.Method, StringComparer.Ordinal)
                                    .ThenBy(x => x.Request.Url, StringComparer.Ordinal)
                                    .ToList();

            if (unknownOperations.Any())
            {
                var builder = new StringBuilder();
                builder.AppendLine("Ensure the provided package sources are correct.");
                builder.AppendFormat("There are {0} unknown operations:", unknownOperations.Count);
                const int take = 10;
                foreach (var operation in unknownOperations.Take(take))
                {
                    builder.AppendLine();
                    builder.AppendFormat("- {0} {1}", operation.Request.Method, operation.Request.Url);
                }

                if (unknownOperations.Count > take)
                {
                    builder.AppendLine();
                    builder.AppendFormat("... and {0} others.", unknownOperations.Count - take);
                }

                throw new ArgumentException(builder.ToString());
            }

            // Initialize all of the NuGet operation nodes.
            var requestToParsedOperation   = parsedOperations.ToDictionary(x => x.Request, x => x.Operation);
            var operationNodes             = new List <OperationNode>();
            var requestNodeToOperationNode = new Dictionary <RequestNode, OperationNode>();

            foreach (var requestNode in graph.Nodes)
            {
                var operationNode = new OperationNode(
                    requestNode.HitIndex,
                    requestToParsedOperation[requestNode.StartRequest]);

                operationNodes.Add(operationNode);
                requestNodeToOperationNode.Add(requestNode, operationNode);
            }

            // Initialize dependencies.
            foreach (var requestNode in graph.Nodes)
            {
                var operationNode = requestNodeToOperationNode[requestNode];
                foreach (var dependency in requestNode.Dependencies)
                {
                    operationNode.Dependencies.Add(requestNodeToOperationNode[dependency]);
                }
            }

            return(new OperationGraph(operationNodes));
        }