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
        /// <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 #3
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext ctx,
            PlatformDetectorResult detectorResult)
        {
            var nodePlatformDetectorResult = detectorResult as NodePlatformDetectorResult;

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

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

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

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

            if (_nodeScriptGeneratorOptions.EnableNodeMonorepoBuild &&
                nodePlatformDetectorResult.HasLernaJsonFile &&
                nodePlatformDetectorResult.HasLageConfigJSFile)
            {
                _logger.LogError(
                    "Could not build monorepo with multiple package management tools. Both 'lerna.json' and 'lage.config.js' files are found.");
                throw new InvalidUsageException("Multiple monorepo package management tools are found, please choose to use either Lerna or Lage.");
            }

            if (ctx.SourceRepo.FileExists(NodeConstants.YarnLockFileName))
            {
                packageManagerCmd              = NodeConstants.YarnCommand;
                configureYarnCache             = false;
                packageInstallerVersionCommand = NodeConstants.YarnVersionCommand;

                // In Yarn 2+ and .yarnrc.yml file replaces .yarnrc in Yarn 2+.
                // Applying yarn 2 cache folder name and package install command.
                if (nodePlatformDetectorResult.HasYarnrcYmlFile)
                {
                    yarnCacheFolderName   = NodeConstants.Yarn2ConfigFolderName;
                    packageInstallCommand = NodeConstants.Yarn2PackageInstallCommand;
                }
                else
                {
                    yarnCacheFolderName   = NodeConstants.Yarn1ConfigFolderName;
                    packageInstallCommand = NodeConstants.YarnPackageInstallCommand;
                }
            }
            else
            {
                packageManagerCmd              = NodeConstants.NpmCommand;
                packageInstallCommand          = NodeConstants.NpmPackageInstallCommand;
                packageInstallerVersionCommand = NodeConstants.NpmVersionCommand;
            }

            if (_nodeScriptGeneratorOptions.EnableNodeMonorepoBuild)
            {
                // If a 'lerna.json' file exists, override the npm client that lerna chosen to build monorepo.
                if (nodePlatformDetectorResult.HasLernaJsonFile)
                {
                    packageManagerCmd    = nodePlatformDetectorResult.LernaNpmClient;
                    runBuildLernaCommand = string.Format(
                        NodeConstants.PkgMgrRunBuildCommandTemplate,
                        NodeConstants.LernaCommand);
                    if (!string.IsNullOrEmpty(nodePlatformDetectorResult.LernaNpmClient) &&
                        nodePlatformDetectorResult.LernaNpmClient.Equals(
                            NodeConstants.YarnCommand, StringComparison.OrdinalIgnoreCase))
                    {
                        packageInstallCommand          = NodeConstants.YarnPackageInstallCommand;
                        configureYarnCache             = false;
                        packageInstallerVersionCommand = NodeConstants.YarnVersionCommand;
                        installLernaCommand            = NodeConstants.InstallLernaCommandYarn;
                    }
                    else
                    {
                        packageInstallCommand          = NodeConstants.NpmPackageInstallCommand;
                        packageInstallerVersionCommand = NodeConstants.NpmVersionCommand;
                        installLernaCommand            = NodeConstants.InstallLernaCommandNpm;
                    }
                }

                // If a 'lage.config.js' file exits, run build using lage specifc commands.
                if (nodePlatformDetectorResult.HasLageConfigJSFile)
                {
                    runBuildLageCommand = ctx.SourceRepo.FileExists(NodeConstants.YarnLockFileName) ?
                                          NodeConstants.YarnRunLageBuildCommand : NodeConstants.NpmRunLageBuildCommand;
                }
            }

            _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.CustomBuildCommand) &&
                string.IsNullOrEmpty(_nodeScriptGeneratorOptions.CustomRunBuildCommand) &&
                string.IsNullOrEmpty(runBuildLernaCommand) &&
                string.IsNullOrEmpty(runBuildLageCommand))
            {
                var scriptsNode = packageJson?.scripts;
                if (scriptsNode != null)
                {
                    if (scriptsNode.build != null)
                    {
                        runBuildCommand = string.Format(NodeConstants.PkgMgrRunBuildCommandTemplate, packageManagerCmd);
                    }

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

            if (IsBuildRequired(ctx) &&
                string.IsNullOrEmpty(_nodeScriptGeneratorOptions.CustomBuildCommand) &&
                string.IsNullOrEmpty(_nodeScriptGeneratorOptions.CustomRunBuildCommand) &&
                string.IsNullOrEmpty(runBuildCommand) &&
                string.IsNullOrEmpty(runBuildAzureCommand) &&
                string.IsNullOrEmpty(runBuildLernaCommand) &&
                string.IsNullOrEmpty(runBuildLageCommand))
            {
                throw new NoBuildStepException(
                          "Could not find either 'build' or 'build:azure' node under 'scripts' in package.json. " +
                          "Could not find value for custom run build command using the environment variable " +
                          "key 'RUN_BUILD_COMMAND'." +
                          "Could not find tools for building monorepos, no 'lerna.json' or 'lage.config.js' files found.");
            }

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

            if (packageJson?.devDependencies != null)
            {
                var depSpecs = ((JObject)packageJson.devDependencies).ToObject <IDictionary <string, string> >();
                _logger.LogDependencies(
                    _commonOptions.PlatformName,
                    nodePlatformDetectorResult.PlatformVersion,
                    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.PlatformName}_{RegistryUrlPropertyKey}"] = customRegistryUrl;
                }
            }

            string packageDir = null;

            if (ctx.Properties != null)
            {
                ctx.Properties.TryGetValue(PackageDirectoryPropertyKey, out packageDir);
                if (!string.IsNullOrWhiteSpace(packageDir))
                {
                    // Write the package directory to the build manifest
                    manifestFileProperties[$"{PackageDirectoryPropertyKey}"] = packageDir;
                }
            }

            var scriptProps = new NodeBashBuildSnippetProperties
            {
                PackageRegistryUrl                  = customRegistryUrl,
                PackageDirectory                    = packageDir,
                PackageInstallCommand               = packageInstallCommand,
                NpmRunBuildCommand                  = runBuildCommand,
                NpmRunBuildAzureCommand             = runBuildAzureCommand,
                HasProdDependencies                 = hasProdDependencies,
                HasDevDependencies                  = hasDevDependencies,
                ProductionOnlyPackageInstallCommand = productionOnlyPackageInstallCommand,
                CompressNodeModulesCommand          = compressNodeModulesCommand,
                CompressedNodeModulesFileName       = compressedNodeModulesFileName,
                ConfigureYarnCache                  = configureYarnCache,
                YarnCacheFolderName                 = yarnCacheFolderName,
                PruneDevDependencies                = pruneDevDependencies,
                AppInsightsInjectCommand            = appInsightsInjectCommand,
                AppInsightsPackageName              = NodeConstants.NodeAppInsightsPackageName,
                AppInsightsLoaderFileName           = NodeAppInsightsLoader.NodeAppInsightsLoaderFileName,
                PackageInstallerVersionCommand      = packageInstallerVersionCommand,
                RunNpmPack            = _commonOptions.ShouldPackage,
                CustomBuildCommand    = _nodeScriptGeneratorOptions.CustomBuildCommand,
                CustomRunBuildCommand = _nodeScriptGeneratorOptions.CustomRunBuildCommand,
                LernaRunBuildCommand  = runBuildLernaCommand,
                InstallLernaCommand   = installLernaCommand,
                LernaInitCommand      = NodeConstants.LernaInitCommand,
                LernaBootstrapCommand = NodeConstants.LernaBootstrapCommand,
                InstallLageCommand    = NodeConstants.InstallLageCommand,
                LageRunBuildCommand   = runBuildLageCommand,
            };

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

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
            });
        }
