Skip to content

Commit 546dae7

Browse files
Allow custom PinGet executable paths (#4872)
1 parent e8918f5 commit 546dae7

5 files changed

Lines changed: 165 additions & 29 deletions

File tree

‎src/UniGetUI.Avalonia/Views/Pages/SettingsPages/PackageManagerPage.axaml.cs‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Avalonia.Input.Platform;
44
using Avalonia.Layout;
55
using Avalonia.Media;
6+
using Avalonia.Platform.Storage;
67
using UniGetUI.Avalonia.Infrastructure;
78
using UniGetUI.Avalonia.ViewModels;
89
using UniGetUI.Avalonia.ViewModels.Pages.SettingsPages;
@@ -78,7 +79,7 @@ private void BuildPage()
7879

7980
var execHint = new TextBlock
8081
{
81-
Text = CoreTools.Translate("Not finding the file you are looking for? Make sure it has been added to path."),
82+
Text = CoreTools.Translate("Not finding the file you are looking for? Browse to it or make sure it has been added to PATH."),
8283
FontSize = 12,
8384
FontWeight = FontWeight.SemiBold,
8485
Opacity = 0.7,
@@ -90,14 +91,21 @@ private void BuildPage()
9091
var execCombo = new ComboBox { HorizontalAlignment = HorizontalAlignment.Stretch };
9192
AutomationProperties.SetName(execCombo, CoreTools.Translate("Select the executable to be used. The following list shows the executables found by UniGetUI"));
9293
foreach (var path in manager.FindCandidateExecutableFiles())
93-
execCombo.Items.Add(path);
94+
AddExecutablePathItem(execCombo, path);
9495

9596
string savedPath = CoreSettings.GetDictionaryItem<string, string>(CoreSettings.K.ManagerPaths, manager.Name) ?? "";
97+
if (!string.IsNullOrEmpty(savedPath) && File.Exists(savedPath))
98+
AddExecutablePathItem(execCombo, savedPath);
9699
if (string.IsNullOrEmpty(savedPath))
97100
{
98101
var (found, path) = manager.GetExecutableFile();
99102
savedPath = found ? path : "";
100103
}
104+
else if (!File.Exists(savedPath))
105+
{
106+
var (found, path) = manager.GetExecutableFile();
107+
savedPath = found ? path : "";
108+
}
101109
execCombo.SelectedItem = savedPath;
102110
execCombo.IsEnabled = customPathsAllowed;
103111
execCombo.SelectionChanged += (s, _) =>
@@ -106,9 +114,37 @@ private void BuildPage()
106114
ViewModel.OnExecutableSelected(selected);
107115
};
108116
Grid.SetRow(execCombo, 1);
109-
Grid.SetColumnSpan(execCombo, 2);
117+
Grid.SetColumn(execCombo, 0);
110118
execGrid.Children.Add(execCombo);
111119

120+
var browseExecutableButton = new Button
121+
{
122+
Content = CoreTools.Translate("Browse..."),
123+
IsEnabled = customPathsAllowed,
124+
Margin = new Thickness(8, 0, 0, 0),
125+
};
126+
browseExecutableButton.Click += async (_, _) =>
127+
{
128+
if (TopLevel.GetTopLevel(this) is not { } topLevel) return;
129+
var files = await topLevel.StorageProvider.OpenFilePickerAsync(
130+
new FilePickerOpenOptions
131+
{
132+
AllowMultiple = false,
133+
Title = CoreTools.Translate("Select executable"),
134+
FileTypeFilter = GetExecutableFileTypeFilter(),
135+
});
136+
if (files is not [{ } file]) return;
137+
138+
string? path = file.TryGetLocalPath();
139+
if (string.IsNullOrWhiteSpace(path)) return;
140+
141+
AddExecutablePathItem(execCombo, path);
142+
execCombo.SelectedItem = path;
143+
};
144+
Grid.SetRow(browseExecutableButton, 1);
145+
Grid.SetColumn(browseExecutableButton, 1);
146+
execGrid.Children.Add(browseExecutableButton);
147+
112148
if (!customPathsAllowed)
113149
{
114150
var securityWarning = new TextBlock
@@ -342,6 +378,34 @@ private void BuildPage()
342378
}
343379
}
344380

