Skip to content

Commit

Permalink
(chocolatey#1005) Normalize version numbers
Browse files Browse the repository at this point in the history
This updates all displaying of version numbers
and usages internally to ensure that the version
numbers are normalized.

This is the equivalent of changes made in Chocolatey CLI.
  • Loading branch information
AdmiringWorm authored and gep13 committed Jun 6, 2023
1 parent 5402115 commit 834479d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<Compile Include="Services\IPackageArgumentsService.cs" />
<Compile Include="Services\PackageArgumentsService.cs" />
<Compile Include="Utilities\Converters\LocalizationConverter.cs" />
<Compile Include="Utilities\Converters\NuGetVersionToString.cs" />
<Compile Include="Utilities\Extensions\LocalizeExtension.cs" />
<Compile Include="Utilities\Converters\NullToInverseBool.cs" />
<Compile Include="Utilities\ToolTipBehavior.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public async Task<PackageOperationResult> InstallPackage(
if (version != null)
{
config.Version = version.ToString();
config.Version = version;
}
if (source != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace ChocolateyGui.Common.Windows.Utilities.Converters
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using chocolatey;
using NuGet.Versioning;

public class NuGetVersionToString : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is NuGetVersion version)
{
return version.ToNormalizedStringChecked();
}

return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string sValue && NuGetVersion.TryParse(sValue, out var version))
{
return version;
}

