Exemple #1
0
        public Data.Blockchain ReadBlockchain(BlockchainKey blockchainKey)
        {
            CheckDatabaseFolder();

            var blockListBuilder = ImmutableList.CreateBuilder <ChainedBlock>();
            var utxoBuilder      = ImmutableDictionary.CreateBuilder <UInt256, UnspentTx>();

            var connString = @"Server=localhost; Database=BitSharp_Blockchains; Trusted_Connection=true;";

            using (var conn = new SqlConnection(connString))
            {
                conn.Open();

                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
                        SELECT BlockHash, PreviousBlockHash, Height, TotalWork
                        FROM ChainedBlocks
                        WHERE Guid = @guid AND RootBlockHash = @rootBlockHash
                        ORDER BY Height ASC";

                    cmd.Parameters.SetValue("@guid", SqlDbType.Binary, 16).Value          = blockchainKey.Guid.ToByteArray();
                    cmd.Parameters.SetValue("@rootBlockHash", SqlDbType.Binary, 32).Value = blockchainKey.RootBlockHash.ToDbByteArray();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var blockHash         = reader.GetUInt256(0);
                            var previousBlockHash = reader.GetUInt256(1);
                            var height            = reader.GetInt32(2);
                            var totalWork         = reader.GetBigInteger(3);

                            blockListBuilder.Add(new ChainedBlock(blockHash, previousBlockHash, height, totalWork));
                        }
                    }
                }

                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
                        SELECT UtxoChunkBytes
                        FROM UtxoData
                        WHERE Guid = @guid AND RootBlockHash = @rootBlockHash";

                    cmd.Parameters.SetValue("@guid", SqlDbType.Binary, 16).Value          = blockchainKey.Guid.ToByteArray();
                    cmd.Parameters.SetValue("@rootBlockHash", SqlDbType.Binary, 32).Value = blockchainKey.RootBlockHash.ToDbByteArray();

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var chunkBytes  = reader.GetBytes(0);
                            var chunkStream = new MemoryStream(chunkBytes);
                            using (var chunkReader = new BinaryReader(chunkStream))
                            {
                                var chunkLength = chunkReader.Read4Bytes().ToIntChecked();

                                var outputs = new TxOutputKey[chunkLength];

                                for (var i = 0; i < chunkLength; i++)
                                {
                                    var prevTxHash        = chunkReader.Read32Bytes();
                                    var prevTxOutputIndex = chunkReader.Read4Bytes();

                                    outputs[i] = new TxOutputKey(prevTxHash, prevTxOutputIndex);
                                }

                                //TODO utxoBuilder.UnionWith(outputs);
                            }
                        }
                    }
                }
            }

            return(new Data.Blockchain(blockListBuilder.ToImmutable(), blockListBuilder.Select(x => x.BlockHash).ToImmutableHashSet(), utxoBuilder.ToImmutable()));
        }
            protected override ImmutableList <I> SelectItems(ImmutableList <ItemData> .Builder listBuilder, ImmutableHashSet <string> globsToIgnore)
            {
                var itemsToAdd = ImmutableList.CreateBuilder <I>();

                Lazy <Func <string, bool> > excludeTester = null;

                ImmutableList <string> .Builder excludePatterns = ImmutableList.CreateBuilder <string>();
                if (_excludes != null)
                {
                    // STEP 4: Evaluate, split, expand and subtract any Exclude
                    foreach (string exclude in _excludes)
                    {
                        string excludeExpanded = _expander.ExpandIntoStringLeaveEscaped(exclude, ExpanderOptions.ExpandPropertiesAndItems, _itemElement.ExcludeLocation);
                        var    excludeSplits   = ExpressionShredder.SplitSemiColonSeparatedList(excludeExpanded);
                        excludePatterns.AddRange(excludeSplits);
                    }

                    if (excludePatterns.Count > 0)
                    {
                        excludeTester = new Lazy <Func <string, bool> >(() => EngineFileUtilities.GetFileSpecMatchTester(excludePatterns, _rootDirectory));
                    }
                }

                ISet <string> excludePatternsForGlobs = null;

                foreach (var fragment in _itemSpec.Fragments)
                {
                    if (fragment is ItemSpec <P, I> .ItemExpressionFragment itemReferenceFragment)
                    {
                        // STEP 3: If expression is "@(x)" copy specified list with its metadata, otherwise just treat as string
                        var itemsFromExpression = _expander.ExpandExpressionCaptureIntoItems(
                            itemReferenceFragment.Capture,
                            _evaluatorData,
                            _itemFactory,
                            ExpanderOptions.ExpandItems,
                            includeNullEntries: false,
                            isTransformExpression: out _,
                            elementLocation: _itemElement.IncludeLocation);

                        itemsToAdd.AddRange(
                            excludeTester != null
                                ? itemsFromExpression.Where(item => !excludeTester.Value(item.EvaluatedInclude))
                                : itemsFromExpression);
                    }
                    else if (fragment is ValueFragment valueFragment)
                    {
                        string value = valueFragment.TextFragment;

                        if (excludeTester?.Value(EscapingUtilities.UnescapeAll(value)) != true)
                        {
                            var item = _itemFactory.CreateItem(value, value, _itemElement.ContainingProject.FullPath);
                            itemsToAdd.Add(item);
                        }
                    }
                    else if (fragment is GlobFragment globFragment)
                    {
                        // If this item is behind a false condition and represents a full drive/filesystem scan, expanding it is
                        // almost certainly undesired. It should be skipped to avoid evaluation taking an excessive amount of time.
                        bool skipGlob = !_conditionResult && globFragment.IsFullFileSystemScan && !Traits.Instance.EscapeHatches.AlwaysEvaluateDangerousGlobs && ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave16_8);
                        if (!skipGlob)
                        {
                            string glob = globFragment.TextFragment;

                            if (excludePatternsForGlobs == null)
                            {
                                excludePatternsForGlobs = BuildExcludePatternsForGlobs(globsToIgnore, excludePatterns);
                            }

                            string[] includeSplitFilesEscaped;
                            if (MSBuildEventSource.Log.IsEnabled())
                            {
                                MSBuildEventSource.Log.ExpandGlobStart(_rootDirectory, glob, string.Join(", ", excludePatternsForGlobs));
                            }
                            using (_lazyEvaluator._evaluationProfiler.TrackGlob(_rootDirectory, glob, excludePatternsForGlobs))
                            {
                                includeSplitFilesEscaped = EngineFileUtilities.GetFileListEscaped(
                                    _rootDirectory,
                                    glob,
                                    excludePatternsForGlobs
                                    );
                            }
                            if (MSBuildEventSource.Log.IsEnabled())
                            {
                                MSBuildEventSource.Log.ExpandGlobStop(_rootDirectory, glob, string.Join(", ", excludePatternsForGlobs));
                            }

                            foreach (string includeSplitFileEscaped in includeSplitFilesEscaped)
                            {
                                itemsToAdd.Add(_itemFactory.CreateItem(includeSplitFileEscaped, glob, _itemElement.ContainingProject.FullPath));
                            }
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(fragment.GetType().ToString());
                    }
                }

                return(itemsToAdd.ToImmutable());
            }
Exemple #3
0
 static ImmutableList <int> .Builder createBuilder() => ImmutableList.CreateBuilder <int>();
Exemple #4
0
        public async Task ProcessAsync()
        {
            var options = _options;

            var target = _options.Target ?? FindFallbackProjectFile();

            target = new FileInfo(target).FullName;

            WriteVerboseInfo($"Using project {target} as generation target.");
            WriteVerboseInfo($"MSBuildLocator executing...");

            MSBuildLocator.RegisterDefaults();

            using (_workspace = MSBuildWorkspace.Create())
            {
                var projectsBuilder = ImmutableList.CreateBuilder <Project>();
                _project = await _workspace.OpenProjectAsync(target);

                if (_options.SourceProjects.Any())
                {
                    foreach (var item in _options.SourceProjects)
                    {
                        WriteVerboseInfo($"Looking for project {item}.");

                        var found = _project.Solution.Projects.FirstOrDefault(p => p.Name == item);
                        if (found == null)
                        {
                            WriteErrorOutput($"Project {item} was not found. It needs to be referenced from the target project.");
                            Environment.Exit(404);
                        }

                        projectsBuilder.Add(found);
                        WriteVerboseInfo($"Found project {item}.");
                    }
                }
                else
                {
                    projectsBuilder.Add(_project);
                }

                var projects = projectsBuilder.ToImmutable();
                var interfacesPerProjectBuilder =
                    ImmutableDictionary.CreateBuilder <Project, ImmutableList <InterfaceDeclarationSyntax> >();

                foreach (var project in projects)
                {
                    var interfacesBuilder = ImmutableList.CreateBuilder <InterfaceDeclarationSyntax>();
                    foreach (var document in project.Documents)
                    {
                        var root = await document.GetSyntaxRootAsync();

                        var interfaces = root.DescendantNodes()
                                         .OfType <InterfaceDeclarationSyntax>()
                                         .Where(p => IsRestContract(p))
                                         .ToImmutableList();

                        interfacesBuilder.AddRange(interfaces);
                    }

                    var allInterfacesPerProject = interfacesBuilder.ToImmutable();
                    interfacesPerProjectBuilder.Add(project, allInterfacesPerProject);
                }

                var interfacesPerProject = interfacesPerProjectBuilder.ToImmutable();
                var foundInterfaces      = interfacesPerProject.SelectMany(pair => pair.Value).ToImmutableList();

                Console.WriteLine($"Found {foundInterfaces.Count} matching interfaces.");

                if (_options.Verbose)
                {
                    foreach (var item in foundInterfaces)
                    {
                        WriteVerboseInfo($"Found interface {item.Identifier.Text}");
                    }
                }

                foreach (var projectInterfacesPair in interfacesPerProject)
                {
                    var project    = projectInterfacesPair.Key;
                    var interfaces = projectInterfacesPair.Value;

                    var compilation = await project.GetCompilationAsync();

                    foreach (var item in interfaces)
                    {
                        var syntaxTree      = item.SyntaxTree;
                        var semanticModel   = compilation.GetSemanticModel(syntaxTree);
                        var interfaceSymbol = semanticModel.GetDeclaredSymbol(item);

                        await WriteTargetProxyClassAsync(interfaceSymbol);
                    }
                }
            }

            _workspace = null;
            _project   = null;
        }
