Beispiel #1
0
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var    packageJson          = GetPackageJsonObject(context.SourceRepo, _logger);
            string runBuildCommand      = null;
            string runBuildAzureCommand = null;

            string packageManagerCmd = context.SourceRepo.FileExists(NodeConstants.YarnLockFileName) ? NodeConstants.YarnCommand : NodeConstants.NpmCommand;

            _logger.LogInformation("Using {packageManager}", packageManagerCmd);

            var packageInstallCommand = string.Format(NodeConstants.PackageInstallCommandTemplate, packageManagerCmd);

            var scriptsNode = packageJson?.scripts;

            if (scriptsNode != null)
            {
                if (scriptsNode.build != null)
                {
                    runBuildCommand = string.Format(NodeConstants.PkgMgrRunBuildCommandTemplate, packageManagerCmd);
                }

                if (scriptsNode["build:azure"] != null)
                {
                    runBuildAzureCommand = string.Format(NodeConstants.PkgMgrRunBuildAzureCommandTemplate, packageManagerCmd);
                }
            }

            if (packageJson?.dependencies != null)
            {
                Newtonsoft.Json.Linq.JObject deps = packageJson.dependencies;
                var depSpecs = deps.ToObject <IDictionary <string, string> >();
                _logger.LogDependencies(context.Language, context.NodeVersion, depSpecs.Select(kv => kv.Key + kv.Value));
            }

            var scriptProps = new NodeBashBuildSnippetProperties(
                packageInstallCommand: packageInstallCommand,
                runBuildCommand: runBuildCommand,
                runBuildAzureCommand: runBuildAzureCommand);
            string script = TemplateHelpers.Render(TemplateHelpers.TemplateResource.NodeBuildSnippet, scriptProps, _logger);

            return(new BuildScriptSnippet {
                BashBuildScriptSnippet = script
            });
        }
Beispiel #2
0
        public void GeneratedSnippet_HaveInstallScript_IfCustomRequirementsTxtPathSpecified()
        {
            // Arrange
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions()
            {
                CustomRequirementsTxtPath = "foo/requirements.txt"
            };
            var commonOptions = new BuildScriptGeneratorOptions()
            {
                EnableDynamicInstall      = true,
                CustomRequirementsTxtPath = "foo/requirements.txt"
            };
            var installerScriptSnippet = "##INSTALLER_SCRIPT##";
            var versionProvider        = new TestPythonVersionProvider(new[] { "3.7.5", "3.8.0" }, defaultVersion: "3.7.5");
            var platformInstaller      = new TestPythonPlatformInstaller(
                isVersionAlreadyInstalled: false,
                installerScript: installerScriptSnippet,
                Options.Create(commonOptions),
                NullLoggerFactory.Instance);
            var platform = CreatePlatform(
                versionProvider,
                platformInstaller,
                commonOptions,
                pythonScriptGeneratorOptions);
            var repo = new MemorySourceRepo();

            repo.AddFile("", "foo/requirements.txt");
            repo.AddFile("print(1)", "bla.py");
            var context = new BuildScriptGeneratorContext {
                SourceRepo = repo
            };
            var detectorResult = new PythonPlatformDetectorResult
            {
                Platform               = PythonConstants.PlatformName,
                PlatformVersion        = "3.7.5",
                HasRequirementsTxtFile = true,
            };

            // Act
            var snippet = platform.GetInstallerScriptSnippet(context, detectorResult);

            // Assert
            Assert.NotNull(snippet);
        }
Beispiel #3
0
        /// <inheritdoc/>
        public string GetInstallerScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var pythonPlatformDetectorResult = detectorResult as PythonPlatformDetectorResult;

            if (pythonPlatformDetectorResult != null && IsCondaEnvironment(pythonPlatformDetectorResult))
            {
                _logger.LogDebug(
                    "Application in the source directory is a Conda based app, " +
                    "so skipping dynamic installation of Python SDK.");
                return(null);
            }

            string installationScriptSnippet = null;

            if (_commonOptions.EnableDynamicInstall)
            {
                _logger.LogDebug("Dynamic install is enabled.");

                if (_platformInstaller.IsVersionAlreadyInstalled(detectorResult.PlatformVersion))
                {
                    _logger.LogDebug(
                        "Python version {version} is already installed. So skipping installing it again.",
                        detectorResult.PlatformVersion);
                }
                else
                {
                    _logger.LogDebug(
                        "Python version {version} is not installed. " +
                        "So generating an installation script snippet for it.",
                        detectorResult.PlatformVersion);

                    installationScriptSnippet = _platformInstaller.GetInstallerScriptSnippet(
                        detectorResult.PlatformVersion);
                }
            }
            else
            {
                _logger.LogDebug("Dynamic install not enabled.");
            }

            return(installationScriptSnippet);
        }
