public ConfigurationItems GetConfigurationItems()
        {
            string json = File.ReadAllText(_fileFullPath, Encoding.UTF8);

            ConfigurationItems config;

            try
            {
                config = JsonConfigurationSerializer.Deserialize(json);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          $"Could not read JSON configuration from file path '{_fileFullPath}' and JSON '{json}'", ex);
            }

            return(config);
        }
Exemplo n.º 2
0
        public static string SetVersionFile(
            [NotNull] InstalledPackage installedPackage,
            [NotNull] DirectoryInfo targetDirectoryInfo,
            [NotNull] DeploymentExecutionDefinition deploymentExecutionDefinition,
            [NotNull] IEnumerable <string> xmlTransformedFiles,
            [NotNull] IEnumerable <string> replacedFiles,
            [NotNull] EnvironmentPackageResult environmentPackageResult,
            ILogger logger)
        {
            if (installedPackage == null)
            {
                throw new ArgumentNullException(nameof(installedPackage));
            }

            if (targetDirectoryInfo == null)
            {
                throw new ArgumentNullException(nameof(targetDirectoryInfo));
            }

            if (deploymentExecutionDefinition == null)
            {
                throw new ArgumentNullException(nameof(deploymentExecutionDefinition));
            }

            if (xmlTransformedFiles == null)
            {
                throw new ArgumentNullException(nameof(xmlTransformedFiles));
            }

            if (replacedFiles == null)
            {
                throw new ArgumentNullException(nameof(replacedFiles));
            }

            if (environmentPackageResult == null)
            {
                throw new ArgumentNullException(nameof(environmentPackageResult));
            }

            string applicationMetadataJsonFilePath = Path.Combine(targetDirectoryInfo.FullName,
                                                                  ConfigurationKeys.ApplicationMetadataFileName);

            var existingKeys = new List <KeyValue>();

            if (File.Exists(applicationMetadataJsonFilePath))
            {
                logger?.Debug("Appending existing metadata file {Path}", applicationMetadataJsonFilePath);

                string json = File.ReadAllText(applicationMetadataJsonFilePath, Encoding.UTF8);

                ConfigurationItems configurationItems = JsonConfigurationSerializer.Deserialize(json);

                if (!configurationItems.Keys.IsDefaultOrEmpty)
                {
                    existingKeys.AddRange(configurationItems.Keys);
                }
            }

            var version = new KeyValue(ConfigurationKeys.SemVer2Normalized,
                                       installedPackage.Version.ToNormalizedString(),
                                       null);

            var packageId = new KeyValue(ConfigurationKeys.PackageId,
                                         installedPackage.PackageId,
                                         null);

            var deployStartTimeUtc = new KeyValue(
                ConfigurationKeys.DeployStartTimeUtc,
                DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture),
                null);

            var deployedFromMachine = new KeyValue(
                ConfigurationKeys.DeployerDeployedFromMachine,
                Environment.MachineName,
                null);

            var deployerAssemblyVersion = new KeyValue(
                ConfigurationKeys.DeployerAssemblyVersion,
                GetAssemblyVersion(),
                null);

            var deployerAssemblyFileVersion = new KeyValue(
                ConfigurationKeys.DeployerAssemblyFileVersion,
                GetAssemblyFileVersion(),
                null);

            var environmentConfiguration = new KeyValue(
                ConfigurationKeys.DeployerEnvironmentConfiguration,
                deploymentExecutionDefinition.EnvironmentConfig,
                null);

            var keys = new List <KeyValue>(existingKeys)
            {
                version,
                deployStartTimeUtc,
                deployerAssemblyVersion,
                deployerAssemblyFileVersion,
                packageId
            }.ToImmutableArray();

            if (environmentPackageResult.Version != null)
            {
                keys.Add(environmentConfiguration);
            }

            string serialized = JsonConfigurationSerializer.Serialize(new ConfigurationItems("1.0", keys));

            File.WriteAllText(applicationMetadataJsonFilePath, serialized, Encoding.UTF8);

            logger?.Debug("Metadata file update {Path}", applicationMetadataJsonFilePath);

            return(applicationMetadataJsonFilePath);
        }