Skip to content

Commit

Permalink
temp commit
Browse files Browse the repository at this point in the history
stuff that's still a bit busted

- RemoteDownloader
- WebHelper
- GetPackageParameterCommand (maybe?)
  • Loading branch information
vexx32 committed Oct 15, 2024
1 parent 5ace56d commit 97e5b7b
Show file tree
Hide file tree
Showing 45 changed files with 5,324 additions and 46 deletions.
33 changes: 33 additions & 0 deletions src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,51 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\AddChocolateyPinnedTaskbarItemCommand.cs" />
<Compile Include="Commands\ExpandChocolateyArchiveCommand.cs" />
<Compile Include="Commands\GetChocolateyConfigValueCommand.cs" />
<Compile Include="Commands\GetChocolateyPathCommand.cs" />
<Compile Include="Commands\AssertValidChecksumCommand.cs" />
<Compile Include="Shared\ChecksumExeNotFoundException.cs" />
<Compile Include="Shared\ChecksumVerificationFailedException.cs" />
<Compile Include="Shared\ChecksumMissingException.cs" />
<Compile Include="Commands\GetEnvironmentVariableCommand.cs" />
<Compile Include="Commands\GetEnvironmentVariableNameCommand.cs" />
<Compile Include="Commands\GetEnvironmentVariableNamesCommand.cs" />
<Compile Include="Commands\GetOsArchitectureWidthCommand.cs" />
<Compile Include="Commands\GetPackageParameterCommand.cs" />
<Compile Include="Commands\GetToolsLocationCommand.cs" />
<Compile Include="Commands\GetUacEnabledCommand.cs" />
<Compile Include="Commands\GetUninstallRegistryKeyCommand.cs" />
<Compile Include="Commands\GetVirusCheckValidCommand.cs" />
<Compile Include="Commands\InstallChocolateyEnvironmentVariableCommand.cs" />
<Compile Include="Commands\InstallChocolateyExplorerMenuItemCommand.cs" />
<Compile Include="Commands\InstallChocolateyFileAssociationCommand.cs" />
<Compile Include="Commands\InstallChocolateyInstallPackageCommand.cs" />
<Compile Include="Commands\InstallChocolateyPackageCommand.cs" />
<Compile Include="Commands\InstallChocolateyPathCommand.cs" />
<Compile Include="Commands\InstallChocolateyPowerShellCommandCommand.cs" />
<Compile Include="Commands\NewShimCommand.cs" />
<Compile Include="Commands\SetEnvironmentVariableCommand.cs" />
<Compile Include="Commands\StartChocolateyProcessCommand.cs" />
<Compile Include="Commands\TestProcessAdminRightsCommand.cs" />
<Compile Include="Commands\TestProcessRunningAsAdminCommand.cs" />
<Compile Include="Commands\UninstallChocolateyPathCommand.cs" />
<Compile Include="Commands\UpdateSessionEnvironmentCommand.cs" />
<Compile Include="Extensions\DoubleExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Helpers\ArchitectureWidth.cs" />
<Compile Include="Helpers\CancellableSleepHelper.cs" />
<Compile Include="Helpers\ChecksumValidator.cs" />
<Compile Include="Helpers\Elevation.cs" />
<Compile Include="Helpers\EnvironmentHelper.cs" />
<Compile Include="Helpers\Paths.cs" />
<Compile Include="Helpers\ProcessInformation.cs" />
<Compile Include="Helpers\PSHelper.cs" />
<Compile Include="Helpers\SevenZipHelper.cs" />
<Compile Include="Helpers\StartChocolateyProcessHelper.cs" />
<Compile Include="Helpers\WebHelper.cs" />
<Compile Include="Helpers\WindowsInstallerHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\SolutionVersion.cs">
<Link>Properties\SolutionVersion.cs</Link>
Expand All @@ -84,6 +111,12 @@
<Compile Include="Shared\ChocolateyCmdlet.cs" />
<Compile Include="Shared\ChocolateyPathType.cs" />
<Compile Include="Shared\EnvironmentVariables.cs" />
<Compile Include="Shared\JankySwitchTransformAttribute.cs" />
<Compile Include="Shared\ProcessHandler.cs" />
<Compile Include="Shared\ProxySettings.cs" />
<Compile Include="Shared\RemoteDownloader.cs" />
<Compile Include="Shared\PreferenceVariables.cs" />
<Compile Include="Shared\WebResources.cs" />
<Compile Include="Win32\NativeMethods.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text;
using Chocolatey.PowerShell.Helpers;
using Chocolatey.PowerShell.Shared;

