Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestConfigPersistenceHelper.cs
Kevin Cunnane 7477642854 TSQL Formatter Service (#229)
- TSqlFormatterService with support for formatting document and text range inside document
- Settings support for all formatting options.
- Extensibility support so that the service can be initialized using MEF extensibility, and can find all necessary TSqlFormatters using the same process

Fix Initialize request error on startup
- Messages were being read from the input channel before all request handlers were registered
- In particular, the Initialize request which is key for any server to talk to the client was getting lost because the message reader thread begins consuming, and we take an extra few hundred milliseconds due to MEF startup before we register the handler
- The solution is to initialize the message handler so request handlers can register, but not actually start processing incoming messages until all handers are ready. This is a safer way to go and should improve reliability overall

Improvements from internal prototype:
- Normalizing baselines to handle the line ending differences on Mac & Linux vs. Windows
- Significantly shortened most lines by implementing base class methods to wrap common objects from Visitor.Context and removing unnecessary "this." syntax
- Refactored the SqlCommonTableExpressionFormatter and related classes to reduce code count significantly. This provides a pattern to follow when refactoring other classes for similar clarity. It's likely a lot of common logic could be found and reused across these.
- Reduced overall code size by adding utility methods
2017-02-14 23:40:17 -08:00

159 lines
6.4 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
{
public sealed class TestConfigPersistenceHelper
{
private static string DefaultSettingFileName = Path.Combine(FileUtils.UserRootFolder, "sqlConnectionSettings.json");
private static TestCredentialService credentialService = TestCredentialService.Instance;
public static bool Write(IEnumerable<InstanceInfo> instances)
{
try
{
ConnectionSetting connectionSetting = new Common.ConnectionSetting()
{
Connections = new List<InstanceInfo>(instances)
};
//Remove the passwords and store in credential store and then store the copy without passwords in the file
foreach (var instance in connectionSetting.Connections)
{
if (!string.IsNullOrEmpty(instance.Password))
{
if (!credentialService.SaveCredential(instance))
{
Console.WriteLine("Failed to store the password for server: " + instance.ServerName);
}
instance.Password = null; //Make sure the password is not stored in sqlConnectionSettings.json
instance.AuthenticationType = AuthenticationType.SqlLogin;
}
else
{
instance.AuthenticationType = AuthenticationType.Integrated;
}
}
Console.WriteLine("The SQL connection instances will be written to " + DefaultSettingFileName);
string jsonContent = JsonConvert.SerializeObject(connectionSetting);
if (File.Exists(DefaultSettingFileName))
{
Console.WriteLine("The file " + DefaultSettingFileName + " already exists and it will be overwritten.");
}
File.WriteAllText(DefaultSettingFileName, jsonContent);
Environment.SetEnvironmentVariable(Constants.SqlConectionSettingsEnvironmentVariable, DefaultSettingFileName);
return true;
}
catch (Exception ex)
{
Console.WriteLine("Failed to store the instances.", ex);
return false;
}
}
internal static IEnumerable<TestServerIdentity> InitTestServerNames()
{
try
{
string testServerNamesFileContent = GetTestServerNamesFileContent();
if (!string.IsNullOrEmpty(testServerNamesFileContent))
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<IList<TestServerIdentity>>(testServerNamesFileContent);
}
else
{
return Enumerable.Empty<TestServerIdentity>();
}
}
catch (Exception)
{
return Enumerable.Empty<TestServerIdentity>();
}
}
internal static ConnectionSetting InitSetting()
{
try
{
string settingsFileContents = GetSettingFileContent();
ConnectionSetting setting = Newtonsoft.Json.JsonConvert.DeserializeObject<ConnectionSetting>(settingsFileContents);
Console.WriteLine("Connection Settings loaded successfully");
return setting;
}
catch (Exception ex)
{
Console.WriteLine("Failed to load the connection settings. error: " + ex.Message);
return new ConnectionSetting();
}
}
/// <summary>
/// Get the location of testServerNames.json. Returns the value of environment variable 'SettingsFileName' and if it's empty returns
/// the location of vs code testServerNames.json
/// </summary>
/// <returns></returns>
private static string GetTestServerNamesFileContent()
{
var testServerNameFilePath = Environment.GetEnvironmentVariable("TestServerNamesFile");
if (string.IsNullOrEmpty(testServerNameFilePath))
{
testServerNameFilePath = FileUtils.TestServerNamesDefaultFileName;
}
string testServerNamesFileContent = string.IsNullOrEmpty(testServerNameFilePath) ? string.Empty : File.ReadAllText(testServerNameFilePath);
return testServerNamesFileContent;
}
/// <summary>
/// Get the location of setting.json. Returns the value of environment variable 'SettingsFileName' and if it's empty returns
/// the location of vs code settings.json
/// </summary>
/// <returns></returns>
private static string GetSettingFileContent()
{
var settingsFileName = Environment.GetEnvironmentVariable(Constants.SqlConectionSettingsEnvironmentVariable);
if (string.IsNullOrEmpty(settingsFileName))
{
if (File.Exists(DefaultSettingFileName))
{
settingsFileName = DefaultSettingFileName;
Console.WriteLine(DefaultSettingFileName + " SQL connection instances are not configured. Will try to get connections from VS code settings.json");
}
else
{
//If the SQL connection settings is not set use the VS code one
settingsFileName = FileUtils.VsCodeSettingsFileName;
}
}
if (string.IsNullOrEmpty(settingsFileName))
{
Console.WriteLine("SQL connection instances are not configured. Run dotnet run Microsoft.SqlTools.ServiceLayer.TestEnvConfig from the command line to configure");
}
else
{
Console.WriteLine("SQL Connection settings are loaded from: " + settingsFileName);
}
string settingsFileContents = string.IsNullOrEmpty(settingsFileName) ? string.Empty : File.ReadAllText(settingsFileName);
return settingsFileContents;
}
}
}