381+
private static IReadOnlyList<FilePickerFileType> GetExecutableFileTypeFilter()
382+
{
383+
if (!OperatingSystem.IsWindows())
384+
{
385+
return [new FilePickerFileType(CoreTools.Translate("All files")) { Patterns = ["*"] }];
386+
}
387+
388+
return
389+
[
390+
new FilePickerFileType(CoreTools.Translate("Executable")) { Patterns = ["*.exe"] },
391+
new FilePickerFileType(CoreTools.Translate("All files")) { Patterns = ["*"] },
392+
];
393+
}
394+
395+
private static void AddExecutablePathItem(ComboBox comboBox, string path)
396+
{
397+
if (string.IsNullOrWhiteSpace(path))
398+
return;
399+
400+
foreach (object? item in comboBox.Items)
401+
{
402+
if (string.Equals(item?.ToString(), path, StringComparison.OrdinalIgnoreCase))
403+
return;
404+
}
405+
406+
comboBox.Items.Add(path);
407+
}
408+
345409
private void BuildExtraControls(CheckboxCard_Dict disableNotifsCard)
346410
{
347411
ExtraControls.Children.Clear();

‎src/UniGetUI.PackageEngine.PackageManagerClasses/Manager/PackageManager.cs‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,16 @@ private void _logManagerInfo()
194194
public Tuple<bool, string> GetExecutableFile()
195195
{
196196
var candidates = FindCandidateExecutableFiles();
197-
if (candidates.Count == 0)
198-
{
199-
// No paths were found
200-
return new(false, "");
201-
}
202197

203198
// If custom package manager paths are DISABLED, get the first one (as old UniGetUI did) and return it.
204199
if (!SecureSettings.Get(SecureSettings.K.AllowCustomManagerPaths))
205200
{
201+
if (candidates.Count == 0)
202+
{
203+
// No paths were found
204+
return new(false, "");
205+
}
206+
206207
return new(true, candidates[0]);
207208
}
208209
else
@@ -214,32 +215,33 @@ public Tuple<bool, string> GetExecutableFile()
214215
// If there is no executable selection for this package manager
215216
if (string.IsNullOrEmpty(exeSelection))
216217
{
218+
if (candidates.Count == 0)
219+
{
220+
// No paths were found
221+
return new(false, "");
222+
}
223+
217224
return new(true, candidates[0]);
218225
}
219226
else if (!File.Exists(exeSelection))
220227
{
221228
Logger.Error(
222-
$"The selected executable path {exeSelection} for manager {Name} does not exist, the default one will be used..."
229+
$"The selected executable path {exeSelection} for manager {Name} does not exist, the default one will be used if available..."
223230
);
224-
return new(true, candidates[0]);
225231
}
226232

227-
// While technically executables that are not in the path should work,
228-
// since detection of executables will be performed on the found paths, it is more consistent
229-
// to throw an error when a non-found executable is used. Furthermore, doing this we can filter out
230-
// any invalid paths or files.
231-
if (candidates.Select(x => x.ToLower()).Contains(exeSelection.ToLower()))
233+
else
232234
{
233235
return new(true, exeSelection);
234236
}
235-
else
237+
238+
if (candidates.Count == 0)
236239
{
237-
Logger.Error(
238-
$"The selected executable path {exeSelection} for manager {Name} was not found among the candidates "
239-
+ $"(executables found are [{string.Join(',', candidates)}]), the default will be used..."
240-
);
241-
return new(true, candidates[0]);
240+
// No paths were found
241+
return new(false, "");
242242
}
243+
244+
return new(true, candidates[0]);
243245
}
244246
}
245247

‎src/UniGetUI.PackageEngine.Tests/PackageManagerTests.cs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ public void GetExecutableFileReturnsFalseWhenNoCandidatesExist()
145145
Assert.Equal(string.Empty, executable.Item2);
146146
}
147147

148+
[Fact]
149+
public void GetExecutableFileUsesSavedPathWhenNoCandidatesExistAndCustomPathsAreEnabled()
150+
{
151+
var manager = CreateManager();
152+
var customPath = CreateExecutable("custom-only.exe");
153+
manager.SetCandidateExecutableFiles();
154+
EnableCustomManagerPaths();
155+
Settings.SetDictionaryItem<string, string>(Settings.K.ManagerPaths, manager.Name, customPath);
156+
157+
var executable = manager.GetExecutableFile();
158+
159+
Assert.True(executable.Item1);
160+
Assert.Equal(customPath, executable.Item2);
161+
}
162+
148163
[Fact]
149164
public void GetExecutableFileIgnoresSavedPathWhenCustomPathsAreDisabled()
150165
{
@@ -197,7 +212,7 @@ public void GetExecutableFileFallsBackWhenSavedPathDoesNotExist()
197212
}
198213

199214
[Fact]
200-
public void GetExecutableFileFallsBackWhenSavedPathIsNotACandidate()
215+
public void GetExecutableFileUsesSavedPathWhenSavedPathIsNotACandidate()
201216
{
202217
var manager = CreateManager();
203218
var first = CreateExecutable("outside-a.exe");
@@ -214,7 +229,7 @@ public void GetExecutableFileFallsBackWhenSavedPathIsNotACandidate()
214229
var executable = manager.GetExecutableFile();
215230

216231
Assert.True(executable.Item1);
217-
Assert.Equal(first, executable.Item2);
232+
Assert.Equal(outsideCandidateList, executable.Item2);
218233
}
219234

220235
[Fact]

