Beispiel #1
0
        public void Validate_IsHugoApp(string appName)
        {
            var environment     = new TestEnvironment();
            var sourceDirectory = Path.Combine(_nodeSampleDir, appName);
            var sourceRepo      = new LocalSourceRepo(sourceDirectory);
            var result          = StaticSiteGeneratorHelper.IsHugoApp(sourceRepo, environment);

            Assert.True(result);
        }
        public virtual PlatformDetectorResult Detect(RepositoryContext context)
        {
            var isHugoApp = StaticSiteGeneratorHelper.IsHugoApp(context.SourceRepo, _environment);

            if (isHugoApp)
            {
                return(new PlatformDetectorResult
                {
                    Platform = HugoConstants.PlatformName,
                    PlatformVersion = HugoConstants.Version,
                });
            }

            return(null);
        }
Beispiel #3
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 #4
0
        public LanguageDetectorResult Detect(RepositoryContext context)
        {
            bool isNodeApp = false;

            var sourceRepo = context.SourceRepo;

            if (sourceRepo.FileExists(NodeConstants.PackageJsonFileName) ||
                sourceRepo.FileExists(NodeConstants.PackageLockJsonFileName) ||
                sourceRepo.FileExists(NodeConstants.YarnLockFileName))
            {
                isNodeApp = true;
            }
            else if (StaticSiteGeneratorHelper.IsStaticSite(sourceRepo, _environment))
            {
                isNodeApp = true;
            }
            else
            {
                _logger.LogDebug(
                    $"Could not find {NodeConstants.PackageJsonFileName}/{NodeConstants.PackageLockJsonFileName}" +
                    $"/{NodeConstants.YarnLockFileName} in repo");
            }

            if (!isNodeApp)
            {
                // Copying the logic currently running in Kudu:
                var mightBeNode = false;
                foreach (var typicalNodeFile in TypicalNodeDetectionFiles)
                {
                    if (sourceRepo.FileExists(typicalNodeFile))
                    {
                        mightBeNode = true;
                        break;
                    }
                }

                if (mightBeNode)
                {
                    // Check if any of the known iis start pages exist
                    // If so, then it is not a node.js web site otherwise it is
                    foreach (var iisStartupFile in IisStartupFiles)
                    {
                        if (sourceRepo.FileExists(iisStartupFile))
                        {
                            _logger.LogDebug(
                                "App in repo is not a Node.js app as it has the file {iisStartupFile}",
                                iisStartupFile.Hash());
                            return(null);
                        }
                    }

                    isNodeApp = true;
                }
                else
                {
                    // No point in logging the actual file list, as it's constant
                    _logger.LogDebug("Could not find typical Node.js files in repo");
                }
            }

            if (isNodeApp)
            {
                var packageJson = NodePlatform.GetPackageJsonObject(sourceRepo, _logger);
                var nodeVersion = DetectNodeVersion(packageJson);

                return(new LanguageDetectorResult
                {
                    Language = NodeConstants.NodeJsName,
                    LanguageVersion = nodeVersion,
                });
            }
            else
            {
                _logger.LogDebug("App in repo is not a Node.js app");
            }

            return(null);
        }