mirror of https://github.com/abpframework/abp.git
committed by
GitHub
6 changed files with 204 additions and 3 deletions
@ -0,0 +1,76 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Text.RegularExpressions; |
|||
using Newtonsoft.Json; |
|||
using Newtonsoft.Json.Linq; |
|||
using Volo.Abp.Cli.ProjectBuilding.Files; |
|||
using Volo.Abp.Text; |
|||
|
|||
namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps |
|||
{ |
|||
public class ConnectionStringChangeStep : ProjectBuildPipelineStep |
|||
{ |
|||
private const string DefaultConnectionStringKey = "Default"; |
|||
|
|||
public override void Execute(ProjectBuildContext context) |
|||
{ |
|||
var appSettingsJsonFiles = context.Files.Where(f => |
|||
f.Name.EndsWith("appsettings.json", StringComparison.OrdinalIgnoreCase)) |
|||
.ToArray(); |
|||
|
|||
if (!appSettingsJsonFiles.Any()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var newConnectionString = $"\"{DefaultConnectionStringKey}: \"{context.BuildArgs.ConnectionString}\""; |
|||
|
|||
foreach (var appSettingsJson in appSettingsJsonFiles) |
|||
{ |
|||
try |
|||
{ |
|||
var appSettingJsonContentWithoutBom = StringHelper.ConvertFromBytesWithoutBom(appSettingsJson.Bytes); |
|||
var jsonObject = JObject.Parse(appSettingJsonContentWithoutBom); |
|||
|
|||
var connectionStringContainer = (JContainer)jsonObject?["ConnectionStrings"]; |
|||
if (connectionStringContainer == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
if (!connectionStringContainer.Any()) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
var connectionStrings = connectionStringContainer.ToList(); |
|||
|
|||
foreach (var connectionString in connectionStrings) |
|||
{ |
|||
var property = ((JProperty)connectionString); |
|||
var connectionStringName = property.Name; |
|||
|
|||
if (connectionStringName == DefaultConnectionStringKey) |
|||
{ |
|||
var defaultConnectionString = property.ToString(); |
|||
if (defaultConnectionString == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
appSettingsJson.ReplaceText(defaultConnectionString, newConnectionString); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
Console.WriteLine("Cannot change the connection string in " + appSettingsJson.Name + ". Error: " + ex.Message); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
using System.Text; |
|||
|
|||
namespace Volo.Abp.Text |
|||
{ |
|||
public class StringHelper |
|||
{ |
|||
/// <summary>
|
|||
/// Converts a byte[] to string without BOM (byte order mark).
|
|||
/// </summary>
|
|||
/// <param name="bytes">The byte[] to be converted to string</param>
|
|||
/// <param name="encoding">The encoding to get string. Default is UTF8</param>
|
|||
/// <returns></returns>
|
|||
public static string ConvertFromBytesWithoutBom(byte[] bytes, Encoding encoding = null) |
|||
{ |
|||
if (bytes == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
if (encoding == null) |
|||
{ |
|||
encoding = Encoding.UTF8; |
|||
} |
|||
|
|||
var hasBom = bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF; |
|||
|
|||
if (hasBom) |
|||
{ |
|||
return encoding.GetString(bytes, 3, bytes.Length - 3); |
|||
} |
|||
else |
|||
{ |
|||
return encoding.GetString(bytes); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue