From 7fc140af01ce0e626fc940b23f1e7bbf7b101d89 Mon Sep 17 00:00:00 2001 From: Tomas Agustin Raies Date: Mon, 4 May 2020 14:40:32 -0700 Subject: [PATCH] [Settings] Update version in General Settings (#2601) * Update version in General Settings * Set version in settings.json file * Only overwrite the settings.json file if it is newer than the old version. * Fix tests * Fix test names and added comment --- .../common-md-flag/common-md-flag.vcxproj | 20 ++--- .../common-md-flag.vcxproj.filters | 21 +++--- src/common/common-md-flag/framework.h | 3 - src/common/common-md-flag/pch.cpp | 5 -- src/common/common-md-flag/pch.h | 13 ---- src/common/interop/interop.h | 15 +++- src/common/interop/interop.vcxproj | 4 +- .../GeneralSettings.cs | 2 +- .../Utilities/Helper.cs | 39 ++++++++++ .../ViewModels/GeneralViewModel.cs | 22 ++++++ .../Views/GeneralPage.xaml | 2 +- ...crosoft.PowerToys.Settings.UnitTest.csproj | 1 + .../ModelsTests/HelperTest.cs | 73 +++++++++++++++++++ 13 files changed, 174 insertions(+), 46 deletions(-) delete mode 100644 src/common/common-md-flag/framework.h delete mode 100644 src/common/common-md-flag/pch.cpp delete mode 100644 src/common/common-md-flag/pch.h create mode 100644 src/core/Microsoft.PowerToys.Settings.UnitTest/ModelsTests/HelperTest.cs diff --git a/src/common/common-md-flag/common-md-flag.vcxproj b/src/common/common-md-flag/common-md-flag.vcxproj index ec3785e16f..2aba3ced53 100644 --- a/src/common/common-md-flag/common-md-flag.vcxproj +++ b/src/common/common-md-flag/common-md-flag.vcxproj @@ -51,7 +51,7 @@ - Use + NotUsing Level3 true true @@ -59,7 +59,8 @@ NDEBUG;_LIB;%(PreprocessorDefinitions) true pch.h - stdcpp17 + stdcpplatest + inc;telemetry;..\..\deps\cpprestsdk\include;%(AdditionalIncludeDirectories) Windows @@ -70,13 +71,14 @@ - Use + NotUsing Level3 true _DEBUG;_LIB;%(PreprocessorDefinitions) true pch.h - stdcpp17 + stdcpplatest + inc;telemetry;..\..\deps\cpprestsdk\include;%(AdditionalIncludeDirectories) Windows @@ -84,20 +86,18 @@ + + - - + + - - Create - Create - diff --git a/src/common/common-md-flag/common-md-flag.vcxproj.filters b/src/common/common-md-flag/common-md-flag.vcxproj.filters index 7ecac76e58..f30b22131f 100644 --- a/src/common/common-md-flag/common-md-flag.vcxproj.filters +++ b/src/common/common-md-flag/common-md-flag.vcxproj.filters @@ -15,12 +15,6 @@ - - Header Files - - - Header Files - Header Files @@ -33,16 +27,25 @@ Header Files + + Header Files + + + Header Files + - - Source Files - Source Files Source Files + + Source Files + + + Source Files + \ No newline at end of file diff --git a/src/common/common-md-flag/framework.h b/src/common/common-md-flag/framework.h deleted file mode 100644 index 880eb72076..0000000000 --- a/src/common/common-md-flag/framework.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers diff --git a/src/common/common-md-flag/pch.cpp b/src/common/common-md-flag/pch.cpp deleted file mode 100644 index 91c22df2a1..0000000000 --- a/src/common/common-md-flag/pch.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// pch.cpp: source file corresponding to the pre-compiled header - -#include "pch.h" - -// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. diff --git a/src/common/common-md-flag/pch.h b/src/common/common-md-flag/pch.h deleted file mode 100644 index 04ff4c23fb..0000000000 --- a/src/common/common-md-flag/pch.h +++ /dev/null @@ -1,13 +0,0 @@ -// pch.h: This is a precompiled header file. -// Files listed below are compiled only once, improving build performance for future builds. -// This also affects IntelliSense performance, including code completion and many code browsing features. -// However, files listed here are ALL re-compiled if any one of them is updated between builds. -// Do not add files here that you will be updating frequently as this negates the performance advantage. - -#ifndef PCH_H -#define PCH_H - -// add headers that you want to pre-compile here -#include "framework.h" - -#endif //PCH_H diff --git a/src/common/interop/interop.h b/src/common/interop/interop.h index 07d5ac517f..793d418712 100644 --- a/src/common/interop/interop.h +++ b/src/common/interop/interop.h @@ -1,10 +1,11 @@ #pragma once -#include "..\keyboard_layout.h" -#include "..\two_way_pipe_message_ipc.h" #include #include #include +#include "..\keyboard_layout.h" +#include "..\two_way_pipe_message_ipc.h" +#include "..\common.h" using namespace System; using namespace System::Runtime::InteropServices; @@ -103,4 +104,14 @@ public _callback(gcnew String(msg.c_str())); } }; + + public + ref class CommonManaged + { + public: + static String^ GetProductVersion() + { + return gcnew String(get_product_version().c_str()); + } + }; } diff --git a/src/common/interop/interop.vcxproj b/src/common/interop/interop.vcxproj index 01866c546e..143a0755d9 100644 --- a/src/common/interop/interop.vcxproj +++ b/src/common/interop/interop.vcxproj @@ -73,7 +73,7 @@ stdcpp17 - %(AdditionalDependencies) + WindowsApp.lib;%(AdditionalDependencies) @@ -89,7 +89,7 @@ stdcpp17 - %(AdditionalDependencies) + WindowsApp.lib;%(AdditionalDependencies) diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs index 8250d947e1..6e3ed59038 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs @@ -59,7 +59,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib this.AutoDownloadUpdates = false; this.Theme = "system"; this.SystemTheme = "light"; - this.PowertoysVersion = "v0.15.3"; + this.PowertoysVersion = interop.CommonManaged.GetProductVersion(); this.Enabled = new EnabledModules(); this.CustomActionName = string.Empty; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs index 6d927bcee4..8533a41fd4 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs @@ -5,9 +5,11 @@ using System; using System.Diagnostics; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; +using System.Text.RegularExpressions; using Microsoft.PowerToys.Settings.UI.Lib.CustomAction; namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities @@ -78,5 +80,42 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities { return layoutMap.GetKeyName(key); } + + public static string GetProductVersion() + { + return interop.CommonManaged.GetProductVersion(); + } + + public static int CompareVersions(string version1, string version2) + { + try + { + // Split up the version strings into int[] + // Example: v10.0.2 -> {10, 0, 2}; + var v1 = version1.Substring(1).Split('.').Select(int.Parse).ToArray(); + var v2 = version2.Substring(1).Split('.').Select(int.Parse).ToArray(); + + if (v1.Count() != 3 || v2.Count() != 3) + { + throw new FormatException(); + } + + if (v1[0] - v2[0] != 0) + { + return v1[0] - v2[0]; + } + + if (v1[1] - v2[1] != 0) + { + return v1[1] - v2[1]; + } + + return v1[2] - v2[2]; + } + catch (Exception) + { + throw new FormatException("Bad product version format"); + } + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs index 5b62a3ef43..6e487fb548 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs @@ -10,6 +10,8 @@ using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Lib; using Microsoft.PowerToys.Settings.UI.ViewModels.Commands; using Microsoft.PowerToys.Settings.UI.Views; +using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Windows.Data.Html; using Windows.System; using Windows.UI.Popups; using Windows.UI.Xaml; @@ -32,6 +34,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels try { GeneralSettingsConfigs = SettingsUtils.GetSettings(string.Empty); + + if (Helper.CompareVersions(GeneralSettingsConfigs.PowertoysVersion, Helper.GetProductVersion()) < 0) + { + // Update settings + GeneralSettingsConfigs.PowertoysVersion = Helper.GetProductVersion(); + SettingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty); + } + } + catch (FormatException e) + { + // If there is an issue with the version number format, don't migrate settings. + Debug.WriteLine(e.Message); } catch { @@ -216,6 +230,14 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } + public string PowerToysVersion + { + get + { + return Helper.GetProductVersion(); + } + } + public void RaisePropertyChanged([CallerMemberName] string propertyName = null) { // Notify UI of property change diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml index ab43e216f7..925b9dc95b 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml @@ -89,7 +89,7 @@ Style="{StaticResource SettingsGroupTitleStyle}" Margin="{StaticResource XSmallBottomMargin}"/> - diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj b/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj index 17966b9dcc..93166f86bf 100644 --- a/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj +++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/Microsoft.PowerToys.Settings.UnitTest.csproj @@ -119,6 +119,7 @@ + diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ModelsTests/HelperTest.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ModelsTests/HelperTest.cs new file mode 100644 index 0000000000..fff7a51f23 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ModelsTests/HelperTest.cs @@ -0,0 +1,73 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using System; + +namespace CommonLibTest +{ + [TestClass] + public class HelperTest + { + public static void TestStringIsSmaller(string v1, string v2) + { + var res = Helper.CompareVersions(v1, v2); + Assert.IsTrue(res < 0); + } + + + public static void TestStringsAreEqual(string v1, string v2) + { + var res = Helper.CompareVersions(v1, v2); + Assert.IsTrue(res == 0); + } + + [TestMethod] + public void Helper_CompareVersions_ShouldBeEqual_WhenSuccessful() + { + TestStringsAreEqual("v0.0.0", "v0.0.0"); + TestStringsAreEqual("v0.1.1", "v0.1.1"); + TestStringsAreEqual("v1.1.1", "v1.1.1"); + TestStringsAreEqual("v1.999.99", "v1.999.99"); + } + + [TestMethod] + public void Helper_CompareVersions_ShouldBeSmaller_WhenSuccessful() + { + TestStringIsSmaller("v0.0.0", "v0.0.1"); + TestStringIsSmaller("v0.0.0", "v0.1.0"); + TestStringIsSmaller("v0.0.0", "v1.0.0"); + TestStringIsSmaller("v1.0.1", "v1.0.2"); + TestStringIsSmaller("v1.1.1", "v1.1.2"); + TestStringIsSmaller("v1.1.1", "v1.2.0"); + TestStringIsSmaller("v1.999.99", "v2.0.0"); + TestStringIsSmaller("v1.0.99", "v1.2.0"); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Helper_CompareVersions_ShouldThrowBadFormat_WhenNoVersionString() + { + Helper.CompareVersions("v0.0.1", ""); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Helper_CompareVersions_ShouldThrowBadFormat_WhenShortVersionString() + { + Helper.CompareVersions("v0.0.1", "v0.1"); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Helper_CompareVersions_ShouldThrowBadFormat_WhenLongVersionString() + { + Helper.CompareVersions("v0.0.1", "v0.0.0.1"); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Helper_CompareVersions_ShouldThrowBadFormat_WhenItIsNotAVersionString() + { + Helper.CompareVersions("v0.0.1", "HelloWorld"); + } + } +}