Exemple #5
0
        private Tuple <ITargetBlock <Type>, Task <DiscoveredParts> > CreateDiscoveryBlockChain(bool typeExplicitlyRequested, IProgress <DiscoveryProgress>?progress, CancellationToken cancellationToken)
        {
            string status         = Strings.ScanningMEFAssemblies;
            int    typesScanned   = 0;
            var    transformBlock = new TransformBlock <Type, object?>(
                type =>
            {
                try
                {
                    return(this.CreatePart(type, typeExplicitlyRequested));
                }
                catch (Exception ex)
                {
                    return(new PartDiscoveryException(string.Format(CultureInfo.CurrentCulture, Strings.FailureWhileScanningType, type.FullName), ex)
                    {
                        AssemblyPath = type.GetTypeInfo().Assembly.Location, ScannedType = type
                    });
                }
            },
                new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = Debugger.IsAttached ? 1 : Environment.ProcessorCount,
                CancellationToken      = cancellationToken,
                MaxMessagesPerTask     = 10,
                BoundedCapacity        = 100,
            });
            var parts            = ImmutableHashSet.CreateBuilder <ComposablePartDefinition>();
            var errors           = ImmutableList.CreateBuilder <PartDiscoveryException>();
            var aggregatingBlock = new ActionBlock <object?>(partOrException =>
            {
                var part  = partOrException as ComposablePartDefinition;
                var error = partOrException as PartDiscoveryException;
                Debug.Assert(partOrException is Exception == partOrException is PartDiscoveryException, "Wrong exception type returned.");
                if (part != null)
                {
                    parts.Add(part);
                }
                else if (error != null)
                {
                    errors.Add(error);
                }

                progress.ReportNullSafe(new DiscoveryProgress(++typesScanned, 0, status));
            });

            transformBlock.LinkTo(aggregatingBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });

            var tcs = new TaskCompletionSource <DiscoveredParts>();

            Task.Run(async delegate
            {
                try
                {
                    await aggregatingBlock.Completion.ConfigureAwait(false);
                    tcs.SetResult(new DiscoveredParts(parts.ToImmutable(), errors.ToImmutable()));
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            });

            return(Tuple.Create <ITargetBlock <Type>, Task <DiscoveredParts> >(transformBlock, tcs.Task));
        }
Exemple #6
0
            private static ImmutableList <ItemData> .Builder ComputeItems(LazyItemList lazyItemList, ImmutableHashSet <string> globsToIgnore)
            {
                // Stack of operations up to the first one that's cached (exclusive)
                Stack <LazyItemList> itemListStack = new Stack <LazyItemList>();

                ImmutableList <ItemData> .Builder items = null;

                // Keep a separate stack of lists of globs to ignore that only gets modified for Remove operations
                Stack <ImmutableHashSet <string> > globsToIgnoreStack = null;

                for (var currentList = lazyItemList; currentList != null; currentList = currentList._previous)
                {
                    var globsToIgnoreFromFutureOperations = globsToIgnoreStack?.Peek() ?? globsToIgnore;

                    ImmutableList <ItemData> itemsFromCache;
                    if (currentList._memoizedOperation.TryGetFromCache(globsToIgnoreFromFutureOperations, out itemsFromCache))
                    {
                        // the base items on top of which to apply the uncached operations are the items of the first operation that is cached
                        items = itemsFromCache.ToBuilder();
                        break;
                    }

                    //  If this is a remove operation, then add any globs that will be removed
                    //  to a list of globs to ignore in previous operations
                    var removeOperation = currentList._memoizedOperation.Operation as RemoveOperation;
                    if (removeOperation != null)
                    {
                        if (globsToIgnoreStack == null)
                        {
                            globsToIgnoreStack = new Stack <ImmutableHashSet <string> >();
                        }

                        var globsToIgnoreForPreviousOperations = removeOperation.GetRemovedGlobs();
                        foreach (var globToRemove in globsToIgnoreFromFutureOperations)
                        {
                            globsToIgnoreForPreviousOperations.Add(globToRemove);
                        }

                        globsToIgnoreStack.Push(globsToIgnoreForPreviousOperations.ToImmutable());
                    }

                    itemListStack.Push(currentList);
                }

                if (items == null)
                {
                    items = ImmutableList.CreateBuilder <ItemData>();
                }

                ImmutableHashSet <string> currentGlobsToIgnore = globsToIgnoreStack == null ? globsToIgnore : globsToIgnoreStack.Peek();

                //  Walk back down the stack of item lists applying operations
                while (itemListStack.Count > 0)
                {
                    var currentList = itemListStack.Pop();

                    //  If this is a remove operation, then it could modify the globs to ignore, so pop the potentially
                    //  modified entry off the stack of globs to ignore
                    var removeOperation = currentList._memoizedOperation.Operation as RemoveOperation;
                    if (removeOperation != null)
                    {
                        globsToIgnoreStack.Pop();
                        currentGlobsToIgnore = globsToIgnoreStack.Count == 0 ? globsToIgnore : globsToIgnoreStack.Peek();
                    }

                    currentList._memoizedOperation.Apply(items, currentGlobsToIgnore);
                }

                return(items);
            }
Exemple #7
0
 public void CreateBuilder()
 {
     ImmutableList <string> .Builder builder = ImmutableList.CreateBuilder <string>();
     Assert.NotNull(builder);
 }
Exemple #8
0
 public ChainBuilder()
 {
     this.blocks = ImmutableList.CreateBuilder <ChainedHeader>();
 }
        public object ReadJson(ref JReader reader)
        {
            string key     = null;
            int    version = 0;
            bool   deleted = false;
            bool   on      = false;
            ImmutableList <Prerequisite> prerequisites = null;
            ImmutableList <Target>       targets       = null;
            ImmutableList <FlagRule>     rules         = null;
            string             salt        = null;
            VariationOrRollout fallthrough = new VariationOrRollout();
            int?offVariation = null;
            ImmutableList <LdValue> variations = null;
            bool trackEvents = false, trackEventsFallthrough = false;
            UnixMillisecondTime?debugEventsUntilDate = null;
            bool clientSide = false;

            for (var obj = reader.Object().WithRequiredProperties(_requiredProperties); obj.Next(ref reader);)
            {
                switch (obj.Name)
                {
                case var n when n == "key":
                    key = reader.String();
                    break;

                case var n when n == "version":
                    version = reader.Int();
                    break;

                case var n when n == "deleted":
                    deleted = reader.Bool();
                    break;

                case var n when n == "on":
                    on = reader.Bool();
                    break;

                case var n when n == "prerequisites":
                    var prereqsBuilder = ImmutableList.CreateBuilder <Prerequisite>();
                    for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);)
                    {
                        prereqsBuilder.Add(ReadPrerequisite(ref reader));
                    }
                    prerequisites = prereqsBuilder.ToImmutable();
                    break;

                case var n when n == "targets":
                    var targetsBuilder = ImmutableList.CreateBuilder <Target>();
                    for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);)
                    {
                        targetsBuilder.Add(ReadTarget(ref reader));
                    }
                    targets = targetsBuilder.ToImmutable();
                    break;

                case var n when n == "rules":
                    var rulesBuilder = ImmutableList.CreateBuilder <FlagRule>();
                    for (var arr = reader.ArrayOrNull(); arr.Next(ref reader);)
                    {
                        rulesBuilder.Add(ReadFlagRule(ref reader));
                    }
                    rules = rulesBuilder.ToImmutable();
                    break;

                case var n when n == "fallthrough":
                    fallthrough = ReadVariationOrRollout(ref reader);
                    break;

                case var n when n == "offVariation":
                    offVariation = reader.IntOrNull();
                    break;

                case var n when n == "variations":
                    variations = SerializationHelpers.ReadValues(ref reader);
                    break;

                case var n when n == "salt":
                    salt = reader.StringOrNull();
                    break;

                case var n when n == "trackEvents":
                    trackEvents = reader.Bool();
                    break;

                case var n when n == "trackEventsFallthrough":
                    trackEventsFallthrough = reader.Bool();
                    break;

                case var n when n == "debugEventsUntilDate":
                    var dt = reader.LongOrNull();
                    debugEventsUntilDate = dt.HasValue ? UnixMillisecondTime.OfMillis(dt.Value) : (UnixMillisecondTime?)null;
                    break;

                case var n when n == "clientSide":
                    clientSide = reader.Bool();
                    break;
                }
            }
            if (key is null && !deleted)
            {
                throw new RequiredPropertyException("key", 0);
            }
            return(new FeatureFlag(key, version, deleted, on, prerequisites, targets, rules, fallthrough,
                                   offVariation, variations, salt, trackEvents, trackEventsFallthrough, debugEventsUntilDate, clientSide));
        }
