Esempio n. 1
0
        /// <summary>
        /// Creates a new instance of <see cref="DesignConfigurationSource"/> based on <paramref name="rootSource"/> and <paramref name="filePath"/>.
        /// </summary>
        /// <param name="rootSource">The source that was used to open the main conifguration file.</param>
        /// <param name="filePath">An absolute of relative path to the file to which the source should be created.</param>
        /// <returns>A new instance of <see cref="DesignConfigurationSource"/>.</returns>
        public static IDesignConfigurationSource CreateDesignSource(IDesignConfigurationSource rootSource, string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException(Resources.ExceptionStringNullOrEmpty);
            }

            DesignConfigurationSource rootSourceAsDesignSource = rootSource as DesignConfigurationSource;

            if (rootSourceAsDesignSource == null)
            {
                throw new ArgumentException(Resources.CannotCreateDesignSource, "rootSource");
            }

            {
                string mainConfigurationFileDirectory =
                    Path.GetDirectoryName(rootSourceAsDesignSource.ConfigurationFilePath);

                string fullFilePath = Path.Combine(mainConfigurationFileDirectory, filePath);

                if (!File.Exists(fullFilePath))
                {
                    File.WriteAllText(fullFilePath, @"<configuration />");
                }

                return(new DesignConfigurationSource(fullFilePath));
            }
        }
        protected override void Arrange()
        {
            base.Arrange();

            targetFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "manageableSource.config");
            File.Create(targetFilePath).Dispose();
            File.WriteAllText(targetFilePath, "<configuration/>");

            var source = new DesignConfigurationSource(targetFilePath);

            var section =
                new ConfigurationSourceSection
                {
                    SelectedSource = "manageable",
                    Sources =
                    {
                        new ManageableConfigurationSourceElement
                        {
                            Name = "manageable",
                            FilePath = "file.config",
                        }
                    }
                };

            source.AddLocalSection(ConfigurationSourceSection.SectionName, section);


            var sourceModel = this.Container.Resolve<ConfigurationSourceModel>();
            sourceModel.Load(source);

            SectionViewModel =
                sourceModel.Sections.Where(x => x.ConfigurationType == typeof(ConfigurationSourceSection)).Single();

            ConfigurationSourceViewModel = SectionViewModel.GetDescendentsOfType<ManageableConfigurationSourceElement>().FirstOrDefault();
        }
 protected bool FileContainsSection(string file, string sectionName)
 {
     using (var configSource = new DesignConfigurationSource(file))
     {
         var section = configSource.GetLocalSection(sectionName);
         return section != null;
     }
 }
        protected override void Arrange()
        {
            base.Arrange();

            Mock<AssemblyLocator> assemblyLocator = new Mock<AssemblyLocator>();
            assemblyLocator.Setup(x => x.Assemblies).Returns(new Assembly[] { typeof(ManageableConfigurationSourceElement).Assembly });
            this.Container.RegisterType<ManageableConfigurationSourceViewModel>(
                new InjectionConstructor(
                    typeof(ElementCollectionViewModel),
                    typeof(ConfigurationElement),
                    new InjectionParameter<AssemblyLocator>(assemblyLocator.Object)));

            targetFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "manageableSourcetextnp.config");
            File.Create(targetFilePath).Dispose();
            File.WriteAllText(targetFilePath, "<configuration/>");

            var source = new DesignConfigurationSource(targetFilePath);

            ConfigurationSourceElement =
                new ManageableConfigurationSourceElement
                {
                    Name = "manageable",
                    FilePath = "file.config",
                    ConfigurationManageabilityProviders = 
                    {
                        new ConfigurationSectionManageabilityProviderData
                        {
                            Name = "provider",
                            TypeName = typeof(object).AssemblyQualifiedName
                        }
                    }
                };
            var section =
                new ConfigurationSourceSection
                {
                    SelectedSource = "manageable",
                    Sources =
                    {
                        ConfigurationSourceElement
                    }
                };

            source.AddLocalSection(ConfigurationSourceSection.SectionName, section);


            var sourceModel = this.Container.Resolve<ConfigurationSourceModel>();
            sourceModel.Load(source);

            SectionViewModel =
                sourceModel.Sections.Where(x => x.ConfigurationType == typeof(ConfigurationSourceSection)).Single();

            ConfigurationSourceViewModel = SectionViewModel.GetDescendentsOfType<ManageableConfigurationSourceElement>().FirstOrDefault();
            ConfigurationSourceElement = (ManageableConfigurationSourceElement)ConfigurationSourceViewModel.ConfigurationElement;
        }
        protected override void Arrange()
        {
            base.Arrange();

            var resourceHelper = new ResourceHelper<ConfigFiles.ConfigFileLocator>();
            mainFilePath = resourceHelper.DumpResourceFileToDisk("empty.config", "ds_inv_rel_path");
            mainConfigurationSource = new DesignConfigurationSource(mainFilePath);

            configurationSourceElement = new FileConfigurationSourceElement("relativefile", "doesnt-exist\\relative.config");

            var mainFileDirectory = Path.GetDirectoryName(mainFilePath);
            expectedFilePath = Path.Combine(mainFileDirectory, configurationSourceElement.FilePath);
        }
        protected override void Arrange()
        {
            base.Arrange();

            var resourceHelper = new ResourceHelper<ConfigFiles.ConfigFileLocator>();
            mainFilePath = resourceHelper.DumpResourceFileToDisk("empty.config", "ds_abs_ro_path");
            mainConfigurationSource = new DesignConfigurationSource(mainFilePath);

            var mainFileDirectory = Path.GetDirectoryName(mainFilePath);
            expectedFilePath = Path.Combine(mainFileDirectory, "absolutefile.config");

            configurationSourceElement = new FileConfigurationSourceElement("absolutefile", expectedFilePath);
            File.WriteAllText(expectedFilePath, "<configuration/>");
            File.SetAttributes(expectedFilePath, FileAttributes.ReadOnly);
        }
        protected override void Arrange()
        {
            base.Arrange();

            var resources = new ResourceHelper<ConfigFileLocator>();
            resources.DumpResourceFileToDisk("configurationsource_main_with_missing.config");

            designSource = new DesignConfigurationSource("configurationsource_main_with_missing.config");
            configurationSourceModel = Container.Resolve<ConfigurationSourceModel>();

            UIServiceMock.Setup(x => x.ShowWindow(It.IsAny<Window>()));
            UIServiceMock.Setup(
                x => x.ShowMessageWpf(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButton>()))
                .Returns(MessageBoxResult.OK).Verifiable("Message not invoked");
        }
        protected override void Arrange()
        {
            base.Arrange();
            base.Arrange();

            var resourceHelper = new ResourceHelper<ConfigFiles.ConfigFileLocator>();
            mainFilePath = resourceHelper.DumpResourceFileToDisk("empty.config", "ds_mgmt_subdir_path");
            mainConfigurationSource = new DesignConfigurationSource(mainFilePath);

            configurationSourceElement = new ManageableConfigurationSourceElement("relativefile", "subdir\\relative.config", "app");

            var mainFileDirectory = Path.GetDirectoryName(mainFilePath);
            expectedFilePath = Path.Combine(mainFileDirectory, configurationSourceElement.FilePath);

            Directory.CreateDirectory(Path.GetDirectoryName(expectedFilePath));
        }
        protected override void Arrange()
        {
            base.Arrange();

            var resources = new ResourceHelper<ConfigFileLocator>();
            resources.DumpResourceFileToDisk("configurationsource_main.config");
            resources.DumpResourceFileToDisk("configurationsource_satellite.config");

            var mainConfigFilePath = Path.Combine(Environment.CurrentDirectory, "configurationsource_main.config");
            designSource = new DesignConfigurationSource(mainConfigFilePath);

            configurationSourceModel = Container.Resolve<ConfigurationSourceModel>();

        }
 protected void SaveConfigurationFile()
 {
     File.WriteAllLines(MainConfigurationSourcePath, new[] { @"<configuration />" });
     DesignConfigurationSource outputSource = new DesignConfigurationSource(MainConfigurationSourcePath);
     ConfigurationSource.Save(outputSource);
 }
        public bool SaveMainConfiguration(string fileName, bool saveAs)
        {
            try
            {
                if (!sourceModel.IsValidForSave() || !EnvironmentsCanSave())
                {
                    uiService.ShowError(Resources.SaveConfigurationInvalidError);
                    return false;
                }

                if (String.IsNullOrEmpty(fileName))
                {
                    return SaveAs();
                }

                if (!EnsureCanSaveConfigurationFile(fileName))
                {
                    return false;
                }

                if (saveAs && !string.IsNullOrEmpty(this.configurationFileName) && File.Exists(this.configurationFileName))
                {
                    File.Copy(this.configurationFileName, fileName, true);

                    // make the target file writable (it may have inherited the read-only attribute from the original file)
                    FileAttributes attributes = File.GetAttributes(fileName);
                    File.SetAttributes(fileName, attributes & ~FileAttributes.ReadOnly);
                }


                using (var configurationSource = new DesignConfigurationSource(fileName))
                {
                    var saved = sourceModel.Save(configurationSource);
                    if (!saved)
                    {
                        return false;
                    }
                }
                ConfigurationFilePath = fileName;
                return true;

            }
            catch (Exception ex)
            {
                ConfigurationLogWriter.LogException(ex);
                uiService.ShowMessageWpf(string.Format(CultureInfo.CurrentCulture, Resources.SaveApplicationErrorMessage, ex.Message),
                            Resources.SaveApplicationErrorMessageTitle, MessageBoxButton.OK);

                return false;
            }
        }
 /// <summary>
 /// Loads a new <see cref="ConfigurationSourceModel"/> from <paramref name="configurationFilePath"/>.
 /// </summary>
 /// <param name="configurationFilePath">The path to the configuration file that should be loaded.</param>
 public void Load(string configurationFilePath)
 {
     using (var source = new DesignConfigurationSource(configurationFilePath))
     {
         if (sourceModel.Load(source))
         {
             ConfigurationFilePath = configurationFilePath;
             IsDirty = false;
         }
         else
         {
             sourceModel.New();
         }
     }
 }
        protected override void Arrange()
        {
            base.Arrange();

            var resources = new ResourceHelper<ConfigFileLocator>();
            resources.DumpResourceFileToDisk("configurationsource_with_invalid_satellite.config");
            resources.DumpResourceFileToDisk("configurationsource_invalid_satellite.config");

            designSource = new DesignConfigurationSource("configurationsource_with_invalid_satellite.config");
            configurationSourceModel = Container.Resolve<ConfigurationSourceModel>();

            UIServiceMock.Setup(x => x.ShowWindow(It.IsAny<Window>()));
            UIServiceMock.Setup(
                x => x.ShowError(It.IsAny<Exception>(), It.IsAny<string>())).Verifiable();
        }