Ejemplo n.º 1
0
        private ProvisioningTemplateToolsConfiguration GetProvisioningTemplateToolsConfiguration(string projectFolderPath, bool createIfNotExists = false)
        {
            ProvisioningTemplateToolsConfiguration config = null;
            ProvisioningCredentials creds = null;

            var configFileCredsPath = Path.Combine(projectFolderPath, Resources.FileNameProvisioningUserCreds);
            var configFilePath      = Path.Combine(projectFolderPath, Resources.FileNameProvisioningTemplate);
            var pnpTemplateFilePath = Path.Combine(projectFolderPath, Resources.DefaultFileNamePnPTemplate);

            try
            {
                //get the config from file
                config = Helpers.ProjectHelpers.GetConfigFile <ProvisioningTemplateToolsConfiguration>(configFilePath);
                creds  = Helpers.ProjectHelpers.GetConfigFile <ProvisioningCredentials>(configFileCredsPath, false);

                if (config == null && createIfNotExists)
                {
                    var resourcesFolder = Resources.DefaultResourcesRelativePath;
                    EnsureResourcesFolder(projectFolderPath, resourcesFolder);
                    config = ProvisioningHelper.GenerateDefaultProvisioningConfig(Resources.DefaultFileNamePnPTemplate, resourcesFolder);
                }

                config.EnsureInitialState();

                //set the deserialized creds
                if (creds != null)
                {
                    config.Deployment.Credentials = creds;
                }

                //set paths
                config.FilePath    = configFilePath;
                config.ProjectPath = projectFolderPath;
                config.Deployment.Credentials.FilePath = configFileCredsPath;

                //ensure a default template exists if createIfNotExists is true
                if (config.Templates.Count == 0 && createIfNotExists)
                {
                    var resourcesFolder = Resources.DefaultResourcesRelativePath;
                    EnsureResourcesFolder(projectFolderPath, resourcesFolder);
                    var tempConfig = ProvisioningHelper.GenerateDefaultProvisioningConfig(Resources.DefaultFileNamePnPTemplate, resourcesFolder);
                    config.Templates = tempConfig.Templates;
                    XmlHelpers.SerializeObject(config, configFilePath);

                    //ensure pnp template files
                    foreach (var t in config.Templates)
                    {
                        string templatePath = System.IO.Path.Combine(projectFolderPath, t.Path);
                        if (!System.IO.File.Exists(templatePath))
                        {
                            string resourcesPath = System.IO.Path.Combine(projectFolderPath, Resources.DefaultResourcesRelativePath);
                            ProvisioningHelper.GenerateDefaultPnPTemplate(resourcesPath, templatePath);
                            AddTemplateToProject(templatePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ShowOutputPane();
                LogService.Exception("Error in GetProvisioningTemplateToolsConfiguration", ex);
            }

            return(config);
        }