Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DotNetCorePlatform"/> class.
 /// </summary>
 /// <param name="versionProvider">The .NET version provider.</param>
 /// <param name="logger">The logger of .NET platform.</param>
 /// <param name="detector">The detector of .NET platform.</param>
 /// <param name="commonOptions">The build options for BuildScriptGenerator.</param>
 /// <param name="dotNetCoreScriptGeneratorOptions">The options if .NET platform.</param>
 /// <param name="platformInstaller">The <see cref="DotNetCorePlatformInstaller"/>.</param>
 /// <param name="globalJsonSdkResolver">The <see cref="GlobalJsonSdkResolver"/>.</param>
 public DotNetCorePlatform(
     IDotNetCoreVersionProvider versionProvider,
     ILogger <DotNetCorePlatform> logger,
     IDotNetCorePlatformDetector detector,
     IOptions <BuildScriptGeneratorOptions> commonOptions,
     IOptions <DotNetCoreScriptGeneratorOptions> dotNetCoreScriptGeneratorOptions,
     DotNetCorePlatformInstaller platformInstaller,
     GlobalJsonSdkResolver globalJsonSdkResolver)
 {
     _versionProvider = versionProvider;
     _logger          = logger;
     _detector        = detector;
     _dotNetCoreScriptGeneratorOptions = dotNetCoreScriptGeneratorOptions.Value;
     _commonOptions         = commonOptions.Value;
     _platformInstaller     = platformInstaller;
     _globalJsonSdkResolver = globalJsonSdkResolver;
 }
Example #2
0
        private PythonPlatform CreatePlatform(
            IPythonVersionProvider pythonVersionProvider,
            PythonPlatformInstaller platformInstaller,
            BuildScriptGeneratorOptions commonOptions = null,
            PythonScriptGeneratorOptions pythonScriptGeneratorOptions = null)
        {
            commonOptions = commonOptions ?? new BuildScriptGeneratorOptions();
            pythonScriptGeneratorOptions = pythonScriptGeneratorOptions ?? new PythonScriptGeneratorOptions();

            return(new PythonPlatform(
                       Options.Create(commonOptions),
                       Options.Create(pythonScriptGeneratorOptions),
                       pythonVersionProvider,
                       NullLogger <PythonPlatform> .Instance,
                       detector: null,
                       platformInstaller));
        }
Example #3
0
        private PhpPlatform CreatePhpPlatform(
            string[] supportedPhpVersions         = null,
            string[] supportedPhpComposerVersions = null,
            string defaultVersion                               = null,
            string defaultComposerVersion                       = null,
            string detectedVersion                              = null,
            BuildScriptGeneratorOptions commonOptions           = null,
            PhpScriptGeneratorOptions phpScriptGeneratorOptions = null,
            bool?isPhpVersionAlreadyInstalled                   = null,
            string phpInstallationScript                        = null,
            bool?isPhpComposerAlreadyInstalled                  = null,
            string phpComposerInstallationScript                = null)
        {
            commonOptions                 = commonOptions ?? new BuildScriptGeneratorOptions();
            phpScriptGeneratorOptions     = phpScriptGeneratorOptions ?? new PhpScriptGeneratorOptions();
            isPhpVersionAlreadyInstalled  = isPhpVersionAlreadyInstalled ?? true;
            phpInstallationScript         = phpInstallationScript ?? "default-php-installation-script";
            isPhpComposerAlreadyInstalled = isPhpComposerAlreadyInstalled ?? true;
            phpComposerInstallationScript = phpComposerInstallationScript ?? "default-php-composer-installation-script";
            var versionProvider = new TestPhpVersionProvider(supportedPhpVersions, defaultVersion);

            supportedPhpComposerVersions = supportedPhpComposerVersions ?? new[] { PhpVersions.ComposerVersion };
            defaultComposerVersion       = defaultComposerVersion ?? PhpVersions.ComposerVersion;
            var composerVersionProvider = new TestPhpComposerVersionProvider(
                supportedPhpComposerVersions,
                defaultComposerVersion);
            var detector     = new TestPhpPlatformDetector(detectedVersion: detectedVersion);
            var phpInstaller = new TestPhpPlatformInstaller(
                Options.Create(commonOptions),
                isPhpVersionAlreadyInstalled.Value,
                phpInstallationScript);
            var phpComposerInstaller = new TestPhpComposerInstaller(
                Options.Create(commonOptions),
                isPhpComposerAlreadyInstalled.Value,
                phpComposerInstallationScript);

            return(new TestPhpPlatform(
                       Options.Create(phpScriptGeneratorOptions),
                       Options.Create(commonOptions),
                       versionProvider,
                       composerVersionProvider,
                       NullLogger <TestPhpPlatform> .Instance,
                       detector,
                       phpInstaller,
                       phpComposerInstaller));
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PhpPlatform"/> class.
 /// </summary>
 /// <param name="phpScriptGeneratorOptions">The options of phpScriptGenerator.</param>
 /// <param name="phpVersionProvider">The PHP version provider.</param>
 /// <param name="logger">The logger of PHP platform.</param>
 /// <param name="detector">The detector of PHP platform.</param>
 /// <param name="commonOptions">The <see cref="BuildScriptGeneratorOptions"/>.</param>
 /// <param name="phpComposerInstaller">The <see cref="PhpComposerInstaller"/>.</param>
 /// <param name="phpInstaller">The <see cref="PhpPlatformInstaller"/>.</param>
 public PhpPlatform(
     IOptions <PhpScriptGeneratorOptions> phpScriptGeneratorOptions,
     IOptions <BuildScriptGeneratorOptions> commonOptions,
     IPhpVersionProvider phpVersionProvider,
     ILogger <PhpPlatform> logger,
     PhpPlatformDetector detector,
     PhpPlatformInstaller phpInstaller,
     PhpComposerInstaller phpComposerInstaller)
 {
     _phpScriptGeneratorOptions = phpScriptGeneratorOptions.Value;
     _commonOptions             = commonOptions.Value;
     _phpVersionProvider        = phpVersionProvider;
     _logger               = logger;
     _detector             = detector;
     _phpInstaller         = phpInstaller;
     _phpComposerInstaller = phpComposerInstaller;
 }
Example #5
0
 public DotNetCorePlatform(
     IDotNetCoreVersionProvider versionProvider,
     DefaultProjectFileProvider projectFileProvider,
     IEnvironmentSettingsProvider environmentSettingsProvider,
     ILogger <DotNetCorePlatform> logger,
     DotNetCoreLanguageDetector detector,
     IOptions <BuildScriptGeneratorOptions> buildOptions,
     IOptions <DotNetCoreScriptGeneratorOptions> dotNetCorePlatformOptions)
 {
     _versionProvider             = versionProvider;
     _projectFileProvider         = projectFileProvider;
     _environmentSettingsProvider = environmentSettingsProvider;
     _logger   = logger;
     _detector = detector;
     _dotNetCorePlatformOptions = dotNetCorePlatformOptions.Value;
     _buildOptions = buildOptions.Value;
 }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NodePlatform"/> class.
 /// </summary>
 /// <param name="commonOptions">The <see cref="BuildScriptGeneratorOptions"/>.</param>
 /// <param name="nodeScriptGeneratorOptions">The options for nodeScriptGenerator.</param>
 /// <param name="nodeVersionProvider">The Node.js version provider.</param>
 /// <param name="logger">The logger of Node.js platform.</param>
 /// <param name="detector">The detector of Node.js platform.</param>
 /// <param name="environment">The environment of Node.js platform.</param>
 /// <param name="nodePlatformInstaller">The <see cref="NodePlatformInstaller"/>.</param>
 public NodePlatform(
     IOptions <BuildScriptGeneratorOptions> commonOptions,
     IOptions <NodeScriptGeneratorOptions> nodeScriptGeneratorOptions,
     INodeVersionProvider nodeVersionProvider,
     ILogger <NodePlatform> logger,
     INodePlatformDetector detector,
     IEnvironment environment,
     NodePlatformInstaller nodePlatformInstaller)
 {
     _commonOptions = commonOptions.Value;
     _nodeScriptGeneratorOptions = nodeScriptGeneratorOptions.Value;
     _nodeVersionProvider        = nodeVersionProvider;
     _logger            = logger;
     _detector          = detector;
     _environment       = environment;
     _platformInstaller = nodePlatformInstaller;
 }
        public static void ConfigureBuildScriptGeneratorOptions(
            BuildScriptGeneratorOptions options,
            string sourceDir,
            string destinationDir,
            string intermediateDir,
            string manifestDir,
            string platform,
            string platformVersion,
            bool shouldPackage,
            string[] requiredOsPackages,
            bool scriptOnly,
            string[] properties)
        {
            options.SourceDir = string.IsNullOrEmpty(sourceDir)
                ? Directory.GetCurrentDirectory() : Path.GetFullPath(sourceDir);
            options.PlatformName    = platform;
            options.PlatformVersion = platformVersion;

            if (!string.IsNullOrEmpty(destinationDir))
            {
                options.DestinationDir = Path.GetFullPath(destinationDir);
            }

            if (!string.IsNullOrEmpty(intermediateDir))
            {
                options.IntermediateDir = Path.GetFullPath(intermediateDir);
            }

            if (!string.IsNullOrEmpty(manifestDir))
            {
                options.ManifestDir = Path.GetFullPath(manifestDir);
            }

            options.ShouldPackage = shouldPackage;

            options.RequiredOsPackages = requiredOsPackages;

            options.ScriptOnly = scriptOnly;

            // Process properties
            if (properties != null)
            {
                options.Properties = ProcessProperties(properties);
            }
        }
        public void GeneratedScript_ZipsNodeModules_IfZipNodeProperty_IsNull()
        {
            // Arrange
            var commonOptions = new BuildScriptGeneratorOptions();

            commonOptions.PlatformVersion = "8.2.1";
            commonOptions.Properties      = new Dictionary <string, string>();
            commonOptions.Properties[NodePlatform.CompressNodeModulesPropertyKey] = null;
            var scriptGenerator = GetNodePlatform(
                defaultNodeVersion: NodeVersions.Node12Version,
                commonOptions,
                new NodeScriptGeneratorOptions());
            var repo = new MemorySourceRepo();

            repo.AddFile(PackageJsonWithBuildScript, NodeConstants.PackageJsonFileName);
            var context = CreateScriptGeneratorContext(repo);

            context.Properties[NodePlatform.CompressNodeModulesPropertyKey] = null;

            var expected = new NodeBashBuildSnippetProperties
            {
                PackageInstallCommand               = NpmInstallCommand,
                PackageInstallerVersionCommand      = NodeConstants.NpmVersionCommand,
                NpmRunBuildCommand                  = "npm run build",
                NpmRunBuildAzureCommand             = "npm run build:azure",
                HasProdDependencies                 = true,
                HasDevDependencies                  = true,
                ProductionOnlyPackageInstallCommand = string.Format(
                    NodeConstants.ProductionOnlyPackageInstallCommandTemplate,
                    NpmInstallCommand),
                CompressedNodeModulesFileName = "node_modules.tar.gz",
                CompressNodeModulesCommand    = "tar -zcf",
            };

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

            // Assert
            Assert.NotNull(snippet);
            Assert.Contains("echo Zipping existing 'node_modules' folder", snippet.BashBuildScriptSnippet);
            Assert.Equal(
                TemplateHelper.Render(TemplateHelper.TemplateResource.NodeBuildSnippet, expected),
                snippet.BashBuildScriptSnippet);
            Assert.True(scriptGenerator.IsCleanRepo(repo));
        }
Example #9
0
        public void GenerateDockerfile_GeneratesBuildTagAndRuntime_ForMultiPlatformBuild(
            string platformName,
            string platformVersion,
            string runtimePlatformName,
            string runtimePlatformVersion)
        {
            // Arrange
            var detector = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: platformName,
                detectedPlatformVersion: platformVersion);
            var platform = new TestProgrammingPlatform(
                platformName,
                new[] { platformVersion },
                detector: detector);

            var runtimeDetector = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: runtimePlatformName,
                detectedPlatformVersion: runtimePlatformVersion);
            var runtimePlatform = new TestProgrammingPlatform(
                runtimePlatformName,
                new[] { runtimePlatformVersion },
                detector: runtimeDetector);
            var commonOptions = new BuildScriptGeneratorOptions
            {
                EnableMultiPlatformBuild = true,
                PlatformName             = platformName,
                PlatformVersion          = platformVersion,
                RuntimePlatformName      = runtimePlatformName,
                RuntimePlatformVersion   = runtimePlatformVersion,
            };
            var generator = CreateDefaultDockerfileGenerator(new[] { platform, runtimePlatform }, commonOptions);
            var ctx       = CreateDockerfileContext();

            // Act
            var dockerfile = generator.GenerateDockerfile(ctx);

            // Assert
            Assert.NotNull(dockerfile);
            Assert.NotEqual(string.Empty, dockerfile);
            Assert.Contains(string.Format(_buildImageFormat, _buildImageName, _buildImageTag), dockerfile);
            Assert.Contains(string.Format(_argRuntimeFormat,
                                          ConvertToRuntimeName(runtimePlatformName),
                                          runtimePlatformVersion),
                            dockerfile);
        }