‎src/UniGetUI/Pages/SettingsPages/ManagersPages/PackageManager.xaml‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,23 @@
7575
FontSize="12"
7676
FontWeight="SemiBold"
7777
Opacity="0.7"
78-
Text="Not finding the file you are looking for? Make sure it has been added to path."
78+
Text="Not finding the file you are looking for? Browse to it or make sure it has been added to PATH."
7979
/>
8080
<ComboBox
8181
x:Name="ExecutableComboBox"
8282
Grid.Row="1"
83-
Grid.ColumnSpan="2"
83+
Grid.Column="0"
8484
HorizontalAlignment="Stretch"
8585
/>
86+
<Button
87+
x:Name="BrowseExecutableButton"
88+
Grid.Row="1"
89+
Grid.Column="1"
90+
Padding="12,4"
91+
Click="BrowseExecutableButton_Click"
92+
>
93+
<widgets:TranslatedTextBlock Text="Browse..." />
94+
</Button>
8695
<widgets:TranslatedTextBlock
8796
x:Name="ExeFileWarningText"
8897
Grid.Row="2"

‎src/UniGetUI/Pages/SettingsPages/ManagersPages/PackageManager.xaml.cs‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
128128
ExecutableComboBox.IsEnabled = SecureSettings.Get(
129129
SecureSettings.K.AllowCustomManagerPaths
130130
);
131+
BrowseExecutableButton.IsEnabled = SecureSettings.Get(
132+
SecureSettings.K.AllowCustomManagerPaths
133+
);
131134

132135
InstallOptionsPanel.Description = new InstallOptions_Manager(Manager);
133136
InstallOptionsPanel.Padding = new(18, 24, 18, 24);
@@ -613,16 +616,26 @@ void ApplyManagerState(bool ShowVersion = false)
613616
ExecutableComboBox.Items.Clear();
614617
foreach (var path in Manager.FindCandidateExecutableFiles())
615618
{
616-
ExecutableComboBox.Items.Add(path);
619+
AddExecutableComboBoxItem(path);
617620
}
618-
string selectedValue =
621+
string configuredValue =
619622
Settings.GetDictionaryItem<string, string>(Settings.K.ManagerPaths, Manager.Name)
620623
?? "";
624+
string selectedValue = configuredValue;
625+
if (!string.IsNullOrEmpty(configuredValue) && File.Exists(configuredValue))
626+
{
627+
AddExecutableComboBoxItem(configuredValue);
628+
}
621629
if (string.IsNullOrEmpty(selectedValue))
622630
{
623631
var exe = Manager.GetExecutableFile();
624632
selectedValue = exe.Item1 ? exe.Item2 : "";
625633
}
634+
else if (!File.Exists(selectedValue))
635+
{
636+
var exe = Manager.GetExecutableFile();
637+
selectedValue = exe.Item1 ? exe.Item2 : "";
638+
}
626639

627640
ExecutableComboBox.SelectedValue = selectedValue;
628641
ExecutableComboBox.SelectionChanged += ExecutableComboBox_SelectionChanged;
@@ -699,17 +712,50 @@ private void ManagerLogs_Click(object sender, RoutedEventArgs e)
699712

700713
private void ExecutableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
701714
{
702-
if (string.IsNullOrEmpty(ExecutableComboBox.SelectedValue.ToString()))
715+
if (ExecutableComboBox.SelectedValue?.ToString() is not { Length: > 0 } selectedValue)
703716
return;
704717

705718
Settings.SetDictionaryItem(
706719
Settings.K.ManagerPaths,
707720
Manager!.Name,
708-
ExecutableComboBox.SelectedValue.ToString()
721+
selectedValue
709722
);
710723
_ = ReloadPackageManager();
711724
}
712725

726+
private void BrowseExecutableButton_Click(object sender, RoutedEventArgs e) =>
727+
_ = _browseExecutableButton_Click();
728+
729+
private async Task _browseExecutableButton_Click()
730+
{
731+
if (Manager is null)
732+
return;
733+
734+
ExternalLibraries.Pickers.FileOpenPicker picker = new(
735+
MainApp.Instance.MainWindow.GetWindowHandle()
736+
);
737+
string file = picker.Show(["*.exe"]);
738+
if (file == string.Empty)
739+
return;
740+
741+
Settings.SetDictionaryItem(Settings.K.ManagerPaths, Manager.Name, file);
742+
await ReloadPackageManager();
743+
}
744+
745+
private void AddExecutableComboBoxItem(string path)
746+
{
747+
if (string.IsNullOrWhiteSpace(path))
748+
return;
749+
750+
foreach (object? item in ExecutableComboBox.Items)
751+
{
752+
if (string.Equals(item?.ToString(), path, StringComparison.OrdinalIgnoreCase))
753+
return;
754+
}
755+
756+
ExecutableComboBox.Items.Add(path);
757+
}
758+
713759
private void GoToSecureSettingsBtn_Click(object sender, RoutedEventArgs e)
714760
{
715761
MainApp.Instance.MainWindow.NavigationPage.OpenSettingsPage(typeof(Administrator));

0 commit comments

Comments
 (0)