示例#1
0
 public CompatibilityProfileCacheEntry(
     CompatibilityProfileDataMut mutableProfileData,
     CompatibilityProfileData profile)
 {
     MutableProfileData = mutableProfileData;
     Profile            = profile;
 }
示例#2
0
        /// <summary>
        /// Combine a list of compatibility profiles together so that any assembly, module,
        /// type, command, etc. available in one is listed in the final result.
        /// </summary>
        /// <param name="profileId">The profile ID to assign to the generated union profile.</param>
        /// <param name="profiles">The profiles to union together to generate the result.</param>
        /// <returns>The deep union of all the given profiles, with the configured ID.</returns>
        public static CompatibilityProfileData UnionMany(string profileId, IEnumerable <CompatibilityProfileData> profiles)
        {
            CompatibilityProfileData unionProfile = CombineProfiles(profiles, Union);

            unionProfile.Platform            = null;
            unionProfile.Id                  = profileId;
            unionProfile.ConstituentProfiles = profiles.Select(p => p.Id).ToArray();
            return(unionProfile);
        }
        /// <summary>
        /// Deserialize a compatibility profile from a text reader object.
        /// </summary>
        /// <param name="textReader">The text reader to read JSON data from to hydrate the compatibility profile.</param>
        /// <returns>The hydrated compatibility profile as a .NET object.</returns>
        public CompatibilityProfileData Deserialize(TextReader textReader)
        {
            CompatibilityProfileData profile = _serializer.Deserialize <CompatibilityProfileData>(new JsonTextReader(textReader));

            if (profile.ProfileSchemaVersion == null)
            {
                profile.ProfileSchemaVersion = new Version(1, 0);
            }

            return(profile);
        }
示例#4
0
        /// <summary>
        /// For a set of profile paths, retrieve those profiles,
        /// along with the union profile for comparison.
        /// </summary>
        /// <param name="profileDirPath">The absolute path to the profile directory.</param>
        /// <param name="profilePaths">Absolute paths to all profiles to load.</param>
        /// <param name="unionProfile">The loaded union profile to compare against.</param>
        /// <returns>A list of hydrated profiles from all the profile paths given, not necessarily in order.</returns>
        public IEnumerable <CompatibilityProfileData> GetProfilesWithUnion(
            DirectoryInfo profileDirPath,
            IEnumerable <string> profilePaths,
            out CompatibilityProfileData unionProfile)
        {
            Task <CompatibilityProfileCacheEntry[]> profileEntries = GetProfilesFromPaths(profilePaths);

            unionProfile = GetUnionProfile(profileDirPath).Result.Profile;

            return(profileEntries.Result.Select(p => p.Profile));
        }
 public TypeCompatibilityVisitor(
     string analyzedFileName,
     CompatibilityProfileData anyProfile,
     IEnumerable <CompatibilityProfileData> compatibilityTargetProfiles,
     IEnumerable <string> typesToIgnore,
     UseCompatibleTypes rule)
 {
     _analyzedFileName     = analyzedFileName;
     _anyProfile           = anyProfile;
     _compatibilityTargets = compatibilityTargetProfiles;
     _typesToIgnore        = new HashSet <string>(typesToIgnore, StringComparer.OrdinalIgnoreCase);
     _rule = rule;
     _diagnosticAccumulator = new List <DiagnosticRecord>();
 }
        /// <summary>
        /// Call this method to load the compatibilty profiles configured for this rule.
        /// </summary>
        /// <returns>The any profile and a list of target profiles for this rule.</returns>
        protected IEnumerable <CompatibilityProfileData> LoadCompatibilityProfiles(out CompatibilityProfileData unionProfile)
        {
            if (TargetProfiles == null)
            {
                throw new InvalidOperationException($"{nameof(TargetProfiles)} cannot be null");
            }

            if (TargetProfiles.Length == 0)
            {
                throw new InvalidOperationException($"{nameof(TargetProfiles)} cannot be empty");
            }

            return(_profileLoader.GetProfilesWithUnion(_profileDir, TargetProfiles.Select(path => NormalizeProfileNameToAbsolutePath(path)), out unionProfile));
        }