Beispiel #4
0
        /// <inheritdoc/>
        public string GetInstallerScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;

            if (dotNetCorePlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            string installationScriptSnippet = null;

            if (this.commonOptions.EnableDynamicInstall)
            {
                this.logger.LogDebug("Dynamic install is enabled.");

                if (this.platformInstaller.IsVersionAlreadyInstalled(dotNetCorePlatformDetectorResult.SdkVersion))
                {
                    this.logger.LogDebug(
                        "DotNetCore SDK version {globalJsonSdkVersion} is already installed. " +
                        "So skipping installing it again.",
                        dotNetCorePlatformDetectorResult.SdkVersion);
                }
                else
                {
                    this.logger.LogDebug(
                        "DotNetCore SDK version {globalJsonSdkVersion} is not installed. " +
                        "So generating an installation script snippet for it.",
                        dotNetCorePlatformDetectorResult.SdkVersion);

                    installationScriptSnippet = this.platformInstaller.GetInstallerScriptSnippet(
                        dotNetCorePlatformDetectorResult.SdkVersion);
                }
            }
            else
            {
                this.logger.LogDebug("Dynamic install is not enabled.");
            }

            return(installationScriptSnippet);
        }
Beispiel #5
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext ctx)
        {
            var buildProperties = new Dictionary <string, string>();

            // Write the version to the manifest file
            buildProperties[ManifestFilePropertyKeys.PhpVersion] = ctx.ResolvedPhpVersion;

            _logger.LogDebug("Selected PHP version: {phpVer}", ctx.ResolvedPhpVersion);
            bool composerFileExists = false;

            if (ctx.SourceRepo.FileExists(PhpConstants.ComposerFileName))
            {
                composerFileExists = true;

                try
                {
                    dynamic composerFile = ctx.SourceRepo.ReadJsonObjectFromFile(PhpConstants.ComposerFileName);
                    if (composerFile?.require != null)
                    {
                        Newtonsoft.Json.Linq.JObject deps = composerFile?.require;
                        var depSpecs = deps.ToObject <IDictionary <string, string> >();
                        _logger.LogDependencies(
                            this.Name,
                            ctx.ResolvedPhpVersion,
                            depSpecs.Select(kv => kv.Key + kv.Value));
                    }
                }
                catch (Exception exc)
                {
                    // Leave malformed composer.json files for Composer to handle.
                    // This prevents Oryx from erroring out when Composer itself might be able to tolerate the file.
                    _logger.LogWarning(exc, $"Exception caught while trying to deserialize {PhpConstants.ComposerFileName.Hash()}");
                }
            }

            var props = new PhpBashBuildSnippetProperties {
                ComposerFileExists = composerFileExists
            };
            string snippet = TemplateHelper.Render(TemplateHelper.TemplateResource.PhpBuildSnippet, props, _logger);

            return(new BuildScriptSnippet {
                BashBuildScriptSnippet = snippet, BuildProperties = buildProperties
            });
        }
Beispiel #6
0
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var buildProperties = new Dictionary <string, string>();

            (string projectFile, string publishDir) = GetProjectFileAndPublishDir(context.SourceRepo);
            if (string.IsNullOrEmpty(projectFile) || string.IsNullOrEmpty(publishDir))
            {
                return(null);
            }

            bool zipAllOutput = ShouldZipAllOutput(context);

            buildProperties[ManifestFilePropertyKeys.ZipAllOutput] = zipAllOutput.ToString().ToLowerInvariant();

            _environmentSettingsProvider.TryGetAndLoadSettings(out var environmentSettings);

            (var preBuildCommand, var postBuildCommand) = PreAndPostBuildCommandHelper.GetPreAndPostBuildCommands(
                context.SourceRepo,
                environmentSettings);

            var templateProperties = new DotNetCoreBashBuildSnippetProperties
            {
                ProjectFile      = projectFile,
                PublishDirectory = publishDir,
                BuildProperties  = buildProperties,
                BenvArgs         = $"dotnet={context.DotnetCoreVersion}",
                DirectoriesToExcludeFromCopyToIntermediateDir = GetDirectoriesToExcludeFromCopyToIntermediateDir(
                    context),
                PreBuildCommand  = preBuildCommand,
                PostBuildCommand = postBuildCommand,
                ManifestFileName = Constants.ManifestFileName,
                ZipAllOutput     = zipAllOutput,
                Configuration    = GetBuildConfiguration()
            };
            var script = TemplateHelpers.Render(
                TemplateHelpers.TemplateResource.DotNetCoreSnippet,
                templateProperties,
                _logger);

            return(new BuildScriptSnippet {
                BashBuildScriptSnippet = script, IsFullScript = true
            });
        }
