/// <summary>
        /// Determines whether a component exists having a specific version
        /// </summary>
        /// <param name="name">The name of the component</param>
        /// <param name="version">The specific version of the component</param>
        /// <returns>True if the component exists at the version; false otherwise</returns>
        public bool ComponentExists(IComponentName name, IComponentVersion version)
        {
            ValidateComponentName(name);
            ValidateComponentVersion(version);

            var componentVersionPath = Path.Combine(ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl), name.ToString(), version.ToString());

            if (Directory.Exists(componentVersionPath))
            {
                // ReSharper disable LoopCanBeConvertedToQuery
                foreach (var dependencyDefinitionFileName in _validDependencyDefinitionFileNames)
                // ReSharper restore LoopCanBeConvertedToQuery
                {
                    var dependencyDefinitionFile = Path.Combine(componentVersionPath, dependencyDefinitionFileName);
                    if (File.Exists(dependencyDefinitionFile))
                    {
                        Logger.Instance().Log(TraceLevel.Info, "{0}: Component {1}#{2} was found on file share", ResolverType, name, version);
                        return(true);
                    }
                }
            }

            Logger.Instance().Log(TraceLevel.Warning, "{0}: Component {1}#{2} was not found on file share", ResolverType, name, version);
            return(false);
        }
        /// <summary>
        /// Discover all available component names in the file share.
        /// </summary>
        /// <returns>Returns a list with all component names</returns>
        public IEnumerable <IComponentName> GetAvailableComponentNames()
        {
            var componentNames = new List <IComponentName>();

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying available components...", ResolverType);

            try
            {
                foreach (var componentPath in Directory.GetDirectories(ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl)))
                {
                    var componentName = componentPath.Split(Path.DirectorySeparatorChar).Last();
                    componentNames.Add(new ComponentName(componentName));
                    Logger.Instance().Log(TraceLevel.Info, "{0}: Found component {1}", ResolverType, componentName);
                }
            }
            catch (UnauthorizedAccessException uae)
            {
                Logger.Instance().Log(
                    TraceLevel.Error,
                    "{0}: Could not access file share {1} (Unauthorized access exception {2})",
                    ResolverType,
                    ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl),
                    uae.Message);
                throw new InvalidAccessRightsException(string.Format("Could not access file share ({0})", ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl)));
            }

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying components finished successfully", ResolverType);

            return(componentNames);
        }
        /// <summary>
        /// Determines whether a component exists
        /// </summary>
        /// <param name="name">The name of the component</param>
        /// <returns>true if the component exists; false if not</returns>
        public bool ComponentExists(IComponentName name)
        {
            ValidateComponentName(name);

            if (Directory.Exists(Path.Combine(ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl), name.ToString())))
            {
                Logger.Instance().Log(TraceLevel.Info, "{0}: Component folder for component {1} was found on file share", ResolverType, name);
                return(true);
            }

            Logger.Instance().Log(TraceLevel.Warning, "{0}: Component folder for component {1} was not found on file share", ResolverType, name);
            return(false);
        }
        /// <summary>
        /// Discover all available versions for a specific component.
        /// </summary>
        /// <param name="componentName">The name of the component</param>
        /// <returns>A list with all versions</returns>
        public IEnumerable <IComponentVersion> GetAvailableVersions(IComponentName componentName)
        {
            var componentVersions = new List <IComponentVersion>();
            var componentPath     = Path.Combine(ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl), componentName.Path);

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying available component versions...", ResolverType);

            try
            {
                if (Directory.Exists(componentPath))
                {
                    foreach (var versionPath in Directory.GetDirectories(componentPath))
                    {
                        var componentVersion = versionPath.Split(Path.DirectorySeparatorChar).Last();

                        foreach (var dependencyDefinitionFileName in _validDependencyDefinitionFileNames)
                        {
                            var dependencyDefinitionFile = Path.Combine(versionPath, dependencyDefinitionFileName);

                            if (File.Exists(dependencyDefinitionFile))
                            {
                                Logger.Instance().Log(TraceLevel.Info, "{0}: Found version {1}", ResolverType, componentVersion);
                                componentVersions.Add(new ComponentVersion(componentVersion));
                            }
                        }

                        Logger.Instance().Log(TraceLevel.Warning, "{0}: Skipping version {1} (Dependency definition file is not present)", ResolverType, componentVersion);
                    }
                }
                else
                {
                    Logger.Instance().Log(TraceLevel.Error, "{0}: Directory {1} for component {2} does not exist", ResolverType, componentPath, componentName);
                    throw new InvalidComponentException(string.Format("Could not find component {0} on file share", componentName));
                }
            }
            catch (UnauthorizedAccessException uae)
            {
                Logger.Instance().Log(
                    TraceLevel.Error,
                    "{0}: Could not access file share {1} (Unauthorized access exception {2})",
                    ResolverType,
                    ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl),
                    uae.Message);
                throw new InvalidAccessRightsException(string.Format("Could not access file share ({0})", ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl)));
            }

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying component versions finished successfully", ResolverType);

            return(componentVersions);
        }
        /// <summary>
        /// Loads a specific dependency definition file.
        /// </summary>
        /// <param name="name">The name of the component</param>
        /// <param name="version">The exact version of the component</param>
        /// <returns>The loaded dependency definition xml file</returns>
        public XDocument LoadComponentTarget(IComponentName name, IComponentVersion version)
        {
            ValidateComponentName(name);

            foreach (var dependencyDefinitionFileName in _validDependencyDefinitionFileNames)
            {
                var dependencyDefinitionFileLocation = Path.Combine(ResolverSettings.GetSetting(ResolverValidSettings.FileShareUrl), name.ToString(), version.ToString(), dependencyDefinitionFileName);
                if (!File.Exists(dependencyDefinitionFileLocation))
                {
                    Logger.Instance().Log(TraceLevel.Verbose, "{0}: Dependency definition file {1} for component {2}#{3} was not found", ResolverType, dependencyDefinitionFileLocation, name, version);
                    continue;
                }

                var xdoc = XDocument.Load(dependencyDefinitionFileLocation);

                Logger.Instance().Log(TraceLevel.Info, "{0}: Loading dependency definition file {1} for component {2}#{3} finished successfully", ResolverType, dependencyDefinitionFileLocation, name, version);
                return(xdoc);
            }

            return(null);
        }
        /// <summary>
        /// Discover all available versions.
        /// </summary>
        /// <param name="name">The component name</param>
        /// <returns>A list with all versions</returns>
        public IEnumerable <IComponentVersion> GetAvailableVersions(IComponentName name)
        {
            var versList = new List <IComponentVersion>();

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying available component versions...", ResolverType);

            var teamProject = _versionControlServer.TryGetTeamProject(name.TeamProject);

            if (teamProject == null)
            {
                Logger.Instance().Log(
                    TraceLevel.Info,
                    "{0}: Could not find team project {1} on version control server for team project collection {2}",
                    ResolverType,
                    name.TeamProject,
                    ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl));
                throw new InvalidComponentException(
                          string.Format(
                              "Could not find team project {0} on version control server for team project collection {1}",
                              name.TeamProject,
                              ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl)));
            }

            var buildDef = (_client.GetDefinitionsAsync(project: teamProject.Name, name: name.BuildDefinition)).Result;

            if (buildDef == null)
            {
                Logger.Instance().Log(
                    TraceLevel.Info,
                    "{0}: Could not find build definition {1} for team project {2} on version control server for tfs {3}",
                    ResolverType,
                    name.BuildDefinition,
                    name.TeamProject,
                    ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl));
                throw new InvalidComponentException(
                          string.Format(
                              "Could not find build definition {0} for team project {1} on version control server for tfs {2}",
                              name.BuildDefinition,
                              name.TeamProject,
                              ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl)));
            }

            // Query all builds with this teamProject and build definition name
            var build = _client.GetBuildsAsync(teamProject.Name, definitions: new List <int> {
                buildDef.First().Id
            }, type: TFSWebApi.DefinitionType.Build).Result;

            foreach (var buildResult in build)
            {
                List <string> availableBuildTags = null;
                if (buildResult.Tags != null && buildResult.Tags.Count() > 0)
                {
                    availableBuildTags = buildResult.Tags;
                }
                var availableBuildStatus = new List <string> {
                    buildResult.Status.ToString()
                };
                var vers = new ComponentVersion(buildResult.BuildNumber, availableBuildStatus, acceptedBuildQuality: null, acceptedBuildTags: availableBuildTags);
                versList.Add(vers);
                Logger.Instance().Log(TraceLevel.Info, "{0}: Found build {1}", ResolverType, vers.ToString());
            }

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying component versions finished successfully", ResolverType);
            return(versList);
        }
