Example #1
0
        public void SetRequiredTools(ISourceRepo sourceRepo, string targetPlatformVersion, IDictionary <string, string> toolsToVersion)
        {
            Debug.Assert(toolsToVersion != null, $"{nameof(toolsToVersion)} must not be null");
            Debug.Assert(sourceRepo != null, $"{nameof(sourceRepo)} must not be null since Node needs access to the repository");
            if (!string.IsNullOrWhiteSpace(targetPlatformVersion))
            {
                toolsToVersion["node"] = targetPlatformVersion;
            }

            var packageJson = GetPackageJsonObject(sourceRepo, _logger);

            if (packageJson != null)
            {
                var npmVersion = GetNpmVersion(packageJson);
                if (!string.IsNullOrEmpty(npmVersion))
                {
                    toolsToVersion["npm"] = npmVersion;
                }
            }
        }
Example #2
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 #3
0
 public static BuildScriptGeneratorContext CreateContext(
     BuildScriptGeneratorOptions options,
     CliEnvironmentSettings envSettings,
     ISourceRepo sourceRepo,
     string operationId)
 {
     return(new BuildScriptGeneratorContext
     {
         OperationId = operationId,
         SourceRepo = sourceRepo,
         Language = options.Language,
         LanguageVersion = options.LanguageVersion,
         Properties = options.Properties,
         EnableCheckers = !envSettings.DisableCheckers,
         EnableDotNetCore = !envSettings.DisableDotNetCore,
         EnableNodeJs = !envSettings.DisableNodeJs,
         EnablePython = !envSettings.DisablePython,
         EnablePhp = !envSettings.DisablePhp,
         DisableMultiPlatformBuild = envSettings.DisableMultiPlatformBuild
     });
 }
Example #4
0
        private void TryLogDependencies(string pythonVersion, ISourceRepo repo)
        {
            var customRequirementsTxtPath = this.pythonScriptGeneratorOptions.CustomRequirementsTxtPath;
            var requirementsTxtPath       = customRequirementsTxtPath == null ? PythonConstants.RequirementsFileName : customRequirementsTxtPath;

            if (!repo.FileExists(requirementsTxtPath))
            {
                return;
            }

            try
            {
                var deps = repo.ReadAllLines(requirementsTxtPath)
                           .Where(line => !line.TrimStart().StartsWith("#"));
                this.logger.LogDependencies(PythonConstants.PlatformName, pythonVersion, deps);
            }
            catch (Exception exc)
            {
                this.logger.LogWarning(exc, "Exception caught while logging dependencies");
            }
        }
Example #5
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 #6
0
        private bool IsPythonApp(ISourceRepo sourceRepo)
        {
            if (sourceRepo.FileExists(PythonConstants.RequirementsFileName))
            {
                _logger.LogInformation($"Found {PythonConstants.RequirementsFileName} at the root of the repo.");
                return(true);
            }
            else
            {
                _logger.LogInformation(
                    $"Cound not find {PythonConstants.RequirementsFileName} at the root of the repo.");
            }

            var searchSubDirectories = !_options.DisableRecursiveLookUp;

            if (!searchSubDirectories)
            {
                _logger.LogDebug("Skipping search for files in sub-directories as it has been disabled.");
            }

            var files = sourceRepo.EnumerateFiles(PythonConstants.PythonFileNamePattern, searchSubDirectories);

            if (files != null && files.Any())
            {
                _logger.LogInformation(
                    $"Found files with extension '{PythonConstants.PythonFileNamePattern}' " +
                    $"in the repo.");
                return(true);
            }
            else
            {
                _logger.LogInformation(
                    $"Could not find any file with extension '{PythonConstants.PythonFileNamePattern}' " +
                    $"in the repo.");
            }

            return(false);
        }