示例#7
0
        /// <summary>
        /// Load a profile from a path.
        /// Caches profiles based on path, so that repeated calls do not require JSON deserialization.
        /// </summary>
        /// <param name="path">The path to load a profile from.</param>
        /// <returns>A query object around the loaded profile.</returns>
        private Lazy <Task <CompatibilityProfileCacheEntry> > GetProfileFromPath(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            return(_profileCache.GetOrAdd(path, new Lazy <Task <CompatibilityProfileCacheEntry> >(() => Task.Run(() => {
                CompatibilityProfileDataMut compatibilityProfileMut = _jsonSerializer.DeserializeFromFile(path);

                var compatibilityProfile = new CompatibilityProfileData(compatibilityProfileMut);

                return new CompatibilityProfileCacheEntry(
                    compatibilityProfileMut,
                    compatibilityProfile);
            }))));
        }
示例#8
0
            private void CheckCommandInvocationParameters(
                CompatibilityProfileData targetProfile,
                string commandName,
                CommandAst commandAst,
                IEnumerable <CommandData> commandsToCheck)
            {
                // TODO:
                // Ideally we would go through each command and emulate the parameter binding algorithm
                // to work out what positions and what parameters will and won't work,
                // but this is very involved.
                // For now, we'll just check that the parameters exist

                for (int i = 0; i < commandAst.CommandElements.Count; i++)
                {
                    CommandElementAst commandElement = commandAst.CommandElements[i];
                    if (!(commandElement is CommandParameterAst parameterAst))
                    {
                        continue;
                    }

                    bool isGoodParam = false;
                    foreach (CommandData command in commandsToCheck)
                    {
                        if ((command.Parameters != null && command.Parameters.ContainsKey(parameterAst.ParameterName)) ||
                            (command.IsCmdletBinding && targetProfile.Runtime.Common.Parameters.ContainsKey(parameterAst.ParameterName)))
                        {
                            isGoodParam = true;
                            break;
                        }
                    }

                    if (isGoodParam)
                    {
                        continue;
                    }

                    _diagnosticAccumulator.Add(CommandCompatibilityDiagnostic.CreateForParameter(
                                                   parameterAst.ParameterName,
                                                   commandName,
                                                   targetProfile.Platform,
                                                   parameterAst.Extent,
                                                   _analyzedFileName,
                                                   _rule));
                }
            }
示例#9
0
        private static CompatibilityProfileData CombineProfiles(IEnumerable <CompatibilityProfileData> profiles, Func <CompatibilityProfileData, CompatibilityProfileData, object> combinator)
        {
            IEnumerator <CompatibilityProfileData> profileEnumerator = profiles.GetEnumerator();

            if (!profileEnumerator.MoveNext())
            {
                return(null);
            }

            CompatibilityProfileData mutProfileBase = (CompatibilityProfileData)profileEnumerator.Current.Clone();

            while (profileEnumerator.MoveNext())
            {
                mutProfileBase = (CompatibilityProfileData)combinator(mutProfileBase, profileEnumerator.Current);
            }

            return(mutProfileBase);
        }
示例#10
0
        /// <inheritdoc/>
        public override bool IsProfileValid(Data.CompatibilityProfileData profileData, out IEnumerable <Exception> errors)
        {
            CompatibilityProfileData queryableProfile;

            try
            {
                queryableProfile = new CompatibilityProfileData(profileData);
            }
            catch (Exception e)
            {
                errors = new [] { e };
                return(false);
            }

            try
            {
                _psVersionMajor = queryableProfile.Platform.PowerShell.Version.Major;
                ValidatePlatformData(queryableProfile.Id, queryableProfile.Platform);
            }
            catch (Exception e)
            {
                _errAcc.AddError(e);
            }

            try
            {
                ValidateRuntimeData(queryableProfile.Runtime);
            }
            catch (Exception e)
            {
                _errAcc.AddError(e);
            }

            if (_errAcc.HasErrors())
            {
                errors = _errAcc.GetErrors();
                return(false);
            }

            errors = null;
            return(true);
        }
示例#11
0
        private static object Union(CompatibilityProfileData thisProfile, CompatibilityProfileData thatProfile)
        {
            Union(thisProfile.Runtime, thatProfile.Runtime);

            return(thisProfile);
        }