Skip to content

Commit

Permalink
part of restore
Browse files Browse the repository at this point in the history
  • Loading branch information
cildhdi committed Nov 12, 2018
1 parent ee7eed7 commit f334c8d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
75 changes: 48 additions & 27 deletions Wox.Plugin.PluginBackup/Main.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System;
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Wox.Plugin.PluginBackup
{
public static class State
public static class GlobalState
{
private static string _plugin_name;
private static string _plugin_name = "Wox.Plugin.PluginBackup";
private static string _plugin_path;
private static string _wox_path;
private static string _desktop_path;
private static string _backup_file_name = "Wox.Plugins.zip";

public static void Init()
{
_plugin_name = "Wox.Plugin.PluginBackup";

UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
_plugin_path = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
_wox_path = _plugin_path.Remove(_plugin_path.LastIndexOf('\\'));
_wox_path = _wox_path.Remove(_wox_path.LastIndexOf('\\'));
_wox_path = _plugin_path.Remove(_plugin_path.LastIndexOf(Path.DirectorySeparatorChar));
_wox_path = _wox_path.Remove(_wox_path.LastIndexOf(Path.DirectorySeparatorChar));

_desktop_path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
}
Expand All @@ -29,22 +29,20 @@ public static void Init()

public static string WoxDir() { return _wox_path; }

public static string WoxPluginsDir() { return _wox_path + "\\Plugins"; }
public static string WoxPluginsDir() { return _wox_path + Path.DirectorySeparatorChar + "Plugins"; }

public static string WoxSettingsDir() { return _wox_path + "\\Settings"; }
public static string WoxSettingsDir() { return _wox_path + Path.DirectorySeparatorChar + "Settings"; }

public static string DesktopDir() { return _desktop_path; }

public static string TempDir() { return ThisPluginDir() + "\\Wox.PluginBackup"; }

public static string BackupFilePath() { return ThisPluginDir() + "\\Wox.wpb"; }
public static string BackupFileName() { return _backup_file_name; }
}

public class Main : IPlugin
{
public void Init(PluginInitContext context)
{
State.Init();
GlobalState.Init();
}

public List<Result> Query(Query query)
Expand All @@ -55,29 +53,52 @@ public List<Result> Query(Query query)

public void Backup(string path)
{
if (Directory.Exists(State.TempDir()))
{
Directory.Delete(State.TempDir(), true);
}
if (Directory.Exists(State.BackupFilePath()))
path = path.Length == 0 ? GlobalState.DesktopDir() + Path.DirectorySeparatorChar + GlobalState.BackupFileName() : path;
if (File.Exists(path))
{
Directory.Delete(State.BackupFilePath(), true);
}
Directory.CreateDirectory(State.TempDir());

DirectoryInfo plugins_dir_info = new DirectoryInfo(State.WoxPluginsDir());
foreach (DirectoryInfo plugin_dir in plugins_dir_info.GetDirectories())
{
if (!plugin_dir.Name.StartsWith(State.PluginName()))
try
{
File.Delete(path);
}
catch (System.IO.IOException)
{
//copy to dest;
return;
}
}
ZipFile zip = new ZipFile();
AddDirToZip(zip, GlobalState.WoxPluginsDir());
AddDirToZip(zip, GlobalState.WoxSettingsDir());
zip.Save(path);
}

private void AddDirToZip(ZipFile zip, string dir)
{
foreach (string file in Directory.GetFiles(dir))
zip.AddFile(file, dir.Replace(GlobalState.WoxDir(), ""));
foreach (string sub in Directory.GetDirectories(dir))
AddDirToZip(zip, sub);
}

public void Restore(string path)
{
path = path.Length == 0 ? GlobalState.DesktopDir() + Path.DirectorySeparatorChar + GlobalState.BackupFileName() : path;
ZipFile zip = ZipFile.Read(path);
List<string> existed_plugins = new List<string>();
foreach (string pd in Directory.GetDirectories(GlobalState.WoxPluginsDir()))
{
if (pd.Contains("-"))
existed_plugins.Add(pd.Remove(pd.IndexOf('-')).Replace(GlobalState.WoxPluginsDir() + Path.DirectorySeparatorChar, ""));
else
existed_plugins.Add(pd.Replace(GlobalState.WoxPluginsDir() + Path.DirectorySeparatorChar, ""));
}
foreach (ZipEntry entry in zip)
{
string filename = entry.FileName.Remove(entry.FileName.IndexOf('-')).Remove(0, entry.FileName.IndexOf('/') + 1);
if (!existed_plugins.Contains(filename))

entry.Extract(GlobalState.WoxDir());
}
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
}
3 changes: 3 additions & 0 deletions Wox.Plugin.PluginBackup/Wox.Plugin.PluginBackup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DotNetZip, Version=1.12.0.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.12.0\lib\net20\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
1 change: 1 addition & 0 deletions Wox.Plugin.PluginBackup/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.12.0" targetFramework="net40" />
<package id="Wox.Plugin" version="1.2.0.486" targetFramework="net40" />
</packages>

0 comments on commit f334c8d

Please sign in to comment.