예제 #1
0
        /// <inheritdoc/>
        public string GetInstallerScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            string installationScriptSnippet = null;

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

                if (_platformInstaller.IsVersionAlreadyInstalled(detectorResult.PlatformVersion))
                {
                    _logger.LogDebug(
                        "Node version {version} is already installed. So skipping installing it again.",
                        detectorResult.PlatformVersion);
                }
                else
                {
                    _logger.LogDebug(
                        "Node 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);
        }
예제 #2
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,
            });
        }