Example #10
0
        //[Fact]
        //public void GeneratedScript_UsesNpmInstall_IfPackageLockJsonFileIsPresent()
        //{
        //    // Arrange
        //    var scriptGenerator = GetScriptGenerator(defaultNpmVersion: "6.0.0");
        //    var repo = new MemorySourceRepo();
        //    repo.AddFile(ComposerFileWithNoNpmVersion, NodeConstants.ComposerFileFileName);
        //    repo.AddFile("Package lock json file content here", NodeConstants.PackageLockJsonFileName);
        //    var context = CreateBuildScriptGeneratorContext(repo);
        //    context.LanguageVersion = "8.2.1";
        //    var expected = new NodeBashBuildSnippetProperties(
        //        packageInstallCommand: NpmInstallCommand,
        //        runBuildCommand: null,
        //        runBuildAzureCommand: null);

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

        //    // Assert
        //    Assert.NotNull(snippet);
        //    Assert.Equal(TemplateHelpers.Render(
        //        TemplateHelpers.TemplateResource.NodeSnippet, expected), snippet.BashBuildScriptSnippet);
        //}

        //[Fact]
        //public void GeneratedScript_UsesNpmRunBuild_IfBuildNodeIsPresentUnderScripts()
        //{
        //    // Arrange
        //    var scriptGenerator = GetScriptGenerator(defaultNpmVersion: "6.0.0");
        //    var repo = new MemorySourceRepo();
        //    repo.AddFile(ComposerFileWithBuildScript, NodeConstants.ComposerFileFileName);
        //    var context = CreateBuildScriptGeneratorContext(repo);
        //    context.LanguageVersion = "8.2.1";
        //    var expected = new NodeBashBuildSnippetProperties(
        //        packageInstallCommand: NpmInstallCommand,
        //        runBuildCommand: "npm run build",
        //        runBuildAzureCommand: "npm run build:azure");

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

        //    // Assert
        //    Assert.NotNull(snippet);
        //    Assert.Equal(TemplateHelpers.Render(
        //        TemplateHelpers.TemplateResource.NodeSnippet, expected), snippet.BashBuildScriptSnippet);
        //}

        private IProgrammingPlatform GetScriptGenerator(
            string defaultVersion,
            BuildScriptGeneratorOptions commonOptions,
            PhpScriptGeneratorOptions phpScriptGeneratorOptions)
        {
            var phpVersionProvider = new TestPhpVersionProvider(
                supportedPhpVersions: new[] { "7.2.15", Common.PhpVersions.Php73Version });

            phpScriptGeneratorOptions = phpScriptGeneratorOptions ?? new PhpScriptGeneratorOptions();
            commonOptions             = commonOptions ?? new BuildScriptGeneratorOptions();

            return(new PhpPlatform(
                       Options.Create(phpScriptGeneratorOptions),
                       Options.Create(commonOptions),
                       phpVersionProvider,
                       NullLogger <PhpPlatform> .Instance,
                       detector: null));
        }
