Пример #1
0
        private static async Task AddGeneratedServiceCodeAsync(
            ConnectedServiceHandlerContext context,
            Project project,
            SalesforceConnectedServiceInstance salesforceInstance,
            GeneratedService generatedService)
        {
            string serviceDirectoryName = SalesforceConnectedServiceHandler.GetServiceDirectoryName(context, salesforceInstance.DesignerData.ServiceName);

            await GeneratedCodeHelper.AddGeneratedCodeAsync(
                context,
                project,
                "SalesforceService",
                serviceDirectoryName,
                (host) => SalesforceConnectedServiceHandler.GetServiceT4Sessions(host, generatedService),
                () => new SalesforceService(),
                (session) => "SalesforceService");

            if (salesforceInstance.RuntimeAuthentication.AuthStrategy == AuthenticationStrategy.WebServerFlow)
            {
                await GeneratedCodeHelper.AddGeneratedCodeAsync(
                    context,
                    project,
                    Constants.OAuthRedirectHandlerTypeName,
                    serviceDirectoryName,
                    (host) => SalesforceConnectedServiceHandler.GetServiceT4Sessions(host, generatedService),
                    () => new SalesforceOAuthRedirectHandler(),
                    (session) => Constants.OAuthRedirectHandlerTypeName);
            }
        }
Пример #2
0
        private static async Task AddGeneratedObjectCodeAsync(
            ConnectedServiceHandlerContext context,
            Project project,
            SalesforceConnectedServiceInstance salesforceInstance,
            GeneratedService generatedService,
            string modelsHintPath)
        {
            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_BuildingObjectModel);

            IEnumerable <GeneratedObject> generatedObjects = await CodeModelBuilder.BuildObjectModelAsync(
                salesforceInstance.SelectedObjects,
                salesforceInstance.DesignTimeAuthentication,
                generatedService,
                context.Logger);

            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_AddingGeneratedCodeForObjects, generatedObjects.Count());

            await GeneratedCodeHelper.AddGeneratedCodeAsync(
                context,
                project,
                "SalesforceObject",
                modelsHintPath,
                (host) => SalesforceConnectedServiceHandler.GetObjectT4Sessions(host, generatedObjects),
                () => new SalesforceObject(),
                (session) => ((GeneratedObject)session["generatedObject"]).Model.Name);
        }
Пример #3
0
        private static async Task UpdateConfigFileAsync(ConnectedServiceHandlerContext context, Project project, SalesforceConnectedServiceInstance salesforceInstance)
        {
            await Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // The EditableConfigHelper must run on the UI thread.

            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_UpdatingConfigFile);

            using (EditableXmlConfigHelper configHelper = context.CreateEditableXmlConfigHelper())
            {
                foreach (ConfigSetting configSetting in salesforceInstance.RuntimeAuthentication.GetConfigSettings(salesforceInstance.ConnectedAppName))
                {
                    configHelper.SetAppSetting(
                        ConfigurationKeyNames.GetQualifiedKeyName(configSetting.Key, salesforceInstance.DesignerData.ServiceName),
                        configSetting.Value == null ? string.Empty : configSetting.Value.ToString(),
                        configSetting.Comment);
                }

                if (salesforceInstance.RuntimeAuthentication.AuthStrategy == AuthenticationStrategy.WebServerFlow)
                {
                    string handlerName = Constants.OAuthRedirectHandlerNameFormat.FormatInvariantCulture(salesforceInstance.DesignerData.ServiceName);
                    string qualifiedHandlerTypeName = SalesforceConnectedServiceHandler.GetServiceNamespace(project, salesforceInstance.DesignerData.ServiceName)
                                                      + Type.Delimiter + Constants.OAuthRedirectHandlerTypeName;
                    string redirectUri = ((WebServerFlowInfo)salesforceInstance.RuntimeAuthentication).RedirectUri.ToString();

                    configHelper.RegisterRedirectHandler(handlerName, redirectUri, qualifiedHandlerTypeName);
                }

                configHelper.Save();
            }
        }
Пример #4
0
        private static async Task AddGeneratedCodeAsync(ConnectedServiceHandlerContext context, Project project, SalesforceConnectedServiceInstance salesforceInstance)
        {
            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_AddingGeneratedCode);

            string           modelsHintPath   = salesforceInstance.DesignerData.GetDefaultedModelsHintPath();
            GeneratedService generatedService = new GeneratedService()
            {
                ServiceNamespace       = SalesforceConnectedServiceHandler.GetServiceNamespace(project, salesforceInstance.DesignerData.ServiceName),
                ModelsNamespace        = SalesforceConnectedServiceHandler.GetModelsNamespace(project, modelsHintPath),
                DefaultNamespace       = ProjectHelper.GetProjectNamespace(project),
                AuthenticationStrategy = salesforceInstance.RuntimeAuthentication.AuthStrategy,
                ConfigurationKeyNames  = new ConfigurationKeyNames(salesforceInstance.DesignerData.ServiceName),
            };

            if (!context.IsUpdating)
            {
                await SalesforceConnectedServiceHandler.AddGeneratedServiceCodeAsync(context, project, salesforceInstance, generatedService);
            }

            if (salesforceInstance.SelectedObjects.Any())
            {
                // Only set the ModelsHintPath in the case where objects are generated so that it is only added to
                // the ConnectedService.json when it is applicable.
                salesforceInstance.DesignerData.ModelsHintPath = modelsHintPath;

                await SalesforceConnectedServiceHandler.AddGeneratedObjectCodeAsync(context, project, salesforceInstance, generatedService, modelsHintPath);
            }
        }