Beispiel #4
0
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var    buildProperties       = new Dictionary <string, string>();
            var    packageJson           = GetPackageJsonObject(context.SourceRepo, _logger);
            string runBuildCommand       = null;
            string runBuildAzureCommand  = null;
            bool   configureYarnCache    = false;
            string packageManagerCmd     = null;
            string packageInstallCommand = null;

            if (context.SourceRepo.FileExists(NodeConstants.YarnLockFileName))
            {
                packageManagerCmd     = NodeConstants.YarnCommand;
                packageInstallCommand = NodeConstants.YarnPackageInstallCommand;
                configureYarnCache    = true;
            }
            else
            {
                packageManagerCmd     = NodeConstants.NpmCommand;
                packageInstallCommand = NodeConstants.NpmPackageInstallCommand;
            }

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

            var hasProductionOnlyDependencies = false;

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

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

            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));
            }

            string compressNodeModulesCommand    = null;
            string compressedNodeModulesFileName = null;

            GetNodeModulesPackOptions(context, out compressNodeModulesCommand, out compressedNodeModulesFileName);

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

            bool   pruneDevDependencies     = ShouldPruneDevDependencies(context);
            string appInsightsInjectCommand = string.Empty;
            var    appInsightsKey           = _environment.GetEnvironmentVariable(Constants.AppInsightsKey);
            var    shouldInjectAppInsights  = ShouldInjectAppInsights(packageJson, context, appInsightsKey);

            // node_options is only supported in version 8.0.0 or newer and in 6.12.0
            // so we will be able to set up app-insight only when node version is 6.12.0 or 8.0.0 or newer
            if (shouldInjectAppInsights)
            {
                appInsightsInjectCommand = string.Concat(
                    NodeConstants.NpmPackageInstallCommand,
                    " --save ",
                    NodeConstants.NodeAppInsightsPackageName);

                buildProperties[NodeConstants.InjectedAppInsights] = true.ToString();
                _logger.LogInformation("Oryx setting up Application Insights for auto-collection telemetry... ");
            }

            var scriptProps = new NodeBashBuildSnippetProperties(
                packageInstallCommand: packageInstallCommand,
                runBuildCommand: runBuildCommand,
                runBuildAzureCommand: runBuildAzureCommand,
                hasProductionOnlyDependencies: hasProductionOnlyDependencies,
                productionOnlyPackageInstallCommand: productionOnlyPackageInstallCommand,
                compressNodeModulesCommand: compressNodeModulesCommand,
                compressedNodeModulesFileName: compressedNodeModulesFileName,
                configureYarnCache: configureYarnCache,
                pruneDevDependencies: pruneDevDependencies,
                appInsightsInjectCommand: appInsightsInjectCommand,
                appInsightsPackageName: NodeConstants.NodeAppInsightsPackageName,
                appInsightsLoaderFileName: NodeAppInsightsLoader.NodeAppInsightsLoaderFileName);

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

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

            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
            {
                packageManagerCmd              = NodeConstants.NpmCommand;
                packageInstallCommand          = NodeConstants.NpmPackageInstallCommand;
                packageInstallerVersionCommand = NodeConstants.NpmVersionCommand;
            }

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

            var hasProductionOnlyDependencies = false;

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

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

            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))
            {
                buildProperties[NodeConstants.NodeModulesFileBuildProperty] = compressedNodeModulesFileName;
            }

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

            GetAppOutputDirPath(packageJson, buildProperties);

            var scriptProps = new NodeBashBuildSnippetProperties
            {
                PackageInstallCommand               = packageInstallCommand,
                NpmRunBuildCommand                  = runBuildCommand,
                NpmRunBuildAzureCommand             = runBuildAzureCommand,
                HasProductionOnlyDependencies       = hasProductionOnlyDependencies,
                ProductionOnlyPackageInstallCommand = productionOnlyPackageInstallCommand,
                CompressNodeModulesCommand          = compressNodeModulesCommand,
                CompressedNodeModulesFileName       = compressedNodeModulesFileName,
                ConfigureYarnCache                  = configureYarnCache,
                PruneDevDependencies                = pruneDevDependencies,
                AppInsightsInjectCommand            = appInsightsInjectCommand,
                AppInsightsPackageName              = NodeConstants.NodeAppInsightsPackageName,
                AppInsightsLoaderFileName           = NodeAppInsightsLoader.NodeAppInsightsLoaderFileName,
                PackageInstallerVersionCommand      = packageInstallerVersionCommand,
                RunNpmPack = ctx.IsPackage,
            };

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

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

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

            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
            {
                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.CustomRunBuildCommand))
            {
                var scriptsNode = packageJson?.scripts;
                if (scriptsNode != null)
                {
                    if (scriptsNode.build != null)
                    {
                        runBuildCommand = string.Format(NodeConstants.PkgMgrRunBuildCommandTemplate, packageManagerCmd);
                    }

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

            if (IsBuildRequired(ctx) &&
                string.IsNullOrEmpty(_nodeScriptGeneratorOptions.CustomRunBuildCommand) &&
                string.IsNullOrEmpty(runBuildCommand) &&
                string.IsNullOrEmpty(runBuildAzureCommand))
            {
                throw new NoBuildStepException(
                          "Could not find either 'build' or 'build:azure' node under 'scripts' in package.json. " +
                          "Could not find value for custom run build command using the environment variable " +
                          "key 'RUN_BUILD_COMMAND'.");
            }

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

            if (packageJson?.devDependencies != null)
            {
                var depSpecs = ((JObject)packageJson.devDependencies).ToObject <IDictionary <string, string> >();
                _logger.LogDependencies(
                    _commonOptions.PlatformName,
                    detectorResult.PlatformVersion,
                    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.PlatformName}_{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            = _commonOptions.ShouldPackage,
                CustomRunBuildCommand = _nodeScriptGeneratorOptions.CustomRunBuildCommand,
            };

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

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
            });
        }
