瀏覽代碼

Reporting .NET Core and PowerShell Core versions

Source commit: bc6c0efb5babfdfb95f78ee7cffcb38f4576ed14
Martin Prikryl 4 年之前
父節點
當前提交
acf90ead19

+ 1 - 0
source/core/Common.h

@@ -222,6 +222,7 @@ int __fastcall FindCheck(int Result, const UnicodeString & Path);
 int __fastcall FindFirstUnchecked(const UnicodeString & Path, int Attr, TSearchRecChecked & F);
 int __fastcall FindFirstChecked(const UnicodeString & Path, int Attr, TSearchRecChecked & F);
 int __fastcall FindNextChecked(TSearchRecChecked & F);
+int __fastcall FindNextUnchecked(TSearchRecChecked & F);
 void __fastcall ProcessLocalDirectory(UnicodeString DirName,
   TProcessLocalFileEvent CallBackFunc, void * Param = NULL, int FindAttrs = -1);
 int __fastcall FileGetAttrFix(const UnicodeString FileName);

+ 2 - 0
source/forms/Preferences.cpp

@@ -2668,7 +2668,9 @@ void __fastcall TPreferencesDialog::AddExtension()
           Url = ProgramUrl(Url);
           // The EncodeUrlString should not be necessary, but as we get the value from registry, let's be safe
           Url = AppendUrlParams(Url, FORMAT(L"netframework=%s", (EncodeUrlString(GetNetVersionStr()))));
+          Url = AppendUrlParams(Url, FORMAT(L"netcore=%s", (EncodeUrlString(GetNetCoreVersionStr()))));
           Url = AppendUrlParams(Url, FORMAT(L"powershell=%s", (EncodeUrlString(GetPowerShellVersionStr()))));
+          Url = AppendUrlParams(Url, FORMAT(L"pwsh=%s", (EncodeUrlString(GetPowerShellCoreVersionStr()))));
           Url = AppendUrlParams(Url, FORMAT(L"windows=%s", (EncodeUrlString(WindowsVersion()))));
           Url = CampaignUrl(Url);
         }

+ 99 - 2
source/windows/Setup.cpp

@@ -33,6 +33,7 @@
 #include <OperationWithTimeout.hpp>
 #include <Soap.HTTPUtil.hpp>
 #include <Web.HTTPApp.hpp>
+#include <System.IOUtils.hpp>
 //---------------------------------------------------------------------------
 #define KEY _T("SYSTEM\\CurrentControlSet\\Control\\") \
             _T("Session Manager\\Environment")
@@ -44,7 +45,9 @@
 UnicodeString LastPathError;
 //---------------------------------------------------------------------------
 UnicodeString NetVersionStr;
+UnicodeString NetCoreVersionStr;
 UnicodeString PowerShellVersionStr;
+UnicodeString PowerShellCoreVersionStr;
 //---------------------------------------------------------------------------
 // Display the error "err_msg".
 void err_out(LPCTSTR err_msg)
@@ -2174,7 +2177,7 @@ static void ReadNetVersion(TRegistryStorage * Registry)
   }
 }
 //---------------------------------------------------------------------------