Beispiel #7
0
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext scriptGeneratorContext)
        {
            (string projectFile, string publishDir) = GetProjectFileAndPublishDir(scriptGeneratorContext.SourceRepo);
            if (string.IsNullOrEmpty(projectFile) || string.IsNullOrEmpty(publishDir))
            {
                return(null);
            }

            var props = new DotNetCoreBashBuildSnippetProperties
            {
                ProjectFile      = projectFile,
                PublishDirectory = publishDir
            };
            string script = TemplateHelpers.Render(TemplateHelpers.TemplateResource.DotNetCoreSnippet, props, _logger);

            return(new BuildScriptSnippet {
                BashBuildScriptSnippet = script
            });
        }
Beispiel #8
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var manifestFileProperties = new Dictionary <string, string>();

            manifestFileProperties[ManifestFilePropertyKeys.HugoVersion] = detectorResult.PlatformVersion;

            string script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.HugoSnippet,
                model: null,
                _logger);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
            });
        }
Beispiel #9
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext ctx,
            PlatformDetectorResult detectorResult)
        {
            var rubyPlatformDetectorResult = detectorResult as RubyPlatformDetectorResult;

            if (rubyPlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(RubyPlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            if (!rubyPlatformDetectorResult.GemfileExists)
            {
                throw new InvalidUsageException($"No Gemfile found at the root of the repo. Please provide a Gemfile.");
            }

            var buildProperties = new Dictionary <string, string>();

            // Write the platform name and version to the manifest file
            buildProperties[ManifestFilePropertyKeys.RubyVersion] = rubyPlatformDetectorResult.PlatformVersion;

            _logger.LogDebug("Selected Ruby version: {rubyVer}", rubyPlatformDetectorResult.PlatformVersion);

            var scriptProps = new RubyBashBuildSnippetProperties
            {
                UseBundlerToInstallDependencies = true,
                BundlerVersion = rubyPlatformDetectorResult.BundlerVersion,
            };

            string script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.RubyBuildSnippet,
                scriptProps,
                _logger);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = buildProperties,
            });
        }
        public bool TryGenerateScript(out string generatedScript)
        {
            generatedScript = null;

            try
            {
                var options                = _serviceProvider.GetRequiredService <IOptions <BuildScriptGeneratorOptions> >().Value;
                var scriptGenerator        = _serviceProvider.GetRequiredService <IBuildScriptGenerator>();
                var sourceRepoProvider     = _serviceProvider.GetRequiredService <ISourceRepoProvider>();
                var environment            = _serviceProvider.GetRequiredService <CliEnvironmentSettings>();
                var sourceRepo             = sourceRepoProvider.GetSourceRepo();
                var scriptGeneratorContext = new BuildScriptGeneratorContext
                {
                    SourceRepo                = sourceRepo,
                    Language                  = options.Language,
                    LanguageVersion           = options.LanguageVersion,
                    Properties                = options.Properties,
                    EnableDotNetCore          = !environment.DisableDotNetCore,
                    EnableNodeJs              = !environment.DisableNodeJs,
                    EnablePython              = !environment.DisablePython,
                    EnablePhp                 = !environment.DisablePhp,
                    DisableMultiPlatformBuild = environment.DisableMultiPlatformBuild
                };

                // Try generating a script
                if (!scriptGenerator.TryGenerateBashScript(scriptGeneratorContext, out generatedScript))
                {
                    _console.Error.WriteLine(
                        "Error: Could not find a script generator which can generate a script for " +
                        $"the code in '{options.SourceDir}'.");
                    return(false);
                }

                return(true);
            }
            catch (InvalidUsageException ex)
            {
                _logger.LogError(ex, "Invalid usage");
                _console.Error.WriteLine(ex.Message);
                return(false);
            }
        }
Beispiel #11
0
        public void GeneratedScript_DoesNotUseVenv()
        {
            // Arrange
            var scriptGenerator = CreatePlatform();
            var repo            = new MemorySourceRepo();

            repo.AddFile("", PythonConstants.RequirementsFileName);
            repo.AddFile("print(1)", "bla.py");
            var context = new BuildScriptGeneratorContext {
                SourceRepo = repo
            };

            // Act
            var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context);

            // Assert
            Assert.NotNull(snippet);
            Assert.Contains("Python Virtual Environment", snippet.BashBuildScriptSnippet);
            Assert.True(scriptGenerator.IsCleanRepo(repo));
        }