Exemple #10
0
 public Builder(BlockId id) : base(id)
 {
     _statements = ImmutableList.CreateBuilder <AstStatement>();
 }
        public static ImmutableList <MediaTrack> FromSdp(SessionDescription sdp, Uri baseUri, MimeType filter = null)
        {
            var builder = ImmutableList.CreateBuilder <MediaTrack>();

            string sdpConnectionAddr = baseUri.Host;

            if (sdp.Connection != null && IsAddressSet(sdp.Connection.Address))
            {
                sdpConnectionAddr = sdp.Connection.Address;
            }

            sdp.MediaDescriptions.ForEach(md =>
            {
                Uri controlUri = baseUri;
                string type    = md.Media.ToString().ToLower();
                string subType = "unknown";

                // Prefer the media description's connection address over the sdp connection address.
                var connectionAddr = md.Connection != null && !string.IsNullOrEmpty(md.Connection.Address)
                        ? md.Connection.Address
                        : sdpConnectionAddr;

                var rtmpmaps = md.GetRtpMaps();

                // Via spec there can be multiple types and rtpmaps, but
                // we will assume a single one since all cameras/encoders
                // appear to only provide one per Media Description.
                var rtpmap = rtmpmaps.IsEmpty ? FromPayloadType(md.MediaFormats[0]) : rtmpmaps.First();
                subType    = rtpmap.EncodingName;

                var control = md.Attributes.Where(a => "control" == a.Name).DefaultIfEmpty(null).First();
                if (control != null)
                {
                    controlUri = ResolveUri(baseUri, new Uri(control.Value, UriKind.RelativeOrAbsolute));
                }

                var mimeType = MimeType.Create(type, rtpmap.EncodingName);

                if (mimeType.Is(APPLICATION_SPOOFED))
                {
                    // The MediaGateway and Vxpro us spoofing to determine the client's intentions of
                    // either playing live or recorded data.  Because of the current design they do not
                    // have a way to know the client's intentions until a play call is issued that contains
                    // and RTSP Range header with an absolute start-time.
                    //
                    // To handle metadata, in a pretty hacky way, a new media description is added to the SDP
                    // of type application/vnd.pelco.spoofed to indicate to a client that the data is bogus
                    // The client should continue processing up until the play call is made.  At this point the
                    // MediaGateway or VxPro will issues a redirect to the actual source.
                    //
                    // We will make a bogus metadata track to allow the client to continue processing.
                    builder.Add(MediaTrack.CreateBuilder()
                                .Address(Dns.GetHostAddresses(baseUri.Host)[0])
                                .Port(md.Port)
                                .Type(mimeType)
                                .Uri(controlUri)
                                .Build());
                }
                else if ((filter != null && mimeType.Is(filter)) || (filter == null))
                {
                    // If a filter is defined only return tracks that match the
                    // defined filter type; otherwise, return all tracks.

                    if (IsAddressSet(connectionAddr))
                    {
                        // If defined as 0.0.0.0 or ::0 then replace with the SDP connection address.
                        connectionAddr = sdpConnectionAddr;
                    }

                    var addrs = Dns.GetHostAddresses(connectionAddr);

                    builder.Add(MediaTrack.CreateBuilder()
                                .Address(Dns.GetHostAddresses(connectionAddr)[0])
                                .Port(md.Port)
                                .RtpMap(rtpmap)
                                .Type(mimeType)
                                .Uri(controlUri)
                                .Build());
                }
            });

            return(builder.ToImmutable());
        }
 public ConcurrentListBuilder()
 {
     this.builder     = ImmutableList.CreateBuilder <T>();
     this.builderLock = new ReaderWriterLockSlim();
 }
Exemple #13
0
        private BlockFlow DecodeBlockFlow(
            LNode node,
            FlowGraphBuilder graph,
            Dictionary <Symbol, BasicBlockBuilder> blocks,
            Dictionary <Symbol, ValueTag> valueTags)
        {
            if (node.Calls(CodeSymbols.Goto))
            {
                Branch target;
                if (FeedbackHelpers.AssertArgCount(node, 1, Log) &&
                    AssertDecodeBranch(node.Args[0], graph, blocks, valueTags, out target))
                {
                    return(new JumpFlow(target));
                }
                else
                {
                    return(UnreachableFlow.Instance);
                }
            }
            else if (node.Calls(CodeSymbols.Switch))
            {
                // Decode the value being switched on as well as the default branch.
                Instruction switchVal;
                Branch      defaultTarget;
                if (FeedbackHelpers.AssertArgCount(node, 3, Log) &&
                    AssertDecodeInstruction(node.Args[0], valueTags, out switchVal) &&
                    AssertDecodeBranch(node.Args[1], graph, blocks, valueTags, out defaultTarget))
                {
                    // Decode the switch cases.
                    var switchCases = ImmutableList.CreateBuilder <SwitchCase>();
                    foreach (var caseNode in node.Args[2].Args)
                    {
                        if (!FeedbackHelpers.AssertArgCount(caseNode, 2, Log) ||
                            !FeedbackHelpers.AssertIsCall(caseNode.Args[0], Log))
                        {
                            continue;
                        }

                        var constants = ImmutableHashSet.CreateRange <Constant>(
                            caseNode.Args[0].Args
                            .Select(DecodeConstant)
                            .Where(x => x != null));

                        Branch caseTarget;
                        if (AssertDecodeBranch(caseNode.Args[1], graph, blocks, valueTags, out caseTarget))
                        {
                            switchCases.Add(new SwitchCase(constants, caseTarget));
                        }
                    }
                    return(new SwitchFlow(switchVal, switchCases.ToImmutable(), defaultTarget));
                }
                else
                {
                    return(UnreachableFlow.Instance);
                }
            }
            else if (node.Calls(CodeSymbols.Return))
            {
                Instruction retValue;
                if (FeedbackHelpers.AssertArgCount(node, 1, Log) &&
                    AssertDecodeInstruction(node.Args[0], valueTags, out retValue))
                {
                    return(new ReturnFlow(retValue));
                }
                else
                {
                    return(UnreachableFlow.Instance);
                }
            }
            else if (node.Calls(CodeSymbols.Try))
            {
                Instruction tryValue;
                Branch      successBranch;
                Branch      exceptionBranch;
                if (FeedbackHelpers.AssertArgCount(node, 3, Log) &&
                    AssertDecodeInstruction(node.Args[0], valueTags, out tryValue) &&
                    AssertDecodeBranch(node.Args[1], graph, blocks, valueTags, out successBranch) &&
                    AssertDecodeBranch(node.Args[2], graph, blocks, valueTags, out exceptionBranch))
                {
                    return(new TryFlow(tryValue, successBranch, exceptionBranch));
                }
                else
                {
                    return(UnreachableFlow.Instance);
                }
            }
            else if (node.IsIdNamed(EncoderState.unreachableFlowSymbol))
            {
                return(UnreachableFlow.Instance);
            }
            else
            {
                FeedbackHelpers.LogSyntaxError(
                    Log,
                    node,
                    Quotation.QuoteEvenInBold(
                        "unknown type of flow; expected one of ",
                        CodeSymbols.Goto.Name, ", ",
                        CodeSymbols.Switch.Name, ", ",
                        CodeSymbols.Try.Name, ", ",
                        CodeSymbols.Return.Name, " or ",
                        EncoderState.unreachableFlowSymbol.Name, "."));
                return(UnreachableFlow.Instance);
            }
        }
Exemple #14
0
        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="currentShardAllocations">TBD</param>
        /// <param name="rebalanceInProgress">TBD</param>
        /// <returns>TBD</returns>
        public override Task <IImmutableSet <ShardId> > Rebalance(IImmutableDictionary <IActorRef, IImmutableList <ShardId> > currentShardAllocations, IImmutableSet <ShardId> rebalanceInProgress)
        {
            int Limit(int numberOfShards)
            {
                return(Math.Max(1, Math.Min((int)(_relativeLimit * numberOfShards), _absoluteLimit)));
            }

            IImmutableSet <ShardId> RebalancePhase1(
                int numberOfShards,
                int optimalPerRegion,
                IImmutableList <RegionEntry> sortedEntries
                )
            {
                var selected = ImmutableList.CreateBuilder <ShardId>();

                foreach (var entry in sortedEntries)
                {
                    if (entry.ShardIds.Count > optimalPerRegion)
                    {
                        selected.AddRange(entry.ShardIds.Take(entry.ShardIds.Count - optimalPerRegion));
                    }
                }

                var result = selected.ToImmutable();

                return(result.Take(Limit(numberOfShards)).ToImmutableHashSet());
            }

            Task <IImmutableSet <ShardId> > RebalancePhase2(
                int numberOfShards,
                int optimalPerRegion,
                IImmutableList <RegionEntry> sortedEntries)
            {
                // In the first phase the optimalPerRegion is rounded up, and depending on number of shards per region and number
                // of regions that might not be the exact optimal.
                // In second phase we look for diff of >= 2 below optimalPerRegion and rebalance that number of shards.
                var countBelowOptimal = sortedEntries.Select(entry => Math.Max(0, (optimalPerRegion - 1) - entry.ShardIds.Count)).Sum();

                if (countBelowOptimal == 0)
                {
                    return(emptyRebalanceResult);
                }
                else
                {
                    var selected = ImmutableList.CreateBuilder <ShardId>();
                    foreach (var entry in sortedEntries)
                    {
                        if (entry.ShardIds.Count >= optimalPerRegion)
                        {
                            selected.Add(entry.ShardIds.First());
                        }
                    }

                    var result = selected.ToImmutable().Take(Math.Min(countBelowOptimal, Limit(numberOfShards))).ToImmutableHashSet();
                    return(Task.FromResult <IImmutableSet <ShardId> >(result));
                }
            }

            if (rebalanceInProgress.Count > 0)
            {
                // one rebalance at a time
                return(emptyRebalanceResult);
            }
            else
            {
                var sortedRegionEntries = RegionEntriesFor(currentShardAllocations).OrderBy(i => i, ShardSuitabilityOrdering.Instance).ToImmutableList();
                if (!IsAGoodTimeToRebalance(sortedRegionEntries))
                {
                    return(emptyRebalanceResult);
                }
                else
                {
                    var numberOfShards  = sortedRegionEntries.Select(i => i.ShardIds.Count).Sum();
                    var numberOfRegions = sortedRegionEntries.Count;
                    if (numberOfRegions == 0 || numberOfShards == 0)
                    {
                        return(emptyRebalanceResult);
                    }
                    else
                    {
                        var optimalPerRegion = numberOfShards / numberOfRegions + ((numberOfShards % numberOfRegions == 0) ? 0 : 1);

                        var result1 = RebalancePhase1(numberOfShards, optimalPerRegion, sortedRegionEntries);

                        if (result1.Count > 0)
                        {
                            return(Task.FromResult <IImmutableSet <ShardId> >(result1));
                        }
                        else
                        {
                            return(RebalancePhase2(numberOfShards, optimalPerRegion, sortedRegionEntries));
                        }
                    }
                }
            }
        }
Exemple #15
0
        //This function builds and returns an ImmutableList of Client objects.
        private static ImmutableList <Client> BuildImmutableList()
        {
            var builder = ImmutableList.CreateBuilder <Client>();

            return(builder.ToImmutableList());
        }
        public Constraints11ConstraintElement(
            IdIndexElement dIndexElement,
            IwIndexElement wIndexElement,
            Id d,
            Il l,
            Ipa pa,
            IP P,
            Iprob prob,
            ITPx x,
            Iμ μ)
        {
            Expression LHS = μ.Value[wIndexElement, dIndexElement];

            ImmutableList <Tuple <IpIndexElement, IaIndexElement, double> > .Builder builder = ImmutableList.CreateBuilder <Tuple <IpIndexElement, IaIndexElement, double> >();

            foreach (IpIndexElement pIndexElement in pa.Value.Where(i => P.IsThereElementAt(wIndexElement, i.pIndexElement)).Select(w => w.pIndexElement).Distinct())
            {
                foreach (IaIndexElement aIndexElement in pa.Value.Select(w => w.aIndexElement).Distinct())
                {
                    int dLowerBound = aIndexElement.Key <= dIndexElement.Key ? dIndexElement.Key - aIndexElement.Key : d.GetMaximumKey() + dIndexElement.Key - aIndexElement.Key;

                    double RHSSum = 0;

                    for (int w = dLowerBound;
                         w <= l.GetMaximumLengthOfStay();
                         w = w + 1)
                    {
                        RHSSum +=
                            (double)prob.GetElementAtAsdecimal(
                                pIndexElement,
                                l.GetElementAt(
                                    w));
                    }

                    builder.Add(
                        Tuple.Create(
                            pIndexElement,
                            aIndexElement,
                            RHSSum));
                }
            }

            ImmutableList <Tuple <IpIndexElement, IaIndexElement, double> > RHSSums = builder.ToImmutableList();

            Expression RHS = Expression.Sum(
                pa.Value
                .Where(i => P.IsThereElementAt(wIndexElement, i.pIndexElement))
                .Select(
                    y =>
                    RHSSums.Where(w => w.Item1 == y.pIndexElement && w.Item2 == y.aIndexElement).Select(w => w.Item3).SingleOrDefault()
                    *
                    x.Value[
                        y.pIndexElement,
                        y.aIndexElement]));

            this.Value = LHS == RHS;
        }
        protected override ComposablePartDefinition?CreatePart(Type partType, bool typeExplicitlyRequested)
        {
            Requires.NotNull(partType, nameof(partType));

            var partTypeInfo = partType.GetTypeInfo();

            // We want to ignore abstract classes, but we want to consider static classes.
            // Static classes claim to be both abstract and sealed. So to ignore just abstract
            // ones, we check that they are not sealed.
            if (partTypeInfo.IsAbstract && !partTypeInfo.IsSealed)
            {
                return(null);
            }

            BindingFlags everythingLocal = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
            BindingFlags instanceLocal   = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            // If the type is abstract only find local static exports
            var exportBindingFlags = everythingLocal;

            if (partTypeInfo.IsAbstract)
            {
                exportBindingFlags &= ~BindingFlags.Instance;
            }

            var declaredMethods    = partType.GetMethods(exportBindingFlags); // methods can only export, not import
            var declaredProperties = partType.GetProperties(everythingLocal);
            var declaredFields     = partType.GetFields(everythingLocal);

            var allLocalMembers  = declaredMethods.Concat <MemberInfo>(declaredProperties).Concat(declaredFields);
            var exportingMembers = from member in allLocalMembers
                                   from export in member.GetAttributes <ExportAttribute>()
                                   select new KeyValuePair <MemberInfo, ExportAttribute>(member, export);
            var exportedTypes = from export in partTypeInfo.GetAttributes <ExportAttribute>()
                                select new KeyValuePair <MemberInfo, ExportAttribute>(partTypeInfo, export);
            var inheritedExportedTypes = from baseTypeOrInterface in partType.GetInterfaces().Concat(partType.EnumTypeAndBaseTypes().Skip(1))
                                         where baseTypeOrInterface != typeof(object)
                                         from export in baseTypeOrInterface.GetTypeInfo().GetAttributes <InheritedExportAttribute>()
                                         select new KeyValuePair <MemberInfo, ExportAttribute>(baseTypeOrInterface.GetTypeInfo(), export);

            var exportsByMember = (from export in exportingMembers.Concat(exportedTypes).Concat(inheritedExportedTypes)
                                   group export.Value by export.Key into exportsByType
                                   select exportsByType).Select(g => new KeyValuePair <MemberInfo, ExportAttribute[]>(g.Key, g.ToArray())).ToArray();

            if (exportsByMember.Length == 0)
            {
                return(null);
            }

            // Check for PartNotDiscoverable only after we've established it's an interesting part.
            // This optimizes for the fact that most types have no exports, in which case it's not a discoverable
            // part anyway. Checking for the PartNotDiscoverableAttribute first, which is rarely defined,
            // doesn't usually pay for itself in terms of short-circuiting. But it does add an extra
            // attribute to look for that we don't need to find for all the types that have no export attributes either.
            if (!typeExplicitlyRequested && partTypeInfo.IsAttributeDefined <PartNotDiscoverableAttribute>())
            {
                return(null);
            }

            foreach (var exportingMember in exportsByMember)
            {
                this.ThrowOnInvalidExportingMember(exportingMember.Key);
            }

            TypeRef partTypeRef = TypeRef.Get(partType, this.Resolver);
            Type?   partTypeAsGenericTypeDefinition = partTypeInfo.IsGenericType ? partType.GetGenericTypeDefinition() : null;

            // Collect information for all imports.
            var imports = ImmutableList.CreateBuilder <ImportDefinitionBinding>();

            this.AddImportsFromMembers(declaredProperties, declaredFields, partTypeRef, imports);
            Type?baseType = partTypeInfo.BaseType;

            while (baseType != null && baseType != typeof(object))
            {
                this.AddImportsFromMembers(baseType.GetProperties(instanceLocal), baseType.GetFields(instanceLocal), partTypeRef, imports);
                baseType = baseType.GetTypeInfo().BaseType;
            }

            var partCreationPolicy          = CreationPolicy.Any;
            var partCreationPolicyAttribute = partTypeInfo.GetFirstAttribute <PartCreationPolicyAttribute>();

            if (partCreationPolicyAttribute != null)
            {
                partCreationPolicy = (CreationPolicy)partCreationPolicyAttribute.CreationPolicy;
            }

            var allExportsMetadata = ImmutableDictionary.CreateRange(PartCreationPolicyConstraint.GetExportMetadata(partCreationPolicy));
            var inheritedExportContractNamesFromNonInterfaces = ImmutableHashSet.CreateBuilder <string>();
            var exportDefinitions = ImmutableList.CreateBuilder <KeyValuePair <MemberInfo, ExportDefinition> >();

            foreach (var export in exportsByMember)
            {
                var memberExportMetadata = allExportsMetadata.AddRange(GetExportMetadata(export.Key));

                if (export.Key is MethodInfo method)
                {
                    var exportAttributes = export.Value;
                    if (exportAttributes.Any())
                    {
                        foreach (var exportAttribute in exportAttributes)
                        {
                            Type   exportedType   = exportAttribute.ContractType ?? ReflectionHelpers.GetContractTypeForDelegate(method);
                            string contractName   = string.IsNullOrEmpty(exportAttribute.ContractName) ? GetContractName(exportedType) : exportAttribute.ContractName;
                            var    exportMetadata = memberExportMetadata
                                                    .Add(CompositionConstants.ExportTypeIdentityMetadataName, ContractNameServices.GetTypeIdentity(exportedType));
                            var exportDefinition = new ExportDefinition(contractName, exportMetadata);
                            exportDefinitions.Add(new KeyValuePair <MemberInfo, ExportDefinition>(export.Key, exportDefinition));
                        }
                    }
                }
                else
                {
                    MemberInfo exportingTypeOrPropertyOrField = export.Key;
                    Verify.Operation(export.Key is TypeInfo || !partTypeInfo.IsGenericTypeDefinition, Strings.ExportsOnMembersNotAllowedWhenDeclaringTypeGeneric);
                    Type exportSiteType = ReflectionHelpers.GetMemberType(exportingTypeOrPropertyOrField);
                    foreach (var exportAttribute in export.Value)
                    {
                        Type   exportedType = exportAttribute.ContractType ?? partTypeAsGenericTypeDefinition ?? exportSiteType;
                        string contractName = string.IsNullOrEmpty(exportAttribute.ContractName) ? GetContractName(exportedType) : exportAttribute.ContractName;
                        if (export.Key is TypeInfo && exportAttribute is InheritedExportAttribute)
                        {
                            if (inheritedExportContractNamesFromNonInterfaces.Contains(contractName))
                            {
                                // We already have an export with this contract name on this type (from a more derived type)
                                // using InheritedExportAttribute.
                                continue;
                            }

                            if (!((TypeInfo)export.Key).IsInterface)
                            {
                                inheritedExportContractNamesFromNonInterfaces.Add(contractName);
                            }
                        }

                        var exportMetadata = memberExportMetadata
                                             .Add(CompositionConstants.ExportTypeIdentityMetadataName, ContractNameServices.GetTypeIdentity(exportedType));
                        var exportDefinition = new ExportDefinition(contractName, exportMetadata);
                        exportDefinitions.Add(new KeyValuePair <MemberInfo, ExportDefinition>(export.Key, exportDefinition));
                    }
                }
            }

            MethodInfo?onImportsSatisfied = null;

            if (typeof(IPartImportsSatisfiedNotification).IsAssignableFrom(partType))
            {
                onImportsSatisfied = OnImportsSatisfiedMethodInfo;
            }

            var importingConstructorParameters = ImmutableList.CreateBuilder <ImportDefinitionBinding>();
            var importingCtor = GetImportingConstructor <ImportingConstructorAttribute>(partType, publicOnly: false);

            if (importingCtor != null) // some parts have exports merely for metadata -- they can't be instantiated
            {
                foreach (var parameter in importingCtor.GetParameters())
                {
                    var import = this.CreateImport(parameter);
                    if (import.ImportDefinition.Cardinality == ImportCardinality.ZeroOrMore)
                    {
                        Verify.Operation(PartDiscovery.IsImportManyCollectionTypeCreateable(import), Strings.CollectionMustBePublicAndPublicCtorWhenUsingImportingCtor);
                    }

                    importingConstructorParameters.Add(import);
                }
            }

            var partMetadata = ImmutableDictionary.CreateBuilder <string, object?>();

            foreach (var partMetadataAttribute in partTypeInfo.GetAttributes <PartMetadataAttribute>())
            {
                partMetadata[partMetadataAttribute.Name] = partMetadataAttribute.Value;
            }

            var exportsOnType    = exportDefinitions.Where(kv => kv.Key is TypeInfo).Select(kv => kv.Value).ToArray();
            var exportsOnMembers = (from kv in exportDefinitions
                                    where !(kv.Key is TypeInfo)
                                    group kv.Value by kv.Key into byMember
                                    select byMember).ToDictionary(g => MemberRef.Get(g.Key, this.Resolver), g => (IReadOnlyCollection <ExportDefinition>)g.ToArray());

            var assemblyNamesForMetadataAttributes = ImmutableHashSet.CreateBuilder <AssemblyName>(ByValueEquality.AssemblyName);

            foreach (var export in exportsByMember)
            {
                GetAssemblyNamesFromMetadataAttributes <MetadataAttributeAttribute>(export.Key, assemblyNamesForMetadataAttributes);
            }

            return(new ComposablePartDefinition(
                       TypeRef.Get(partType, this.Resolver),
                       partMetadata.ToImmutable(),
                       exportsOnType,
                       exportsOnMembers,
                       imports.ToImmutable(),
                       partCreationPolicy != CreationPolicy.NonShared ? string.Empty : null,
                       MethodRef.Get(onImportsSatisfied, this.Resolver),
                       MethodRef.Get(importingCtor, this.Resolver),
                       importingCtor != null ? importingConstructorParameters.ToImmutable() : null, // some MEF parts are only for metadata
                       partCreationPolicy,
                       assemblyNamesForMetadataAttributes,
                       partCreationPolicy != CreationPolicy.NonShared));
        }