-UnicodeString __fastcall GetNetVersionStr()
+UnicodeString GetNetVersionStr()
 {
   if (NetVersionStr.IsEmpty())
   {
@@ -2212,7 +2215,66 @@ UnicodeString __fastcall GetNetVersionStr()
   return NetVersionStr;
 }
 //---------------------------------------------------------------------------
-UnicodeString __fastcall GetPowerShellVersionStr()
+UnicodeString GetNetCoreVersionStr()
+{
+  if (NetCoreVersionStr.IsEmpty())
+  {
+    NetCoreVersionStr = L"0"; // not to retry on failure
+
+    UnicodeString ProgramsFolder = DefaultStr(GetEnvironmentVariable(L"ProgramW6432"), GetEnvironmentVariable(L"ProgramFiles"));
+    if (ProgramsFolder.IsEmpty())
+    {
+      ::SpecialFolderLocation(CSIDL_PROGRAM_FILES, ProgramsFolder);
+    }
+    UnicodeString SdkFolder = L"sdk";
+    UnicodeString DotNetPath = TPath::Combine(TPath::Combine(ProgramsFolder, L"dotnet"), SdkFolder);
+    if (!DirectoryExistsFix(DotNetPath))
+    {
+      UnicodeString DotNetExe = L"dotnet.exe";
+      if (FindFile(DotNetExe))
+      {
+        DotNetPath = TPath::Combine(ExtractFilePath(DotNetPath), SdkFolder);
+      }
+    }
+    if (DirectoryExistsFix(DotNetPath))
+    {
+      TSearchRecChecked SearchRec;
+      DotNetPath = TPath::Combine(DotNetPath, L"*.*");
+      if (FindFirstUnchecked(ApiPath(DotNetPath), faDirectory, SearchRec) == 0)
+      {
+        do
+        {
+          if (SearchRec.IsRealFile())
+          {
+            UnicodeString Name = SearchRec.Name;
+            // 1.0.0-preview2-003131
+            UnicodeString VersionStr = CutToChar(Name, L'-', true);
+            if (!VersionStr.IsEmpty() && IsDigit(VersionStr[1]) && (VersionStr.Pos(L".") >= 2))
+            {
+              for (int I = 1; I <= VersionStr.Length(); I++)
+              {
+                if (!IsDigit(VersionStr[I]) && (VersionStr[I] != L'.'))
+                {
+                  VersionStr = EmptyStr;
+                }
+              }
+
+              if (!VersionStr.IsEmpty() && (CompareVersion(VersionStr, NetCoreVersionStr) > 0))
+              {
+                NetCoreVersionStr = VersionStr;
+              }
+            }
+          }
+        }
+        while (FindNextUnchecked(SearchRec) == 0);
+      }
+    }
+  }
+
+  return NetCoreVersionStr;
+}
+//---------------------------------------------------------------------------
+UnicodeString GetPowerShellVersionStr()
 {
   if (PowerShellVersionStr.IsEmpty())
   {
@@ -2243,6 +2305,41 @@ UnicodeString __fastcall GetPowerShellVersionStr()
   return PowerShellVersionStr;
 }
 //---------------------------------------------------------------------------
+UnicodeString GetPowerShellCoreVersionStr()
+{
+  if (PowerShellCoreVersionStr.IsEmpty())
+  {
+    PowerShellCoreVersionStr = L"0"; // not to retry on failure
+
+    // TRegistryStorage does not support KEY_WOW64_64KEY
+    unsigned int Access = KEY_READ | FLAGMASK(IsWin64(), KEY_WOW64_64KEY);
+    std::unique_ptr<TRegistry> Registry(new TRegistry(Access));
+    Registry->RootKey = HKEY_LOCAL_MACHINE;
+    UnicodeString RootKey(L"SOFTWARE\\Microsoft\\PowerShellCore\\InstalledVersions");
+    if (Registry->OpenKeyReadOnly(RootKey))
+    {
+      std::unique_ptr<TStringList> Keys(new TStringList());
+      Registry->GetKeyNames(Keys.get());
+      Registry->CloseKey();
+      for (int Index = 0; Index < Keys->Count; Index++)
+      {
+        UnicodeString Key = RootKey + L"\\" + Keys->Strings[Index];
+        if (Registry->OpenKeyReadOnly(Key))
+        {
+          UnicodeString VersionStr = Registry->ReadString(L"SemanticVersion");
+          if (!VersionStr.IsEmpty() && (CompareVersion(VersionStr, PowerShellCoreVersionStr) > 0))
+          {
+            PowerShellCoreVersionStr = VersionStr;
+          }
+          Registry->CloseKey();
+        }
+      }
+    }
+  }
+
+  return PowerShellCoreVersionStr;
+}
+//---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 static void CollectCLSIDKey(
   TConsole * Console, TStrings * Keys, int PlatformSet, TRegistryStorage * Storage, const UnicodeString & CLSID,

+ 4 - 2
source/windows/Setup.h

@@ -33,8 +33,10 @@ void __fastcall ShowTips();
 UnicodeString __fastcall FirstUnshownTip();
 void __fastcall TipsUpdateStaticUsage();
 int __fastcall GetNetVersion();
-UnicodeString __fastcall GetNetVersionStr();
-UnicodeString __fastcall GetPowerShellVersionStr();
+UnicodeString GetNetVersionStr();
+UnicodeString GetNetCoreVersionStr();
+UnicodeString GetPowerShellVersionStr();
+UnicodeString GetPowerShellCoreVersionStr();
 int ComRegistration(TConsole * Console);
 //---------------------------------------------------------------------------
 #endif

+ 8 - 0
source/windows/WinConfiguration.cpp

@@ -3053,10 +3053,18 @@ void __fastcall TCustomCommandType::LoadExtension(TStrings * Lines, const Unicod
           {
             Failed = (CompareVersion(Value, GetNetVersionStr()) > 0);
           }
+          else if (Dependency == L".netcore")
+          {
+            Failed = (CompareVersion(Value, GetNetCoreVersionStr()) > 0);
+          }
           else if (Dependency == L"powershell")
           {
             Failed = (CompareVersion(Value, GetPowerShellVersionStr()) > 0);
           }
+          else if (Dependency == L"pwsh")
+          {
+            Failed = (CompareVersion(Value, GetPowerShellCoreVersionStr()) > 0);
+          }
           else if (Dependency == L"windows")
           {
             Failed = (CompareVersion(Value, WindowsVersion()) > 0);

+ 2 - 0
source/windows/WinMain.cpp

@@ -538,7 +538,9 @@ void __fastcall UpdateStaticUsage()
   Configuration->Usage->Set(L"IsInstalled", IsInstalled());
   Configuration->Usage->Set(L"Wine", IsWine());
   Configuration->Usage->Set(L"NetFrameworkVersion", GetNetVersionStr());
+  Configuration->Usage->Set(L"NetCoreVersion", GetNetCoreVersionStr());
   Configuration->Usage->Set(L"PowerShellVersion", GetPowerShellVersionStr());
+  Configuration->Usage->Set(L"PwshVersion", GetPowerShellCoreVersionStr());
 
   UnicodeString ParentProcess = GetAncestorProcessName();
   // do not record the installer as a parent process