Example #7
0
        private bool IsHugoTomlFile(ISourceRepo sourceRepo, params string[] subPaths)
        {
            TomlTable tomlTable = null;

            var relativeFilePath = Path.Combine(subPaths);

            try
            {
                tomlTable = ParserHelper.ParseTomlFile(sourceRepo, relativeFilePath);
            }
            catch (FailedToParseFileException ex)
            {
                _logger.LogError(ex, $"An error occurred when trying to parse file '{relativeFilePath}'.");
                return(false);
            }

            if (tomlTable.Keys
                .Any(k => HugoConfigurationKeys.Contains(k, StringComparer.OrdinalIgnoreCase)))
            {
                return(true);
            }

            return(false);
        }
Example #8
0
        /// <summary>
        /// Gets the package json object.
        /// </summary>
        /// <param name="sourceRepo">The source repository.</param>
        /// <param name="logger">The logger of Node.js platform.</param>
        /// <returns>Package json Object.</returns>
        internal static dynamic GetPackageJsonObject(ISourceRepo sourceRepo, ILogger logger)
        {
            if (!sourceRepo.FileExists(NodeConstants.PackageJsonFileName))
            {
                return(null);
            }

            dynamic packageJson = null;

            try
            {
                packageJson = sourceRepo.ReadJsonObjectFromFile(NodeConstants.PackageJsonFileName);
            }
            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.Hash()}");
            }

            return(packageJson);
        }
 public IEnumerable <ICheckerMessage> CheckSourceRepo(ISourceRepo repo) =>
 _sourceRepoMessageProvider();
Example #10
0
 private static XDocument GetXmlDocument(ISourceRepo sourceRepo, string projectFile)
 {
     return(XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile))));
 }
        private bool IsAspNetCoreWebApplicationProject(ISourceRepo sourceRepo, string projectFile)
        {
            var projFileDoc = XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile)));

            return(IsAspNetCoreWebApplicationProject(projFileDoc));
        }
Example #12
0
 public IEnumerable <ICheckerMessage> CheckSourceRepo(ISourceRepo repo) =>
 Enumerable.Empty <ICheckerMessage>();
Example #13
0
        public static bool IsAzureBlazorWebAssemblyProject(ISourceRepo sourceRepo, string projectFile)
        {
            var projFileDoc = GetXmlDocument(sourceRepo, projectFile);

            return(IsBlazorWebAssemblyProject(projFileDoc));
        }
Example #14
0
 public string GetRelativePathToProjectFile(ISourceRepo sourceRepo)
 {
     return(_projectFilePath);
 }
Example #15
0
 public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
 {
     return(_detector.Detect(sourceRepo));
 }
Example #16
0
 /// <summary>
 /// Checks whether or not the given repository uses a static site generator.
 /// </summary>
 /// <param name="sourceRepo">Source repo for the application.</param>
 /// <param name="environment">Environment abstraction.</param>
 /// <returns>True if the app uses a static site generator, false otherwise.</returns>
 public static bool IsStaticSite(ISourceRepo sourceRepo, IEnvironment environment)
 {
     return(IsHugoApp(sourceRepo, environment));
 }
 public void SetRequiredTools(
     ISourceRepo sourceRepo,
     string targetPlatformVersion,
     IDictionary <string, string> toolsToVersion)
 {
 }
 public bool IsCleanRepo(ISourceRepo repo)
 {
     throw new NotImplementedException();
 }
Example #19
0
        private dynamic ReadJsonObjectFromFile(ISourceRepo sourceRepo, string fileName)
        {
            var jsonContent = sourceRepo.ReadFile(fileName);

            return(JsonConvert.DeserializeObject(jsonContent));
        }
Example #20
0
 private static IEnumerable <string> GetAllProjectFilesInRepo(
     ISourceRepo sourceRepo,
     string projectFileExtension)
 {
     return(sourceRepo.EnumerateFiles($"*.{projectFileExtension}", searchSubDirectories: true));
 }