Beispiel #12
0
        public IEnumerable <string> GetDirectoriesToExcludeFromCopyToBuildOutputDir(BuildScriptGeneratorContext ctx)
        {
            var dirs = new List <string>
            {
                NodeConstants.AllNodeModulesDirName,
                NodeConstants.ProdNodeModulesDirName,
            };

            // If the node modules folder is being packaged in a file, we don't copy it to the output
            if (GetNodeModulesPackOptions(ctx, out _, out string compressedFileName))
            {
                dirs.Add(NodeConstants.NodeModulesDirName);
            }
            else if (!string.IsNullOrWhiteSpace(compressedFileName))
            {
                dirs.Add(compressedFileName);
            }

            return(dirs);
        }
Beispiel #13
0
        /// <inheritdoc/>
        public IEnumerable <string> GetDirectoriesToExcludeFromCopyToBuildOutputDir(
            BuildScriptGeneratorContext context)
        {
            var dirs           = new List <string>();
            var virtualEnvName = GetVirtualEnvironmentName(context);

            if (GetVirtualEnvPackOptions(
                    context,
                    virtualEnvName,
                    out _,
                    out string compressedFileName))
            {
                dirs.Add(virtualEnvName);
            }
            else if (!string.IsNullOrWhiteSpace(compressedFileName))
            {
                dirs.Add(compressedFileName);
            }

            return(dirs);
        }
Beispiel #14
0
        /// <inheritdoc/>
        public string GetInstallerScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var javaPlatformDetectorResult = detectorResult as JavaPlatformDetectorResult;

            if (javaPlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(JavaPlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            if (_commonOptions.EnableDynamicInstall)
            {
                _logger.LogDebug("Dynamic install is enabled.");

                var scriptBuilder = new StringBuilder();

                InstallJavaSdk(javaPlatformDetectorResult.PlatformVersion, scriptBuilder);

                // We need not setup Maven if repo already uses a Maven wrapper script.
                if (!javaPlatformDetectorResult.UsesMavenWrapperTool)
                {
                    InstallMaven(javaPlatformDetectorResult.MavenVersion, scriptBuilder);
                }

                if (scriptBuilder.Length == 0)
                {
                    return(null);
                }

                return(scriptBuilder.ToString());
            }
            else
            {
                _logger.LogDebug("Dynamic install not enabled.");
                return(null);
            }
        }
Beispiel #15
0
        public void GeneratedSnippet_HasInstallationScript_IfDynamicInstallIsEnabled()
        {
            // Arrange
            var pythonScriptGeneratorOptions = new PythonScriptGeneratorOptions();
            var commonOptions = new BuildScriptGeneratorOptions()
            {
                EnableDynamicInstall = true
            };
            var installerScriptSnippet = "##INSTALLER_SCRIPT##";
            var versionProvider        = new TestPythonVersionProvider(new[] { "3.7.5", "3.8.0" }, defaultVersion: "3.7.5");
            var platformInstaller      = new TestPythonPlatformInstaller(
                isVersionAlreadyInstalled: false,
                installerScript: installerScriptSnippet,
                Options.Create(commonOptions),
                NullLoggerFactory.Instance);
            var platform = CreatePlatform(
                versionProvider,
                platformInstaller,
                commonOptions,
                pythonScriptGeneratorOptions);
            var repo = new MemorySourceRepo();

            repo.AddFile("", PythonConstants.RequirementsFileName);
            repo.AddFile("print(1)", "bla.py");
            var context = new BuildScriptGeneratorContext {
                SourceRepo = repo
            };

            context.ResolvedPythonVersion = "3.7.5";

            // Act
            var snippet = platform.GenerateBashBuildScriptSnippet(context);

            // Assert
            Assert.NotNull(snippet);
            Assert.NotNull(snippet.PlatformInstallationScriptSnippet);
            Assert.Equal(installerScriptSnippet, snippet.PlatformInstallationScriptSnippet);
            Assert.Contains(ManifestFilePropertyKeys.PythonVersion, snippet.BuildProperties.Keys);
            Assert.Equal("3.7.5", snippet.BuildProperties[ManifestFilePropertyKeys.PythonVersion]);
        }
Beispiel #16
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext ctx,
            PlatformDetectorResult detectorResult)
        {
            // confirm go detector not null
            var goPlatformDetectorResult = detectorResult as GolangPlatformDetectorResult;

            if (goPlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(GolangPlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            if (!goPlatformDetectorResult.GoModExists)
            {
                throw new InvalidUsageException("No go.mod found at the root of the repo. Please provide a go.mod file containing the version.");
            }

            // build properties & script snippets
            var buildProperties = new Dictionary <string, string>();

            // Write platform name and version to the manifest file
            buildProperties[ManifestFilePropertyKeys.GolangVersion] = goPlatformDetectorResult.PlatformVersion;
            this.logger.LogDebug($"Selected Go version: {goPlatformDetectorResult.PlatformVersion}");
            var scriptProps = new GolangBashBuildSnippetProperties(
                goPlatformDetectorResult.GoModExists,
                goPlatformDetectorResult.PlatformVersion);

            string script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.GolangSnippet,
                scriptProps,
                this.logger);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = buildProperties,
            });
        }
