Skip to content

Commit

Permalink
[Settings] Update version in General Settings (#2601)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
traies committed May 4, 2020
1 parent d401474 commit 7fc140a
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 46 deletions.
20 changes: 10 additions & 10 deletions src/common/common-md-flag/common-md-flag.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalIncludeDirectories>inc;telemetry;..\..\deps\cpprestsdk\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -70,34 +71,33 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalIncludeDirectories>inc;telemetry;..\..\deps\cpprestsdk\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\common.h" />
<ClInclude Include="..\keyboard_layout.h" />
<ClInclude Include="..\keyboard_layout_impl.h" />
<ClInclude Include="..\pch.h" />
<ClInclude Include="..\two_way_pipe_message_ipc.h" />
<ClInclude Include="..\two_way_pipe_message_ipc_impl.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\common.cpp" />
<ClCompile Include="..\keyboard_layout.cpp" />
<ClCompile Include="..\pch.cpp" />
<ClCompile Include="..\two_way_pipe_message_ipc.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
21 changes: 12 additions & 9 deletions src/common/common-md-flag/common-md-flag.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="framework.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\keyboard_layout.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand All @@ -33,16 +27,25 @@
<ClInclude Include="..\two_way_pipe_message_ipc_impl.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\common.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\pch.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\keyboard_layout.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\two_way_pipe_message_ipc.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\common.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
3 changes: 0 additions & 3 deletions src/common/common-md-flag/framework.h

This file was deleted.

5 changes: 0 additions & 5 deletions src/common/common-md-flag/pch.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions src/common/common-md-flag/pch.h

This file was deleted.

15 changes: 13 additions & 2 deletions src/common/interop/interop.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "..\keyboard_layout.h"
#include "..\two_way_pipe_message_ipc.h"
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
#include <functional>
#include "..\keyboard_layout.h"
#include "..\two_way_pipe_message_ipc.h"
#include "..\common.h"

using namespace System;
using namespace System::Runtime::InteropServices;
Expand Down Expand Up @@ -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());
}
};
}
4 changes: 2 additions & 2 deletions src/common/interop/interop.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>
</DelayLoadDLLs>
</Link>
Expand All @@ -89,7 +89,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>
</DelayLoadDLLs>
</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public GeneralSettings()
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;
}
Expand Down
39 changes: 39 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,5 +80,42 @@ public static string GetKeyName(uint key)
{
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");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
Expand All @@ -32,6 +34,18 @@ public GeneralViewModel()
try
{
GeneralSettingsConfigs = SettingsUtils.GetSettings<GeneralSettings>(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
{
Expand Down Expand Up @@ -216,6 +230,14 @@ public bool IsSystemThemeRadioButtonChecked
}
}

public string PowerToysVersion
{
get
{
return Helper.GetProductVersion();
}
}

public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
// Notify UI of property change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
Style="{StaticResource SettingsGroupTitleStyle}"
Margin="{StaticResource XSmallBottomMargin}"/>

<TextBlock Text="Version 0.15.2"
<TextBlock Text="{x:Bind ViewModel.PowerToysVersion }"
FontWeight="Bold"
Margin="{StaticResource SmallTopMargin}" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<SDKReference Include="TestPlatform.Universal, Version=$(UnitTestPlatformVersion)" />
</ItemGroup>
<ItemGroup>
<Compile Include="ModelsTests\HelperTest.cs" />
<Compile Include="ViewModelTests\ImageResizer.cs" />
<Compile Include="ModelsTests\BasePTModuleSettingsTest.cs" />
<Compile Include="ModelsTests\BasePTSettingsTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit 7fc140a

Please sign in to comment.