Example #21
0
 private static string GetProjectFileAtRoot(ISourceRepo sourceRepo, string projectFileExtension)
 {
     return(sourceRepo
            .EnumerateFiles($"*.{projectFileExtension}", searchSubDirectories: false)
            .FirstOrDefault());
 }
Example #22
0
 public IEnumerable <ICheckerMessage> CheckSourceRepo(ISourceRepo repo)
 {
     return(Enumerable.Empty <ICheckerMessage>());
 }
Example #23
0
 /// <inheritdoc/>
 public bool IsCleanRepo(ISourceRepo repo)
 {
     return(!repo.DirExists(NodeConstants.NodeModulesDirName));
 }
        public string GetProjectFile(ISourceRepo sourceRepo)
        {
            if (_probedForProjectFile)
            {
                return(_projectFile);
            }

            var projectEnvVariablePath = _options.Project;

            string projectFile = null;

            if (string.IsNullOrEmpty(projectEnvVariablePath))
            {
                // Check if root of the repo has a .csproj file
                projectFile = sourceRepo
                              .EnumerateFiles($"*.{DotnetCoreConstants.ProjectFileExtensionName}", searchSubDirectories: false)
                              .FirstOrDefault();
            }
            else
            {
                projectEnvVariablePath = projectEnvVariablePath.Trim();
                projectFile            = Path.Combine(sourceRepo.RootPath, projectEnvVariablePath);
                if (!sourceRepo.FileExists(projectFile))
                {
                    _logger.LogWarning($"Could not find the project file '{projectFile}'.");
                    return(null);
                }
            }

            if (projectFile != null)
            {
                if (!IsAspNetCoreWebApplicationProject(sourceRepo, projectFile))
                {
                    return(null);
                }

                return(projectFile);
            }

            // Check if any of the sub-directories has a .csproj file and if that .csproj file has references
            // to typical AspNetCore package references.
            if (projectFile == null)
            {
                var projectFiles = sourceRepo
                                   .EnumerateFiles($"*.{DotnetCoreConstants.ProjectFileExtensionName}", searchSubDirectories: true);

                if (!projectFiles.Any())
                {
                    _logger.LogDebug(
                        $"Could not find any files with extension '{DotnetCoreConstants.ProjectFileExtensionName}' in repo.");
                    return(null);
                }

                var webAppProjects = new List <string>();
                foreach (var file in projectFiles)
                {
                    if (IsAspNetCoreWebApplicationProject(sourceRepo, file))
                    {
                        webAppProjects.Add(file);
                    }
                }

                if (webAppProjects.Count == 0)
                {
                    _logger.LogDebug(
                        "Could not find any ASP.NET Core web application projects. " +
                        $"Found the following project files: '{string.Join(" ", projectFiles)}'");
                    return(null);
                }

                if (webAppProjects.Count > 1)
                {
                    throw new InvalidUsageException(
                              "Ambiguity in selecting an ASP.NET Core web application to build. " +
                              "Found multiple applications: " + string.Join(", ", webAppProjects));
                }

                projectFile = webAppProjects[0];
            }

            // Cache the results
            _probedForProjectFile = true;
            _projectFile          = projectFile;

            return(_projectFile);
        }
Example #25
0
 /// <inheritdoc/>
 public bool IsCleanRepo(ISourceRepo repo)
 {
     return(true);
 }
Example #26
0
        public static bool IsAspNetCoreWebApplicationProject(ISourceRepo sourceRepo, string projectFile)
        {
            var projFileDoc = GetXmlDocument(sourceRepo, projectFile);

            return(IsAspNetCoreWebApplicationProject(projFileDoc));
        }
Example #27
0
 /// <inheritdoc/>
 public bool IsCleanRepo(ISourceRepo repo)
 {
     // TODO: support venvs
     return(!repo.DirExists(PythonConstants.DefaultTargetPackageDirectory));
 }