Example #11
0
 public static BuildScriptGeneratorContext CreateContext(
     BuildScriptGeneratorOptions options,
     CliEnvironmentSettings envSettings,
     ISourceRepo sourceRepo)
 {
     return(new BuildScriptGeneratorContext
     {
         SourceRepo = sourceRepo,
         Language = options.Language,
         LanguageVersion = options.LanguageVersion,
         Properties = options.Properties,
         EnableDotNetCore = !envSettings.DisableDotNetCore,
         EnableNodeJs = !envSettings.DisableNodeJs,
         EnablePython = !envSettings.DisablePython,
         EnablePhp = !envSettings.DisablePhp,
         DisableMultiPlatformBuild = envSettings.DisableMultiPlatformBuild
     });
 }
        private DefaultDockerfileGenerator CreateDefaultDockerfileGenerator(
            IProgrammingPlatform[] platforms,
            BuildScriptGeneratorOptions commonOptions)
        {
            commonOptions = commonOptions ?? new BuildScriptGeneratorOptions();
            var configuration = new TestConfiguration();
            var platformName  = commonOptions.PlatformName == "nodejs" ? "node" : commonOptions.PlatformName;

            configuration[$"{platformName}_version"] = commonOptions.PlatformVersion;
            return(new DefaultDockerfileGenerator(
                       new DefaultCompatiblePlatformDetector(
                           platforms,
                           NullLogger <DefaultCompatiblePlatformDetector> .Instance,
                           Options.Create(commonOptions),
                           configuration),
                       NullLogger <DefaultDockerfileGenerator> .Instance,
                       Options.Create(commonOptions)));
        }