Пример #5
0
        private static bool IsSuffixUsedInGeneratedFilesDirectories(ConnectedServiceHandlerContext context, string serviceName, Project project)
        {
            string projectDir = Path.GetDirectoryName(project.FullName);

            Debug.Assert(!String.IsNullOrEmpty(projectDir));  // How can we not have a project path?
            string serviceDirectoryPath = Path.Combine(projectDir, SalesforceConnectedServiceHandler.GetServiceDirectoryName(context, serviceName));
            string modelsDirectoryPath  = Path.Combine(projectDir, SalesforceConnectedServiceHandler.GetModelsDirectoryName(serviceName));

            return(Directory.Exists(serviceDirectoryPath) || Directory.Exists(modelsDirectoryPath));
        }
Пример #6
0
        public string GetDefaultedModelsHintPath()
        {
            string modelsHintPath = this.ModelsHintPath;

            if (string.IsNullOrWhiteSpace(modelsHintPath))
            {
                modelsHintPath = SalesforceConnectedServiceHandler.GetModelsDirectoryName(this.ServiceName);
            }

            return(modelsHintPath);
        }
Пример #7
0
        private static async Task AddAssemblyReferencesAsync(ConnectedServiceHandlerContext context, SalesforceConnectedServiceInstance salesforceInstance)
        {
            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_AddingAssemblyReferences);

            await SalesforceConnectedServiceHandler.AddAssemblyReferenceAsync(context, "System.Configuration");

            await SalesforceConnectedServiceHandler.AddAssemblyReferenceAsync(context, "System.Web");

            if (salesforceInstance.SelectedObjects.Any())
            {
                await SalesforceConnectedServiceHandler.AddAssemblyReferenceAsync(context, "System.ComponentModel.DataAnnotations");
            }
        }
Пример #8
0
 /// <summary>
 /// Returns a suffix for the generated artifacts which guarantees that they don't conflict with any
 /// existing artifacts in the project.
 /// </summary>
 private static string GetGeneratedArtifactSuffix(ConnectedServiceHandlerContext context, Project project, AuthenticationStrategy authStrategy)
 {
     using (XmlConfigHelper configHelper = context.CreateReadOnlyXmlConfigHelper())
     {
         return(GeneralUtilities.GetUniqueSuffix(suffix =>
         {
             string serviceName = SalesforceConnectedServiceHandler.GetServiceInstanceName(suffix);
             return configHelper.IsPrefixUsedInAppSettings(serviceName) ||
             (authStrategy == AuthenticationStrategy.WebServerFlow &&
              configHelper.IsHandlerNameUsed(Constants.OAuthRedirectHandlerNameFormat.FormatInvariantCulture(serviceName))) ||
             SalesforceConnectedServiceHandler.IsSuffixUsedInGeneratedFilesDirectories(context, serviceName, project);
         }));
     }
 }
        public override async Task <UpdateServiceInstanceResult> UpdateServiceInstanceAsync(ConnectedServiceHandlerContext context, CancellationToken ct)
        {
            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_UpdatingConnectedService);

            SalesforceConnectedServiceInstance salesforceInstance = (SalesforceConnectedServiceInstance)context.ServiceInstance;

            try
            {
                Project project = ProjectHelper.GetProjectFromHierarchy(context.ProjectHierarchy);

                await TaskScheduler.Default; // Switch to a worker thread to avoid blocking the UI thread (e.g. the progress dialog).

                // Update currently only supports adding additional scaffolded objects.  Before they are added, ensure the correct NuGets are
                // installed.  Additionally, ensure the required assemblies exist as not all are added initially if no objects are scaffolded.
                await this.AddNuGetPackagesAsync(context, project);

                await SalesforceConnectedServiceHandler.AddAssemblyReferencesAsync(context, salesforceInstance);

                try
                {
                    await SalesforceConnectedServiceHandler.AddGeneratedCodeAsync(context, project, salesforceInstance);
                }
                catch (COMException comException)
                {
                    if (comException.HResult == -2147467259)
                    {
                        // Provide a better exception message for when an invalid path ModelsHintPath was specified.
                        throw new InvalidOperationException(
                                  Resources.LogMessage_InvalidModelsHintPath.FormatCurrentCulture(salesforceInstance.DesignerData.ModelsHintPath),
                                  comException);
                    }
                }

                salesforceInstance.DesignerData.StoreExtendedDesignerData(context);

                salesforceInstance.TelemetryHelper.TrackUpdateServiceSucceededEvent(salesforceInstance);
                await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_UpdatedConnectedService);

                return(new UpdateServiceInstanceResult());
            }
            catch (Exception e)
            {
                salesforceInstance.TelemetryHelper.TrackUpdateServiceFailedEvent(salesforceInstance, e);
                throw;
            }
        }
        public override async Task <AddServiceInstanceResult> AddServiceInstanceAsync(ConnectedServiceHandlerContext context, CancellationToken ct)
        {
            await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_AddingConnectedService);

            SalesforceConnectedServiceInstance salesforceInstance = (SalesforceConnectedServiceInstance)context.ServiceInstance;

            try
            {
                Project project = ProjectHelper.GetProjectFromHierarchy(context.ProjectHierarchy);
                string  generatedArtifactSuffix = SalesforceConnectedServiceHandler.GetGeneratedArtifactSuffix(
                    context, project, salesforceInstance.RuntimeAuthentication.AuthStrategy);
                salesforceInstance.DesignerData.ServiceName = SalesforceConnectedServiceHandler.GetServiceInstanceName(generatedArtifactSuffix);

                await TaskScheduler.Default; // Switch to a worker thread to avoid blocking the UI thread (e.g. the progress dialog).

                await SalesforceConnectedServiceHandler.CreateConnectedAppAsync(context, project, salesforceInstance);

                await SalesforceConnectedServiceHandler.UpdateConfigFileAsync(context, project, salesforceInstance);

                await this.AddNuGetPackagesAsync(context, project);

                await SalesforceConnectedServiceHandler.AddAssemblyReferencesAsync(context, salesforceInstance);

                await SalesforceConnectedServiceHandler.AddGeneratedCodeAsync(context, project, salesforceInstance);

                salesforceInstance.DesignerData.StoreExtendedDesignerData(context);

                salesforceInstance.TelemetryHelper.TrackAddServiceSucceededEvent(salesforceInstance);
                await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, Resources.LogMessage_AddedConnectedService);

                return(new AddServiceInstanceResult(
                           salesforceInstance.DesignerData.ServiceName,
                           new Uri(Constants.NextStepsUrl)));
            }
            catch (Exception e)
            {
                salesforceInstance.TelemetryHelper.TrackAddServiceFailedEvent(salesforceInstance, e);
                throw;
            }
        }