Exemple #7
0
        /// <summary>
        /// Discover all available versions.
        /// </summary>
        /// <param name="name">The component name</param>
        /// <returns>A list with all versions</returns>
        public IEnumerable <IComponentVersion> GetAvailableVersions(IComponentName name)
        {
            var versList = new List <IComponentVersion>();

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying available component versions...", ResolverType);

            var teamProject = _versionControlServer.TryGetTeamProject(name.TeamProject);

            if (teamProject == null)
            {
                Logger.Instance().Log(
                    TraceLevel.Info,
                    "{0}: Could not find team project {1} on version control server for team project collection {2}",
                    ResolverType,
                    name.TeamProject,
                    ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl));
                throw new InvalidComponentException(
                          string.Format(
                              "Could not find team project {0} on version control server for team project collection {1}",
                              name.TeamProject,
                              ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl)));
            }

            var buildDef = _buildServer.GetBuildDefinition(teamProject.Name, name.BuildDefinition);

            if (buildDef == null)
            {
                Logger.Instance().Log(
                    TraceLevel.Info,
                    "{0}: Could not find build definition {1} for team project {2} on version control server for tfs {3}",
                    ResolverType,
                    name.BuildDefinition,
                    name.TeamProject,
                    ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl));
                throw new InvalidComponentException(
                          string.Format(
                              "Could not find build definition {0} for team project {1} on version control server for tfs {2}",
                              name.BuildDefinition,
                              name.TeamProject,
                              ResolverSettings.GetSetting(ResolverValidSettings.TeamProjectCollectionUrl)));
            }

            // Query all builds with this teamProject and build definition name
            // BuildNumber xor (BuildQuality or BuildStatus) = version
            var spec = _buildServer.CreateBuildDetailSpec(buildDef);

            spec.InformationTypes = new string[] { };
            var details = _buildServer.QueryBuilds(spec);

            foreach (var buildResult in details.Builds)
            {
                List <string> availableBuildQuality = null;
                if (buildResult.Quality != null)
                {
                    availableBuildQuality = new List <string> {
                        buildResult.Quality
                    };
                }

                var availableBuildStatus = new List <string> {
                    buildResult.Status.ToString()
                };
                var vers = new ComponentVersion(buildResult.BuildNumber, availableBuildStatus, availableBuildQuality, null);
                versList.Add(vers);
                Logger.Instance().Log(TraceLevel.Info, "{0}: Found build {1}", ResolverType, vers.ToString());
            }

            Logger.Instance().Log(TraceLevel.Info, "{0}: Querying component versions finished successfully", ResolverType);
            return(versList);
        }