Skip to content

Commit

Permalink
Merge pull request #1 from daredloco/feature-subfolders
Browse files Browse the repository at this point in the history
Feature subfolders
  • Loading branch information
daredloco authored Oct 26, 2020
2 parents 3312aa9 + 8444bb3 commit b97849e
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 56 deletions.
1 change: 1 addition & 0 deletions GitInstaller/GitInstaller/GitInstaller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Uninstaller.cs" />
<Compile Include="ZipSettings.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
13 changes: 8 additions & 5 deletions GitInstaller/GitInstaller/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GitInstaller"
mc:Ignorable="d"
Title="Project Name - Powered by GitInstaller" Height="450" Width="800" ResizeMode="CanMinimize">
Title="Project Name - Powered by GitInstaller" Height="500" Width="850" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>

Expand All @@ -22,14 +22,16 @@
<RowDefinition Height="10" />
<RowDefinition Height="23" />
<RowDefinition Height="23" />
<RowDefinition Height="15" />
<RowDefinition Height="10" />
<RowDefinition Height="23" />
<RowDefinition Height="15" />
<RowDefinition Height="5" />
<RowDefinition Height="23" />
<RowDefinition Height="*" />
<RowDefinition Height="25" />
<RowDefinition Height="20" />
<RowDefinition Height="30"/>
<RowDefinition Height="10" />
<RowDefinition Height="30" />
<RowDefinition Height="25" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>

<Label Content="Select Version:" Grid.Row="1"/>
Expand All @@ -38,6 +40,7 @@
</ComboBox>
<CheckBox Name="cb_preview" Content="Show Preview Releases" Grid.Row="4" VerticalAlignment="Center" />
<Button Name="bt_install" Grid.Row="9" Content="Install" />
<Button Name="bt_uninstall" Content="Uninstall" Grid.Row="11" />
<Label Content="Version Details:" Grid.Row="6" />
<RichTextBox Name="rtb_changes" Grid.Row="7" IsReadOnly="True" IsDocumentEnabled="True">
<RichTextBox.Resources>
Expand Down
19 changes: 19 additions & 0 deletions GitInstaller/GitInstaller/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public MainWindow()
cb_preview.Checked += PreviewChecked;
cb_preview.Unchecked += PreviewUnchecked;
rtb_log.IsReadOnly = true;
bt_uninstall.Click += UninstallClicked;
bt_install.IsEnabled = false;
bt_install.Click += InstallClicked;

Expand All @@ -53,6 +54,24 @@ private void PreviewChecked(object sender, RoutedEventArgs e)
{
UpdateVersions(true);
}
private void UninstallClicked(object sender, RoutedEventArgs e)
{
if (System.Windows.Forms.DialogResult.Yes != System.Windows.Forms.MessageBox.Show("Do you really want to uninstall " + Settings.Project + "?","Are you sure?", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question))
return;

using (System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog())
{
fbd.Description = "Choose the installation directory:";
var result = fbd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
if (!Uninstaller.DoUninstall(fbd.SelectedPath))
System.Windows.Forms.MessageBox.Show("Couldn't uninstall the software, read the log for more informations!");
else
WriteLog("Software was uninstalled successfuly!");
}
}
}