Пример #11
0
        private async Task AddNuGetPackagesAsync(ConnectedServiceHandlerContext context, Project project)
        {
            IEnumerable <IVsPackageMetadata> installedPackages = this.PackageInstallerServices.GetInstalledPackages(project);
            Dictionary <string, string>      packagesToInstall = new Dictionary <string, string>();

            foreach (Tuple <string, Version> requiredPackage in SalesforceConnectedServiceHandler.requiredPackages)
            {
                IVsPackageMetadata installedPackage = installedPackages.FirstOrDefault(p => p.Id == requiredPackage.Item1);
                if (installedPackage == null)
                {
                    // The package does not exist - notify and install the package.
                    await context.Logger.WriteMessageAsync(
                        LoggerMessageCategory.Information,
                        Resources.LogMessage_AddingNuGetPackage,
                        requiredPackage.Item1,
                        requiredPackage.Item2.ToString());
                }
                else
                {
                    Version installedVersion = SalesforceConnectedServiceHandler.GetNuGetPackageVersion(installedPackage);
                    if (installedVersion == null)
                    {
                        // Unable to parse the version - continue.
                        continue;
                    }
                    else if (installedVersion.Major < requiredPackage.Item2.Major)
                    {
                        // An older potentially non-compatible version of the package already exists - warn and upgrade the package.
                        await context.Logger.WriteMessageAsync(
                            LoggerMessageCategory.Warning,
                            Resources.LogMessage_OlderMajorVersionNuGetPackageExists,
                            requiredPackage.Item1,
                            installedPackage.VersionString,
                            requiredPackage.Item2.ToString());
                    }
                    else if (installedVersion.Major > requiredPackage.Item2.Major)
                    {
                        // A newer potentially non-compatible version of the package already exists - warn and continue.
                        await context.Logger.WriteMessageAsync(
                            LoggerMessageCategory.Warning,
                            Resources.LogMessage_NewerMajorVersionNuGetPackageExists,
                            requiredPackage.Item1,
                            requiredPackage.Item2.ToString(),
                            installedPackage.VersionString);

                        continue;
                    }
                    else if (installedVersion >= requiredPackage.Item2)
                    {
                        // A newer semantically compatible version of the package already exists - continue.
                        continue;
                    }
                    else
                    {
                        // An older semantically compatible version of the package exists - notify and upgrade the package.
                        await context.Logger.WriteMessageAsync(
                            LoggerMessageCategory.Information,
                            Resources.LogMessage_UpgradingNuGetPackage,
                            requiredPackage.Item1,
                            installedPackage.VersionString,
                            requiredPackage.Item2.ToString());
                    }
                }

                packagesToInstall.Add(requiredPackage.Item1, requiredPackage.Item2.ToString());
            }

            if (packagesToInstall.Any())
            {
                this.PackageInstaller.InstallPackagesFromVSExtensionRepository(
                    "Salesforce.VisualStudio.Services.B161EEC7-17E0-4C35-AF8D-F241325BDD1C",
                    false,
                    false,
                    project,
                    packagesToInstall);
            }
        }