Beispiel #17
0
        /// <summary>
        /// Even though the runtime container has the logic of finding out the startup file based on
        /// 'runtimeconfig.json' prefix, we still set the name in the manifest file because of the following
        /// scenario: let's say output directory currently has 'foo.dll' and user made a change to the project
        /// name or assembly name property to 'bar' which causes 'bar.dll' to be published. If the output
        /// directory was NOT cleaned, then we would now be having both 'foo.runtimeconfig.json' and
        /// 'bar.runtimeconfig.json' which causes a problem for runtime container as it cannot figure out the
        /// right startup DLL. So, to help that scenario we always set the start-up file name in manifest file.
        /// The runtime container will first look into manifest file to find the startup filename, if the
        /// file name is not present or if a manifest file is not present at all(ex: in case of VS Publish where
        /// the build does not happen with Oryx), then the runtime container's logic will fallback to looking at
        /// runtimeconfig.json prefixes.
        /// </summary>
        private void SetStartupFileNameInfoInManifestFile(
            BuildScriptGeneratorContext context,
            string projectFile,
            IDictionary <string, string> buildProperties)
        {
            string startupDllFileName;
            var    projectFileContent  = context.SourceRepo.ReadFile(projectFile);
            var    projFileDoc         = XDocument.Load(new StringReader(projectFileContent));
            var    assemblyNameElement = projFileDoc.XPathSelectElement(DotNetCoreConstants.AssemblyNameXPathExpression);

            if (assemblyNameElement == null)
            {
                var name = Path.GetFileNameWithoutExtension(projectFile);
                startupDllFileName = $"{name}.dll";
            }
            else
            {
                startupDllFileName = $"{assemblyNameElement.Value}.dll";
            }

            buildProperties[DotNetCoreManifestFilePropertyKeys.StartupDllFileName] = startupDllFileName;
        }
Beispiel #18
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var manifestFileProperties = new Dictionary <string, string>();

            manifestFileProperties[ManifestFilePropertyKeys.HugoVersion] = detectorResult.PlatformVersion;
            manifestFileProperties[ManifestFilePropertyKeys.Frameworks]  = "hugo";
            this.logger.LogInformation("Detected the following frameworks: hugo");
            Console.WriteLine("Detected the following frameworks: hugo");

            string script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.HugoSnippet,
                model: null,
                this.logger);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
            });
        }
Beispiel #19
0
        /// <inheritdoc/>
        public IEnumerable <string> GetDirectoriesToExcludeFromCopyToBuildOutputDir(BuildScriptGeneratorContext ctx)
        {
            var dirs = new List <string>
            {
                NodeConstants.AllNodeModulesDirName,
                NodeConstants.ProdNodeModulesDirName,
            };

            // If the node modules folder is being packaged in a file, we don't copy it to the output
            if (GetNodeModulesPackOptions(ctx, out _, out string compressedFileName))
            {
                // we need to make sure we are not copying the root's node_modules folder
                // if there are any other node_modules folder we will copy them to destination
                dirs.Add(string.Concat("/", NodeConstants.NodeModulesDirName));
            }
            else if (!string.IsNullOrWhiteSpace(compressedFileName))
            {
                dirs.Add(compressedFileName);
            }

            return(dirs);
        }
Beispiel #20
0
        public void ConsidersZipVenvBuildProperty()
        {
            // Arrange
            var scriptGenerator = CreatePlatformInstance();
            var repo            = new MemorySourceRepo();

            repo.AddFile("", PythonConstants.RequirementsFileName);
            var venvName = "bla";
            var context  = new BuildScriptGeneratorContext {
                SourceRepo = repo,
                Properties = new Dictionary <string, string> {
                    { "virtualenv_name", venvName },
                    { "zip_venv_dir", "true" }
                }
            };

            // Act
            var excludedDirs = scriptGenerator.GetDirectoriesToExcludeFromCopyToBuildOutputDir(context);

            // Assert
            Assert.NotNull(excludedDirs);
            Assert.Contains(venvName, excludedDirs);
        }
