Skip to content

Commit 024693b

Browse files
committed
adding git support: handle edge cases between different selections in UI #218
1 parent 1c477e9 commit 024693b

4 files changed

Lines changed: 99 additions & 49 deletions

File tree

UnityLauncherPro/NewProject.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@
128128
</Grid>
129129

130130
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
131-
<CheckBox x:Name="chkEnableLfs" Content="Enable Git LFS " ToolTip="Enable Git Large File Storage" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,8,0,0" VerticalAlignment="Center" Checked="chkEnableLfs_Checked" Unchecked="chkEnableLfs_Checked" IsChecked="True"/>
131+
<CheckBox x:Name="chkEnableLfs" Content="Enable Git LFS " ToolTip="Enable Large File Storage and add .gitattributes" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,8,0,0" VerticalAlignment="Center" Checked="chkEnableLfs_Checked" Unchecked="chkEnableLfs_Checked" IsChecked="True"/>
132132
<CheckBox x:Name="chkInitialCommit" Content="Create initial commit" Foreground="{DynamicResource ThemeButtonForeground}" Margin="16,5,0,0" VerticalAlignment="Center" Checked="chkInitialCommit_Checked" Unchecked="chkInitialCommit_Checked" IsChecked="True"/>
133-
<CheckBox x:Name="chkAddUnityGitIgnore" Content="Add Unity .gitignore" Foreground="{DynamicResource ThemeButtonForeground}" Margin="16,5,0,0" VerticalAlignment="Center" IsEnabled="False" IsChecked="True"/>
133+
<CheckBox x:Name="chkAddUnityGitIgnore" Content="Add Unity .gitignore" Foreground="{DynamicResource ThemeButtonForeground}" Margin="16,5,0,0" VerticalAlignment="Center" IsChecked="True" Checked="chkAddUnityGitIgnore_Checked"/>
134134
</StackPanel>
135135
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
136136
<CheckBox x:Name="chkAddReadme" Content="Add README" ToolTip="Add a README file" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,8,0,0" VerticalAlignment="Center" IsChecked="True" Checked="chkAddReadme_Checked" Unchecked="chkAddReadme_Checked"/>

UnityLauncherPro/NewProject.xaml.cs

Lines changed: 82 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,21 @@ public NewProject(string unityVersion, string suggestedName, string targetFolder
128128
}
129129

130130
isInitializing = false;
131-
132-
133131
} // NewProject
134132

135133
private async void LoadSettings()
136134
{
135+
chkEnableVersionControl.IsChecked = Settings.Default.gitEnableVersionControl;
136+
137137
chkForceDX11.IsChecked = Settings.Default.forceDX11;
138138
chkEnableLfs.IsChecked = Settings.Default.gitIEnableLFS;
139+
chkEnableLfs.IsEnabled = chkEnableVersionControl.IsChecked == true;
139140
chkInitialCommit.IsChecked = Settings.Default.gitInitialCommit;
141+
chkInitialCommit.IsEnabled = chkEnableVersionControl.IsChecked == true;
140142
chkAddReadme.IsChecked = Settings.Default.gitAddReadme;
141-
//chkAddUnityGitIgnore.IsChecked = Settings.Default.gitAddUnityGitIgnore; // not used yet
142-
chkEnableVersionControl.IsChecked = Settings.Default.gitEnableVersionControl;
143-
expVersionControl.IsExpanded = Settings.Default.gitPanelExpanded || Settings.Default.gitEnableVersionControl;
143+
chkAddUnityGitIgnore.IsChecked = Settings.Default.gitAddIgnore;
144+
145+
expVersionControl.IsExpanded = Settings.Default.gitPanelExpanded || chkEnableVersionControl.IsChecked == true;
144146

145147
string token = GitHubTokenStore.LoadToken();
146148

@@ -150,21 +152,20 @@ private async void LoadSettings()
150152
return;
151153
}
152154

155+
153156
// TODO no need to validate on every load..
154157
GitHubTokenValidationResult result = await GitHubAuth.ValidateTokenAsync(token);
155158

156159
if (result.IsValid)
157160
{
158-
chkEnableVersionControl.IsChecked = true;
159161
ShowGitAuthorizedUI(true);
160-
// You can also store result.Login somewhere
161162
}
162163
else
163164
{
164-
chkEnableVersionControl.IsChecked = false;
165165
GitHubTokenStore.DeleteToken();
166166
ShowGitAuthorizedUI(false);
167167
}
168+
168169
} // LoadSettings
169170