throw new InvalidOperationException("The passed in value is not a parseable string version!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public AdvancedInstallViewModel(

_cts = new CancellationTokenSource();

_packageVersion = packageVersion.ToString();
_packageVersion = packageVersion.ToNormalizedStringChecked();
SelectedVersion = _packageVersion;

FetchAvailableVersions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using AutoMapper;
using Caliburn.Micro;
using chocolatey;
using ChocolateyGui.Common.Base;
using ChocolateyGui.Common.Models;
using ChocolateyGui.Common.Models.Messages;
Expand Down Expand Up @@ -405,7 +406,8 @@ public bool IsPackageSizeAvailable

public async Task ShowArguments()
{
var decryptedArguments = _packageArgumentsService.DecryptPackageArgumentsFile(Id, Version.ToString()).ToList();
// TODO: Add legacy handling for packages installed prior to v2.0.0.
var decryptedArguments = _packageArgumentsService.DecryptPackageArgumentsFile(Id, Version.ToNormalizedStringChecked()).ToList();

if (decryptedArguments.Count == 0)
{
Expand All @@ -423,7 +425,7 @@ await _dialogService.ShowMessageAsync(

public async Task Install()
{
await InstallPackage(Version.ToString());
await InstallPackage(Version.ToNormalizedStringChecked());
}

public async Task InstallAdvanced()
Expand Down Expand Up @@ -465,7 +467,7 @@ public async Task Reinstall()
{
using (await StartProgressDialog(L(nameof(Resources.PackageViewModel_ReinstallingPackage)), L(nameof(Resources.PackageViewModel_ReinstallingPackage)), Id))
{
await _chocolateyService.InstallPackage(Id, Version.ToString(), Source, true);
await _chocolateyService.InstallPackage(Id, Version.ToNormalizedStringChecked(), Source, true);
_chocolateyGuiCacheService.PurgeOutdatedPackages();
await _eventAggregator.PublishOnUIThreadAsync(new PackageChangedMessage(Id, PackageChangeType.Installed, Version));
}
Expand Down Expand Up @@ -496,7 +498,7 @@ public async Task Uninstall()
{
using (await StartProgressDialog(L(nameof(Resources.PackageViewModel_UninstallingPackage)), L(nameof(Resources.PackageViewModel_UninstallingPackage)), Id))
{
var result = await _chocolateyService.UninstallPackage(Id, Version.ToString(), true);
var result = await _chocolateyService.UninstallPackage(Id, Version.ToNormalizedStringChecked(), true);

if (!result.Successful)
{
Expand Down Expand Up @@ -584,7 +586,7 @@ public async Task Pin()
{
using (await StartProgressDialog(L(nameof(Resources.PackageViewModel_PinningPackage)), L(nameof(Resources.PackageViewModel_PinningPackage)), Id))
{
var result = await _chocolateyService.PinPackage(Id, Version.ToString());
var result = await _chocolateyService.PinPackage(Id, Version.ToNormalizedStringChecked());

if (!result.Successful)
{
Expand Down Expand Up @@ -628,7 +630,7 @@ public async Task Unpin()
{
using (await StartProgressDialog(L(nameof(Resources.PackageViewModel_UnpinningPackage)), L(nameof(Resources.PackageViewModel_UnpinningPackage)), Id))
{
var result = await _chocolateyService.UnpinPackage(Id, Version.ToString());
var result = await _chocolateyService.UnpinPackage(Id, Version.ToNormalizedStringChecked());

if (!result.Successful)
{
Expand Down
10 changes: 6 additions & 4 deletions Source/ChocolateyGui.Common.Windows/Views/LocalSourceView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
xmlns:commands="clr-namespace:ChocolateyGui.Common.Windows.Commands"
xmlns:controls="clr-namespace:ChocolateyGui.Common.Windows.Controls"
xmlns:theming="clr-namespace:ChocolateyGui.Common.Windows.Theming"
xmlns:converters="clr-namespace:ChocolateyGui.Common.Windows.Utilities.Converters"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1366"
d:DataContext="{d:DesignInstance viewModels:LocalSourceViewModel}"
d:DataContext="{d:DesignInstance {x:Type viewModels:LocalSourceViewModel}}"
Background="{DynamicResource MahApps.Brushes.ThemeBackground}">

<UserControl.Resources>
<converters:NuGetVersionToString x:Key="NuGetVersionToString"/>
<utilities:BindingProxy x:Key="BindingProxy" Data="{Binding}" />
<utilities:PackageAuthorsComparer x:Key="PackageAuthorsComparer" />

Expand Down Expand Up @@ -59,12 +61,12 @@
<TextBlock Grid.Row="2"
Margin="4 0"
Style="{DynamicResource TileLatestVersionTextStyle}"
Text="{Binding LatestVersion, Mode=OneWay}" />
Text="{Binding LatestVersion, Converter={StaticResource NuGetVersionToString}, Mode=OneWay}" />

<TextBlock Grid.Row="3"
Margin="4 0 4 1"
Style="{DynamicResource TileVersionTextStyle}"
Text="{Binding Version, Mode=OneWay}" />
Text="{Binding Version, Converter={StaticResource NuGetVersionToString}, Mode=OneWay}" />

<ContentControl x:Name="OutOfDateOverlay" Grid.Row="0" Grid.RowSpan="4"
Visibility="Collapsed"
Expand Down Expand Up @@ -320,7 +322,7 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding LatestVersion, IsAsync=True}"
Text="{Binding LatestVersion, Converter={StaticResource NuGetVersionToString}, IsAsync=True}"
Padding="3 3 2 3"
VerticalAlignment="Stretch"
HorizontalAlignment="Left"
Expand Down
3 changes: 2 additions & 1 deletion Source/ChocolateyGui.Common.Windows/Views/PackageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</FrameworkElement.CommandBindings>

<UserControl.Resources>
<converters:NuGetVersionToString x:Key="NuGetVersionToString"/>
<converters:NullToVisibility x:Key="UriToVisibility" />
<converters:NullToVisibility x:Key="NullToVisibility" />
<converters:LongSizeToFileSizeString x:Key="LongSizeToFileSizeString" />
Expand Down Expand Up @@ -196,7 +197,7 @@
<Label Style="{StaticResource PackageResourceLabel}"
Content="{lang:Localize PackageView_Version}"
Target="{Binding ElementName=Version}" />
<TextBlock x:Name="Version" Text="{Binding Version}"
<TextBlock x:Name="Version" Text="{Binding Version, Converter={StaticResource NuGetVersionToString}}"
Style="{StaticResource PackageResourceValue}" />

<Label Style="{StaticResource PackageResourceLabel}" Content="{lang:Localize PackageView_Downloads}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
xmlns:commands="clr-namespace:ChocolateyGui.Common.Windows.Commands"
xmlns:controls="clr-namespace:ChocolateyGui.Common.Windows.Controls"
xmlns:theming="clr-namespace:ChocolateyGui.Common.Windows.Theming"
xmlns:converters="clr-namespace:ChocolateyGui.Common.Windows.Utilities.Converters"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1366"
d:DataContext="{d:DesignInstance viewModels:RemoteSourceViewModel}"
Background="{DynamicResource MahApps.Brushes.ThemeBackground}">

<UserControl.Resources>
<ResourceDictionary>
<converters:NuGetVersionToString x:Key="NuGetVersionToString"/>
<DataTemplate x:Key="PackageListTemplate" DataType="{x:Type items:IPackageViewModel}">
<Grid Height="115" Margin="5"
Background="Transparent"
Expand All @@ -43,7 +45,7 @@
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Title" Mode="OneWay" />
<Binding Path="Version" Mode="OneWay" />
<Binding Path="Version" Mode="OneWay" Converter="{StaticResource NuGetVersionToString}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Expand Down Expand Up @@ -151,7 +153,7 @@
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Title" Mode="OneWay" />
<Binding Path="Version" Mode="OneWay" />
<Binding Path="Version" Mode="OneWay" Converter="{StaticResource NuGetVersionToString}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Expand Down Expand Up @@ -270,7 +272,7 @@
<TextBlock Grid.Row="2"
Margin="4 0 4 1"
Style="{DynamicResource TileVersionTextStyle}"
Text="{Binding Version, Mode=OneWay}" />
Text="{Binding Version, Converter={StaticResource NuGetVersionToString}, Mode=OneWay}" />

<ContentControl x:Name="IsInstalledOverlay" Grid.Row="0" Grid.RowSpan="3"
Visibility="Collapsed"
Expand Down

0 comments on commit 834479d

Please sign in to comment.