Skip to content

Commit 954e4db

Browse files
authored
Log build info (#772)
* Add default BuildInfo file * Create build information and log at startup * Add System.Runtime.InteropServices to build
1 parent aeb9e7d commit 954e4db

File tree

9 files changed

+96
-45
lines changed

9 files changed

+96
-45
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ docs/_repo/
3434
docs/metadata/
3535
*.zip
3636

37+
# Generated build info file
38+
src/PowerShellEditorServices.Host/BuildInfo/BuildInfo.cs
39+
3740
# quickbuild.exe
3841
/VersionGeneratingLogs/
3942
QLogs

PowerShellEditorServices.build.ps1

+48-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ $script:IsCIBuild = $env:APPVEYOR -ne $null
2020
$script:IsUnix = $PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq "Core" -and !$IsWindows
2121
$script:TargetFrameworksParam = "/p:TargetFrameworks=\`"$(if (!$script:IsUnix) { "net451;" })netstandard1.6\`""
2222
$script:SaveModuleSupportsAllowPrerelease = (Get-Command Save-Module).Parameters.ContainsKey("AllowPrerelease")
23+
$script:BuildInfoPath = [System.IO.Path]::Combine($PSScriptRoot, "src", "PowerShellEditorServices.Host", "BuildInfo", "BuildInfo.cs")
2324

2425
if ($PSVersionTable.PSEdition -ne "Core") {
2526
Add-Type -Assembly System.IO.Compression.FileSystem
@@ -142,6 +143,51 @@ task TestPowerShellApi -If { !$script:IsUnix } {
142143
exec { & $script:dotnetExe restore .\src\PowerShellEditorServices\PowerShellEditorServices.csproj }
143144
}
144145

146+
task CreateBuildInfo -Before Build {
147+
$buildVersion = "<development-build>"
148+
$buildOrigin = "<development>"
149+
150+
# Set build info fields on build platforms
151+
if ($env:APPVEYOR)
152+
{
153+
$buildVersion = $env:APPVEYOR_BUILD_VERSION
154+
$buildOrigin = if ($env:CI) { "AppVeyor CI" } else { "AppVeyor" }
155+
}
156+
elseif ($env:TF_BUILD)
157+
{
158+
$psd1Path = [System.IO.Path]::Combine($PSScriptRoot, "module", "PowerShellEditorServices", "PowerShellEditorServices.psd1")
159+
$buildVersion = (Import-PowerShellDataFile -LiteralPath $psd1Path).Version
160+
$buildOrigin = "VSTS"
161+
}
162+
163+
# Allow override of build info fields (except date)
164+
if ($env:PSES_BUILD_VERSION)
165+
{
166+
$buildVersion = $env:PSES_BUILD_VERSION
167+
}
168+
169+
if ($env:PSES_BUILD_ORIGIN)
170+
{
171+
$buildOrigin = $env:PSES_BUILD_ORIGIN
172+
}
173+
174+
[string]$buildTime = [datetime]::Now.ToString("s", [System.Globalization.CultureInfo]::InvariantCulture)
175+
176+
$buildInfoContents = @"
177+
namespace Microsoft.PowerShell.EditorServices.Host
178+
{
179+
public static class BuildInfo
180+
{
181+
public const string BuildVersion = "$buildVersion";
182+
public const string BuildOrigin = "$buildOrigin";
183+
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime");
184+
}
185+
}
186+
"@
187+
188+
Set-Content -LiteralPath $script:BuildInfoPath -Value $buildInfoContents -Force
189+
}
190+
145191
task Build {
146192
exec { & $script:dotnetExe publish -c $Configuration .\src\PowerShellEditorServices.Host\PowerShellEditorServices.Host.csproj -f netstandard1.6 }
147193
if (!$script:IsUnix) {
@@ -208,6 +254,7 @@ task LayoutModule -After Build {
208254
New-Item -Force $PSScriptRoot\module\PowerShellEditorServices\bin\Core -Type Directory | Out-Null
209255

210256
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices\bin\$Configuration\netstandard1.6\publish\Serilog*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
257+
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices\bin\$Configuration\netstandard1.6\publish\System.Runtime.InteropServices.RuntimeInformation.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
211258

212259
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
213260
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\netstandard1.6\UnixConsoleEcho.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Core\
@@ -216,11 +263,11 @@ task LayoutModule -After Build {
216263

217264
if (!$script:IsUnix) {
218265
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices\bin\$Configuration\net451\Serilog*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop
266+
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices\bin\$Configuration\net451\System.Runtime.InteropServices.RuntimeInformation.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
219267

220268
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\* -Filter Microsoft.PowerShell.EditorServices*.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
221269
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\Newtonsoft.Json.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
222270
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\UnixConsoleEcho.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
223-
Copy-Item -Force -Path $PSScriptRoot\src\PowerShellEditorServices.Host\bin\$Configuration\net451\publish\System.Runtime.InteropServices.RuntimeInformation.dll -Destination $PSScriptRoot\module\PowerShellEditorServices\bin\Desktop\
224271
}
225272

226273
# Copy Third Party Notices.txt to module folder

module/PowerShellEditorServices/Start-EditorServices.ps1

+3
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ function Set-NamedPipeMode {
243243
}
244244
}
245245

246+
LogSection "Console Encoding"
247+
Log $OutputEncoding
248+
246249
# Add BundledModulesPath to $env:PSModulePath
247250
if ($BundledModulesPath) {
248251
$env:PSModulePath = $env:PSModulePath.TrimEnd([System.IO.Path]::PathSeparator) + [System.IO.Path]::PathSeparator + $BundledModulesPath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Microsoft.PowerShell.EditorServices.Host
2+
{
3+
public static class BuildInfo
4+
{
5+
public const string BuildVersion = "<unset>";
6+
public const string BuildOrigin = "<unset>";
7+
public static readonly System.DateTime? BuildTime = null;
8+
}
9+
}

src/PowerShellEditorServices.Host/EditorServicesHost.cs

+29-29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Management.Automation.Runspaces;
2020
using System.Reflection;
2121
using System.Threading.Tasks;
22+
using System.Runtime.InteropServices;
2223

2324
namespace Microsoft.PowerShell.EditorServices.Host
2425
{
@@ -140,39 +141,38 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
140141
.Build();
141142

142143
#if CoreCLR
143-
FileVersionInfo fileVersionInfo =
144-
FileVersionInfo.GetVersionInfo(this.GetType().GetTypeInfo().Assembly.Location);
145-
146-
// TODO #278: Need the correct dependency package for this to work correctly
147-
//string osVersionString = RuntimeInformation.OSDescription;
148-
//string processArchitecture = RuntimeInformation.ProcessArchitecture == Architecture.X64 ? "64-bit" : "32-bit";
149-
//string osArchitecture = RuntimeInformation.OSArchitecture == Architecture.X64 ? "64-bit" : "32-bit";
144+
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(this.GetType().GetTypeInfo().Assembly.Location);
150145
#else
151-
FileVersionInfo fileVersionInfo =
152-
FileVersionInfo.GetVersionInfo(this.GetType().Assembly.Location);
153-
string osVersionString = Environment.OSVersion.VersionString;
154-
string processArchitecture = Environment.Is64BitProcess ? "64-bit" : "32-bit";
155-
string osArchitecture = Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit";
146+
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(this.GetType().Assembly.Location);
156147
#endif
157148

158-
string newLine = Environment.NewLine;
149+
string osVersion = RuntimeInformation.OSDescription;
159150

160-
this.logger.Write(
161-
LogLevel.Normal,
162-
string.Format(
163-
$"PowerShell Editor Services Host v{fileVersionInfo.FileVersion} starting (pid {Process.GetCurrentProcess().Id})..." + newLine + newLine +
164-
" Host application details:" + newLine + newLine +
165-
$" Name: {this.hostDetails.Name}" + newLine +
166-
$" ProfileId: {this.hostDetails.ProfileId}" + newLine +
167-
$" Version: {this.hostDetails.Version}" + newLine +
168-
#if !CoreCLR
169-
$" Arch: {processArchitecture}" + newLine + newLine +
170-
" Operating system details:" + newLine + newLine +
171-
$" Version: {osVersionString}" + newLine +
172-
$" Arch: {osArchitecture}"));
173-
#else
174-
""));
175-
#endif
151+
string buildTime = BuildInfo.BuildTime?.ToString("s", System.Globalization.CultureInfo.InvariantCulture) ?? "<unspecified>";
152+
153+
string logHeader = $@"
154+
PowerShell Editor Services Host v{fileVersionInfo.FileVersion} starting (PID {Process.GetCurrentProcess().Id}
155+
156+
Host application details:
157+
158+
Name: {this.hostDetails.Name}
159+
Version: {this.hostDetails.Version}
160+
ProfileId: {this.hostDetails.ProfileId}
161+
Arch: {RuntimeInformation.OSArchitecture}
162+
163+
Operating system details:
164+
165+
Version: {osVersion}
166+
Arch: {RuntimeInformation.OSArchitecture}
167+
168+
Build information:
169+
170+
Version: {BuildInfo.BuildVersion}
171+
Origin: {BuildInfo.BuildOrigin}
172+
Date: {buildTime}
173+
";
174+
175+
this.logger.Write(LogLevel.Normal, logHeader);
176176
}
177177

178178
/// <summary>

src/PowerShellEditorServices/Console/ConsoleReadLine.cs

-5
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,13 @@ internal class ConsoleReadLine
2929
#region Constructors
3030
static ConsoleReadLine()
3131
{
32-
// Maybe we should just include the RuntimeInformation package for FullCLR?
33-
#if CoreCLR
3432
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
3533
{
3634
s_consoleProxy = new WindowsConsoleOperations();
3735
return;
3836
}
3937

4038
s_consoleProxy = new UnixConsoleOperations();
41-
#else
42-
s_consoleProxy = new WindowsConsoleOperations();
43-
#endif
4439
}
4540

4641
public ConsoleReadLine(PowerShellContext powerShellContext)

src/PowerShellEditorServices/Language/LanguageService.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,10 @@ public async Task<FindReferencesResult> FindReferencesOfSymbol(
325325

326326
// We want to look for references first in referenced files, hence we use ordered dictionary
327327
// TODO: File system case-sensitivity is based on filesystem not OS, but OS is a much cheaper heuristic
328-
#if CoreCLR
329-
// The RuntimeInformation.IsOSPlatform is not supported in .NET Framework
330328
var fileMap = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
331329
? new OrderedDictionary()
332330
: new OrderedDictionary(StringComparer.OrdinalIgnoreCase);
333-
#else
334-
var fileMap = new OrderedDictionary(StringComparer.OrdinalIgnoreCase);
335-
#endif
331+
336332
foreach (ScriptFile file in referencedFiles)
337333
{
338334
fileMap.Add(file.FilePath, file);

src/PowerShellEditorServices/PowerShellEditorServices.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
6666
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
6767
<PackageReference Include="Serilog.Sinks.Async" Version="1.2.0" />
68+
<PackageReference Include="System.Runtime.Extensions" Version="4.3.0" />
69+
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
6870
</ItemGroup>
6971

7072
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">

src/PowerShellEditorServices/Workspace/Workspace.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
using System.IO;
1111
using System.Security;
1212
using System.Text;
13-
14-
#if CoreCLR
1513
using System.Runtime.InteropServices;
16-
#endif
1714

1815
namespace Microsoft.PowerShell.EditorServices
1916
{
@@ -498,12 +495,11 @@ private string ResolveRelativeScriptPath(string baseFilePath, string relativePat
498495
/// <returns>A file-scheme URI string with the drive colon unescaped.</returns>
499496
private static string UnescapeDriveColon(string fileUri)
500497
{
501-
#if CoreCLR
502498
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
503499
{
504500
return fileUri;
505501
}
506-
#endif
502+
507503
// Check here that we have something like "file:///C%3A/" as a prefix (caller must check the file:// part)
508504
if (!(fileUri[7] == '/' &&
509505
char.IsLetter(fileUri[8]) &&

0 commit comments

Comments
 (0)