Beispiel #21
0
        /// <inheritdoc/>
        public string GetInstallerScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var phpPlatformDetectorResult = detectorResult as PhpPlatformDetectorResult;

            if (phpPlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(PhpPlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            if (this.commonOptions.EnableDynamicInstall)
            {
                this.logger.LogDebug("Dynamic install is enabled.");

                var scriptBuilder = new StringBuilder();

                this.InstallPhp(phpPlatformDetectorResult.PlatformVersion, scriptBuilder);

                this.InstallPhpComposer(phpPlatformDetectorResult.PhpComposerVersion, scriptBuilder);

                if (scriptBuilder.Length == 0)
                {
                    return(null);
                }

                return(scriptBuilder.ToString());
            }
            else
            {
                this.logger.LogDebug("Dynamic install not enabled.");
                return(null);
            }
        }
Beispiel #22
0
 public void SetVersion(BuildScriptGeneratorContext context, string version)
 {
     context.DotNetCoreVersion = version;
 }
Beispiel #23
0
 public bool IsEnabledForMultiPlatformBuild(BuildScriptGeneratorContext scriptGeneratorContext)
 {
     // A user has the power to either enable or disable multi-platform builds entirely.
     // However if user enables it, ASP.NET Core platform still explicitly opts out of it.
     return(false);
 }
Beispiel #24
0
 public bool IsEnabled(BuildScriptGeneratorContext scriptGeneratorContext)
 {
     return(scriptGeneratorContext.EnableDotNetCore);
 }
Beispiel #25
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var pythonPlatformDetectorResult = detectorResult as PythonPlatformDetectorResult;

            if (pythonPlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(PythonPlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            if (IsCondaEnvironment(pythonPlatformDetectorResult))
            {
                return(GetBuildScriptSnippetForConda(context, pythonPlatformDetectorResult));
            }

            var manifestFileProperties = new Dictionary <string, string>();

            // Write the platform name and version to the manifest file
            manifestFileProperties[ManifestFilePropertyKeys.PythonVersion] = pythonPlatformDetectorResult.PlatformVersion;

            var packageDir     = GetPackageDirectory(context);
            var virtualEnvName = GetVirtualEnvironmentName(context);
            var isPythonPackageCommandEnabled = _commonOptions.ShouldPackage;
            var pythonPackageWheelType        = GetPythonPackageWheelType(context);

            if (!isPythonPackageCommandEnabled && !string.IsNullOrWhiteSpace(pythonPackageWheelType))
            {
                throw new InvalidUsageException($"Option '{PythonPackageWheelPropertyKey}' can't exist" +
                                                $"without package command being enabled. Please provide --package along with wheel type");
            }

            if (isPythonPackageCommandEnabled &&
                !string.IsNullOrWhiteSpace(pythonPackageWheelType))
            {
                if (!string.Equals(pythonPackageWheelType.ToLower(), "universal"))
                {
                    throw new InvalidUsageException($"Option '{PythonPackageWheelPropertyKey}' can only have 'universal' as value.'");
                }

                manifestFileProperties[PythonManifestFilePropertyKeys.PackageWheel] = pythonPackageWheelType;
            }

            if (!string.IsNullOrWhiteSpace(packageDir) && !string.IsNullOrWhiteSpace(virtualEnvName))
            {
                throw new InvalidUsageException($"Options '{TargetPackageDirectoryPropertyKey}' and " +
                                                $"'{VirtualEnvironmentNamePropertyKey}' are mutually exclusive. Please provide " +
                                                $"only the target package directory or virtual environment name.");
            }

            if (string.IsNullOrWhiteSpace(packageDir))
            {
                // If the package directory was not provided, we default to virtual envs
                if (string.IsNullOrWhiteSpace(virtualEnvName))
                {
                    virtualEnvName = GetDefaultVirtualEnvName(pythonPlatformDetectorResult);
                }

                manifestFileProperties[PythonManifestFilePropertyKeys.VirtualEnvName] = virtualEnvName;
            }
            else
            {
                manifestFileProperties[PythonManifestFilePropertyKeys.PackageDir] = packageDir;
            }

            var virtualEnvModule = string.Empty;
            var virtualEnvParams = string.Empty;

            var pythonVersion = pythonPlatformDetectorResult.PlatformVersion;

            _logger.LogDebug("Selected Python version: {pyVer}", pythonVersion);

            if (!string.IsNullOrEmpty(pythonVersion) && !string.IsNullOrWhiteSpace(virtualEnvName))
            {
                (virtualEnvModule, virtualEnvParams) = GetVirtualEnvModules(pythonVersion);

                _logger.LogDebug(
                    "Using virtual environment {venv}, module {venvModule}",
                    virtualEnvName,
                    virtualEnvModule);
            }

            GetVirtualEnvPackOptions(
                context,
                virtualEnvName,
                out var compressVirtualEnvCommand,
                out var compressedVirtualEnvFileName);

            if (!string.IsNullOrWhiteSpace(compressedVirtualEnvFileName))
            {
                manifestFileProperties[PythonManifestFilePropertyKeys.CompressedVirtualEnvFile]
                    = compressedVirtualEnvFileName;
            }

            TryLogDependencies(pythonVersion, context.SourceRepo);

            var scriptProps = new PythonBashBuildSnippetProperties(
                virtualEnvironmentName: virtualEnvName,
                virtualEnvironmentModule: virtualEnvModule,
                virtualEnvironmentParameters: virtualEnvParams,
                packagesDirectory: packageDir,
                enableCollectStatic: _pythonScriptGeneratorOptions.EnableCollectStatic,
                compressVirtualEnvCommand: compressVirtualEnvCommand,
                compressedVirtualEnvFileName: compressedVirtualEnvFileName,
                runPythonPackageCommand: isPythonPackageCommandEnabled,
                pythonPackageWheelProperty: pythonPackageWheelType);
            string script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.PythonSnippet,
                scriptProps,
                _logger);

            return(new BuildScriptSnippet()
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
            });
        }