Example #13
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);
        }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DotNetCorePlatform"/> class.
 /// </summary>
 /// <param name="versionProvider">The .NET version provider.</param>
 /// <param name="projectFileProvider">The project file provider.</param>
 /// <param name="environmentSettingsProvider">The environment settings provider.</param>
 /// <param name="logger">The logger of .NET platform.</param>
 /// <param name="detector">The detector of .NET platform.</param>
 /// <param name="cliOptions">The build options for BuildScriptGenerator.</param>
 /// <param name="dotNetCoreScriptGeneratorOptions">The options if .NET platform.</param>
 /// <param name="platformInstaller">The <see cref="DotNetCorePlatformInstaller"/>.</param>
 public DotNetCorePlatform(
     IDotNetCoreVersionProvider versionProvider,
     DefaultProjectFileProvider projectFileProvider,
     ILogger <DotNetCorePlatform> logger,
     DotNetCoreLanguageDetector detector,
     IOptions <BuildScriptGeneratorOptions> cliOptions,
     IOptions <DotNetCoreScriptGeneratorOptions> dotNetCoreScriptGeneratorOptions,
     IEnvironment environment,
     DotNetCorePlatformInstaller platformInstaller)
 {
     _versionProvider     = versionProvider;
     _projectFileProvider = projectFileProvider;
     _logger   = logger;
     _detector = detector;
     _dotNetCoreScriptGeneratorOptions = dotNetCoreScriptGeneratorOptions.Value;
     _cliOptions        = cliOptions.Value;
     _environment       = environment;
     _platformInstaller = platformInstaller;
 }
        public void IntermediateDir_IsNotUsed_ByDefault()
        {
            // Arrange
            var guid   = Guid.NewGuid();
            var appDir = Path.Combine(_tempDirRootPath, $"app-{guid}");

            Directory.CreateDirectory(appDir);
            var options = new BuildScriptGeneratorOptions
            {
                SourceDir = appDir,
            };
            var provider = GetSourceRepoProvider(options);

            // Act
            var sourceRepo = provider.GetSourceRepo();

            // Assert
            Assert.Equal(appDir, sourceRepo.RootPath);
        }