namespace Chocolatey.PowerShell.Commands
{
[Cmdlet(VerbsCommon.Add, "ChocolateyPinnedTaskbarItem")]
public class AddChocolateyPinnedTaskbarItemCommand : ChocolateyCmdlet
{
/*
.SYNOPSIS
Creates an item in the task bar linking to the provided path.
.NOTES
Does not work with SYSTEM, but does not error. It warns with the error
message.
.INPUTS
None
.OUTPUTS
None
.PARAMETER TargetFilePath
The path to the application that should be launched when clicking on the
task bar icon.
.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.
.EXAMPLE
>
# This will create a Visual Studio task bar icon.
Install-ChocolateyPinnedTaskBarItem -TargetFilePath "${env:ProgramFiles(x86)}\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"
.LINK
Install-ChocolateyShortcut
.LINK
Install-ChocolateyExplorerMenuItem
*/

[Parameter(Mandatory = true, Position = 0)]
[Alias("TargetFilePath")]
public string Path { get; set; } = string.Empty;

protected override void End()
{
const string verb = "Pin To Taskbar";
var targetFolder = PSHelper.GetParentDirectory(this, Path);
var targetItem = PSHelper.GetFileName(Path);

try
{
if (!PSHelper.ItemExists(this, Path))
{
WriteWarning($"'{Path}' does not exist, not able to pin to task bar");
return;
}

dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
var folder = shell.NameSpace(targetFolder);
var item = folder.ParseName(targetItem);

bool verbFound = false;
foreach (var itemVerb in item.Verbs())
{
var name = (string)itemVerb.Name;
if (name.Replace("&", string.Empty) == verb)
{
verbFound = true;
itemVerb.DoIt();
break;
}
}

if (!verbFound)
{
WriteHost($"TaskBar verb not found for {targetItem}. It may have already been pinned");
}

WriteHost($"'{Path}' has been pinned to the task bar on your desktop");
}
catch (Exception ex)
{
WriteWarning($"Unable to create pin. Error captured was {ex.Message}.");
}

base.EndProcessing();
}
}
}
139 changes: 139 additions & 0 deletions src/Chocolatey.PowerShell/Commands/ExpandChocolateyArchiveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Management.Automation;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Chocolatey.PowerShell.Helpers;
using Chocolatey.PowerShell.Shared;

namespace Chocolatey.PowerShell.Commands
{
[Cmdlet(VerbsData.Expand, "ChocolateyArchive", DefaultParameterSetName = "Path")]
[OutputType(typeof(string))]
public class ExpandChocolateyArchiveCommand : ChocolateyCmdlet
{
/*
.SYNOPSIS
Unzips an archive file and returns the location for further processing.
.DESCRIPTION
This unzips files using the 7-zip command line tool 7z.exe.
Supported archive formats are listed at:
https://sevenzip.osdn.jp/chm/general/formats.htm
.INPUTS
None
.OUTPUTS
Returns the passed in $destination.
.NOTES
If extraction fails, an exception is thrown.
If you are embedding files into a package, ensure that you have the
rights to redistribute those files if you are sharing this package
publicly (like on the community feed). Otherwise, please use
Install-ChocolateyZipPackage to download those resources from their
official distribution points.
Will automatically call Set-PowerShellExitCode to set the package exit code
based on 7-zip's exit code.
.PARAMETER FileFullPath
This is the full path to the zip file. If embedding it in the package
next to the install script, the path will be like
`"$(Split-Path -Parent $MyInvocation.MyCommand.Definition)\\file.zip"`
`File` is an alias for FileFullPath.
This can be a 32-bit or 64-bit file. This is mandatory in earlier versions
of Chocolatey, but optional if FileFullPath64 has been provided.
.PARAMETER FileFullPath64
Full file path to a 64-bit native installer to run.
If embedding in the package, you can get it to the path with
`"$(Split-Path -parent $MyInvocation.MyCommand.Definition)\\INSTALLER_FILE"`
Provide this when you want to provide both 32-bit and 64-bit
installers or explicitly only a 64-bit installer (which will cause a package
install failure on 32-bit systems).
.PARAMETER Destination
This is a directory where you would like the unzipped files to end up.
If it does not exist, it will be created.
.PARAMETER SpecificFolder
OPTIONAL - This is a specific directory within zip file to extract. The
folder and its contents will be extracted to the destination.
.PARAMETER PackageName
OPTIONAL - This will facilitate logging unzip activity for subsequent
uninstalls
.PARAMETER DisableLogging
OPTIONAL - This disables logging of the extracted items. It speeds up
extraction of archives with many files.
Usage of this parameter will prevent Uninstall-ChocolateyZipPackage
from working, extracted files will have to be cleaned up with
Remove-Item or a similar command instead.
.PARAMETER IgnoredArguments
Allows splatting with arguments that do not apply. Do not use directly.
.EXAMPLE
>
# Path to the folder where the script is executing
$toolsDir = (Split-Path -parent $MyInvocation.MyCommand.Definition)
Get-ChocolateyUnzip -FileFullPath "c:\someFile.zip" -Destination $toolsDir
.LINK
Install-ChocolateyZipPackage
*/

[Alias("File", "FileFullPath")]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "Path")]
public string Path { get; set; } = string.Empty;

