Skip to content

Commit

Permalink
(chocolatey#770) Begins to scaffold a Package Arguments button
Browse files Browse the repository at this point in the history
* Adds PackageArgumentsService Interface
* Adds button to PackageViewModel
* Adds method for decrypting .arguments file
  • Loading branch information
steviecoaster committed Mar 28, 2021
1 parent 7c94a89 commit 3503989
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -208,6 +209,8 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Services\IPackageArgumentsService.cs" />
<Compile Include="Services\PackageArgumentsService.cs" />
<Compile Include="Utilities\ToolTipBehavior.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Commands\CommandExecutionManager.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="IPackageArgumentsService.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChocolateyGui.Common.Windows.Services
{
public interface IPackageArgumentsService
{
string Decrypt(string id, string version);

string Encrypt(string id, string version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="PackageArgumentsService.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks;
using chocolatey;

namespace ChocolateyGui.Common.Windows.Services
{
public class PackageArgumentsService : IPackageArgumentsService
{
private static readonly byte[] EntropyBytes = Encoding.UTF8.GetBytes("Chocolatey");

public string Decrypt(string id, string version)
{
var packageFolder = "{0}.{1}";
var packageId = id;
var packageVersion = version;
var chocolateyPath = @"C:\ProgramData\chocolatey\.chocolatey";
var argumentsPath = Path.Combine(chocolateyPath, packageFolder.format_with(id, version));
var argumentsFile = Path.Combine(argumentsPath, ".arguments");

// Get the arguments decrypted in here and return them
try
{
if (File.Exists(argumentsFile))
{
string fileName = argumentsFile;
var encryptedBytes = Convert.FromBase64String(File.ReadAllText(fileName));
var decryptedBytes = ProtectedData.Unprotect(encryptedBytes, EntropyBytes, DataProtectionScope.LocalMachine);
string decryptedChocoArgs = Encoding.UTF8.GetString(decryptedBytes);
return decryptedChocoArgs;
}
else
{
throw new FileNotFoundException();
}
}
catch (FileNotFoundException)
{
return "Chocolatey argument file not found";
}
catch (Exception ex)
{
return ex.Message;
}

}

public string Encrypt(string id, string version)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<ChocolateyConfigurationProvider>().As<IChocolateyConfigurationProvider>().SingleInstance();
builder.RegisterType<ChocolateyService>().As<IChocolateyService>().SingleInstance();
builder.RegisterType<DotNetFileSystem>().As<chocolatey.infrastructure.filesystem.IFileSystem>().SingleInstance();
builder.RegisterType<PackageArgumentsService>().As<IPackageArgumentsService>().SingleInstance();

// Register ViewModels
builder.RegisterAssemblyTypes(viewModelAssembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using AutoMapper;
using Caliburn.Micro;
using ChocolateyGui.Common.Base;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class PackageViewModel :
private readonly IChocolateyGuiCacheService _chocolateyGuiCacheService;
private readonly IConfigService _configService;
private readonly IAllowedCommandsService _allowedCommandsService;
private readonly IPackageArgumentsService _packageArgumentsService;

private string[] _authors;

Expand Down Expand Up @@ -118,7 +120,8 @@ public PackageViewModel(
IProgressService progressService,
IChocolateyGuiCacheService chocolateyGuiCacheService,
IConfigService configService,
IAllowedCommandsService allowedCommandsService)
IAllowedCommandsService allowedCommandsService,
IPackageArgumentsService packageArgumentsService)
{
_chocolateyService = chocolateyService;
_eventAggregator = eventAggregator;
Expand All @@ -128,6 +131,7 @@ public PackageViewModel(
_chocolateyGuiCacheService = chocolateyGuiCacheService;
_configService = configService;
_allowedCommandsService = allowedCommandsService;
_packageArgumentsService = packageArgumentsService;
}

public DateTime Created
Expand Down Expand Up @@ -416,6 +420,12 @@ public bool IsPackageSizeAvailable
get { return PackageSize != -1; }
}

public void ShowArguments()
{
var decryptedArguments = _packageArgumentsService.Decrypt(Id, Version.ToString());
MessageBox.Show(decryptedArguments);
}

public async Task Install()
{
try
Expand Down
7 changes: 7 additions & 0 deletions Source/ChocolateyGui.Common.Windows/Views/PackageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
VerticalAlignment="Stretch"
Background="{DynamicResource MahApps.Brushes.Accent4}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,20,10">
<Button Padding="10" Margin="5 0"
Command="{commands:DataContextCommandAdapter ShowArguments}">
<StackPanel Orientation="Horizontal">
<iconPacks:PackIconEntypo Kind="Info" Margin="0 0 5 0 " VerticalAlignment="Center" />
<TextBlock Text="Package Arguments" FontSize="16"/>
</StackPanel>
</Button>
<StackPanel Orientation="Horizontal"
Visibility="{Binding IsInstalled, Converter={StaticResource BooleanToVisibility}, ConverterParameter=True}">
<Button Padding="10" Margin="5 0"
Expand Down

0 comments on commit 3503989

Please sign in to comment.