Ejemplo n.º 1
0
    public async Task <ProjectBuildResult> BuildAsync(ProjectBuildArgs args)
    {
        var templateInfo = await GetTemplateInfoAsync(args);

        NormalizeArgs(args, templateInfo);

        await EventBus.PublishAsync(new ProjectCreationProgressEvent {
            Message = "Downloading the solution template"
        }, false);

        var templateFile = await SourceCodeStore.GetAsync(
            args.TemplateName,
            SourceCodeTypes.Template,
            args.Version,
            args.TemplateSource,
            args.ExtraProperties.ContainsKey(NewCommand.Options.Preview.Long)
            );

        DeveloperApiKeyResult apiKeyResult = null;

#if DEBUG
        try
        {
            var apiKeyResultSection = _configuration.GetSection("apiKeyResult");
            if (apiKeyResultSection.Exists())
            {
                apiKeyResult = apiKeyResultSection.Get <DeveloperApiKeyResult>(); //you can use user secrets
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        if (apiKeyResult == null)
        {
            apiKeyResult = await ApiKeyService.GetApiKeyOrNullAsync();
        }
#else
        apiKeyResult = await ApiKeyService.GetApiKeyOrNullAsync();
#endif

        if (apiKeyResult != null)
        {
            if (apiKeyResult.ApiKey != null)
            {
                args.ExtraProperties["api-key"] = apiKeyResult.ApiKey;
            }
            else if (templateInfo.Name == AppProTemplate.TemplateName)
            {
                throw new UserFriendlyException(apiKeyResult.ErrorMessage);
            }
        }

        if (apiKeyResult?.LicenseCode != null)
        {
            args.ExtraProperties["license-code"] = apiKeyResult.LicenseCode;
        }

        var context = new ProjectBuildContext(
            templateInfo,
            null,
            null,
            null,
            templateFile,
            args
            );

        if (context.Template is AppTemplateBase appTemplateBase)
        {
            appTemplateBase.HasDbMigrations = SemanticVersion.Parse(templateFile.Version) < new SemanticVersion(4, 3, 99);
        }

        await EventBus.PublishAsync(new ProjectCreationProgressEvent {
            Message = "Customizing the solution template"
        }, false);

        TemplateProjectBuildPipelineBuilder.Build(context).Execute();

        if (!templateInfo.DocumentUrl.IsNullOrEmpty())
        {
            Logger.LogInformation("Check out the documents at " + templateInfo.DocumentUrl);
        }

        // Exclude unwanted or known options.
        var options = args.ExtraProperties
                      .Where(x => !x.Key.Equals(CliConsts.Command, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.Tiered.Long, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.Preview.Long, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.DatabaseProvider.Long, StringComparison.InvariantCultureIgnoreCase) &&
                             !x.Key.Equals(NewCommand.Options.DatabaseProvider.Short, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.OutputFolder.Long, StringComparison.InvariantCultureIgnoreCase) &&
                             !x.Key.Equals(NewCommand.Options.OutputFolder.Short, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.UiFramework.Long, StringComparison.InvariantCultureIgnoreCase) &&
                             !x.Key.Equals(NewCommand.Options.UiFramework.Short, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.Mobile.Long, StringComparison.InvariantCultureIgnoreCase) &&
                             !x.Key.Equals(NewCommand.Options.Mobile.Short, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.Version.Long, StringComparison.InvariantCultureIgnoreCase) &&
                             !x.Key.Equals(NewCommand.Options.Version.Short, StringComparison.InvariantCultureIgnoreCase))
                      .Where(x => !x.Key.Equals(NewCommand.Options.TemplateSource.Short, StringComparison.InvariantCultureIgnoreCase) &&
                             !x.Key.Equals(NewCommand.Options.TemplateSource.Long, StringComparison.InvariantCultureIgnoreCase))
                      .Select(x => x.Key).ToList();

        await CliAnalyticsCollect.CollectAsync(new CliAnalyticsCollectInputDto
        {
            Tool             = Options.ToolName,
            Command          = args.ExtraProperties.ContainsKey(CliConsts.Command) ? args.ExtraProperties[CliConsts.Command] : "",
            DatabaseProvider = args.DatabaseProvider.ToProviderName(),
            IsTiered         = args.ExtraProperties.ContainsKey(NewCommand.Options.Tiered.Long),
            UiFramework      = args.UiFramework.ToFrameworkName(),
            Options          = JsonSerializer.Serialize(options),
            ProjectName      = args.SolutionName.FullName,
            TemplateName     = args.TemplateName,
            TemplateVersion  = templateFile.Version
        });

        return(new ProjectBuildResult(context.Result.ZipContent, args.SolutionName.ProjectName));
    }