Beispiel #26
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;

            if (dotNetCorePlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            var versionMap = _versionProvider.GetSupportedVersions();

            string globalJsonSdkVersion = null;

            if (_commonOptions.EnableDynamicInstall)
            {
                var availableSdks = versionMap.Values;
                globalJsonSdkVersion = _globalJsonSdkResolver.GetSatisfyingSdkVersion(
                    context.SourceRepo,
                    detectorResult.PlatformVersion,
                    availableSdks);
            }

            var manifestFileProperties = new Dictionary <string, string>();

            manifestFileProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;
            manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreRuntimeVersion]
                = detectorResult.PlatformVersion;

            if (string.IsNullOrEmpty(globalJsonSdkVersion))
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion]
                    = versionMap[detectorResult.PlatformVersion];
            }
            else
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion] = globalJsonSdkVersion;
            }

            var projectFile = dotNetCorePlatformDetectorResult.ProjectFile;

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

            var templateProperties = new DotNetCoreBashBuildSnippetProperties
            {
                ProjectFile   = projectFile,
                Configuration = GetBuildConfiguration(),
            };

            var script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.DotNetCoreSnippet,
                templateProperties,
                _logger);

            SetStartupFileNameInfoInManifestFile(context, projectFile, manifestFileProperties);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,

                // Setting this to false to avoid copying files like '.cs' to the destination
                CopySourceDirectoryContentToDestinationDirectory = false,
            });
        }