Example #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JavaPlatform"/> class.
 /// </summary>
 /// <param name="commonOptions">The <see cref="BuildScriptGeneratorOptions"/>.</param>
 /// <param name="javaScriptGeneratorOptions">The options for JavaScriptGenerator.</param>
 /// <param name="javaVersionProvider">The <see cref="JavaVersionProvider"/>.</param>
 /// <param name="mavenVersionProvider">The <see cref="IMavenVersionProvider"/>.</param>
 /// <param name="logger">The logger of Java platform.</param>
 /// <param name="detector">The detector of Java platform.</param>
 /// <param name="environment">The environment of Java platform.</param>
 /// <param name="javaPlatformInstaller">The <see cref="JavaPlatformInstaller"/>.</param>
 /// <param name="mavenInstaller">The <see cref="MavenInstaller"/>.</param>
 public JavaPlatform(
     IOptions <BuildScriptGeneratorOptions> commonOptions,
     IOptions <ScriptGeneratorOptionsForJava> javaScriptGeneratorOptions,
     IJavaVersionProvider javaVersionProvider,
     IMavenVersionProvider mavenVersionProvider,
     ILogger <JavaPlatform> logger,
     IJavaPlatformDetector detector,
     JavaPlatformInstaller javaPlatformInstaller,
     MavenInstaller mavenInstaller)
 {
     _commonOptions = commonOptions.Value;
     _javaScriptGeneratorOptions = javaScriptGeneratorOptions.Value;
     _javaVersionProvider        = javaVersionProvider;
     _mavenVersionProvider       = mavenVersionProvider;
     _logger   = logger;
     _detector = detector;
     _javaPlatformInstaller = javaPlatformInstaller;
     _mavenInstaller        = mavenInstaller;
 }
        public void TryGenerateScript_Throws_IfPlatformIsProvidedButDisabled()
        {
            // Arrange
            var detector = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: "test",
                detectedPlatformVersion: "1.0.0");
            var platform      = new TestProgrammingPlatform("test", new[] { "1.0.0" }, detector: detector, enabled: false);
            var commonOptions = new BuildScriptGeneratorOptions
            {
                PlatformName    = "test",
                PlatformVersion = "1.0.0",
            };
            var generator = CreateDefaultScriptGenerator(platform, commonOptions);
            var context   = CreateScriptGeneratorContext();

            // Act & Assert
            var exception = Assert.Throws <UnsupportedPlatformException>(
                () => generator.GenerateBashScript(context, out var generatedScript));
        }
        public void GeneratesScript_UsingTheFirstPlatform_WhichCanGenerateScript()
        {
            // Arrange
            var detector1 = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: "lang1",
                detectedPlatformVersion: "1.0.0");
            var platform1 = new TestProgrammingPlatform(
                "lang1",
                new[] { "1.0.0" },
                canGenerateScript: false,
                scriptContent: null,
                installationScriptContent: "lang1-installationscript",
                detector: detector1);
            var detector2 = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: "lang2",
                detectedPlatformVersion: "1.0.0");
            var platform2 = new TestProgrammingPlatform(
                "lang2",
                new[] { "1.0.0" },
                canGenerateScript: true,
                installationScriptContent: "lang2-installationscript",
                scriptContent: "script-content",
                detector: detector2);

            var commonOptions = new BuildScriptGeneratorOptions
            {
                PlatformName    = "lang2",
                PlatformVersion = "1.0.0",
            };
            var generator = CreateDefaultScriptGenerator(new[] { platform1, platform2 }, commonOptions);
            var context   = CreateScriptGeneratorContext();

            // Act
            generator.GenerateBashScript(context, out var generatedScript);

            // Assert
            Assert.Contains("script-content", generatedScript);
            Assert.True(detector1.DetectInvoked);
            Assert.True(detector2.DetectInvoked);
            Assert.Contains("lang1-installationscript", generatedScript);
            Assert.Contains("lang2-installationscript", generatedScript);
        }
        public void GeneratesScript_AddsSnippetsForMultiplePlatforms()
        {
            // Arrange
            var platform1 = new TestProgrammingPlatform(
                platformName: "lang1",
                new[] { "1.0.0" },
                canGenerateScript: true,
                scriptContent: "ABCDEFG",
                detector: new TestPlatformDetectorSimpleMatch(
                    shouldMatch: true,
                    platformName: "lang1",
                    platformVersion: "1.0.0"));
            var platform2 = new TestProgrammingPlatform(
                platformName: "lang2",
                new[] { "1.0.0" },
                canGenerateScript: true,
                scriptContent: "123456",
                detector: new TestPlatformDetectorSimpleMatch(
                    shouldMatch: true,
                    platformName: "lang2",
                    platformVersion: "1.0.0"));

            var commonOptions = new BuildScriptGeneratorOptions
            {
                PlatformName             = "lang1",
                PlatformVersion          = "1.0.0",
                EnableMultiPlatformBuild = true,
            };
            var generator = CreateDefaultScriptGenerator(new[] { platform1, platform2 }, commonOptions);
            var context   = CreateScriptGeneratorContext();

            // Act
            generator.GenerateBashScript(context, out var generatedScript);
            var expectedPlatformNameManifestEntry = "echo \"PlatformName=\\\"lang1,lang2\\\"\" >> \"$MANIFEST_DIR/$MANIFEST_FILE\"";
            var buggyPlatformNameManifestEntry    = "echo \"PlatformName=\\\",lang1lang2\\\"\" >> \"$MANIFEST_DIR/$MANIFEST_FILE\"";

            // Assert
            Assert.Contains("ABCDEFG", generatedScript);
            Assert.Contains("123456", generatedScript);
            Assert.Contains(expectedPlatformNameManifestEntry, generatedScript, StringComparison.OrdinalIgnoreCase);
            Assert.DoesNotContain(buggyPlatformNameManifestEntry, generatedScript, StringComparison.OrdinalIgnoreCase);
        }