Example #28
0
        public static bool IsAzureFunctionsProject(ISourceRepo sourceRepo, string projectFile)
        {
            var projFileDoc = GetXmlDocument(sourceRepo, projectFile);

            return(IsAzureFunctionsProject(projFileDoc));
        }
Example #29
0
 public bool IsCleanRepo(ISourceRepo repo)
 {
     (_, string expectedPublishDir) = GetProjectFileAndPublishDir(repo);
     return(!repo.DirExists(expectedPublishDir));
 }
Example #30
0
        /// <summary>
        /// Checks whether or not the given repository is a Hugo application.
        /// </summary>
        /// <param name="sourceRepo">Source repo for the application.</param>
        /// <param name="environment">Environment abstraction.</param>
        /// <returns>True if the app is a Hugo app, false otherwise.</returns>
        public static bool IsHugoApp(ISourceRepo sourceRepo, IEnvironment environment)
        {
            // Check for Hugo environment variables
            var environmentVariables = environment.GetEnvironmentVariables();

            foreach (var key in environmentVariables?.Keys)
            {
                if (key.ToString().StartsWith(HugoEnvironmentVariablePrefix))
                {
                    return(true);
                }
            }

            // Hugo configuration variables: https://gohugo.io/getting-started/configuration/#all-configuration-settings
            var tomlFilePaths = new List <string>();
            var yamlFilePaths = new List <string>();
            var jsonFilePaths = new List <string>();

            // Search for config.toml
            if (sourceRepo.FileExists(NodeConstants.HugoTomlFileName))
            {
                tomlFilePaths.Add(Path.Combine(sourceRepo.RootPath, NodeConstants.HugoTomlFileName));
            }

            // Search for config.yaml
            if (sourceRepo.FileExists(NodeConstants.HugoYamlFileName))
            {
                yamlFilePaths.Add(Path.Combine(sourceRepo.RootPath, NodeConstants.HugoYamlFileName));
            }

            // Search for config.json
            if (sourceRepo.FileExists(NodeConstants.HugoJsonFileName))
            {
                jsonFilePaths.Add(Path.Combine(sourceRepo.RootPath, NodeConstants.HugoJsonFileName));
            }

            if (sourceRepo.DirExists(NodeConstants.HugoConfigFolderName))
            {
                var configSourceRepo = new LocalSourceRepo(Path.Combine(sourceRepo.RootPath, NodeConstants.HugoConfigFolderName));

                // Search for config/*.toml
                tomlFilePaths.AddRange(configSourceRepo.EnumerateFiles("*.toml", true));

                // Search for config/*.yaml
                yamlFilePaths.AddRange(configSourceRepo.EnumerateFiles("*.yaml", true));

                // Search for config/*.json
                jsonFilePaths.AddRange(configSourceRepo.EnumerateFiles("*.json", true));
            }

            foreach (var path in tomlFilePaths)
            {
                var tomlTable = ParserHelper.ParseTomlFile(sourceRepo, path);
                if (tomlTable.Keys
                    .Any(k => HugoConfigurationVariables.Contains(k, StringComparer.OrdinalIgnoreCase)))
                {
                    return(true);
                }
            }

            foreach (var path in yamlFilePaths)
            {
                var yamlNode = ParserHelper.ParseYamlFile(sourceRepo, path);
                if (yamlNode.Children.Keys
                    .Select(k => k.ToString())
                    .Any(k => HugoConfigurationVariables.Contains(k, StringComparer.OrdinalIgnoreCase)))
                {
                    return(true);
                }
            }

            foreach (var path in jsonFilePaths)
            {
                var jObject = ParserHelper.ParseJsonFile(sourceRepo, path);
                if (jObject.Children()
                    .Select(c => c.Path)
                    .Any(c => HugoConfigurationVariables.Contains(c, StringComparer.OrdinalIgnoreCase)))
                {
                    return(true);
                }
            }

            return(false);
        }