Beispiel #1
0
        private bool IsCompatiblePlatform(
            RepositoryContext ctx,
            string platformName,
            out Tuple <IProgrammingPlatform, string> platformResult)
        {
            platformResult = null;
            var selectedPlatform = _programmingPlatforms
                                   .Where(p => string.Equals(platformName, p.Name, StringComparison.OrdinalIgnoreCase))
                                   .FirstOrDefault();

            if (selectedPlatform == null)
            {
                var languages = string.Join(", ", _programmingPlatforms.Select(p => p.Name));
                var exec      = new UnsupportedPlatformException($"'{platformName}' platform is not supported. " +
                                                                 $"Supported platforms are: {languages}");
                _logger.LogError(exec, $"Exception caught, provided platform '{platformName}' is not supported.");
                throw exec;
            }

            if (!selectedPlatform.IsEnabled(ctx))
            {
                var exc = new UnsupportedPlatformException($"Platform '{selectedPlatform.Name}' has been disabled.");
                _logger.LogError(exc, $"Exception caught, platform '{selectedPlatform.Name}' has been disabled.");
                throw exc;
            }

            var platformVersionFromOptions = GetPlatformVersion(platformName);

            string detectedPlatformVersion = null;

            if (string.IsNullOrEmpty(platformVersionFromOptions))
            {
                var detectionResult = selectedPlatform.Detect(ctx);
                if (detectionResult == null)
                {
                    _logger.LogError($"Platform '{platformName}' was not detected in the given repository.");
                    return(false);
                }
                else if (string.IsNullOrEmpty(detectionResult.PlatformVersion))
                {
                    _logger.LogError($"Platform '{platformName}' was detected in the given repository, but " +
                                     $"no compatible version was found.");
                    return(false);
                }

                _logger.LogDebug($"No platform version found, " +
                                 $"setting to the detected version '{detectionResult.PlatformVersion}'.");
                detectedPlatformVersion = detectionResult.PlatformVersion;

                platformResult = Tuple.Create(selectedPlatform, detectedPlatformVersion);
                _logger.LogDebug($"Detected platform '{platformName}' with version '{detectedPlatformVersion}'.");
            }
            else
            {
                platformResult = Tuple.Create(selectedPlatform, platformVersionFromOptions);
            }

            return(true);
        }
        public void _05_UnsupportedPlatformExceptionSerializationTest()
        {
            UnsupportedPlatformException exception1 = new UnsupportedPlatformException("Hello world!");

            Assert.IsTrue(exception1.Message == "Hello world!");

            UnsupportedPlatformException exception2 = SerializeAndDeserializeException <UnsupportedPlatformException>(exception1);

            Assert.IsTrue(exception2.Message == exception1.Message);
            Assert.IsTrue(exception2.ToString() == exception1.ToString());
        }
        private bool IsCompatiblePlatform(
            RepositoryContext ctx,
            string platformName,
            IEnumerable <PlatformDetectorResult> detectionResults,
            bool runDetection,
            out Tuple <IProgrammingPlatform, PlatformDetectorResult> platformResult)
        {
            platformResult = null;
            var selectedPlatform = _programmingPlatforms
                                   .Where(p => string.Equals(platformName, p.Name, StringComparison.OrdinalIgnoreCase))
                                   .FirstOrDefault();

            if (selectedPlatform == null)
            {
                var platforms = string.Join(", ", _programmingPlatforms.Select(p => p.Name));
                var exec      = new UnsupportedPlatformException($"'{platformName}' platform is not supported. " +
                                                                 $"Supported platforms are: {platforms}");
                _logger.LogError(exec, $"Exception caught, provided platform '{platformName}' is not supported.");
                throw exec;
            }

            if (!selectedPlatform.IsEnabled(ctx))
            {
                var exc = new UnsupportedPlatformException($"Platform '{selectedPlatform.Name}' has been disabled.");
                _logger.LogError(exc, $"Exception caught, platform '{selectedPlatform.Name}' has been disabled.");
                throw exc;
            }

            PlatformDetectorResult detectionResult = null;

            if (runDetection)
            {
                detectionResult = selectedPlatform.Detect(ctx);
            }
            else
            {
                if (detectionResults != null)
                {
                    detectionResult = detectionResults
                                      .Where(result => result.Platform.EqualsIgnoreCase(selectedPlatform.Name))
                                      .FirstOrDefault();
                }
            }

            if (detectionResult == null)
            {
                _logger.LogError($"Platform '{platformName}' was not detected in the given repository.");
                return(false);
            }
            else if (string.IsNullOrEmpty(detectionResult.PlatformVersion))
            {
                _logger.LogError($"Platform '{platformName}' was detected in the given repository, but " +
                                 $"no compatible version was found.");
                return(false);
            }

            platformResult = Tuple.Create(selectedPlatform, detectionResult);
            _logger.LogDebug($"Detected platform '{platformName}' with version '{detectionResult.PlatformVersion}'.");

            return(true);
        }