Example #20
0
        private TestNodePlatform CreateNodePlatform(
            BuildScriptGeneratorOptions commonOptions,
            NodeScriptGeneratorOptions nodeScriptGeneratorOptions,
            NodePlatformInstaller platformInstaller,
            string detectedVersion = null)
        {
            var environment = new TestEnvironment();

            var versionProvider = new TestNodeVersionProvider();
            var detector        = new TestNodePlatformDetector(detectedVersion: detectedVersion);

            return(new TestNodePlatform(
                       Options.Create(commonOptions),
                       Options.Create(nodeScriptGeneratorOptions),
                       versionProvider,
                       NullLogger <NodePlatform> .Instance,
                       detector,
                       environment,
                       platformInstaller));
        }
        private static IProgrammingPlatform GetNodePlatform(
            string defaultNodeVersion,
            BuildScriptGeneratorOptions commonOptions,
            NodeScriptGeneratorOptions nodeScriptGeneratorOptions)
        {
            var nodeVersionProvider = new TestNodeVersionProvider(
                new[] { "6.11.0", NodeVersions.Node8Version, NodeVersions.Node10Version, NodeVersions.Node12Version },
                defaultVersion: defaultNodeVersion);

            nodeScriptGeneratorOptions = nodeScriptGeneratorOptions ?? new NodeScriptGeneratorOptions();
            commonOptions = commonOptions ?? new BuildScriptGeneratorOptions();
            return(new NodePlatform(
                       Options.Create(commonOptions),
                       Options.Create(nodeScriptGeneratorOptions),
                       nodeVersionProvider,
                       NullLogger <NodePlatform> .Instance,
                       detector: null,
                       new TestEnvironment(),
                       new NodePlatformInstaller(Options.Create(commonOptions), NullLoggerFactory.Instance)));
        }
        public void TryGenerateScript_Throws_IfPlatformIsProvided_AndCannotDetectPlatform()
        {
            // Arrange
            var detector = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: null,
                detectedPlatformVersion: null);
            var platform      = new TestProgrammingPlatform("test1", new[] { "1.0.0" }, detector: detector);
            var commonOptions = new BuildScriptGeneratorOptions
            {
                PlatformName = "test2",
            };
            var generator = CreateDefaultScriptGenerator(platform, commonOptions);
            var context   = CreateScriptGeneratorContext();

            // Act & Assert
            var exception = Assert.Throws <UnsupportedPlatformException>(
                () => generator.GenerateBashScript(context, out var generatedScript));

            Assert.Equal("'test2' platform is not supported. Supported platforms are: test1", exception.Message);
        }
        public void TryGenerateScript_Throws_IfNoPlatformIsProvided_AndCannotDetectPlatform()
        {
            // Arrange
            var detector = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: null,
                detectedPlatformVersion: null);
            var platform      = new TestProgrammingPlatform("test", new[] { "1.0.0" }, detector: detector);
            var commonOptions = new BuildScriptGeneratorOptions
            {
                EnableMultiPlatformBuild = true,
            };
            var generator = CreateDefaultScriptGenerator(platform, commonOptions);
            var context   = CreateScriptGeneratorContext();

            // Act & Assert
            var exception = Assert.Throws <UnsupportedPlatformException>(
                () => generator.GenerateBashScript(context, out var generatedScript));

            Assert.Equal(Labels.UnableToDetectPlatformMessage, exception.Message);
            Assert.True(detector.DetectInvoked);
        }
        public void TryGenerateScript_Throws_IfPlatformIsProvidedButNoVersion_AndCannotDetectVersion()
        {
            // Arrange
            var detector = new TestPlatformDetectorUsingPlatformName(
                detectedPlatformName: "test",
                detectedPlatformVersion: null);
            var platform      = new TestProgrammingPlatform("test", new[] { "1.0.0" }, detector: detector);
            var commonOptions = new BuildScriptGeneratorOptions
            {
                PlatformName = "test",
            };
            var generator = CreateDefaultScriptGenerator(platform, commonOptions);
            var context   = CreateScriptGeneratorContext();

            // Act & Assert
            var exception = Assert.Throws <UnsupportedVersionException>(
                () => generator.GenerateBashScript(context, out var generatedScript));

            Assert.Equal("Couldn't detect a version for the platform 'test' in the repo.", exception.Message);
            Assert.True(detector.DetectInvoked);
        }
Example #25
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]);
        }
        public void ResolvesToAbsolutePath_WhenAbsolutePathIsGiven()
        {
            // Arrange
            var options      = new BuildScriptGeneratorOptions();
            var absolutePath = Path.GetTempPath();

            // Act
            BuildScriptGeneratorOptionsHelper.ConfigureBuildScriptGeneratorOptions(
                options,
                sourceDir: absolutePath,
                destinationDir: absolutePath,
                intermediateDir: absolutePath,
                language: null,
                languageVersion: null,
                scriptOnly: false,
                properties: null);

            // Assert
            Assert.Equal(absolutePath, options.SourceDir);
            Assert.Equal(absolutePath, options.DestinationDir);
            Assert.Equal(absolutePath, options.IntermediateDir);
        }
