mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Initial commit of SqlTools Service API
This commit is contained in:
159
ServiceHost/Session/EditorSession.cs
Normal file
159
ServiceHost/Session/EditorSession.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
// using Microsoft.PowerShell.EditorServices.Console;
|
||||
// using Microsoft.PowerShell.EditorServices.Extensions;
|
||||
using Microsoft.PowerShell.EditorServices.Session;
|
||||
// using Microsoft.PowerShell.EditorServices.Utility;
|
||||
// using System.IO;
|
||||
|
||||
namespace Microsoft.PowerShell.EditorServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages a single session for all editor services. This
|
||||
/// includes managing all open script files for the session.
|
||||
/// </summary>
|
||||
public class EditorSession
|
||||
{
|
||||
public void StartSession(HostDetails hostDetails, ProfilePaths profilePaths)
|
||||
{
|
||||
}
|
||||
#if false
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Workspace instance for this session.
|
||||
/// </summary>
|
||||
public Workspace Workspace { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the PowerShellContext instance for this session.
|
||||
/// </summary>
|
||||
public PowerShellContext PowerShellContext { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the LanguageService instance for this session.
|
||||
/// </summary>
|
||||
public LanguageService LanguageService { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the AnalysisService instance for this session.
|
||||
/// </summary>
|
||||
public AnalysisService AnalysisService { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DebugService instance for this session.
|
||||
/// </summary>
|
||||
public DebugService DebugService { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ConsoleService instance for this session.
|
||||
/// </summary>
|
||||
public ConsoleService ConsoleService { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ExtensionService instance for this session.
|
||||
/// </summary>
|
||||
public ExtensionService ExtensionService { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Starts the session using the provided IConsoleHost implementation
|
||||
/// for the ConsoleService.
|
||||
/// </summary>
|
||||
/// <param name="hostDetails">
|
||||
/// Provides details about the host application.
|
||||
/// </param>
|
||||
/// <param name="profilePaths">
|
||||
/// An object containing the profile paths for the session.
|
||||
/// </param>
|
||||
public void StartSession(HostDetails hostDetails, ProfilePaths profilePaths)
|
||||
{
|
||||
// Initialize all services
|
||||
this.PowerShellContext = new PowerShellContext(hostDetails, profilePaths);
|
||||
this.LanguageService = new LanguageService(this.PowerShellContext);
|
||||
this.DebugService = new DebugService(this.PowerShellContext);
|
||||
this.ConsoleService = new ConsoleService(this.PowerShellContext);
|
||||
this.ExtensionService = new ExtensionService(this.PowerShellContext);
|
||||
|
||||
this.InstantiateAnalysisService();
|
||||
|
||||
// Create a workspace to contain open files
|
||||
this.Workspace = new Workspace(this.PowerShellContext.PowerShellVersion);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restarts the AnalysisService so it can be configured with a new settings file.
|
||||
/// </summary>
|
||||
/// <param name="settingsPath">Path to the settings file.</param>
|
||||
public void RestartAnalysisService(string settingsPath)
|
||||
{
|
||||
this.AnalysisService?.Dispose();
|
||||
InstantiateAnalysisService(settingsPath);
|
||||
}
|
||||
|
||||
internal void InstantiateAnalysisService(string settingsPath = null)
|
||||
{
|
||||
// Only enable the AnalysisService if the machine has PowerShell
|
||||
// v5 installed. Script Analyzer works on earlier PowerShell
|
||||
// versions but our hard dependency on their binaries complicates
|
||||
// the deployment and assembly loading since we would have to
|
||||
// conditionally load the binaries for v3/v4 support. This problem
|
||||
// will be solved in the future by using Script Analyzer as a
|
||||
// module rather than an assembly dependency.
|
||||
if (this.PowerShellContext.PowerShellVersion.Major >= 5)
|
||||
{
|
||||
// AnalysisService will throw FileNotFoundException if
|
||||
// Script Analyzer binaries are not included.
|
||||
try
|
||||
{
|
||||
this.AnalysisService = new AnalysisService(this.PowerShellContext.ConsoleHost, settingsPath);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
Logger.Write(
|
||||
LogLevel.Warning,
|
||||
"Script Analyzer binaries not found, AnalysisService will be disabled.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Write(
|
||||
LogLevel.Normal,
|
||||
"Script Analyzer cannot be loaded due to unsupported PowerShell version " +
|
||||
this.PowerShellContext.PowerShellVersion.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Implementation
|
||||
|
||||
/// <summary>
|
||||
/// Disposes of any Runspaces that were created for the
|
||||
/// services used in this session.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (this.AnalysisService != null)
|
||||
{
|
||||
this.AnalysisService.Dispose();
|
||||
this.AnalysisService = null;
|
||||
}
|
||||
|
||||
if (this.PowerShellContext != null)
|
||||
{
|
||||
this.PowerShellContext.Dispose();
|
||||
this.PowerShellContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endif
|
||||
}
|
||||
}
|
||||
92
ServiceHost/Session/HostDetails.cs
Normal file
92
ServiceHost/Session/HostDetails.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.PowerShell.EditorServices.Session
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains details about the current host application (most
|
||||
/// likely the editor which is using the host process).
|
||||
/// </summary>
|
||||
public class HostDetails
|
||||
{
|
||||
#region Constants
|
||||
|
||||
/// <summary>
|
||||
/// The default host name for PowerShell Editor Services. Used
|
||||
/// if no host name is specified by the host application.
|
||||
/// </summary>
|
||||
public const string DefaultHostName = "PowerShell Editor Services Host";
|
||||
|
||||
/// <summary>
|
||||
/// The default host ID for PowerShell Editor Services. Used
|
||||
/// for the host-specific profile path if no host ID is specified.
|
||||
/// </summary>
|
||||
public const string DefaultHostProfileId = "Microsoft.PowerShellEditorServices";
|
||||
|
||||
/// <summary>
|
||||
/// The default host version for PowerShell Editor Services. If
|
||||
/// no version is specified by the host application, we use 0.0.0
|
||||
/// to indicate a lack of version.
|
||||
/// </summary>
|
||||
public static readonly Version DefaultHostVersion = new Version("0.0.0");
|
||||
|
||||
/// <summary>
|
||||
/// The default host details in a HostDetails object.
|
||||
/// </summary>
|
||||
public static readonly HostDetails Default = new HostDetails(null, null, null);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the host.
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the profile ID of the host, used to determine the
|
||||
/// host-specific profile path.
|
||||
/// </summary>
|
||||
public string ProfileId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version of the host.
|
||||
/// </summary>
|
||||
public Version Version { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the HostDetails class.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// The display name for the host, typically in the form of
|
||||
/// "[Application Name] Host".
|
||||
/// </param>
|
||||
/// <param name="profileId">
|
||||
/// The identifier of the PowerShell host to use for its profile path.
|
||||
/// loaded. Used to resolve a profile path of the form 'X_profile.ps1'
|
||||
/// where 'X' represents the value of hostProfileId. If null, a default
|
||||
/// will be used.
|
||||
/// </param>
|
||||
/// <param name="version">The host application's version.</param>
|
||||
public HostDetails(
|
||||
string name,
|
||||
string profileId,
|
||||
Version version)
|
||||
{
|
||||
this.Name = name ?? DefaultHostName;
|
||||
this.ProfileId = profileId ?? DefaultHostProfileId;
|
||||
this.Version = version ?? DefaultHostVersion;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
41
ServiceHost/Session/OutputType.cs
Normal file
41
ServiceHost/Session/OutputType.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
namespace Microsoft.PowerShell.EditorServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumerates the types of output lines that will be sent
|
||||
/// to an IConsoleHost implementation.
|
||||
/// </summary>
|
||||
public enum OutputType
|
||||
{
|
||||
/// <summary>
|
||||
/// A normal output line, usually written with the or Write-Host or
|
||||
/// Write-Output cmdlets.
|
||||
/// </summary>
|
||||
Normal,
|
||||
|
||||
/// <summary>
|
||||
/// A debug output line, written with the Write-Debug cmdlet.
|
||||
/// </summary>
|
||||
Debug,
|
||||
|
||||
/// <summary>
|
||||
/// A verbose output line, written with the Write-Verbose cmdlet.
|
||||
/// </summary>
|
||||
Verbose,
|
||||
|
||||
/// <summary>
|
||||
/// A warning output line, written with the Write-Warning cmdlet.
|
||||
/// </summary>
|
||||
Warning,
|
||||
|
||||
/// <summary>
|
||||
/// An error output line, written with the Write-Error cmdlet or
|
||||
/// as a result of some error during PowerShell pipeline execution.
|
||||
/// </summary>
|
||||
Error
|
||||
}
|
||||
}
|
||||
65
ServiceHost/Session/OutputWrittenEventArgs.cs
Normal file
65
ServiceHost/Session/OutputWrittenEventArgs.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.PowerShell.EditorServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides details about output that has been written to the
|
||||
/// PowerShell host.
|
||||
/// </summary>
|
||||
public class OutputWrittenEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the text of the output.
|
||||
/// </summary>
|
||||
public string OutputText { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the output.
|
||||
/// </summary>
|
||||
public OutputType OutputType { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean which indicates whether a newline
|
||||
/// should be written after the output.
|
||||
/// </summary>
|
||||
public bool IncludeNewLine { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the foreground color of the output text.
|
||||
/// </summary>
|
||||
public ConsoleColor ForegroundColor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the background color of the output text.
|
||||
/// </summary>
|
||||
public ConsoleColor BackgroundColor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the OutputWrittenEventArgs class.
|
||||
/// </summary>
|
||||
/// <param name="outputText">The text of the output.</param>
|
||||
/// <param name="includeNewLine">A boolean which indicates whether a newline should be written after the output.</param>
|
||||
/// <param name="outputType">The type of the output.</param>
|
||||
/// <param name="foregroundColor">The foreground color of the output text.</param>
|
||||
/// <param name="backgroundColor">The background color of the output text.</param>
|
||||
public OutputWrittenEventArgs(
|
||||
string outputText,
|
||||
bool includeNewLine,
|
||||
OutputType outputType,
|
||||
ConsoleColor foregroundColor,
|
||||
ConsoleColor backgroundColor)
|
||||
{
|
||||
this.OutputText = outputText;
|
||||
this.IncludeNewLine = includeNewLine;
|
||||
this.OutputType = outputType;
|
||||
this.ForegroundColor = foregroundColor;
|
||||
this.BackgroundColor = backgroundColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
109
ServiceHost/Session/ProfilePaths.cs
Normal file
109
ServiceHost/Session/ProfilePaths.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// 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;
|
||||
|
||||
namespace Microsoft.PowerShell.EditorServices.Session
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides profile path resolution behavior relative to the name
|
||||
/// of a particular PowerShell host.
|
||||
/// </summary>
|
||||
public class ProfilePaths
|
||||
{
|
||||
#region Constants
|
||||
|
||||
/// <summary>
|
||||
/// The file name for the "all hosts" profile. Also used as the
|
||||
/// suffix for the host-specific profile filenames.
|
||||
/// </summary>
|
||||
public const string AllHostsProfileName = "profile.ps1";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the profile path for all users, all hosts.
|
||||
/// </summary>
|
||||
public string AllUsersAllHosts { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the profile path for all users, current host.
|
||||
/// </summary>
|
||||
public string AllUsersCurrentHost { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the profile path for the current user, all hosts.
|
||||
/// </summary>
|
||||
public string CurrentUserAllHosts { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the profile path for the current user and host.
|
||||
/// </summary>
|
||||
public string CurrentUserCurrentHost { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the ProfilePaths class.
|
||||
/// </summary>
|
||||
/// <param name="hostProfileId">
|
||||
/// The identifier of the host used in the host-specific X_profile.ps1 filename.
|
||||
/// </param>
|
||||
/// <param name="baseAllUsersPath">The base path to use for constructing AllUsers profile paths.</param>
|
||||
/// <param name="baseCurrentUserPath">The base path to use for constructing CurrentUser profile paths.</param>
|
||||
public ProfilePaths(
|
||||
string hostProfileId,
|
||||
string baseAllUsersPath,
|
||||
string baseCurrentUserPath)
|
||||
{
|
||||
this.Initialize(hostProfileId, baseAllUsersPath, baseCurrentUserPath);
|
||||
}
|
||||
|
||||
private void Initialize(
|
||||
string hostProfileId,
|
||||
string baseAllUsersPath,
|
||||
string baseCurrentUserPath)
|
||||
{
|
||||
string currentHostProfileName =
|
||||
string.Format(
|
||||
"{0}_{1}",
|
||||
hostProfileId,
|
||||
AllHostsProfileName);
|
||||
|
||||
this.AllUsersCurrentHost = Path.Combine(baseAllUsersPath, currentHostProfileName);
|
||||
this.CurrentUserCurrentHost = Path.Combine(baseCurrentUserPath, currentHostProfileName);
|
||||
this.AllUsersAllHosts = Path.Combine(baseAllUsersPath, AllHostsProfileName);
|
||||
this.CurrentUserAllHosts = Path.Combine(baseCurrentUserPath, AllHostsProfileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of profile paths that exist on the filesystem.
|
||||
/// </summary>
|
||||
/// <returns>An IEnumerable of profile path strings to be loaded.</returns>
|
||||
public IEnumerable<string> GetLoadableProfilePaths()
|
||||
{
|
||||
var profilePaths =
|
||||
new string[]
|
||||
{
|
||||
this.AllUsersAllHosts,
|
||||
this.AllUsersCurrentHost,
|
||||
this.CurrentUserAllHosts,
|
||||
this.CurrentUserCurrentHost
|
||||
};
|
||||
|
||||
return profilePaths.Where(p => File.Exists(p));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user