Exemple #18
0
        static DeviceInfo()
        {
            var macAddresses = ImmutableDictionary.CreateBuilder <string, string>();

            using (var mo = new ManagementObjectSearcher(@"
                SELECT 
                    MACAddress, Name 
                FROM 
                    Win32_NetworkAdapter 
                WHERE 
                    (MACAddress IS NOT NULL) AND
                    (PNPDeviceID LIKE 'PCI\\%')")
                            .Get())
                foreach (var obj in mo)
                {
                    macAddresses.Add(obj["Name"] as string, (obj["MACAddress"] as string).Replace(":", null));
                }
            MacAddresses = macAddresses.ToImmutable();

            var cpus = ImmutableArray.CreateBuilder <string>();

            using (var mo = new ManagementObjectSearcher(@"
                SELECT 
                    Name 
                FROM 
                    Win32_Processor")
                            .Get())
                foreach (var obj in mo)
                {
                    cpus.Add(obj["Name"] as string);
                }
            CPUs = cpus.ToImmutable();

            var gpus = ImmutableDictionary.CreateBuilder <string, uint>();

            using (var mo = new ManagementObjectSearcher(@"
                SELECT 
                    Name, AdapterRAM
                FROM 
                    Win32_VideoController")
                            .Get())
                foreach (var obj in mo)
                {
                    gpus.Add(obj["Name"] as string, (uint)obj["AdapterRAM"] / (1 << 30));
                }
            GPUs = gpus.ToImmutable();

            var disks = ImmutableDictionary.CreateBuilder <string, ulong>();

            using (var mo = new ManagementObjectSearcher(@"
                SELECT 
                    Caption, Size
                FROM 
                    Win32_DiskDrive")
                            .Get())
                foreach (var obj in mo)
                {
                    disks.Add(obj["Caption"] as string, (ulong)obj["Size"] / (1 << 30));
                }
            Disks = disks.ToImmutable();

            var rams = ImmutableList.CreateBuilder <ulong>();

            using (var mo = new ManagementObjectSearcher(@"
                SELECT 
                    Capacity
                FROM 
                    Win32_PhysicalMemory")
                            .Get())
                foreach (var obj in mo)
                {
                    rams.Add((ulong)obj["Capacity"] / (1 << 30));
                }
            RAMs = rams.ToImmutable();


            int        i   = 0;
            BigInteger big = 1;

            using (var mos = new ManagementObjectSearcher(@"
                SELECT 
                    SerialNumber
                FROM 
                    Win32_DiskDrive")
                             .Get())
            {
                foreach (var mo in mos.Cast <ManagementObject>()
                         .OrderBy(x => x.Properties["SerialNumber"].Value as string))
                {
                    big ^= (new BigInteger(Encoding.UTF8.GetBytes(mo["SerialNumber"] as string)) << i++);
                }
            }

            using (var mos = new ManagementObjectSearcher(@"
                SELECT 
                    MACAddress 
                FROM 
                    Win32_NetworkAdapter 
                WHERE 
                    (MACAddress IS NOT NULL) AND
                    (PNPDeviceID LIKE 'PCI\\%')")
                             .Get())
            {
                foreach (var mo in mos.Cast <ManagementObject>()
                         .OrderBy(x => x.Properties["MACAddress"].Value as string))
                {
                    big ^= (new BigInteger(Encoding.UTF8.GetBytes(mo["MACAddress"] as string)) << i++);
                }
            }
            Identifier = Convert.ToBase64String(big.ToByteArray());
        }
        protected override ComposablePartDefinition CreatePart(Type partType, bool typeExplicitlyRequested)
        {
            Requires.NotNull(partType, nameof(partType));

            if (!typeExplicitlyRequested)
            {
                bool isPublic = partType.IsNested ? partType.IsNestedPublic : partType.IsPublic;
                if (!this.IsNonPublicSupported && !isPublic)
                {
                    // Skip non-public types.
                    return(null);
                }
            }

            var declaredProperties  = partType.GetProperties(BindingFlags.Instance | this.PublicVsNonPublicFlags);
            var exportingProperties = from member in declaredProperties
                                      from export in member.GetAttributes <ExportAttribute>()
                                      select new KeyValuePair <MemberInfo, ExportAttribute>(member, export);
            var exportedTypes = from export in partType.GetAttributes <ExportAttribute>()
                                select new KeyValuePair <MemberInfo, ExportAttribute>(partType, export);
            var exportsByMember = (from export in exportingProperties.Concat(exportedTypes)
                                   group export.Value by export.Key into exportsByType
                                   select exportsByType).Select(g => new KeyValuePair <MemberInfo, ExportAttribute[]>(g.Key, g.ToArray())).ToArray();

            if (exportsByMember.Length == 0)
            {
                return(null);
            }

            // Check for PartNotDiscoverable only after we've established it's an interesting part.
            // This optimizes for the fact that most types have no exports, in which case it's not a discoverable
            // part anyway. Checking for the PartNotDiscoverableAttribute first, which is rarely defined,
            // doesn't usually pay for itself in terms of short-circuiting. But it does add an extra
            // attribute to look for that we don't need to find for all the types that have no export attributes either.
            if (!typeExplicitlyRequested && partType.IsAttributeDefined <PartNotDiscoverableAttribute>())
            {
                return(null);
            }

            TypeRef partTypeRef = TypeRef.Get(partType, this.Resolver);
            Type    partTypeAsGenericTypeDefinition = partType.IsGenericType ? partType.GetGenericTypeDefinition() : null;

            string sharingBoundary = null;
            var    sharedAttribute = partType.GetFirstAttribute <SharedAttribute>();

            if (sharedAttribute != null)
            {
                sharingBoundary = sharedAttribute.SharingBoundary ?? string.Empty;
            }

            CreationPolicy partCreationPolicy = sharingBoundary != null ? CreationPolicy.Shared : CreationPolicy.NonShared;
            var            allExportsMetadata = ImmutableDictionary.CreateRange(PartCreationPolicyConstraint.GetExportMetadata(partCreationPolicy));

            var exportsOnType    = ImmutableList.CreateBuilder <ExportDefinition>();
            var exportsOnMembers = ImmutableDictionary.CreateBuilder <MemberRef, IReadOnlyCollection <ExportDefinition> >();
            var imports          = ImmutableList.CreateBuilder <ImportDefinitionBinding>();

            foreach (var export in exportsByMember)
            {
                var member = export.Key;
                var memberExportMetadata = allExportsMetadata.AddRange(this.GetExportMetadata(member));

                if (member is Type)
                {
                    foreach (var exportAttribute in export.Value)
                    {
                        Type             exportedType     = exportAttribute.ContractType ?? partTypeAsGenericTypeDefinition ?? partType;
                        ExportDefinition exportDefinition = CreateExportDefinition(memberExportMetadata, exportAttribute, exportedType);
                        exportsOnType.Add(exportDefinition);
                    }
                }
                else // property
                {
                    var property = (PropertyInfo)member;
                    Verify.Operation(!partType.IsGenericTypeDefinition, Strings.ExportsOnMembersNotAllowedWhenDeclaringTypeGeneric);
                    var exportDefinitions = ImmutableList.CreateBuilder <ExportDefinition>();
                    foreach (var exportAttribute in export.Value)
                    {
                        Type             exportedType     = exportAttribute.ContractType ?? property.PropertyType;
                        ExportDefinition exportDefinition = CreateExportDefinition(memberExportMetadata, exportAttribute, exportedType);
                        exportDefinitions.Add(exportDefinition);
                    }

                    exportsOnMembers.Add(MemberRef.Get(member, this.Resolver), exportDefinitions.ToImmutable());
                }
            }

            foreach (var member in declaredProperties)
            {
                var importAttribute     = member.GetFirstAttribute <ImportAttribute>();
                var importManyAttribute = member.GetFirstAttribute <ImportManyAttribute>();
                Requires.Argument(!(importAttribute != null && importManyAttribute != null), "partType", Strings.MemberContainsBothImportAndImportMany, member.Name);

                var importConstraints = GetImportConstraints(member);
                ImportDefinition importDefinition;
                if (this.TryCreateImportDefinition(ReflectionHelpers.GetMemberType(member), member, importConstraints, out importDefinition))
                {
                    imports.Add(new ImportDefinitionBinding(importDefinition, TypeRef.Get(partType, this.Resolver), MemberRef.Get(member, this.Resolver)));
                }
            }

            MethodInfo onImportsSatisfied = null;

            foreach (var method in partType.GetMethods(this.PublicVsNonPublicFlags | BindingFlags.Instance))
            {
                if (method.IsAttributeDefined <OnImportsSatisfiedAttribute>())
                {
                    Verify.Operation(method.GetParameters().Length == 0, Strings.OnImportsSatisfiedTakeNoParameters);
                    Verify.Operation(onImportsSatisfied == null, Strings.OnlyOneOnImportsSatisfiedMethodIsSupported);
                    onImportsSatisfied = method;
                }
            }

            var importingConstructorParameters = ImmutableList.CreateBuilder <ImportDefinitionBinding>();
            var importingCtor = GetImportingConstructor <ImportingConstructorAttribute>(partType, publicOnly: !this.IsNonPublicSupported);

            Verify.Operation(importingCtor != null, Strings.NoImportingConstructorFound);
            foreach (var parameter in importingCtor.GetParameters())
            {
                var import = this.CreateImport(parameter, GetImportConstraints(parameter));
                if (import.ImportDefinition.Cardinality == ImportCardinality.ZeroOrMore)
                {
                    Verify.Operation(PartDiscovery.IsImportManyCollectionTypeCreateable(import), Strings.CollectionMustBePublicAndPublicCtorWhenUsingImportingCtor);
                }

                importingConstructorParameters.Add(import);
            }

            var partMetadata = ImmutableDictionary.CreateBuilder <string, object>();

            foreach (var partMetadataAttribute in partType.GetAttributes <PartMetadataAttribute>())
            {
                partMetadata[partMetadataAttribute.Name] = partMetadataAttribute.Value;
            }

            var assemblyNamesForMetadataAttributes = ImmutableHashSet.CreateBuilder <AssemblyName>();

            foreach (var export in exportsByMember)
            {
                GetAssemblyNamesFromMetadataAttributes <MetadataAttributeAttribute>(export.Key, assemblyNamesForMetadataAttributes);
            }

            return(new ComposablePartDefinition(
                       TypeRef.Get(partType, this.Resolver),
                       partMetadata.ToImmutable(),
                       exportsOnType.ToImmutable(),
                       exportsOnMembers.ToImmutable(),
                       imports.ToImmutable(),
                       sharingBoundary,
                       MethodRef.Get(onImportsSatisfied, this.Resolver),
                       ConstructorRef.Get(importingCtor, this.Resolver),
                       importingConstructorParameters.ToImmutable(),
                       partCreationPolicy,
                       assemblyNamesForMetadataAttributes));
        }
Exemple #20
0
 public static Parser <ImmutableList <A> > immutableListParser <A>(Parser <A> parser) =>
 collectionParser(parser, count => ImmutableList.CreateBuilder <A>(), (b, a) => {
     b.Add(a);
     return(b);
 }).map(_ => _.ToImmutable());
Exemple #21
0
 public void DebuggerAttributesValid()
 {
     DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableList.CreateBuilder <int>());
     DebuggerAttributes.ValidateDebuggerTypeProxyProperties(ImmutableList.CreateBuilder <string>());
 }
 protected override ImmutableList <TItem> .Builder CreateProxy(int knownSize)
 => ImmutableList.CreateBuilder <TItem>();
Exemple #23
0
        public Dependency(IDependencyModel dependencyModel, ITargetFramework targetFramework, string containingProjectPath)
        {
            Requires.NotNull(dependencyModel, nameof(dependencyModel));
            Requires.NotNullOrEmpty(dependencyModel.ProviderType, nameof(dependencyModel.ProviderType));
            Requires.NotNullOrEmpty(dependencyModel.Id, nameof(dependencyModel.Id));
            Requires.NotNull(targetFramework, nameof(targetFramework));
            Requires.NotNullOrEmpty(containingProjectPath, nameof(containingProjectPath));

            TargetFramework = targetFramework;

            _modelId = dependencyModel.Id;
            _containingProjectPath = containingProjectPath;

            ProviderType     = dependencyModel.ProviderType;
            Name             = dependencyModel.Name ?? string.Empty;
            Version          = dependencyModel.Version ?? string.Empty;
            Caption          = dependencyModel.Caption ?? string.Empty;
            OriginalItemSpec = dependencyModel.OriginalItemSpec ?? string.Empty;
            Path             = dependencyModel.Path ?? string.Empty;
            SchemaName       = dependencyModel.SchemaName ?? Folder.SchemaName;
            _schemaItemType  = dependencyModel.SchemaItemType ?? Folder.PrimaryDataSourceItemType;
            Resolved         = dependencyModel.Resolved;
            TopLevel         = dependencyModel.TopLevel;
            Implicit         = dependencyModel.Implicit;
            Visible          = dependencyModel.Visible;
            Priority         = dependencyModel.Priority;
            Flags            = dependencyModel.Flags;

            // Just in case custom providers don't do it, add corresponding flags for Resolved state.
            // This is needed for tree update logic to track if tree node changing state from unresolved
            // to resolved or vice-versa (it helps to decide if we need to remove it or update in-place
            // in the tree to avoid flicks).
            if (Resolved)
            {
                Flags = Flags.Union(DependencyTreeFlags.ResolvedFlags);
            }
            else
            {
                Flags = Flags.Union(DependencyTreeFlags.UnresolvedFlags);
            }

            Icon                   = dependencyModel.Icon;
            ExpandedIcon           = dependencyModel.ExpandedIcon;
            UnresolvedIcon         = dependencyModel.UnresolvedIcon;
            UnresolvedExpandedIcon = dependencyModel.UnresolvedExpandedIcon;
            Properties             = dependencyModel.Properties ??
                                     ImmutableDictionary <string, string> .Empty
                                     .Add(Folder.IdentityProperty, Caption)
                                     .Add(Folder.FullPathProperty, Path);

            if (dependencyModel.DependencyIDs == null)
            {
                DependencyIDs = ImmutableList <string> .Empty;
            }
            else
            {
                var normalizedDependencyIDs = ImmutableList.CreateBuilder <string>();
                foreach (var id in dependencyModel.DependencyIDs)
                {
                    normalizedDependencyIDs.Add(GetID(TargetFramework, ProviderType, id));
                }

                DependencyIDs = normalizedDependencyIDs.ToImmutable();
            }
        }
Exemple #24
0
            protected override void ApplyImpl(ImmutableList <ItemData> .Builder listBuilder, ImmutableHashSet <string> globsToIgnore)
            {
                if (!_conditionResult)
                {
                    return;
                }

                ItemSpecMatchesItem matchItemspec;
                bool?needToExpandMetadataForEachItem = null;

                if (ItemspecContainsASingleItemReference(_itemSpec, _itemElement.ItemType))
                {
                    // Perf optimization: If the Update operation references itself (e.g. <I Update="@(I)"/>)
                    // then all items are updated and matching is not necessary
                    matchItemspec = (itemSpec, item) => new MatchResult(true, null);
                }
                else if (ItemSpecContainsItemReferences(_itemSpec) &&
                         QualifiedMetadataReferencesExist(_metadata, out needToExpandMetadataForEachItem) &&
                         !Traits.Instance.EscapeHatches.DoNotExpandQualifiedMetadataInUpdateOperation)
                {
                    var itemReferenceFragments    = _itemSpec.Fragments.OfType <ItemSpec <P, I> .ItemExpressionFragment>().ToArray();
                    var nonItemReferenceFragments = _itemSpec.Fragments.Where(f => !(f is ItemSpec <P, I> .ItemExpressionFragment)).ToArray();

                    matchItemspec = (itemSpec, item) =>
                    {
                        var isMatch = nonItemReferenceFragments.Any(f => f.IsMatch(item.EvaluatedInclude));
                        Dictionary <string, I> capturedItemsFromReferencedItemTypes = null;

                        foreach (var itemReferenceFragment in itemReferenceFragments)
                        {
                            foreach (var referencedItem in itemReferenceFragment.ReferencedItems)
                            {
                                if (referencedItem.ItemAsValueFragment.IsMatch(item.EvaluatedInclude))
                                {
                                    isMatch = true;

                                    capturedItemsFromReferencedItemTypes ??= new Dictionary <string, I>(StringComparer.OrdinalIgnoreCase);

                                    capturedItemsFromReferencedItemTypes[referencedItem.Item.Key] = referencedItem.Item;
                                }
                            }
                        }

                        return(new MatchResult(isMatch, capturedItemsFromReferencedItemTypes));
                    };
                }
                else
                {
                    matchItemspec = (itemSpec, item) => new MatchResult(itemSpec.MatchesItem(item), null);
                }

                var itemsToUpdate = ImmutableList.CreateBuilder <ItemBatchingContext>();

                for (int i = 0; i < listBuilder.Count; i++)
                {
                    var itemData = listBuilder[i];

                    var matchResult = matchItemspec(_itemSpec, itemData.Item);

                    if (matchResult.IsMatch)
                    {
                        // items should be deep immutable, so clone and replace items before mutating them
                        // otherwise, with GetItems caching enabled, the mutations would leak into the cache causing
                        // future operations to mutate the state of past operations
                        var clonedItemData = listBuilder[i].Clone(_itemFactory, _itemElement);
                        listBuilder[i] = clonedItemData;

                        itemsToUpdate.Add(new ItemBatchingContext(clonedItemData.Item, matchResult.CapturedItemsFromReferencedItemTypes));
                    }
                }

                DecorateItemsWithMetadata(itemsToUpdate.ToImmutableList(), _metadata, needToExpandMetadataForEachItem);
            }
            protected override ICollection <I> SelectItems(ImmutableList <ItemData> .Builder listBuilder, ImmutableHashSet <string> globsToIgnore)
            {
                List <I> itemsToAdd = new List <I>();

                Lazy <Func <string, bool> > excludeTester = null;

                ImmutableList <string> .Builder excludePatterns = ImmutableList.CreateBuilder <string>();
                if (_excludes != null)
                {
                    // STEP 4: Evaluate, split, expand and subtract any Exclude
                    foreach (string exclude in _excludes)
                    {
                        string         excludeExpanded = _expander.ExpandIntoStringLeaveEscaped(exclude, ExpanderOptions.ExpandPropertiesAndItems, _itemElement.ExcludeLocation);
                        IList <string> excludeSplits   = ExpressionShredder.SplitSemiColonSeparatedList(excludeExpanded);
                        excludePatterns.AddRange(excludeSplits);
                    }

                    if (excludePatterns.Any())
                    {
                        excludeTester = new Lazy <Func <string, bool> >(() => EngineFileUtilities.GetMatchTester(excludePatterns, _rootDirectory));
                    }
                }

                ISet <string> excludePatternsForGlobs = null;

                foreach (var fragment in _itemSpec.Fragments)
                {
                    if (fragment is ItemExpressionFragment <P, I> )
                    {
                        // STEP 3: If expression is "@(x)" copy specified list with its metadata, otherwise just treat as string
                        bool throwaway;
                        var  itemsFromExpression = _expander.ExpandExpressionCaptureIntoItems(
                            ((ItemExpressionFragment <P, I>)fragment).Capture, _evaluatorData, _itemFactory, ExpanderOptions.ExpandItems,
                            false /* do not include null expansion results */, out throwaway, _itemElement.IncludeLocation);

                        if (excludeTester != null)
                        {
                            itemsToAdd.AddRange(itemsFromExpression.Where(item => !excludeTester.Value(item.EvaluatedInclude)));
                        }
                        else
                        {
                            itemsToAdd.AddRange(itemsFromExpression);
                        }
                    }
                    else if (fragment is ValueFragment)
                    {
                        string value = ((ValueFragment)fragment).ItemSpecFragment;

                        if (excludeTester == null ||
                            !excludeTester.Value(value))
                        {
                            var item = _itemFactory.CreateItem(value, value, _itemElement.ContainingProject.FullPath);
                            itemsToAdd.Add(item);
                        }
                    }
                    else if (fragment is GlobFragment)
                    {
                        string glob = ((GlobFragment)fragment).ItemSpecFragment;

                        if (excludePatternsForGlobs == null)
                        {
                            excludePatternsForGlobs = BuildExcludePatternsForGlobs(globsToIgnore, excludePatterns);
                        }

                        string[] includeSplitFilesEscaped = EngineFileUtilities.GetFileListEscaped(
                            _rootDirectory,
                            glob,
                            excludePatternsForGlobs
                            );

                        // itemsToAdd might grow 0 or more times during the following iteration. Proactively increase its capacity to ensure only one growth happens
                        IncreaseListCapacityIfNecessary(itemsToAdd, includeSplitFilesEscaped.Length);

                        foreach (string includeSplitFileEscaped in includeSplitFilesEscaped)
                        {
                            itemsToAdd.Add(_itemFactory.CreateItem(includeSplitFileEscaped, glob, _itemElement.ContainingProject.FullPath));
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(fragment.GetType().ToString());
                    }
                }

                return(itemsToAdd);
            }
Exemple #26
0
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
        public DataSetInfo(Options opts)
        {
            Bin = Enum.IsDefined(typeof(BinType), opts.Bin)
                ? (BinType)(opts.Bin)
                : throw new ArgumentException("Invalid binning type.", nameof(opts));
            OutFolder = opts.OutFolder;
            Suffix    = opts.Suffix;

            if (opts.Darks?.Any() ?? false)
            {
                Dark =
                    (from glob in opts.Darks
                     from desc in Ganss.IO.Glob.Expand(glob)
                     select desc).ToImmutableList();
            }

            if (opts.Bias?.Any() ?? false)
            {
                Bias =
                    (from glob in opts.Bias
                     from desc in Ganss.IO.Glob.Expand(glob)
                     select desc).ToImmutableList();
            }

            var darkPattern = Dark is null && !string.IsNullOrWhiteSpace(opts.DarkPattern)
                ? new Regex(opts.DarkPattern)
                : null;
            var biasPattern = Bias is null && !string.IsNullOrWhiteSpace(opts.BiasPattern)
                ? new Regex(opts.BiasPattern)
                : null;

            var darkBuilder = darkPattern is null
                ? null
                : ImmutableList.CreateBuilder <IFileSystemInfo>();
            var biasBuilder = darkPattern is null
                ? null
                : ImmutableList.CreateBuilder <IFileSystemInfo>();
            var filesBuilder = ImmutableList.CreateBuilder <IFileSystemInfo>();

            foreach (var item in
                     from glob in opts.Files
                     from desc in Ganss.IO.Glob.Expand(glob)
                     select desc)
            {
                if (darkPattern?.IsMatch(item.Name) ?? false)
                {
                    darkBuilder?.Add(item);
                }
                else if (biasPattern?.IsMatch(item.Name) ?? false)
                {
                    biasBuilder?.Add(item);
                }
                else if ((Dark?.Any(x => x.FullName == item.FullName) ?? false) ||
                         (Bias?.Any(x => x.FullName == item.FullName) ?? false))
                {
                    Console.Error.WriteLine($"File {item.FullName} is present in calibrations, omitting...");
                }
                else
                {
                    filesBuilder.Add(item);
                }
            }

            Files = filesBuilder.ToImmutable();
            Dark ??= darkBuilder?.ToImmutable() ?? ImmutableList <IFileSystemInfo> .Empty;
            Bias ??= biasBuilder?.ToImmutable() ?? ImmutableList <IFileSystemInfo> .Empty;
        }
Exemple #27
0
        internal static Options Create(string[] args)
        {
            string analyzerPath                = null;
            string solutionPath                = null;
            var    builder                     = ImmutableHashSet.CreateBuilder <string>();
            var    refactoringBuilder          = ImmutableHashSet.CreateBuilder <string>();
            bool   runConcurrent               = false;
            bool   reportSuppressedDiagnostics = false;
            bool   applyChanges                = false;
            bool   showStats                   = false;
            bool   showCompilerDiagnostics     = false;
            bool   useAll        = false;
            int    iterations    = 1;
            bool   testDocuments = false;
            Func <string, bool> testDocumentMatch = _ => true;
            int    testDocumentIterations         = 10;
            string logFileName              = null;
            string profileRoot              = null;
            var    usePersistentStorage     = false;
            var    analysisScope            = BackgroundAnalysisScope.Default;
            var    incrementalAnalyzerNames = ImmutableList.CreateBuilder <string>();

            int i = 0;

            while (i < args.Length)
            {
                var arg = args[i++];
                string ReadValue() => (i < args.Length) ? args[i++] : throw new InvalidDataException($"Missing value for option {arg}");

                switch (arg)
                {
                case "/all":
                    useAll = true;
                    break;

                case "/stats":
                    showStats = true;
                    break;

                case "/compilerStats":
                    showCompilerDiagnostics = true;
                    break;

                case "/concurrent":
                    runConcurrent = true;
                    break;

                case "/editperf":
                    testDocuments = true;
                    break;

                case var _ when arg.StartsWith("/editperf:"):
                    testDocuments = true;

                    var expression = new Regex(arg.Substring("/editperf:".Length), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    testDocumentMatch = documentPath => expression.IsMatch(documentPath);
                    break;

                case var _ when arg.StartsWith("/edititer:"):
                    testDocumentIterations = int.Parse(arg.Substring("/edititer:".Length));

                    break;

                case var _ when arg.StartsWith("/iter:"):
                    iterations = int.Parse(arg.Substring("/iter:".Length));

                    break;

                case "/suppressed":
                    reportSuppressedDiagnostics = true;
                    break;

                case "/apply":
                    applyChanges = true;
                    break;

                case "/a":
                    builder.Add(ReadValue());
                    break;

                case "/refactor":
                    refactoringBuilder.Add(ReadValue());
                    break;

                case "/log":
                    logFileName = ReadValue();
                    break;

                case "/profileroot":
                    profileRoot = ReadValue();
                    break;

                case "/persist":
                    usePersistentStorage = true;
                    break;

                case "/fsa":
                    analysisScope = BackgroundAnalysisScope.FullSolution;
                    break;

                case "/ia":
                    incrementalAnalyzerNames.Add(ReadValue());
                    break;

                default:
                    if (analyzerPath == null)
                    {
                        analyzerPath = arg;
                    }
                    else if (solutionPath == null)
                    {
                        solutionPath = arg;
                    }
                    else
                    {
                        throw new InvalidDataException((arg.StartsWith("/", StringComparison.Ordinal) ?
                                                        "Unrecognized option " + arg :
                                                        "Unrecognized parameter " + arg));
                    }
                    break;
                }
            }

            if (analyzerPath == null)
            {
                throw new InvalidDataException("Missing analyzer path");
            }

            if (solutionPath == null)
            {
                throw new InvalidDataException("Missing solution path");
            }

            return(new Options(
                       analyzerPath: analyzerPath,
                       solutionPath: solutionPath,
                       analyzerIds: builder.ToImmutableHashSet(),
                       refactoringNodes: refactoringBuilder.ToImmutableHashSet(),
                       runConcurrent: runConcurrent,
                       reportSuppressedDiagnostics: reportSuppressedDiagnostics,
                       applyChanges: applyChanges,
                       showStats: showStats,
                       showCompilerDiagnostics: showCompilerDiagnostics,
                       useAll: useAll,
                       iterations: iterations,
                       testDocuments: testDocuments,
                       testDocumentMatch: testDocumentMatch,
                       testDocumentIterations: testDocumentIterations,
                       logFileName: logFileName,
                       profileRoot: profileRoot,
                       usePersistentStorage: usePersistentStorage,
                       analysisScope: analysisScope,
                       incrementalAnalyzerNames: incrementalAnalyzerNames.ToImmutable()));
        }
Exemple #28
0
        public ArchiveFileModel ReadMetadata(Stream stream, PasswordStorage password)
        {
            if (mIsRunning)
            {
                throw new InvalidOperationException("Recursive invocation.");
            }

            try
            {
                mIsRunning = true;

                // reset fields to detect bugs
                mFileNames = null;
                mItemsWithoutStreamMarkers = null;
                mEmptyFileMarkers          = null;
                mDeletionMarkers           = null;
                mOffsets            = null;
                mAttributes         = null;
                mCDates             = null;
                mMDates             = null;
                mADates             = null;
                mItemCount          = -1;
                mItemsWithoutStream = -1;

                var metadata = ReadMetadataCore(stream, password);

                mRootFolder = new ArchivedFolder.Builder();
                mItemMap    = new Dictionary <ArchivedFolder.Builder, List <ArchivedItem.Builder> >();
                mItemMap.Add(mRootFolder, new List <ArchivedItem.Builder>());
                mFiles      = new HashSet <ArchivedFile.Builder>();
                mStreamMap  = new List <ArchivedFile.Builder>();
                mSectionMap = ImmutableArray.CreateBuilder <int>(metadata.DecoderSections.Length + 1);

                ArchiveDecoderSection currentDecoder = null;
                int currentSectionIndex = -1;
                int currentStreamIndex  = 0;
                int currentStreamCount  = 0;
                int currentEmptyIndex   = 0;

                for (int currentFileIndex = 0; currentFileIndex < mFileNames.Count; currentFileIndex++)
                {
                    var  filename             = mFileNames[currentFileIndex];
                    bool hasStream            = false;
                    ArchivedFile.Builder file = null;

                    if (mItemsWithoutStreamMarkers != null && mItemsWithoutStreamMarkers[currentFileIndex])
                    {
                        var isFile           = (mEmptyFileMarkers == null || mEmptyFileMarkers[currentEmptyIndex]);
                        var isDeletionMarker = (mDeletionMarkers != null && mDeletionMarkers[currentEmptyIndex]);

                        if (isDeletionMarker)
                        {
                            RemoveItem(mItemMap[mRootFolder], filename, 0);
                        }
                        else
                        {
                            file = AddItem(mItemMap[mRootFolder], filename, 0, isFile);
                        }

                        currentEmptyIndex++;
                    }
                    else
                    {
                        hasStream = true;
                        file      = AddItem(mItemMap[mRootFolder], filename, 0, true);
                    }

                    if (file != null)
                    {
                        if (mOffsets != null)
                        {
                            file.Offset = mOffsets[currentFileIndex] ?? 0;
                        }

                        if (mAttributes != null)
                        {
                            var attr = mAttributes[currentFileIndex];

                            if (attr.HasValue && (attr.Value & ArchivedAttributesExtensions.DirectoryAttribute) != 0)
                            {
                                throw new InvalidDataException();
                            }

                            file.Attributes = attr;
                        }

                        if (mCDates != null)
                        {
                            file.Creation = mCDates[currentFileIndex];
                        }

                        if (mMDates != null)
                        {
                            file.LastWrite = mMDates[currentFileIndex];
                        }

                        if (mADates != null)
                        {
                            file.LastAccess = mADates[currentFileIndex];
                        }

                        if (hasStream)
                        {
                            while (currentStreamIndex == currentStreamCount)
                            {
                                if (currentSectionIndex == metadata.DecoderSections.Length - 1)
                                {
                                    throw new InvalidDataException();
                                }

                                currentDecoder     = metadata.DecoderSections[++currentSectionIndex];
                                currentStreamCount = currentDecoder.Streams.Length;
                                currentStreamIndex = 0;

                                mSectionMap.Add(mStreamMap.Count);
                            }

                            file.Stream = new DecodedStreamIndex(currentSectionIndex, currentStreamIndex);

                            var streamMetadata = currentDecoder.Streams[currentStreamIndex++];
                            file.Length   = streamMetadata.Length;
                            file.Checksum = streamMetadata.Checksum;
                        }
                    }

                    if (hasStream)
                    {
                        mStreamMap.Add(file);
                    }
                }

                var finalStreamMap = ImmutableList.CreateBuilder <ArchivedFile>();
                foreach (var file in mStreamMap)
                {
                    finalStreamMap.Add(mFiles.Contains(file) ? file.ToImmutable() : null);
                }

                if (currentStreamIndex != currentStreamCount || currentSectionIndex != metadata.DecoderSections.Length - 1)
                {
                    throw new InvalidDataException();
                }

                mSectionMap.Add(finalStreamMap.Count);

                return(new ArchiveFileModel(metadata, BuildFolder(mRootFolder), mSectionMap.MoveToImmutable(), finalStreamMap.ToImmutable()));
            }
            finally
            {
                mIsRunning = false;
            }
        }
        /// <summary>
        /// Analyzes the project and returns information about the diagnostics in it.
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <ImmutableList <StyleCopDiagnostic> > GetDiagnosticsAsync()
        {
            var diagnostics = ImmutableList.CreateBuilder <StyleCopDiagnostic>();

            var syntaxTrees = this.analyzerCompilation.SyntaxTrees;

            foreach (var syntaxTree in syntaxTrees)
            {
                var match = DiagnosticPathRegex.Match(syntaxTree.FilePath);
                if (!match.Success)
                {
                    continue;
                }

                string shortName       = match.Groups["name"].Value;
                string noCodeFixReason = null;

                // Check if this syntax tree represents a diagnostic
                SyntaxNode syntaxRoot = await syntaxTree.GetRootAsync().ConfigureAwait(false);

                SemanticModel semanticModel   = this.analyzerCompilation.GetSemanticModel(syntaxTree);
                SyntaxNode    classSyntaxNode = syntaxRoot.DescendantNodes().FirstOrDefault(x => x.IsKind(SyntaxKind.ClassDeclaration));

                if (classSyntaxNode == null)
                {
                    continue;
                }

                INamedTypeSymbol classSymbol = semanticModel.GetDeclaredSymbol(classSyntaxNode) as INamedTypeSymbol;

                if (!this.InheritsFrom(classSymbol, this.diagnosticAnalyzerTypeSymbol))
                {
                    continue;
                }

                if (classSymbol.IsAbstract)
                {
                    continue;
                }

                bool hasImplementation = HasImplementation(syntaxRoot);

                IEnumerable <DiagnosticDescriptor> descriptorInfos = this.GetDescriptor(classSymbol);

                foreach (var descriptorInfo in descriptorInfos)
                {
                    var(codeFixStatus, fixAllStatus) = this.GetCodeFixAndFixAllStatus(descriptorInfo.Id, classSymbol, out noCodeFixReason);
                    string status = this.GetStatus(classSymbol, syntaxRoot, semanticModel, descriptorInfo);
                    if (descriptorInfo.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))
                    {
                        continue;
                    }

                    var diagnostic = new StyleCopDiagnostic
                    {
                        Id                = descriptorInfo.Id,
                        Category          = descriptorInfo.Category,
                        HasImplementation = hasImplementation,
                        Status            = status,
                        Name              = shortName,
                        Title             = descriptorInfo.Title.ToString(),
                        HelpLink          = descriptorInfo.HelpLinkUri,
                        CodeFixStatus     = codeFixStatus,
                        FixAllStatus      = fixAllStatus,
                        NoCodeFixReason   = noCodeFixReason,
                    };
                    diagnostics.Add(diagnostic);
                }
            }

            return(diagnostics.ToImmutable());
        }
Exemple #30
0
 protected override ImmutableList <T> .Builder Create(int count)
 {
     return(ImmutableList.CreateBuilder <T>());
 }