Example #1
0
        private string DetectPythonVersionFromRuntimeFile(ISourceRepo sourceRepo)
        {
            const string versionPrefix = "python-";

            // Most Python sites will have at least a .py file in the root, but
            // some may not. In that case, let them opt in with the runtime.txt
            // file, which is used to specify the version of Python.
            if (sourceRepo.FileExists(PythonConstants.RuntimeFileName))
            {
                try
                {
                    var content          = sourceRepo.ReadFile(PythonConstants.RuntimeFileName);
                    var hasPythonVersion = content.StartsWith(versionPrefix, StringComparison.OrdinalIgnoreCase);
                    if (!hasPythonVersion)
                    {
                        _logger.LogDebug("Prefix {verPrefix} was not found in file {rtFileName}", versionPrefix, PythonConstants.RuntimeFileName);
                        return(null);
                    }

                    var pythonVersion = content.Remove(0, versionPrefix.Length);
                    _logger.LogDebug("Found version {pyVer} in runtime file", pythonVersion);
                    return(pythonVersion);
                }
                catch (IOException ex)
                {
                    _logger.LogError(ex, "An error occurred while reading file {rtFileName}", PythonConstants.RuntimeFileName);
                }
            }
            else
            {
                _logger.LogDebug("Could not find file '{rtFileName}' in source repo", PythonConstants.RuntimeFileName);
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Parse a .yaml file into a YamlMappingNode from the YamlDotNet library.
        /// See https://github.com/aaubry/YamlDotNet for more information.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="filePath">The path to the .yaml file.</param>
        /// <returns>A YamlMappingNode object containing information about the .yaml file.</returns>
        public static YamlMappingNode ParseYamlFile(ISourceRepo sourceRepo, string filePath)
        {
            var yamlContent = sourceRepo.ReadFile(filePath);
            var yamlStream  = new YamlStream();

            yamlStream.Load(new StringReader(yamlContent));
            return((YamlMappingNode)yamlStream.Documents[0].RootNode);
        }
Example #3
0
        public string GetSatisfyingSdkVersion(
            ISourceRepo sourceRepo,
            string runtimeVersion,
            IEnumerable <string> availableSdks)
        {
            string sdkVersion;

            if (sourceRepo.FileExists(DotNetCoreConstants.GlobalJsonFileName))
            {
                var globalJsonContent = sourceRepo.ReadFile(
                    Path.Combine(sourceRepo.RootPath, DotNetCoreConstants.GlobalJsonFileName));

                _logger.LogDebug(
                    "Detected presence of global.json file with content {globalJsonContent}",
                    globalJsonContent);

                var globalJsonModel = JsonConvert.DeserializeObject <GlobalJsonModel>(globalJsonContent);
                sdkVersion = GetSatisfyingSdkVersion(globalJsonModel, availableSdks);

                _logger.LogDebug(
                    "Resolved sdk version to {resolvedSdkVersion} based on global.json file and available sdk versions",
                    sdkVersion);
            }
            else
            {
                // As per global.json spec, if a global.json file is not present, then roll forward policy is
                // considered as 'latestMajor'. This can cause end users apps to fail since in this case even prelreease
                // versions are considered. So here we minimize the impact by relying on the runtime version instead.
                // We choose only the 'major' and 'minor' part of the runtime version.
                // For example, 2.1.14 of runtime will result in a latest minor sdk in '1', for example
                // 2.1.202 or 2.1.400
                var version         = new SemVer.Version(runtimeVersion);
                var globalJsonModel = new GlobalJsonModel
                {
                    Sdk = new SdkModel
                    {
                        Version = $"{version.Major}.{version.Minor}.100",

                        // Get latest feature and patch of the version
                        RollForward     = RollForwardPolicy.LatestFeature,
                        AllowPreRelease = true,
                    },
                };

                _logger.LogDebug(
                    "global.json file was not find in the repo, so choosing an sdk version which satisfies the " +
                    "version {defaultSdkVersion}, roll forward policy of {defaultRollForwardPolicy} and " +
                    "allowPrerelease value of {defaultAllowPrerelease}.",
                    globalJsonModel.Sdk.Version,
                    globalJsonModel.Sdk.RollForward,
                    globalJsonModel.Sdk.AllowPreRelease);

                sdkVersion = GetSatisfyingSdkVersion(globalJsonModel, availableSdks);
            }

            return(sdkVersion);
        }
        public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
        {
            var projectFile = _aspNetCoreWebAppProjectFileProvider.GetRelativePathToProjectFile(sourceRepo);

            if (string.IsNullOrEmpty(projectFile))
            {
                return(null);
            }

            var projectFileDoc         = XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile)));
            var targetFrameworkElement = projectFileDoc.XPathSelectElement(
                DotnetCoreConstants.TargetFrameworkElementXPathExpression);
            var targetFramework = targetFrameworkElement?.Value;

            if (string.IsNullOrEmpty(targetFramework))
            {
                _logger.LogDebug(
                    $"Could not find 'TargetFramework' element in the project file '{projectFile}'.");
                return(null);
            }

            // If a repo explicitly specifies an sdk version, then just use it as it is.
            string languageVersion = null;

            if (sourceRepo.FileExists(DotnetCoreConstants.GlobalJsonFileName))
            {
                var globalJson = GetGlobalJsonObject(sourceRepo);
                var sdkVersion = globalJson?.sdk?.version?.Value as string;
                if (sdkVersion != null)
                {
                    languageVersion = sdkVersion;
                }
            }

            if (string.IsNullOrEmpty(languageVersion))
            {
                languageVersion = DetermineSdkVersion(targetFramework);
            }

            if (languageVersion == null)
            {
                _logger.LogDebug(
                    $"Could not find a {DotnetCoreConstants.LanguageName} version corresponding to 'TargetFramework'" +
                    $" '{targetFramework}'.");
                return(null);
            }

            languageVersion = VerifyAndResolveVersion(languageVersion);

            return(new LanguageDetectorResult
            {
                Language = DotnetCoreConstants.LanguageName,
                LanguageVersion = languageVersion
            });
        }
