public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
        {
            if (!sourceRepo.FileExists(PythonConstants.RequirementsFileName))
            {
                _logger.LogDebug($"File '{PythonConstants.RequirementsFileName}' does not exist in source repo");
                return(null);
            }

            string runtimeVersion = DetectPythonVersionFromRuntimeFile(sourceRepo);

            if (string.IsNullOrEmpty(runtimeVersion))
            {
                var files = sourceRepo.EnumerateFiles(
                    PythonConstants.PythonFileNamePattern,
                    searchSubDirectories: false);

                if (files == null || !files.Any())
                {
                    _logger.LogDebug($"Files with extension '{PythonConstants.PythonFileNamePattern}' do not exist " +
                                     "in source repo root");
                    return(null);
                }
            }

            runtimeVersion = VerifyAndResolveVersion(runtimeVersion);

            return(new LanguageDetectorResult
            {
                Language = PythonConstants.PythonName,
                LanguageVersion = runtimeVersion,
            });
        }
        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 files = sourceRepo.EnumerateFiles(PythonConstants.PythonFileNamePattern, searchSubDirectories: true);

            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);
        }
        private IEnumerable <string> GetAllProjectFilesInRepo(
            ISourceRepo sourceRepo,
            string projectFileExtension)
        {
            var searchSubDirectories = !_options.DisableRecursiveLookUp;

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

            return(sourceRepo.EnumerateFiles($"*.{projectFileExtension}", searchSubDirectories));
        }
Example #4
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 #5
0
 private static IEnumerable <string> GetAllProjectFilesInRepo(
     ISourceRepo sourceRepo,
     string projectFileExtension)
 {
     return(sourceRepo.EnumerateFiles($"*.{projectFileExtension}", searchSubDirectories: true));
 }
Example #6
0
 private static string GetProjectFileAtRoot(ISourceRepo sourceRepo, string projectFileExtension)
 {
     return(sourceRepo
            .EnumerateFiles($"*.{projectFileExtension}", searchSubDirectories: false)
            .FirstOrDefault());
 }
        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 #8
0
        private bool IsHugoApp(ISourceRepo sourceRepo, out string appDirectory)
        {
            // Hugo configuration variables:
            // https://gohugo.io/getting-started/configuration/#all-configuration-settings
            appDirectory = string.Empty;

            // Search for config.toml
            if (sourceRepo.FileExists(HugoConstants.TomlFileName) &&
                IsHugoTomlFile(sourceRepo, HugoConstants.TomlFileName))
            {
                return(true);
            }

            // Search for config.yml
            if (sourceRepo.FileExists(HugoConstants.YmlFileName) &&
                IsHugoYamlFile(sourceRepo, HugoConstants.YmlFileName))
            {
                return(true);
            }

            // Search for config.yaml
            if (sourceRepo.FileExists(HugoConstants.YamlFileName) &&
                IsHugoYamlFile(sourceRepo, HugoConstants.YamlFileName))
            {
                return(true);
            }

            // Search for config.json
            if (sourceRepo.FileExists(HugoConstants.JsonFileName) &&
                IsHugoYamlFile(sourceRepo, HugoConstants.JsonFileName))
            {
                return(true);
            }

            // NOTE: we do NOT disable looking up into the 'config' folder because it is a special folder
            // from perspective of Hugo where users can have configuration files.
            if (sourceRepo.DirExists(HugoConstants.ConfigFolderName))
            {
                // Search for config/**/*.toml
                var tomlFiles = sourceRepo.EnumerateFiles(
                    "*.toml",
                    searchSubDirectories: true,
                    subDirectoryToSearchUnder: HugoConstants.ConfigFolderName);
                foreach (var tomlFile in tomlFiles)
                {
                    if (IsHugoTomlFile(sourceRepo, tomlFile))
                    {
                        return(true);
                    }
                }

                // Search for config/**/*.yaml and config/**/*.yml
                var yamlFiles = sourceRepo.EnumerateFiles(
                    "*.yaml",
                    searchSubDirectories: true,
                    subDirectoryToSearchUnder: HugoConstants.ConfigFolderName);
                foreach (var yamlFile in yamlFiles)
                {
                    if (IsHugoYamlFile(sourceRepo, yamlFile))
                    {
                        return(true);
                    }
                }

                var ymlFiles = sourceRepo.EnumerateFiles(
                    "*.yml",
                    searchSubDirectories: true,
                    subDirectoryToSearchUnder: HugoConstants.ConfigFolderName);
                foreach (var ymlFile in ymlFiles)
                {
                    if (IsHugoYamlFile(sourceRepo, ymlFile))
                    {
                        return(true);
                    }
                }

                // Search for config/**/*.json
                var jsonFiles = sourceRepo.EnumerateFiles(
                    "*.json",
                    searchSubDirectories: true,
                    subDirectoryToSearchUnder: HugoConstants.ConfigFolderName);
                foreach (var jsonFile in jsonFiles)
                {
                    if (IsHugoJsonFile(sourceRepo, jsonFile))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #9
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(HugoConstants.TomlFileName))
            {
                tomlFilePaths.Add(Path.Combine(sourceRepo.RootPath, HugoConstants.TomlFileName));
            }

            // Search for config.yml
            if (sourceRepo.FileExists(HugoConstants.YmlFileName))
            {
                yamlFilePaths.Add(Path.Combine(sourceRepo.RootPath, HugoConstants.YmlFileName));
            }

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

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

            if (sourceRepo.DirExists(HugoConstants.ConfigFolderName))
            {
                // Search for config/*.toml
                tomlFilePaths.AddRange(sourceRepo.EnumerateFiles("*.toml", searchSubDirectories: true));

                // Search for config/*.yaml and config/*.yml
                yamlFilePaths.AddRange(sourceRepo.EnumerateFiles("*.yaml", searchSubDirectories: true));
                yamlFilePaths.AddRange(sourceRepo.EnumerateFiles("*.yml", searchSubDirectories: true));

                // Search for config/*.json
                jsonFilePaths.AddRange(sourceRepo.EnumerateFiles("*.json", searchSubDirectories: 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);
        }
        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}'.");
                    throw new InvalidUsageException(
                              $"Could not find the project file '{projectFile}' specified by the environment variable" +
                              $" '{EnvironmentSettingsKeys.Project}' with value '{projectEnvVariablePath}'. " +
                              "Make sure the path to the project file is relative to the root of the repo. " +
                              "For example: PROJECT=src/Dashboard/Dashboard.csproj");
                }

                // NOTE: Do not check if the project file specified by the end user is a web application since this
                // can be a escape hatch for end users if our logic to determine a web app is incorrect.
                return(projectFile);
            }

            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 web sdk.
            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)
                {
                    var projects = string.Join(", ", webAppProjects);
                    throw new InvalidUsageException(
                              "Ambiguity in selecting an ASP.NET Core web application to build. " +
                              $"Found multiple applications: '{projects}'. Use the environment variable " +
                              $"'{EnvironmentSettingsKeys.Project}' to specify the relative path to the project " +
                              "to be deployed.");
                }

                projectFile = webAppProjects[0];
            }

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

            return(_projectFile);
        }