Skip to content

Commit 671b8a9

Browse files
committed
fix: don't open auth window if source already exists
1 parent e281e3c commit 671b8a9

File tree

5 files changed

+95
-48
lines changed

5 files changed

+95
-48
lines changed

InstallRbxcs.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ namespace Installer;
1414

1515
public static class Installation
1616
{
17-
private const uint steps = 9;
17+
private const string _sourceName = "rbxcs";
18+
private const string _sourceURL = "https://nuget.pkg.github.com/roblox-csharp/index.json";
19+
private const uint _steps = 9;
1820

19-
private static readonly float _step = (1f / steps) * 100;
21+
private static readonly float _step = (1f / _steps) * 100;
2022
private static float _progress = 0;
2123
private static Action<int>? _updateProgress;
2224
private static Action<string>? _updateTitle;
2325
private static Action? _markErrored;
2426
private static bool _errored = false;
2527
private static string _path = "";
28+
private static string _latestTag = "";
2629

2730
public static async Task InstallRbxcs(
2831
Action<int> updateProgress,
@@ -36,9 +39,6 @@ string path
3639
_markErrored = markErrored;
3740
_path = path;
3841

39-
// if (OperatingSystem.IsWindows() && !IsAdmin())
40-
// ShowErrorMessageBox($"Cannot install. You are not running with elevated privileges.\nRestart the app as an administrator and try again.");
41-
4242
if (_errored) return;
4343
Display("Creating installation environment...");
4444

@@ -95,37 +95,43 @@ string path
9595
StepProgress();
9696

9797
Display("Fetching latest release...");
98-
var latestTag = ExecuteGitCommand("describe --tags --abbrev=0", "Failed to get the latest release tag");
98+
_latestTag = ExecuteGitCommand("describe --tags --abbrev=0", "Failed to get the latest release tag");
9999
StepProgress();
100100

101101
Display("Checking out latest release...");
102-
ExecuteGitCommand($"checkout {latestTag}", "Failed to checkout the latest release");
102+
ExecuteGitCommand($"checkout {_latestTag}", "Failed to checkout the latest release");
103103
StepProgress();
104104

105105
Display("Building roblox-cs...");
106+
var sourcesResult = ExecuteCommand(null, "dotnet", $"nuget list source");
107+
var sources = sourcesResult.StandardOutput;
108+
var sourceAlreadyAdded = sources.Contains(_sourceName) && sources.Contains(_sourceURL);
106109
await Dispatcher.UIThread.InvokeAsync(() =>
107110
{
108-
var credentialsWindow = new SourceCredentialsWindow()
111+
new SourceCredentialsWindow(sourceAlreadyAdded)
109112
{
110113
DataContext = new SourceCredentialsWindowViewModel()
111114
};
112-
credentialsWindow.Show();
113115
});
114116
}
115117

116-
public static void OnCredentialsAcquired(SourceCredentialsWindow credentialsWindow)
118+
public static void OnCredentialsAcquired(SourceCredentialsWindow credentialsWindow, bool sourceAlreadyAdded)
117119
{
118-
var usernameTextBox = credentialsWindow.FindControl<TextBox>("UsernameBox")!;
119-
var tokenTextBox = credentialsWindow.FindControl<TextBox>("TokenBox")!;
120-
Display("Adding NuGet source for RobloxCS packages...");
121-
ExecuteCommand(null, "dotnet", $"nuget add source \"https://nuget.pkg.github.com/roblox-csharp/index.json\" -u {usernameTextBox.Text} -p {tokenTextBox.Text} -n github");
122-
StepProgress();
120+
if (!sourceAlreadyAdded)
121+
{
122+
// add the source
123+
var usernameTextBox = credentialsWindow.FindControl<TextBox>("UsernameBox")!;
124+
var tokenTextBox = credentialsWindow.FindControl<TextBox>("TokenBox")!;
125+
Display("Adding NuGet source for RobloxCS packages...");
126+
ExecuteCommand(null, "dotnet", $"nuget add source \"{_sourceURL}\" -u {usernameTextBox.Text} -p {tokenTextBox.Text} -n {_sourceName}");
127+
StepProgress();
128+
}
123129

124130
Display("Compiling...");
125131
ExecuteCommand("Failed to compile roblox-cs", "dotnet", "build -c Release");
126132
StepProgress();
127133

128-
Display("Successfully built roblox-cs.");
134+
Display("Successfully compiled roblox-cs.");
129135
if (OperatingSystem.IsWindows())
130136
{
131137
UpdateEnvironmentPath(_path);
@@ -137,7 +143,7 @@ public static void OnCredentialsAcquired(SourceCredentialsWindow credentialsWind
137143
Display("Successfully added roblox-cs to your PATH.");
138144

139145
StepProgress();
140-
Display("Successfully installed roblox-cs.");
146+
Display($"Successfully installed roblox-cs ({_latestTag}).");
141147
}
142148

143149
private static void UpdateEnvironmentPath(string path)
@@ -216,8 +222,9 @@ private static ProcessResult ExecuteCommand(string? errorMessage, string command
216222
};
217223

218224
if (result.ExitCode != 0 && errorMessage != null)
225+
{
219226
ShowErrorMessageBox($"{errorMessage}: {(!string.IsNullOrEmpty(result.StandardError) ? result.StandardError : result.StandardOutput)}");
220-
227+
}
221228
return result;
222229
}
223230

Installer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview1" />
2525
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview1" />
2626
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview1" />
27+
<PackageReference Include="LibGit2Sharp" Version="0.30.0" />
2728
<PackageReference Include="MessageBox.Avalonia" Version="2.2.0" />
2829
<PackageReference Include="XamlNameReferenceGenerator" Version="1.6.1" />
2930
</ItemGroup>

RobloxCSInstaller.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ VisualStudioVersion = 17.6.33717.318
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Installer", "Installer.csproj", "{4193B9CF-A584-4A3C-9D4E-AB56C51A0CC9}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AA9AF768-F53F-4E72-81B6-452F4CDC3F52}"
9+
ProjectSection(SolutionItems) = preProject
10+
README.md = README.md
11+
EndProjectSection
12+
EndProject
813
Global
914
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1015
Debug|Any CPU = Debug|Any CPU

ViewModels/MainWindowViewModel.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using ReactiveUI;
66
using Avalonia.Threading;
77
using MessageBox.Avalonia;
8+
using System.Threading.Tasks;
89

910
namespace Installer.ViewModels;
1011

@@ -85,7 +86,7 @@ private string GetDefaultInstallationDirectory()
8586
return defaultDirectory;
8687
}
8788

