fetcher: prepare for multibackend
This commit is contained in:
parent
feb4debdd5
commit
825ea5b200
|
@ -18,14 +18,13 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace SparkleLib {
|
namespace SparkleLib {
|
||||||
|
|
||||||
// A helper class that fetches and configures
|
// Sets up a fetcher that can get remote folders
|
||||||
// a remote repository
|
|
||||||
public class SparkleFetcher {
|
public class SparkleFetcher {
|
||||||
|
|
||||||
// TODO: remove 'cloning' prefix
|
|
||||||
public delegate void StartedEventHandler (object o, SparkleEventArgs args);
|
public delegate void StartedEventHandler (object o, SparkleEventArgs args);
|
||||||
public delegate void FinishedEventHandler (object o, SparkleEventArgs args);
|
public delegate void FinishedEventHandler (object o, SparkleEventArgs args);
|
||||||
public delegate void FailedEventHandler (object o, SparkleEventArgs args);
|
public delegate void FailedEventHandler (object o, SparkleEventArgs args);
|
||||||
|
@ -34,52 +33,64 @@ namespace SparkleLib {
|
||||||
public event FinishedEventHandler Finished;
|
public event FinishedEventHandler Finished;
|
||||||
public event FailedEventHandler Failed;
|
public event FailedEventHandler Failed;
|
||||||
|
|
||||||
private string TargetFolder;
|
private string target_folder;
|
||||||
private string RemoteOriginUrl;
|
private string remote_url;
|
||||||
|
|
||||||
|
|
||||||
public SparkleFetcher (string url, string folder)
|
public SparkleFetcher (string remote_url, string target_folder)
|
||||||
{
|
{
|
||||||
TargetFolder = folder;
|
this.target_folder = target_folder;
|
||||||
RemoteOriginUrl = url;
|
this.remote_url = remote_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Clones the remote repository
|
// Clones the remote repository
|
||||||
public void Start ()
|
public void Start ()
|
||||||
{
|
{
|
||||||
SparkleHelpers.DebugInfo ("Git", "[" + TargetFolder + "] Cloning Repository");
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder...");
|
||||||
|
|
||||||
if (Directory.Exists (TargetFolder))
|
|
||||||
Directory.Delete (TargetFolder, true);
|
|
||||||
|
|
||||||
|
|
||||||
if (Started != null)
|
if (Started != null)
|
||||||
Started (this, new SparkleEventArgs ("Started"));
|
Started (this, new SparkleEventArgs ("Started"));
|
||||||
|
|
||||||
SparkleGit git = new SparkleGit (SparklePaths.SparkleTmpPath,
|
if (Directory.Exists (this.target_folder))
|
||||||
"clone \"" + RemoteOriginUrl + "\" " + "\"" + TargetFolder + "\"");
|
Directory.Delete (this.target_folder, true);
|
||||||
|
|
||||||
git.Exited += delegate {
|
Thread thread = new Thread (new ThreadStart (delegate {
|
||||||
SparkleHelpers.DebugInfo ("Git", "Exit code " + git.ExitCode.ToString ());
|
if (Fetch ()) {
|
||||||
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching finished");
|
||||||
if (git.ExitCode != 0) {
|
|
||||||
SparkleHelpers.DebugInfo ("Git", "[" + TargetFolder + "] Cloning failed");
|
|
||||||
|
|
||||||
if (Failed != null)
|
|
||||||
Failed (this, new SparkleEventArgs ("CloningFailed"));
|
|
||||||
} else {
|
|
||||||
InstallConfiguration ();
|
|
||||||
InstallExcludeRules ();
|
|
||||||
|
|
||||||
SparkleHelpers.DebugInfo ("Git", "[" + TargetFolder + "] Repository cloned");
|
|
||||||
|
|
||||||
if (Finished != null)
|
if (Finished != null)
|
||||||
Finished (this, new SparkleEventArgs ("Finished"));
|
Finished (this, new SparkleEventArgs ("Finished"));
|
||||||
|
} else {
|
||||||
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching failed");
|
||||||
|
|
||||||
|
if (Failed != null)
|
||||||
|
Failed (this, new SparkleEventArgs ("Failed"));
|
||||||
}
|
}
|
||||||
};
|
}));
|
||||||
|
|
||||||
|
thread.Start ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: abstract -> override
|
||||||
|
public virtual bool Fetch ()
|
||||||
|
{
|
||||||
|
SparkleGit git = new SparkleGit (SparklePaths.SparkleTmpPath,
|
||||||
|
"clone \"" + this.remote_url + "\" " + "\"" + this.target_folder + "\"");
|
||||||
|
|
||||||
git.Start ();
|
git.Start ();
|
||||||
|
git.WaitForExit ();
|
||||||
|
|
||||||
|
SparkleHelpers.DebugInfo ("Git", "Exit code " + git.ExitCode.ToString ());
|
||||||
|
|
||||||
|
if (git.ExitCode != 0) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
InstallConfiguration ();
|
||||||
|
InstallExcludeRules ();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -94,7 +105,7 @@ namespace SparkleLib {
|
||||||
string user_info = reader.ReadToEnd ();
|
string user_info = reader.ReadToEnd ();
|
||||||
reader.Close ();
|
reader.Close ();
|
||||||
|
|
||||||
string repo_config_file_path = SparkleHelpers.CombineMore (TargetFolder, ".git", "config");
|
string repo_config_file_path = SparkleHelpers.CombineMore (this.target_folder, ".git", "config");
|
||||||
string config = String.Join ("\n", File.ReadAllLines (repo_config_file_path));
|
string config = String.Join ("\n", File.ReadAllLines (repo_config_file_path));
|
||||||
|
|
||||||
// Be case sensitive explicitly to work on Mac
|
// Be case sensitive explicitly to work on Mac
|
||||||
|
@ -119,7 +130,7 @@ namespace SparkleLib {
|
||||||
private void InstallExcludeRules ()
|
private void InstallExcludeRules ()
|
||||||
{
|
{
|
||||||
string exlude_rules_file_path = SparkleHelpers.CombineMore (
|
string exlude_rules_file_path = SparkleHelpers.CombineMore (
|
||||||
TargetFolder, ".git", "info", "exclude");
|
this.target_folder, ".git", "info", "exclude");
|
||||||
|
|
||||||
TextWriter writer = new StreamWriter (exlude_rules_file_path);
|
TextWriter writer = new StreamWriter (exlude_rules_file_path);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue