MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.IO;7using System.Runtime.InteropServices;8 9namespace Microsoft.PowerShell.GlobalTool.Shim10{11 /// <summary>12 /// Shim layer to chose the appropriate runtime for PowerShell DotNet Global tool.13 /// </summary>14 public static class EntryPoint15 {16 private const string PwshDllName = "pwsh.dll";17 18 private const string WinFolderName = "win";19 20 private const string UnixFolderName = "unix";21 22 /// <summary>23 /// Entry point for the global tool.24 /// </summary>25 /// <param name="args">Arguments passed to the global tool.</param>'26 /// <returns>Exit code returned by pwsh.</returns>27 public static int Main(string[] args)28 {29 var currentPath = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).Directory.FullName;30 var isWindows = OperatingSystem.IsWindows();31 32 string platformFolder = isWindows ? WinFolderName : UnixFolderName;33 34 var arguments = new List<string>(args.Length + 1);35 var pwshPath = Path.Combine(currentPath, platformFolder, PwshDllName);36 arguments.Add(pwshPath);37 arguments.AddRange(args);38 39 if (File.Exists(pwshPath))40 {41 Console.CancelKeyPress += (sender, e) =>42 {43 e.Cancel = true;44 };45 46 var process = System.Diagnostics.Process.Start("dotnet", arguments);47 process.WaitForExit();48 return process.ExitCode;49 }50 else51 {52 throw new FileNotFoundException(pwshPath);53 }54 }55 }56}57 