Beispiel #27
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext ctx)
        {
            string installationScriptSnippet = null;

            if (_commonOptions.EnableDynamicInstall &&
                !_platformInstaller.IsVersionAlreadyInstalled(ctx.NodeVersion))
            {
                installationScriptSnippet = _platformInstaller.GetInstallerScriptSnippet(ctx.NodeVersion);
            }

            var manifestFileProperties = new Dictionary <string, string>();

            // Write the version to the manifest file
            manifestFileProperties[ManifestFilePropertyKeys.NodeVersion] = ctx.NodeVersion;

            var    packageJson                    = GetPackageJsonObject(ctx.SourceRepo, _logger);
            string runBuildCommand                = null;
            string runBuildAzureCommand           = null;
            bool   configureYarnCache             = false;
            string packageManagerCmd              = null;
            string packageInstallCommand          = null;
            string packageInstallerVersionCommand = null;

            if (ctx.SourceRepo.FileExists(NodeConstants.YarnLockFileName))
            {
                packageManagerCmd              = NodeConstants.YarnCommand;
                packageInstallCommand          = NodeConstants.YarnPackageInstallCommand;
                configureYarnCache             = true;
                packageInstallerVersionCommand = NodeConstants.YarnVersionCommand;
            }
            else if (StaticSiteGeneratorHelper.IsHugoApp(ctx.SourceRepo, _environment))
            {
                packageManagerCmd              = NodeConstants.HugoCommand;
                packageInstallCommand          = NodeConstants.HugoCommand;
                packageInstallerVersionCommand = NodeConstants.HugoVersionCommand;
            }
            else
            {
                packageManagerCmd              = NodeConstants.NpmCommand;
                packageInstallCommand          = NodeConstants.NpmPackageInstallCommand;
                packageInstallerVersionCommand = NodeConstants.NpmVersionCommand;
            }

            _logger.LogInformation("Using {packageManager}", packageManagerCmd);

            var hasProdDependencies = false;

            if (packageJson?.dependencies != null)
            {
                hasProdDependencies = true;
            }

            var hasDevDependencies = false;

            if (packageJson?.devDependencies != null)
            {
                // If development time dependencies are present we want to avoid copying them to improve performance
                hasDevDependencies = true;
            }

            var productionOnlyPackageInstallCommand = string.Format(
                NodeConstants.ProductionOnlyPackageInstallCommandTemplate, packageInstallCommand);

            if (string.IsNullOrEmpty(_nodeScriptGeneratorOptions.CustomNpmRunBuildCommand))
            {
                var scriptsNode = packageJson?.scripts;
                if (scriptsNode != null)
                {
                    if (scriptsNode.build != null)
                    {
                        runBuildCommand = string.Format(NodeConstants.PkgMgrRunBuildCommandTemplate, packageManagerCmd);
                    }

                    if (scriptsNode["build:azure"] != null && !ctx.IsPackage)
                    {
                        runBuildAzureCommand = string.Format(
                            NodeConstants.PkgMgrRunBuildAzureCommandTemplate,
                            packageManagerCmd);
                    }
                }
            }

            if (packageJson?.dependencies != null)
            {
                var depSpecs = ((JObject)packageJson.dependencies).ToObject <IDictionary <string, string> >();
                _logger.LogDependencies(ctx.Language, ctx.NodeVersion, depSpecs.Select(d => d.Key + d.Value));
            }

            if (packageJson?.devDependencies != null)
            {
                var depSpecs = ((JObject)packageJson.devDependencies).ToObject <IDictionary <string, string> >();
                _logger.LogDependencies(ctx.Language, ctx.NodeVersion, depSpecs.Select(d => d.Key + d.Value), true);
            }

            string compressNodeModulesCommand    = null;
            string compressedNodeModulesFileName = null;

            GetNodeModulesPackOptions(ctx, out compressNodeModulesCommand, out compressedNodeModulesFileName);

            if (!string.IsNullOrWhiteSpace(compressedNodeModulesFileName))
            {
                manifestFileProperties[NodeConstants.NodeModulesFileBuildProperty] = compressedNodeModulesFileName;
            }

            bool   pruneDevDependencies     = ShouldPruneDevDependencies(ctx);
            string appInsightsInjectCommand = string.Empty;

            GetAppOutputDirPath(packageJson, manifestFileProperties);

            string customRegistryUrl = null;

            if (ctx.Properties != null)
            {
                ctx.Properties.TryGetValue(RegistryUrlPropertyKey, out customRegistryUrl);
                if (!string.IsNullOrWhiteSpace(customRegistryUrl))
                {
                    // Write the custom registry to the build manifest
                    manifestFileProperties[$"{NodeConstants.NodeJsName}_{RegistryUrlPropertyKey}"] = customRegistryUrl;
                }
            }

            var scriptProps = new NodeBashBuildSnippetProperties
            {
                PackageRegistryUrl                  = customRegistryUrl,
                PackageInstallCommand               = packageInstallCommand,
                NpmRunBuildCommand                  = runBuildCommand,
                NpmRunBuildAzureCommand             = runBuildAzureCommand,
                HasProdDependencies                 = hasProdDependencies,
                HasDevDependencies                  = hasDevDependencies,
                ProductionOnlyPackageInstallCommand = productionOnlyPackageInstallCommand,
                CompressNodeModulesCommand          = compressNodeModulesCommand,
                CompressedNodeModulesFileName       = compressedNodeModulesFileName,
                ConfigureYarnCache                  = configureYarnCache,
                PruneDevDependencies                = pruneDevDependencies,
                AppInsightsInjectCommand            = appInsightsInjectCommand,
                AppInsightsPackageName              = NodeConstants.NodeAppInsightsPackageName,
                AppInsightsLoaderFileName           = NodeAppInsightsLoader.NodeAppInsightsLoaderFileName,
                PackageInstallerVersionCommand      = packageInstallerVersionCommand,
                RunNpmPack = ctx.IsPackage,
                CustomNpmRunBuildCommand = _nodeScriptGeneratorOptions.CustomNpmRunBuildCommand,
            };

            string script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.NodeBuildSnippet,
                scriptProps,
                _logger);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
                PlatformInstallationScriptSnippet = installationScriptSnippet,
            });
        }
Beispiel #28
0
 public IEnumerable <string> GetDirectoriesToExcludeFromCopyToIntermediateDir(BuildScriptGeneratorContext ctx)
 {
     return(Array.Empty <string>());
 }
Beispiel #29
0
 public IEnumerable <string> GetDirectoriesToExcludeFromCopyToBuildOutputDir(BuildScriptGeneratorContext scriptGeneratorContext)
 {
     return(Array.Empty <string>());
 }
Beispiel #30
0
 private static bool ShouldPruneDevDependencies(BuildScriptGeneratorContext context)
 {
     return(BuildPropertiesHelper.IsTrue(PruneDevDependenciesPropertyKey, context, valueIsRequired: false));
 }