170171
void UpdateTemplatesDropDown(string unityPath)
@@ -315,59 +316,77 @@ private async void BtnCreateNewProject_Click(object sender, RoutedEventArgs e)
315316
txtNewProjectStatus.Text += " | GitHub repo creation failed: " + ex.Message;
316317
}
317318

318-
// create readme if enabled
319-
if (chkAddReadme.IsChecked == true)
320-
{
321-
var readmePath = Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, "README.md");
322-
try
323-
{
324-
if (Directory.Exists(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text)) == false)
325-
{
326-
Directory.CreateDirectory(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text));
327-
}
328-
File.WriteAllText(readmePath, "# " + txtRepoName.Text + "\n\n" + txtRepoDescription.Text);
329-
}
330-
catch (Exception ex)
331-
{
332-
Tools.SetStatus("Failed to create README file for this project: " + ex.Message);
333-
}
334-
}
335-
336319
// create .gitattributes if LFS enabled?
337320
if (chkEnableLfs.IsChecked == true)
338321
{
339-
//var attributesUrl = "https://raw.githubusercontent.com/gitattributes/gitattributes/refs/heads/master/Unity.gitattributes";
340-
//var res = await Tools.DownloadFileAsync(attributesUrl, Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitattributes"));
341-
//if (res == false)
342-
//{
343-
// Tools.SetStatus("Failed to download .gitattributes file for this project.");
344-
//}
322+
//var gitAttributesUrl = "https://raw.githubusercontent.com/gitattributes/gitattributes/refs/heads/master/Unity.gitattributes";
345323

346324
// load from resources
347325
try
348326
{
349-
var attributesPath = Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitattributes");
350-
//File.WriteAllText(attributesPath, attributesContent);
327+
var assembly = typeof(NewProject).Assembly;
328+
var resourceName = $"{typeof(NewProject).Namespace}.Resources..gitattributes";
329+
330+
using var stream = assembly.GetManifestResourceStream(resourceName);
331+
if (stream == null) return;
332+
333+
var gitattributesPath = Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitattributes");
334+
using var fileStream = File.Create(gitattributesPath);
335+
stream.CopyTo(fileStream);
351336
}
352-
catch (Exception ex)
337+
catch
353338
{
354-
Tools.SetStatus("Failed to create .gitattributes file for this project: " + ex.Message);
339+
Tools.SetStatus("Failed to create .gitattributes file for this project.");
355340
}
356-
357341
}
342+
} // if version control enabled
358343

359-
// download .gitignore if enabled
360-
if (chkAddUnityGitIgnore.IsChecked == true)
344+
// create readme if enabled
345+
if (chkAddReadme.IsChecked == true)
346+
{
347+
var readmePath = Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, "README.md");
348+
try
361349
{
362-
var ignoreUrl = "https://raw.githubusercontent.com/github/gitignore/refs/heads/main/Unity.gitignore";
363-
var res = await Tools.DownloadFileAsync(ignoreUrl, Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitignore"));
364-
if (res == false)
350+
if (Directory.Exists(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text)) == false)
365351
{
366-
Tools.SetStatus("Failed to download .gitignore file for this project.");
352+
Directory.CreateDirectory(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text));
367353
}
354+
File.WriteAllText(readmePath, "# " + txtRepoName.Text + "\n\n" + txtRepoDescription.Text);
355+
}
356+
catch (Exception ex)
357+
{
358+
Tools.SetStatus("Failed to create README file for this project: " + ex.Message);
368359
}
360+
}
361+
369362

370363

364+
// download .gitignore if enabled
365+
if (chkAddUnityGitIgnore.IsChecked == true)
366+
{
367+
//var gitIgnoreUrl = "https://raw.githubusercontent.com/github/gitignore/refs/heads/main/Unity.gitignore";
368+
369+
// load from resources
370+
try
371+
{
372+
var assembly = typeof(NewProject).Assembly;
373+
var resourceName = $"{typeof(NewProject).Namespace}.Resources..gitignore";
374+
375+
using var stream = assembly.GetManifestResourceStream(resourceName);
376+
if (stream == null) return;
377+
378+
var gitignorePath = Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitignore");
379+
using var fileStream = File.Create(gitignorePath);
380+
stream.CopyTo(fileStream);
381+
}
382+
catch
383+
{
384+
Tools.SetStatus("Failed to create .gitignore file for this project.");
385+
}
386+
}
387+
388+
if (chkEnableVersionControl.IsChecked == true)
389+
{
371390
if (chkInitialCommit.IsChecked == true)
372391
{
373392
Console.WriteLine(1);
@@ -377,8 +396,8 @@ private async void BtnCreateNewProject_Click(object sender, RoutedEventArgs e)
377396
await GithubActions.RunGitAsync(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text), "commit -m \"Initial commit from " + MainWindow.appName + "\"");
378397
await GithubActions.RunGitAsync(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text), "push -u origin main");
379398