[Alias("UnzipLocation")]
[Parameter(Mandatory = true, Position = 1)]
public string Destination { get; set; } = string.Empty;

[Parameter(Position = 2)]
public string SpecificFolder { get; set; }

[Parameter(Position = 3)]
public string PackageName { get; set; }

[Alias("File64", "FileFullPath64")]
[Parameter(Mandatory = true, ParameterSetName = "Path64")]
[Parameter(ParameterSetName = "Path")]
public string Path64 { get; set; }

[Parameter]
public SwitchParameter DisableLogging { get; set; }

protected override void End()
{
// This case should be prevented by the parameter set definitions,
// but it doesn't hurt to make absolutely sure here as well.
if (!(BoundParameters.ContainsKey(nameof(Path)) || BoundParameters.ContainsKey(nameof(Path64))))
{
ThrowTerminatingError(new RuntimeException("Parameters are incorrect; either -Path or -Path64 must be specified.").ErrorRecord);
}

var helper = new SevenZipHelper(this, PipelineStopToken);
helper.Run7zip(Path, Path64, PackageName, Destination, SpecificFolder, DisableLogging);

WriteObject(Destination);

base.EndProcessing();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using chocolatey;
using Chocolatey.PowerShell;
using Chocolatey.PowerShell.Helpers;
using Chocolatey.PowerShell.Shared;
using System;
using System.Collections;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Chocolatey.PowerShell.Commands
{
[Cmdlet(VerbsCommon.Get, "ChocolateyConfigValue")]
public class GetChocolateyConfigValueCommand : ChocolateyCmdlet
{
[Parameter(Mandatory = true)]
public string ConfigKey { get; set; }

protected override void End()
{
var result = GetConfigValue(ConfigKey);

WriteObject(result);
}

private string GetConfigValue(string key)
{
if (key is null)
{
return null;
}

string configString = null;
Exception error = null;
foreach (var reader in InvokeProvider.Content.GetReader(ApplicationParameters.GlobalConfigFileLocation))
{
try
{
var results = reader.Read(1);
if (results.Count > 0)
{
configString = PSHelper.ConvertTo<string>(results[0]);
break;
}
}
catch (Exception ex)
{
WriteWarning($"Could not read configuration file: {ex.Message}");
}
}

if (configString is null)
{
// TODO: Replace RuntimeException
var exception = error is null
? new RuntimeException("Config file is missing or empty.")
: new RuntimeException($"Config file is missing or empty. Error reading configuration file: {error.Message}", error);
ThrowTerminatingError(exception.ErrorRecord);
}

var xmlConfig = new XmlDocument();
xmlConfig.LoadXml(configString);

foreach (XmlNode configEntry in xmlConfig.SelectNodes("chocolatey/config/add"))
{
var nodeKey = configEntry.Attributes["key"];
if (nodeKey is null || !IsEqual(nodeKey.Value, ConfigKey))
{
continue;
}

var value = configEntry.Attributes["value"];
if (!(value is null))
{
// We don't support duplicate config entries; once found, we're done here.
return value.Value;
}
}

return null;
}
}
}
Loading

0 comments on commit 97e5b7b

Please sign in to comment.