88-
private async void InstallRbxcs()
89+
private void InstallRbxcs()
8990
{
9091
ProgressBarVisible = true;
9192
IsNotInstalling = false;
@@ -95,33 +96,35 @@ private async void InstallRbxcs()
9596
string fullSelectedDir = Path.GetFullPath(_selectedDirectory);
9697
string absolutePath = Path.Combine(fullSelectedDir, "roblox-cs");
9798

98-
try
99+
Dispatcher.UIThread.InvokeAsync(async () =>
99100
{
100-
await Installation.InstallRbxcs(
101-
UpdateProgress,
102-
UpdateTitle,
103-
MarkErrored,
104-
absolutePath
105-
);
106-
}
107-
catch (Exception err)
108-
{
109-
MarkErrored();
110-
UpdateTitle("Error!");
111-
112-
await Dispatcher.UIThread.InvokeAsync(() =>
101+
try
113102
{
114-
MessageBoxManager
115-
.GetMessageBoxStandardWindow("Error", err.Message)
116-
.Show()
117-
.ContinueWith(_ => Environment.Exit(1));
118-
});
119-
}
120-
121-
122-
ProgressBarVisible = false;
123-
if (_errored) return;
124-
FinishedCloseVisible = true;
103+
await Installation.InstallRbxcs(
104+
UpdateProgress,
105+
UpdateTitle,
106+
MarkErrored,
107+
absolutePath
108+
);
109+
}
110+
catch (Exception err)
111+
{
112+
MarkErrored();
113+
UpdateTitle("Error!");
114+
115+
await Dispatcher.UIThread.InvokeAsync(() =>
116+
{
117+
MessageBoxManager
118+
.GetMessageBoxStandardWindow("Error", err.Message)
119+
.Show()
120+
.ContinueWith(_ => Environment.Exit(1));
121+
});
122+
}
123+
124+
ProgressBarVisible = false;
125+
if (_errored) return;
126+
FinishedCloseVisible = true;
127+
});
125128
}
126129

127130
private async void SuccessfulExit()
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
using Avalonia.Controls;
22
using Avalonia.Interactivity;
33
using Avalonia.Markup.Xaml;
4+
using Avalonia.Threading;
45
using Installer.ViewModels;
56

67
namespace Installer.Views;
78

89
public partial class SourceCredentialsWindow : Window
910
{
1011
new SourceCredentialsWindowViewModel? DataContext { get; set; }
12+
private readonly bool _sourceAlreadyAdded;
1113

12-
public SourceCredentialsWindow()
14+
public SourceCredentialsWindow() : this(false)
15+
{
16+
}
17+
18+
public SourceCredentialsWindow(bool sourceAlreadyAdded)
1319
{
1420
DataContext = new SourceCredentialsWindowViewModel();
21+
_sourceAlreadyAdded = sourceAlreadyAdded;
22+
if (_sourceAlreadyAdded)
23+
{
24+
ContinueInstallation();
25+
return;
26+
}
27+
28+
Closed += (s, e) => ContinueInstallation();
1529
InitializeComponent();
30+
Show();
1631
}
1732

1833
private void InitializeComponent()
19-
=> AvaloniaXamlLoader.Load(this);
34+
=> AvaloniaXamlLoader.Load(this);
2035

2136
private void SubmitButton_Click(object sender, RoutedEventArgs e)
2237
{
23-
Installation.OnCredentialsAcquired(this);
24-
Close();
38+
ContinueInstallation(true);
39+
}
40+
41+
private void ContinueInstallation(bool close)
42+
{
43+
Dispatcher.UIThread.InvokeAsync(() =>
44+
{
45+
Installation.OnCredentialsAcquired(this, _sourceAlreadyAdded);
46+
if (close)
47+
{
48+
Close();
49+
}
50+
});
51+
}
52+
53+
private void ContinueInstallation()
54+
{
55+
ContinueInstallation(false);
2556
}
2657
}

0 commit comments

Comments
 (0)