private void InstallClicked(object sender, RoutedEventArgs e)
{
Expand Down
4 changes: 2 additions & 2 deletions GitInstaller/GitInstaller/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.1.0")]
[assembly: AssemblyFileVersion("1.4.1.0")]
[assembly: AssemblyVersion("1.5.3.0")]
[assembly: AssemblyFileVersion("1.5.3.0")]
4 changes: 3 additions & 1 deletion GitInstaller/GitInstaller/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ internal enum SettingsState
internal static string Repo { get { return file.repo; } }
internal static bool Unzip { get { return file.unzip; } }
internal static bool Preview { get { return file.preview; } }
internal static bool Uninstall { get { return file.uninstall; } }
internal static string[] Ignored_Tags { get { return file.ignored_tags; } }
internal static string[] Ignored_Files { get { return file.ignored_files; } }

/// <summary>
/// Returns the Url to the Releases of the GitHub Api or NULL if settings aren't loaded
/// </summary>
Expand Down Expand Up @@ -90,6 +91,7 @@ public class SettingsFile
public string repo;
public bool unzip;
public bool preview;
public bool uninstall;
[JsonProperty("ignored-tags")]
public string[] ignored_tags;
[JsonProperty("ignored-files")]
Expand Down
116 changes: 116 additions & 0 deletions GitInstaller/GitInstaller/Uninstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GitInstaller
{
public class Uninstaller
{
public string location;
public List<string> files = new List<string>();
public List<string> directories = new List<string>();

public Uninstaller(string loca)
{
location = loca;
}

public Uninstaller(string loca, string[] fs, string[] dirs)
{
location = loca;
files.AddRange(fs);
directories.AddRange(dirs);
}

public void GenerateFile()
{
string fname = Path.Combine(location, "gituninstaller.cfg");
List<string> flines = new List<string>() { $"###{Settings.Project} Uninstaller###" };

flines.Add("This is a file generated by GitInstaller, ");
flines.Add("to use it either install a newer version of the software installed with it");
flines.Add("or open the GitInstaller for this application, select this folder and press \"Uninstall\"");

flines.Add("###FILES###");
foreach (string f in files)
{
flines.Add(f);
}

flines.Add("###DIRECTORIES###");
foreach (string d in directories)
{
flines.Add(d);
}

File.WriteAllLines(fname, flines.ToArray());
}

public static bool DoUninstall(string flocation)
{
MainWindow.Instance.prog_loading.IsIndeterminate = true;
flocation = Path.Combine(flocation, "gituninstaller.cfg");
if (!File.Exists(flocation))
{
MainWindow.Instance.WriteLog("Couldn't find gituninstaller.cfg, did you choose the right path?");
return false;
}

bool infiles = false;
bool indirs = false;
bool wassuccess = true;
foreach(string fline in File.ReadAllLines(flocation))
{
if(fline != "")
{
if (infiles && fline != "###FILES###" && fline != "###DIRECTORIES###")
{
try
{
if (!File.Exists(fline))
throw new Exception("File \"" + fline + "\" does not exist");
File.Delete(fline);
MainWindow.Instance.WriteLog("File \"" + fline + "\" deleted!");
}
catch (Exception ex)
{
MainWindow.Instance.WriteLog("Couldn't delete file \"" + fline + "\" => " + ex.Message);
wassuccess = false;
}
}
else if (indirs && fline != "###FILES###" && fline != "###DIRECTORIES###")
{
try
{
if (!Directory.Exists(fline))
throw new Exception("Directory \"" + fline + "\" does not exist");
Directory.Delete(fline, true);
MainWindow.Instance.WriteLog("Directory \"" + fline + "\" deleted!");
}
catch (Exception ex)
{
MainWindow.Instance.WriteLog("Couldn't delete directory \"" + fline + "\" => " + ex.Message);
wassuccess = false;
}
}
else if (fline == "###FILES###")
{
infiles = true;
indirs = false;
}
else if (fline == "###DIRECTORIES###")
{
infiles = false;
indirs = true;
}
}
}
File.Delete(flocation);
MainWindow.Instance.prog_loading.IsIndeterminate = false;
return wassuccess;
}
}
}
40 changes: 0 additions & 40 deletions GitInstaller/GitInstaller/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,5 @@ public static bool HasWildcard(string value, string wildcard, string wildcardcha
}
return false;
}

public static List<string> Wildcard(string[] list, string wildcard, string wildcardchar = "*")
{
List<string> lst = new List<string>();

if(!wildcard.Contains(wildcardchar))
{
lst.AddRange(list);
return lst;
}
bool startswith = false;
if(wildcard.EndsWith(wildcardchar))
{
startswith = false;
wildcard = wildcard.Remove(wildcard.Length - 1);
}
else if(wildcard.StartsWith(wildcardchar))
{
startswith = true;
wildcard = wildcard.Remove(0, 1);
}else
{
throw new Exception("Invalid wildcard (Use wildcard* or *wildcard)");
}

foreach(string element in list)
{
if(startswith)
{
if (element.StartsWith(wildcard))
lst.Add(element);
}
else
{
if (element.EndsWith(wildcard))
lst.Add(element);
}
}
return lst;
}
}
}
21 changes: 19 additions & 2 deletions GitInstaller/GitInstaller/ZipArchiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ public static class ZipArchiveExtensions
{
public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
Uninstaller uninstaller = new Uninstaller(destinationDirectoryName);

if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectoryName);
uninstaller.directories.Add(destinationDirectoryName);
if(Settings.Uninstall)
uninstaller.GenerateFile();
return;
}

foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
Expand All @@ -27,30 +33,41 @@ public static void ExtractToDirectory(this ZipArchive archive, string destinatio
Directory.CreateDirectory(directory);

if (!string.IsNullOrEmpty(file.Name) && file.Name != "zipsettings.json")
{
file.ExtractToFile(completeFileName, true);
uninstaller.files.Add(completeFileName);
}
}
if(Settings.Uninstall)
uninstaller.GenerateFile();
}

//Added by daRedLoCo
public static void ExtractWithSettings(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
ZipSettings settings = ZipSettings.FromStream(archive.GetEntry("zipsettings.json").Open());
if(settings == null)
if(settings == null || settings.Subfolders.Count < 1)
{
ExtractToDirectory(archive, destinationDirectoryName, overwrite);
return;
}
Uninstaller uninstaller = new Uninstaller(destinationDirectoryName);
foreach(ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.Combine(destinationDirectoryName, settings.Subfolder, file.FullName);
string completeFileName = Path.Combine(destinationDirectoryName, settings.GetSubfolder(file.FullName), file.FullName);
string directory = Path.GetDirectoryName(completeFileName);

if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

if (!string.IsNullOrEmpty(file.Name) && file.Name != "zipsettings.json")
{
file.ExtractToFile(completeFileName, overwrite);
uninstaller.files.Add(completeFileName);
}
}
if(Settings.Uninstall)
uninstaller.GenerateFile();
}
}
}
Loading

0 comments on commit b97849e

Please sign in to comment.