Beispiel #7
0
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var    buildProperties       = new Dictionary <string, string>();
            var    packageJson           = GetPackageJsonObject(context.SourceRepo, _logger);
            string runBuildCommand       = null;
            string runBuildAzureCommand  = null;
            bool   configureYarnCache    = false;
            string packageManagerCmd     = null;
            string packageInstallCommand = null;

            if (context.SourceRepo.FileExists(NodeConstants.YarnLockFileName))
            {
                packageManagerCmd     = NodeConstants.YarnCommand;
                packageInstallCommand = NodeConstants.YarnPackageInstallCommand;
                configureYarnCache    = true;
            }
            else
            {
                packageManagerCmd     = NodeConstants.NpmCommand;
                packageInstallCommand = NodeConstants.NpmPackageInstallCommand;
            }

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

            var hasProductionOnlyDependencies = false;

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

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

            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));
            }

            string compressNodeModulesCommand    = null;
            string compressedNodeModulesFileName = null;

            GetNodeModulesPackOptions(context, out compressNodeModulesCommand, out compressedNodeModulesFileName);

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

            bool pruneDevDependencies = ShouldPruneDevDependencies(context);
            var  scriptProps          = new NodeBashBuildSnippetProperties(
                packageInstallCommand: packageInstallCommand,
                runBuildCommand: runBuildCommand,
                runBuildAzureCommand: runBuildAzureCommand,
                hasProductionOnlyDependencies: hasProductionOnlyDependencies,
                productionOnlyPackageInstallCommand: productionOnlyPackageInstallCommand,
                compressNodeModulesCommand: compressNodeModulesCommand,
                compressedNodeModulesFileName: compressedNodeModulesFileName,
                configureYarnCache: configureYarnCache,
                pruneDevDependencies: pruneDevDependencies);

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

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = buildProperties
            });
        }