Example #5
0
 private bool IsYarnLockFileYamlFile(ISourceRepo sourceRepo, string filePath)
 {
     try
     {
         using (var reader = new StringReader(sourceRepo.ReadFile(filePath)))
         {
             var yamlStream = new YamlStream();
             yamlStream.Load(reader);
         }
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Example #6
0
        /// <summary>
        /// Parse a .toml file into a TomlTable from the Nett library.
        /// See https://github.com/paiden/Nett for more information.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="filePath">The path to the .toml file.</param>
        /// <returns>A TomlTable object containing information about the .toml file.</returns>
        public static TomlTable ParseTomlFile(ISourceRepo sourceRepo, string filePath)
        {
            var tomlContent = sourceRepo.ReadFile(filePath);

            try
            {
                return(Toml.ReadString(tomlContent));
            }
            catch (Exception ex)
            {
                throw new FailedToParseFileException(
                          filePath,
                          string.Format(Messages.FailedToParseFileExceptionFormat, filePath),
                          ex);
            }
        }
Example #7
0
        internal static dynamic GetPackageJsonObject(ISourceRepo sourceRepo, ILogger logger)
        {
            dynamic packageJson = null;

            try
            {
                var jsonContent = sourceRepo.ReadFile(NodeConstants.PackageJsonFileName);
                packageJson = JsonConvert.DeserializeObject(jsonContent);
            }
            catch (Exception exc)
            {
                // Leave malformed package.json files for Node.js to handle.
                // This prevents Oryx from erroring out when Node.js itself might be able to tolerate the file.
                logger.LogWarning(exc, $"Exception caught while trying to deserialize {NodeConstants.PackageJsonFileName}");
            }

            return(packageJson);
        }
Example #8
0
        /// <summary>
        /// Parse a .yaml file into a YamlMappingNode from the YamlDotNet library.
        /// See https://github.com/aaubry/YamlDotNet for more information.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="filePath">The path to the .yaml file.</param>
        /// <returns>A YamlMappingNode object containing information about the .yaml file.</returns>
        public static YamlNode ParseYamlFile(ISourceRepo sourceRepo, string filePath)
        {
            var yamlContent = sourceRepo.ReadFile(filePath);
            var yamlStream  = new YamlStream();

            try
            {
                yamlStream.Load(new StringReader(yamlContent));
            }
            catch (Exception ex)
            {
                throw new FailedToParseFileException(
                          filePath,
                          string.Format(Messages.FailedToParseFileExceptionFormat, filePath),
                          ex);
            }

            return(yamlStream.Documents[0].RootNode);
        }
Example #9
0
        /// <summary>
        /// Parse a .toml file into a TomlTable from the Tomlyn library.
        /// See https://github.com/xoofx/Tomlyn for more information.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="filePath">The path to the .toml file.</param>
        /// <returns>A TomlTable object containing information about the .toml file.</returns>
        public static TomlTable ParseTomlFile(ISourceRepo sourceRepo, string filePath)
        {
            var tomlContent = sourceRepo.ReadFile(filePath);

            try
            {
                // Gets a syntax tree of the TOML text
                var doc = Toml.Parse(tomlContent);
                // Gets a runtime representation of the syntax tree
                var table = doc.ToModel();
                return(table);
            }
            catch (Exception ex)
            {
                throw new FailedToParseFileException(
                          filePath,
                          string.Format(Messages.FailedToParseFileExceptionFormat, filePath),
                          ex);
            }
        }
Example #10
0
        private dynamic GetGlobalJsonObject(ISourceRepo sourceRepo)
        {
            dynamic globalJson = null;

            try
            {
                var jsonContent = sourceRepo.ReadFile(DotnetCoreConstants.GlobalJsonFileName);
                globalJson = JsonConvert.DeserializeObject(jsonContent);
            }
            catch (Exception ex)
            {
                // We just ignore errors, so we leave malformed package.json
                // files for node.js to handle, not us. This prevents us from
                // erroring out when node itself might be able to tolerate some errors
                // in the package.json file.
                _logger.LogError(
                    ex,
                    $"An error occurred while trying to deserialize {DotnetCoreConstants.GlobalJsonFileName}");
            }

            return(globalJson);
        }
Example #11
0
 private static XDocument GetXmlDocument(ISourceRepo sourceRepo, string projectFile)
 {
     return(XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile))));
 }
