Exemplo n.º 1
0
        /// <summary>
        /// Converts a collection of <see cref="ModuleReference"/>s into an array of <see cref="DurianModule"/>s.
        /// </summary>
        /// <param name="references">A collection of <see cref="ModuleReference"/>s to convert.</param>
        /// <exception cref="ArgumentNullException"><paramref name="references"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">Null <see cref="ModuleReference"/> detected.</exception>
        public static DurianModule[] ToEnums(IEnumerable <ModuleReference> references)
        {
            if (references is null)
            {
                throw new ArgumentNullException(nameof(references));
            }

            ModuleReference[] array = references.ToArray();

            if (array.Length == 0)
            {
                return(Array.Empty <DurianModule>());
            }

            DurianModule[] enums = new DurianModule[array.Length];

            for (int i = 0; i < enums.Length; i++)
            {
                ModuleReference?reference = array[i];

                if (reference is null)
                {
                    throw new InvalidOperationException($"Null module reference at index: {i}");
                }

                enums[i] = reference.EnumValue;
            }

            return(enums);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Removes all occurrences of the specified Durian <paramref name="module"/> from the container.
        /// </summary>
        /// <returns><see langword="true"/> if any <see cref="DurianModule"/>s has been successfully removed, <see langword="false"/> otherwise.</returns>
        /// <param name="module"><see cref="DurianModule"/> to remove all occurrences of.</param>
        public bool RemoveAll(DurianModule module)
        {
            List <int> duplicates = new(Count);

            for (int i = 0; i < Count; i++)
            {
                if (_enums[i] == module)
                {
                    duplicates.Add(i);
                }
            }

            if (duplicates.Count == 0)
            {
                return(false);
            }

            for (int i = duplicates.Count - 1; i > -1; i++)
            {
                int dup = duplicates[i];

                _references.RemoveAt(dup);
                _enums.RemoveAt(dup);
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts a collection of <see cref="ModuleIdentity"/>s into an array of <see cref="DurianModule"/>s.
        /// </summary>
        /// <param name="modules">A collection of <see cref="ModuleIdentity"/>s to convert.</param>
        /// <exception cref="ArgumentNullException"><paramref name="modules"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">Null <see cref="ModuleIdentity"/> detected.</exception>
        public static DurianModule[] ToEnums(IEnumerable <ModuleIdentity> modules)
        {
            if (modules is null)
            {
                throw new ArgumentNullException(nameof(modules));
            }

            ModuleIdentity[] array = modules.ToArray();

            if (array.Length == 0)
            {
                return(Array.Empty <DurianModule>());
            }

            DurianModule[] enums = new DurianModule[array.Length];

            for (int i = 0; i < enums.Length; i++)
            {
                ModuleIdentity?identity = array[i];

                if (identity is null)
                {
                    throw new InvalidOperationException($"Null module identity at index: {i}");
                }

                enums[i] = identity.Module;
            }

            return(enums);
        }
Exemplo n.º 4
0
 public static bool ShouldAnalyze(Compilation compilation, DurianModule module)
 {
     return
         (IsCSharpCompilationAnalyzer.Analyze(compilation) &&
          DisabledModuleAnalyzer.IsEnabled(module, compilation) &&
          DependencyAnalyzer.Analyze(compilation));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Includes the specified <paramref name="module"/> in the container unless it is already included.
        /// </summary>
        /// <returns><see langword="true"/> if the <paramref name="module"/> has been successfully included, <see langword="false"/> otherwise.</returns>
        /// <param name="module"><see cref="DurianModule"/> to include.</param>
        /// <exception cref="ArgumentException"><paramref name="module"/> is not a valid <see cref="DurianModule"/> value.</exception>
        public bool TryInclude(DurianModule module)
        {
            if (!Contains(module))
            {
                Include(module);
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Determines whether the specified <paramref name="module"/> is enabled for the given <paramref name="assembly"/>.
        /// </summary>
        /// <param name="assembly"><see cref="Assembly"/> to check if the <paramref name="module"/> is enabled for.</param>
        /// <param name="module"><see cref="DurianModule"/> representing a Durian module to check for.</param>
        /// <exception cref="ArgumentNullException"><paramref name="assembly"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">Unknown <see cref="DurianModule"/> value detected. -or- <see cref="DurianModule.None"/> is not a valid Durian module.</exception>
        public static bool IsEnabled(this Assembly assembly, DurianModule module)
        {
            if (assembly is null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            ModuleIdentity.EnsureIsValidModuleEnum(module);
            return(IsEnabled_Internal(assembly, module));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ModuleReference"/> class.
        /// </summary>
        /// <param name="module">The module this <see cref="ModuleReference"/> references.</param>
        /// <exception cref="ArgumentNullException"><paramref name="module"/> is <see langword="null"/>.</exception>
        public ModuleReference(ModuleIdentity module)
        {
            if (module is null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            _module   = module;
            EnumValue = module.Module;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Includes the specified <paramref name="module"/> in the container.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to include.</param>
        /// <exception cref="ArgumentException"><paramref name="module"/> is not a valid <see cref="DurianModule"/> value.</exception>
        public void Include(DurianModule module)
        {
            if (!GlobalInfo.IsValidModuleValue(module))
            {
                throw new ArgumentException($"Value '{module}' is not a valid {nameof(DurianModule)} value!", nameof(module));
            }

            _enums.Add(module);
            _references.Add(null);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Determines whether the specified <paramref name="assembly"/> references a Durian module with the given <paramref name="moduleName"/>.
        /// </summary>
        /// <param name="assembly"><see cref="Assembly"/> to check if contains the reference.</param>
        /// <param name="moduleName">Name of the Durian module to check for.</param>
        /// <exception cref="ArgumentNullException"><paramref name="moduleName"/> is <see langword="null"/>. -or- <paramref name="assembly"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">Unknown Durian module name: <paramref name="moduleName"/>.</exception>
        public static bool IsEnabled(this Assembly assembly, string moduleName)
        {
            if (assembly is null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            DurianModule module = ModuleIdentity.ParseModule(moduleName);

            return(IsEnabled_Internal(assembly, module));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Throws an <see cref="InvalidOperationException"/> if the specified <paramref name="module"/> is not a valid <see cref="DurianModule"/> value.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to ensure that is valid.</param>
        /// <exception cref="InvalidOperationException">Unknown <see cref="DurianModule"/> value detected. -or- <see cref="DurianModule.None"/> is not a valid Durian module.</exception>
        public static void EnsureIsValidModuleEnum(DurianModule module)
        {
            if (module == DurianModule.None)
            {
                throw new InvalidOperationException($"{nameof(DurianModule)}.{nameof(DurianModule.None)} is not a valid Durian module!");
            }

            if (!GlobalInfo.IsValidModuleValue(module))
            {
                throw new InvalidOperationException($"Unknown {nameof(DurianModule)} value: {module}!");
            }
        }
Exemplo n.º 11
0
 internal ModuleIdentity(
     DurianModule module,
     int id,
     DurianPackage[]?packages,
     string?docsPath,
     DiagnosticData[]?diagnostics,
     TypeIdentity[]?types
     )
 {
     Module        = module;
     AnalysisId    = (IdSection)id;
     Documentation = docsPath is not null ? @$ "{GlobalInfo.Repository}\{docsPath}" : string.Empty;
Exemplo n.º 12
0
        /// <summary>
        /// Returns the first <see cref="ModuleReference"/> that references the specified <paramref name="module"/>.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to get the <see cref="ModuleIdentity"/> for.</param>
        public ModuleReference?GetReference(DurianModule module)
        {
            for (int i = 0; i < Count; i++)
            {
                if (_enums[i] == module)
                {
                    ModuleReference reference = GetReference(i);
                    return(reference);
                }
            }

            return(null);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Returns a new instance of <see cref="ModuleIdentity"/> corresponding with the specified <see cref="DurianModule"/>.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to get <see cref="ModuleIdentity"/> for.</param>
        /// <exception cref="InvalidOperationException">Unknown <see cref="DurianModule"/> value detected. -or- <see cref="DurianModule.None"/> is not a valid Durian module.</exception>
        public static ModuleIdentity GetModule(DurianModule module)
        {
            return(module switch
            {
                DurianModule.Core => ModuleRepository.Core,
                DurianModule.DefaultParam => ModuleRepository.DefaultParam,

                //DurianModule.EnumServices => ModuleRepository.EnumServices,
                DurianModule.FriendClass => ModuleRepository.FriendClass,
                DurianModule.InterfaceTargets => ModuleRepository.InterfaceTargets,
                DurianModule.None => throw new InvalidOperationException($"{nameof(DurianModule)}.{nameof(DurianModule.None)} is not a valid Durian module!"),
                _ => throw new InvalidOperationException($"Unknown {nameof(DurianModule)} value: {module}!")
            });
Exemplo n.º 14
0
        /// <summary>
        /// Removes first occurrence of the specified <paramref name="module"/> from the container.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to remove.</param>
        /// <returns><see langword="true"/> if the <paramref name="module"/> has been successfully removed, <see langword="false"/> otherwise.</returns>
        public bool Remove(DurianModule module)
        {
            int index = _enums.IndexOf(module);

            if (index == -1)
            {
                return(false);
            }

            _enums.RemoveAt(index);
            _references.RemoveAt(index);

            return(true);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Returns all elements in the container as values of <see cref="DurianModule"/>.
        /// </summary>
        public DurianModule[] AsEnums()
        {
            if (Count == 0)
            {
                return(Array.Empty <DurianModule>());
            }

            DurianModule[] array = new DurianModule[Count];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = _enums[i];
            }

            return(array);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Returns a collection of all <see cref="ModuleReference"/>s that point to the specified <paramref name="module"/>.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to get the <see cref="ModuleReference"/>s for.</param>
        public IEnumerable <ModuleReference> GetAllReferences(DurianModule module)
        {
            List <ModuleReference> modules = new(Count);

            for (int i = 0; i < Count; i++)
            {
                if (_enums[i] == module)
                {
                    ModuleReference reference = GetReference(i);

                    modules.Add(reference);
                }
            }

            return(modules);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Removes first occurrence of the specified <paramref name="module"/> from the container.
        /// </summary>
        /// <param name="module"><see cref="DurianModule"/> to remove.</param>
        /// <param name="reference">Reference to a module that was removed from the container.</param>
        /// <returns><see langword="true"/> if the <paramref name="module"/> has been successfully removed, <see langword="false"/> otherwise.</returns>
        public bool Remove(DurianModule module, [NotNullWhen(true)] out ModuleReference?reference)
        {
            int index = _enums.IndexOf(module);

            if (index == -1)
            {
                reference = null;
                return(false);
            }

            ModuleReference?r = _references[index] ?? new ModuleReference(module);

            _enums.RemoveAt(index);
            _references.RemoveAt(index);

            reference = r;

            return(true);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ModuleReference"/> class.
 /// </summary>
 /// <param name="module">The module this <see cref="ModuleReference"/> references.</param>
 /// <exception cref="InvalidOperationException">Unknown <see cref="DurianModule"/> value detected. -or- <see cref="DurianModule.None"/> is not a valid Durian module.</exception>
 public ModuleReference(DurianModule module)
 {
     ModuleIdentity.EnsureIsValidModuleEnum(module);
     EnumValue = module;
 }
Exemplo n.º 19
0
 internal ModuleReference(DurianModule module, PackageIdentity onAllocate)
 {
     EnumValue   = module;
     _onAllocate = onAllocate;
 }
Exemplo n.º 20
0
 /// <summary>
 /// Determines whether the specified <paramref name="module"/> is a valid <see cref="DurianModule"/> value.
 /// </summary>
 /// <param name="module"><see cref="DurianModule"/> to check.</param>
 public static bool IsValidModuleValue(DurianModule module)
 {
     return(module >= ModuleMin && module <= ModuleMax);
 }
Exemplo n.º 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DurianGeneratedAttribute"/> class.
 /// </summary>
 /// <param name="module">Module that is enabled.</param>
 public EnableModuleAttribute(DurianModule module)
 {
     Module = module;
 }
Exemplo n.º 22
0
 private static bool IsEnabled_Internal(Assembly assembly, DurianModule module)
 {
     return(IsEnabled_Internal(assembly.GetCustomAttributes <EnableModuleAttribute>(), module));
 }
Exemplo n.º 23
0
        /// <summary>
        /// Attempts to return <see cref="ModuleReference"/> that refers to the specified <paramref name="module"/>.
        /// </summary>
        /// <returns><see langword="true"/> if an appropriate <see cref="ModuleReference"/> has been found, <see langword="false"/> otherwise.</returns>
        /// <param name="module"><see cref="DurianModule"/> to get the <see cref="ModuleReference"/> for.</param>
        /// <param name="reference"><see cref="ModuleReference"/> that represents the specified <paramref name="module"/>.</param>
        public bool TryGetReference(DurianModule module, [NotNullWhen(true)] out ModuleReference?reference)
        {
            reference = GetReference(module);

            return(reference is not null);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Attempts to return <see cref="ModuleIdentity"/> that represents the specified <paramref name="module"/>.
        /// </summary>
        /// <returns><see langword="true"/> if an appropriate <see cref="ModuleIdentity"/> has been found, <see langword="false"/> otherwise.</returns>
        /// <param name="module"><see cref="DurianModule"/> to get the <see cref="ModuleIdentity"/> for.</param>
        /// <param name="identity"><see cref="ModuleIdentity"/> that represents the specified <paramref name="module"/>.</param>
        public bool TryGetModule(DurianModule module, [NotNullWhen(true)] out ModuleIdentity?identity)
        {
            identity = GetModule(module);

            return(identity is not null);
        }
Exemplo n.º 25
0
        private static bool IsEnabled_Internal(IEnumerable <EnableModuleAttribute> attributes, DurianModule module)
        {
            foreach (EnableModuleAttribute attribute in attributes)
            {
                if (attribute.Module == module)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 26
0
 /// <summary>
 /// Determines whether the specified <paramref name="module"/> is included withing this instance.
 /// </summary>
 /// <param name="module"><see cref="DurianModule"/> to check.</param>
 public bool Contains(DurianModule module)
 {
     return(_enums.Contains(module));
 }