380-
Console.WriteLine(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text)+" add .");
381-
Console.WriteLine(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text)+ " commit -m \"Initial commit from " + MainWindow.appName + "\"");
399+
Console.WriteLine(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text) + " add .");
400+
Console.WriteLine(Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text) + " commit -m \"Initial commit from " + MainWindow.appName + "\"");
382401

383402
txtNewProjectStatus.Text += " | Initial commit created";
384403
}
@@ -624,8 +643,8 @@ private void chkEnableVersionControl_Checked(object sender, RoutedEventArgs e)
624643
txtRepoDescription.IsEnabled = state;
625644
chkEnableLfs.IsEnabled = state;
626645
chkInitialCommit.IsEnabled = state;
627-
//chkAddUnityGitIgnore.IsEnabled = state; // not used yet
628-
chkAddReadme.IsEnabled = state;
646+
//chkAddUnityGitIgnore.IsEnabled = state;
647+
//chkAddReadme.IsEnabled = state;
629648

630649
Settings.Default.gitEnableVersionControl = state;
631650
Settings.Default.Save();
@@ -1026,6 +1045,9 @@ private void ShowGitAuthorizedUI(bool show)
10261045
{
10271046
lblGithubUsername.Content = Settings.Default.gitUsername;
10281047

1048+
chkEnableVersionControl.IsChecked = true;
1049+
chkEnableVersionControl.IsEnabled = true;
1050+
10291051
txtTokenInput.Visibility = Visibility.Collapsed;
10301052
btnAuthorizeToken.Visibility = Visibility.Collapsed;
10311053
btnDisconnectToken.Visibility = Visibility.Visible;
@@ -1041,6 +1063,9 @@ private void ShowGitAuthorizedUI(bool show)
10411063
{
10421064
lblGithubUsername.Content = string.Empty;
10431065

1066+
chkEnableVersionControl.IsChecked = false;
1067+
chkEnableVersionControl.IsEnabled = false;
1068+
10441069
txtTokenInput.Visibility = Visibility.Visible;
10451070
btnAuthorizeToken.Visibility = Visibility.Visible;
10461071
btnDisconnectToken.Visibility = Visibility.Collapsed;
@@ -1118,6 +1143,14 @@ private void chkAddReadme_Checked(object sender, RoutedEventArgs e)
11181143
Settings.Default.Save();
11191144
}
11201145

1146+
private void chkAddUnityGitIgnore_Checked(object sender, RoutedEventArgs e)
1147+
{
1148+
if (isInitializing) return;
1149+
1150+
Settings.Default.gitAddIgnore = chkAddUnityGitIgnore.IsChecked == true;
1151+
Settings.Default.Save();
1152+
}
1153+
11211154
private CancellationTokenSource _repoNameCts;
11221155

11231156
private async void txtRepoName_TextChanged(object sender, TextChangedEventArgs e)
@@ -1185,5 +1218,7 @@ private void txtTokenInput_PasswordChanged(object sender, RoutedEventArgs e)
11851218

11861219
btnAuthorizeToken.IsEnabled = tokenSeemsOK;
11871220
}
1221+
1222+
11881223
} // class NewProject
11891224
} // namespace UnityLauncherPro

UnityLauncherPro/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityLauncherPro/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,8 @@
187187
<Setting Name="gitUsername" Type="System.String" Scope="User">
188188
<Value Profile="(Default)" />
189189
</Setting>
190+
<Setting Name="gitAddIgnore" Type="System.Boolean" Scope="User">
191+
<Value Profile="(Default)">True</Value>
192+
</Setting>
190193
</Settings>
191194
</SettingsFile>

0 commit comments

Comments
 (0)