NuDeploy.Core.Services.Transformation.ConfigurationFileTransformationService.TransformConfigurationFiles C# (CSharp) Méthode

TransformConfigurationFiles() public méthode

public TransformConfigurationFiles ( string baseDirectoryPath, string transformationProfileNames ) : IServiceResult
baseDirectoryPath string
transformationProfileNames string
Résultat IServiceResult
        public IServiceResult TransformConfigurationFiles(string baseDirectoryPath, string[] transformationProfileNames)
        {
            if (string.IsNullOrWhiteSpace(baseDirectoryPath))
            {
                throw new ArgumentException("baseDirectoryPath");
            }

            if (transformationProfileNames == null)
            {
                throw new ArgumentNullException("transformationProfileNames");
            }

            if (transformationProfileNames.Length == 0)
            {
                return new SuccessResult(Resources.ConfigurationFileTransformationService.NoTransformationProfilesSupplied);
            }

            // get all config files in the content folder
            string contentFolder = Path.Combine(baseDirectoryPath, PackageContentFolderName);

            var configurationFiles =
                this.filesystemAccessor.GetAllFiles(contentFolder).Where(
                    file => SupportedBaseConfigFile.Contains(file.Name.ToLower())||
                    file.Name.EndsWith(ApplicationConfigurationFilenameSuffix, StringComparison.OrdinalIgnoreCase));

            // transform config files
            foreach (var configurationFile in configurationFiles)
            {
                Console.WriteLine("Processing Sourcefile");
                string sourceFileFolder = configurationFile.Directory.FullName;
                string sourceFileName = configurationFile.Name;
                string sourceFilePath = configurationFile.FullName;
                //string configFileType = sourceFileName.Replace().Substring(0, sourceFileName.IndexOf('.'));

                var cutExtension = new Regex(".exe.config|.config", RegexOptions.IgnoreCase);
                string configFileType = cutExtension.Replace(sourceFileName, String.Empty);

                foreach (var systemSettingTransformationProfileName in transformationProfileNames)
                {
                    // assemble file names
                    string transformationFilename = sourceFileName.Replace(
                        ConfigurationFileExtension, string.Format(".{0}{1}", systemSettingTransformationProfileName, ConfigurationFileExtension));

                    string transformationFilePath = Path.Combine(sourceFileFolder, transformationFilename);

                    if (this.filesystemAccessor.FileExists(transformationFilePath))
                    {
                        // transform
                        IServiceResult transformationResult = this.configurationFileTransformer.Transform(sourceFilePath, transformationFilePath, destinationFilePath: sourceFilePath);
                        if (transformationResult.Status == ServiceResultType.Failure)
                        {
                            return new FailureResult(
                                Resources.ConfigurationFileTransformationService.TransformationFailedForProfileMessageTemplate,
                                systemSettingTransformationProfileName)
                            {
                                InnerResult = transformationResult
                            };
                        }
                    }
                }

                // cleanup
                var transformationFiles =
                    this.filesystemAccessor.GetFiles(sourceFileFolder).Where(
                        file =>
                            file.Extension.Equals(ConfigurationFileExtension, StringComparison.OrdinalIgnoreCase) &&
                            file.Name.StartsWith(configFileType, StringComparison.OrdinalIgnoreCase) &&
                            !file.Name.Equals(sourceFileName, StringComparison.OrdinalIgnoreCase));

                foreach (var transformationFile in transformationFiles)
                {
                    var filePath = transformationFile.FullName;

                    if (!this.filesystemAccessor.DeleteFile(filePath))
                    {
                        return new FailureResult(Resources.ConfigurationFileTransformationService.CleanupFailedForFileMessageTemplate, filePath);
                    }
                }
            }

            return new SuccessResult(
                Resources.ConfigurationFileTransformationService.SuccessMessageTemplate, baseDirectoryPath, string.Join(", ", transformationProfileNames));
        }

Usage Example

        public void TransformConfigurationFiles_BaseDirectoryPathParameterIsInvalid_ArgumentExceptionIsThrown(string baseDirectoryPath)
        {
            // Arrange
            var transformationProfileNames = new string[] { };

            var filesystemAccessor = new Mock<IFilesystemAccessor>();
            var configurationFileTransformer = new Mock<IConfigurationFileTransformer>();

            var configurationFileTransformationService = new ConfigurationFileTransformationService(
                filesystemAccessor.Object, configurationFileTransformer.Object);

            // Act
            configurationFileTransformationService.TransformConfigurationFiles(baseDirectoryPath, transformationProfileNames);
        }
All Usage Examples Of NuDeploy.Core.Services.Transformation.ConfigurationFileTransformationService::TransformConfigurationFiles