Example #12
0
        private dynamic ReadJsonObjectFromFile(ISourceRepo sourceRepo, string fileName)
        {
            var jsonContent = sourceRepo.ReadFile(fileName);

            return(JsonConvert.DeserializeObject(jsonContent));
        }
        private bool IsAspNetCoreWebApplicationProject(ISourceRepo sourceRepo, string projectFile)
        {
            var projFileDoc = XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile)));

            return(IsAspNetCoreWebApplicationProject(projFileDoc));
        }
Example #14
0
        /// <summary>
        /// Parse a .json file into a JObject from the Newtonsoft.Json library.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="filePath">The path to the .json file.</param>
        /// <returns>A JObject object containing information about the .json file.</returns>
        public static JObject ParseJsonFile(ISourceRepo sourceRepo, string filePath)
        {
            var jsonContent = sourceRepo.ReadFile(filePath);

            return(JObject.Parse(jsonContent));
        }
Example #15
0
        /// <summary>
        /// Parse a .toml file into a TomlTable from the Nett library.
        /// See https://github.com/paiden/Nett for more information.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="filePath">The path to the .toml file.</param>
        /// <returns>A TomlTable object containing information about the .toml file.</returns>
        public static TomlTable ParseTomlFile(ISourceRepo sourceRepo, string filePath)
        {
            var tomlContent = sourceRepo.ReadFile(filePath);

            return(Toml.ReadString(tomlContent));
        }