Example #27
0
        public void GeneratedBuildSnippet_HasCustomNpmRunBuildCommand_EvenIfPackageJsonHasBuildNodes()
        {
            // Arrange
            const string packageJson   = @"{
              ""main"": ""server.js"",
              ""scripts"": {
                ""build"": ""build-node"",
                ""build:azure"": ""azure-node"",
              },
            }";
            var          expectedText  = "custom-npm-run-build";
            var          commonOptions = new BuildScriptGeneratorOptions();
            var          nodePlatform  = CreateNodePlatform(
                commonOptions,
                new NodeScriptGeneratorOptions {
                CustomRunBuildCommand = expectedText
            },
                new NodePlatformInstaller(
                    Options.Create(commonOptions),
                    NullLoggerFactory.Instance));
            var repo = new MemorySourceRepo();

            repo.AddFile(packageJson, NodeConstants.PackageJsonFileName);
            var context        = CreateContext(repo);
            var detectorResult = new NodePlatformDetectorResult
            {
                Platform        = NodeConstants.PlatformName,
                PlatformVersion = "10.10",
            };

            // Act
            var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult);

            // Assert
            Assert.NotNull(buildScriptSnippet);
            Assert.Contains(expectedText, buildScriptSnippet.BashBuildScriptSnippet);
            Assert.DoesNotContain("npm run build", buildScriptSnippet.BashBuildScriptSnippet);
            Assert.DoesNotContain("npm run build:azure", buildScriptSnippet.BashBuildScriptSnippet);
        }
Example #28
0
        public void GeneratedBuildSnippet_UsingYarn2Commands()
        {
            // Arrange
            const string packageJson   = @"{
              ""main"": ""server.js"",
              ""scripts"": {
              },
            }";
            var          commonOptions = new BuildScriptGeneratorOptions();
            var          nodePlatform  = CreateNodePlatform(
                commonOptions,
                new NodeScriptGeneratorOptions {
                CustomRunBuildCommand = null
            },
                new NodePlatformInstaller(
                    Options.Create(commonOptions),
                    NullLoggerFactory.Instance));
            var repo = new MemorySourceRepo();

            repo.AddFile(packageJson, NodeConstants.PackageJsonFileName);
            repo.AddFile("", NodeConstants.YarnLockFileName);
            repo.AddFile("", ".yarnrc.yml");
            var context        = CreateContext(repo);
            var detectorResult = new NodePlatformDetectorResult
            {
                Platform         = NodeConstants.PlatformName,
                PlatformVersion  = "10.10",
                HasYarnrcYmlFile = true,
                IsYarnLockFileValidYamlFormat = true,
            };

            // Act
            var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult);

            // Assert
            Assert.NotNull(buildScriptSnippet);
            Assert.Contains("yarnCacheFolderName=cacheFolder", buildScriptSnippet.BashBuildScriptSnippet);
            Assert.Contains("yarn workspaces focus --all --production", buildScriptSnippet.BashBuildScriptSnippet);
        }
        public void ResolvesToCurrentDirectoryAbsolutePath_WhenDotNotationIsUsed()
        {
            // Arrange
            var options    = new BuildScriptGeneratorOptions();
            var currentDir = Directory.GetCurrentDirectory();

            // Act
            BuildScriptGeneratorOptionsHelper.ConfigureBuildScriptGeneratorOptions(
                options,
                sourceDir: ".",
                destinationDir: ".",
                intermediateDir: ".",
                language: null,
                languageVersion: null,
                scriptOnly: false,
                properties: null);

            // Assert
            Assert.Equal(currentDir, options.SourceDir);
            Assert.Equal(currentDir, options.DestinationDir);
            Assert.Equal(currentDir, options.IntermediateDir);
        }
        public void GetCompatiblePlatforms_ReturnsOnlyPlatforms_ParticipatingIn_MultiPlatformBuilds()
        {
            // Arrange
            var platform1 = new TestProgrammingPlatform(
                platformName: "lang1",
                new[] { "1.0.0" },
                canGenerateScript: true,
                scriptContent: "ABCDEFG",
                detector: new TestPlatformDetectorSimpleMatch(
                    shouldMatch: true,
                    platformName: "lang1",
                    platformVersion: "1.0.0"));
            var platform2 = new TestProgrammingPlatform(
                platformName: "lang2",
                new[] { "1.0.0" },
                canGenerateScript: true,
                scriptContent: "123456",
                detector: new TestPlatformDetectorSimpleMatch(
                    shouldMatch: true,
                    platformName: "lang2",
                    platformVersion: "1.0.0"),
                platformIsEnabledForMultiPlatformBuild: false); // This platform explicitly opts out

            var commonOptions = new BuildScriptGeneratorOptions
            {
                PlatformName             = "lang1",
                PlatformVersion          = "1.0.0",
                EnableMultiPlatformBuild = true,
            };
            var detector = CreateDefaultCompatibleDetector(new[] { platform1, platform2 }, commonOptions);
            var context  = CreateScriptGeneratorContext();

            // Act
            var compatiblePlatforms = detector.GetCompatiblePlatforms(context);

            // Assert
            Assert.NotNull(compatiblePlatforms);
            Assert.Equal(2, compatiblePlatforms.Count);
        }