From 3be31fbcb6b716ece704c0f571157d17a5d2a6dd Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Mon, 16 May 2011 21:19:58 +0100 Subject: [PATCH] Split up the fetcher code into FetcherBase and FetcherGit --- SparkleLib/Makefile.am | 3 +- SparkleLib/SparkleFetcher.cs | 191 ------------------------------ SparkleLib/SparkleFetcherBase.cs | 86 ++++++++++++++ SparkleLib/SparkleFetcherGit.cs | 139 ++++++++++++++++++++++ SparkleLib/SparkleLib.csproj | 3 +- SparkleLib/SparkleRepo.cs | 3 +- SparkleShare/SparkleController.cs | 6 +- 7 files changed, 235 insertions(+), 196 deletions(-) delete mode 100644 SparkleLib/SparkleFetcher.cs create mode 100644 SparkleLib/SparkleFetcherBase.cs create mode 100644 SparkleLib/SparkleFetcherGit.cs diff --git a/SparkleLib/Makefile.am b/SparkleLib/Makefile.am index e0886078..be122afc 100644 --- a/SparkleLib/Makefile.am +++ b/SparkleLib/Makefile.am @@ -8,7 +8,8 @@ SOURCES = \ SparkleBackend.cs \ SparkleCommit.cs \ SparkleEvents.cs \ - SparkleFetcher.cs \ + SparkleFetcherBase.cs \ + SparkleFetcherGit.cs \ SparkleGit.cs \ SparkleHelpers.cs \ SparkleListenerBase.cs \ diff --git a/SparkleLib/SparkleFetcher.cs b/SparkleLib/SparkleFetcher.cs deleted file mode 100644 index d94b6d9c..00000000 --- a/SparkleLib/SparkleFetcher.cs +++ /dev/null @@ -1,191 +0,0 @@ -// SparkleShare, an instant update workflow to Git. -// Copyright (C) 2010 Hylke Bons -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - - -using System; -using System.IO; -using System.Diagnostics; -using System.Threading; -using System.Xml; // TODO: move to subclass later - -namespace SparkleLib { - - // Sets up a fetcher that can get remote folders - public class SparkleFetcher { - - public delegate void StartedEventHandler (object o, SparkleEventArgs args); - public delegate void FinishedEventHandler (object o, SparkleEventArgs args); - public delegate void FailedEventHandler (object o, SparkleEventArgs args); - - public event StartedEventHandler Started; - public event FinishedEventHandler Finished; - public event FailedEventHandler Failed; - - private string target_folder; - private string remote_url; - private Thread thread; - - - public SparkleFetcher (string remote_url, string target_folder) - { - this.target_folder = target_folder; - this.remote_url = remote_url; - } - - - // Clones the remote repository - public void Start () - { - SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder..."); - - if (Started != null) - Started (this, new SparkleEventArgs ("Started")); - - if (Directory.Exists (this.target_folder)) - Directory.Delete (this.target_folder, true); - - this.thread = new Thread (new ThreadStart (delegate { - if (Fetch ()) { - SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching finished"); - - if (Finished != null) - Finished (this, new SparkleEventArgs ("Finished")); - } else { - SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching failed"); - - if (Failed != null) - Failed (this, new SparkleEventArgs ("Failed")); - } - })); - - this.thread.Start (); - } - - - public void Dispose () - { - this.thread.Abort (); - this.thread.Join (); - } - - - // TODO: abstract -> override - public virtual bool Fetch () - { - SparkleGit git = new SparkleGit (SparklePaths.SparkleTmpPath, - "clone \"" + this.remote_url + "\" " + "\"" + this.target_folder + "\""); - - git.Start (); - git.WaitForExit (); - - SparkleHelpers.DebugInfo ("Git", "Exit code " + git.ExitCode.ToString ()); - - if (git.ExitCode != 0) { - return false; - } else { - InstallConfiguration (); - InstallExcludeRules (); - return true; - } - } - - - // Install the user's name and email and some config into - // the newly cloned repository - private void InstallConfiguration () - { - string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); - - if (File.Exists (global_config_file_path)) { - string repo_config_file_path = SparkleHelpers.CombineMore (this.target_folder, ".git", "config"); - string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path)); - - // Be case sensitive explicitly to work on Mac - config = config.Replace ("ignorecase = true", "ignorecase = false"); - - // Ignore permission changes - config = config.Replace ("filemode = true", "filemode = false"); - - // Add user info - string n = Environment.NewLine; - XmlDocument xml = new XmlDocument(); - xml.Load (global_config_file_path); - - XmlNode node_name = xml.SelectSingleNode ("//user/name/text()"); - XmlNode node_email = xml.SelectSingleNode ("//user/email/text()"); - - config += n + - "[user]" + n + - "\tname = " + node_name.Value + n + - "\temail = " + node_email.Value + n; - - // Write the config to the file - TextWriter writer = new StreamWriter (repo_config_file_path); - writer.WriteLine (config); - writer.Close (); - - SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'"); - } - } - - - // Add a .gitignore file to the repo - private void InstallExcludeRules () - { - string exlude_rules_file_path = SparkleHelpers.CombineMore ( - this.target_folder, ".git", "info", "exclude"); - - TextWriter writer = new StreamWriter (exlude_rules_file_path); - - // gedit and emacs - writer.WriteLine ("*~"); - - // vi(m) - writer.WriteLine (".*.sw[a-z]"); - writer.WriteLine ("*.un~"); - writer.WriteLine ("*.swp"); - writer.WriteLine ("*.swo"); - - // KDE - writer.WriteLine (".directory"); - - // Mac OSX - writer.WriteLine (".DS_Store"); - writer.WriteLine ("Icon?"); - writer.WriteLine ("._*"); - writer.WriteLine (".Spotlight-V100"); - writer.WriteLine (".Trashes"); - - // Mac OSX - writer.WriteLine ("*(Autosaved).graffle"); - - // Windows - writer.WriteLine ("Thumbs.db"); - writer.WriteLine ("Desktop.ini"); - - // CVS - writer.WriteLine ("*/CVS/*"); - writer.WriteLine (".cvsignore"); - writer.WriteLine ("*/.cvsignore"); - - // Subversion - writer.WriteLine ("/.svn/*"); - writer.WriteLine ("*/.svn/*"); - - writer.Close (); - } - } -} diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs new file mode 100644 index 00000000..90e3e09e --- /dev/null +++ b/SparkleLib/SparkleFetcherBase.cs @@ -0,0 +1,86 @@ +// SparkleShare, an instant update workflow to Git. +// Copyright (C) 2010 Hylke Bons +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +using System; +using System.IO; +using System.Diagnostics; +using System.Threading; + +namespace SparkleLib { + + // Sets up a fetcher that can get remote folders + public abstract class SparkleFetcherBase { + + public delegate void StartedEventHandler (object o, SparkleEventArgs args); + public delegate void FinishedEventHandler (object o, SparkleEventArgs args); + public delegate void FailedEventHandler (object o, SparkleEventArgs args); + + public event StartedEventHandler Started; + public event FinishedEventHandler Finished; + public event FailedEventHandler Failed; + + protected string target_folder; + protected string remote_url; + private Thread thread; + + + public SparkleFetcherBase (string remote_url, string target_folder) + { + this.target_folder = target_folder; + this.remote_url = remote_url; + } + + + // Clones the remote repository + public void Start () + { + SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder..."); + + if (Started != null) + Started (this, new SparkleEventArgs ("Started")); + + if (Directory.Exists (this.target_folder)) + Directory.Delete (this.target_folder, true); + + this.thread = new Thread (new ThreadStart (delegate { + if (Fetch ()) { + SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching finished"); + + if (Finished != null) + Finished (this, new SparkleEventArgs ("Finished")); + } else { + SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching failed"); + + if (Failed != null) + Failed (this, new SparkleEventArgs ("Failed")); + } + })); + + this.thread.Start (); + } + + + public void Dispose () + { + this.thread.Abort (); + this.thread.Join (); + } + + + public abstract bool Fetch (); + } +} diff --git a/SparkleLib/SparkleFetcherGit.cs b/SparkleLib/SparkleFetcherGit.cs new file mode 100644 index 00000000..157b2db5 --- /dev/null +++ b/SparkleLib/SparkleFetcherGit.cs @@ -0,0 +1,139 @@ +// SparkleShare, an instant update workflow to Git. +// Copyright (C) 2010 Hylke Bons +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +using System; +using System.IO; +using System.Diagnostics; +using System.Xml; // TODO: move to subclass later + +namespace SparkleLib { + + // Sets up a fetcher that can get remote folders + public class SparkleFetcherGit : SparkleFetcherBase { + + public SparkleFetcherGit (string remote_url, string target_folder) : + base (remote_url, target_folder) { } + + + public override bool Fetch () + { + SparkleGit git = new SparkleGit (SparklePaths.SparkleTmpPath, + "clone \"" + base.remote_url + "\" " + "\"" + base.target_folder + "\""); + + git.Start (); + git.WaitForExit (); + + SparkleHelpers.DebugInfo ("Git", "Exit code " + git.ExitCode.ToString ()); + + if (git.ExitCode != 0) { + return false; + } else { + InstallConfiguration (); + InstallExcludeRules (); + return true; + } + } + + + // Install the user's name and email and some config into + // the newly cloned repository + private void InstallConfiguration () + { + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); + + if (!File.Exists (global_config_file_path)) + return; + + string repo_config_file_path = SparkleHelpers.CombineMore (base.target_folder, ".git", "config"); + string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path)); + + // Be case sensitive explicitly to work on Mac + config = config.Replace ("ignorecase = true", "ignorecase = false"); + + // Ignore permission changes + config = config.Replace ("filemode = true", "filemode = false"); + + // Add user info + string n = Environment.NewLine; + XmlDocument xml = new XmlDocument(); + xml.Load (global_config_file_path); + + XmlNode node_name = xml.SelectSingleNode ("//user/name/text()"); + XmlNode node_email = xml.SelectSingleNode ("//user/email/text()"); + + // TODO: just use commands instead of messing with the config file + config += n + + "[user]" + n + + "\tname = " + node_name.Value + n + + "\temail = " + node_email.Value + n; + + // Write the config to the file + TextWriter writer = new StreamWriter (repo_config_file_path); + writer.WriteLine (config); + writer.Close (); + + SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'"); + } + + + // Add a .gitignore file to the repo + private void InstallExcludeRules () + { + string exlude_rules_file_path = SparkleHelpers.CombineMore ( + this.target_folder, ".git", "info", "exclude"); + + TextWriter writer = new StreamWriter (exlude_rules_file_path); + + // gedit and emacs + writer.WriteLine ("*~"); + + // vi(m) + writer.WriteLine (".*.sw[a-z]"); + writer.WriteLine ("*.un~"); + writer.WriteLine ("*.swp"); + writer.WriteLine ("*.swo"); + + // KDE + writer.WriteLine (".directory"); + + // Mac OSX + writer.WriteLine (".DS_Store"); + writer.WriteLine ("Icon?"); + writer.WriteLine ("._*"); + writer.WriteLine (".Spotlight-V100"); + writer.WriteLine (".Trashes"); + + // Mac OSX + writer.WriteLine ("*(Autosaved).graffle"); + + // Windows + writer.WriteLine ("Thumbs.db"); + writer.WriteLine ("Desktop.ini"); + + // CVS + writer.WriteLine ("*/CVS/*"); + writer.WriteLine (".cvsignore"); + writer.WriteLine ("*/.cvsignore"); + + // Subversion + writer.WriteLine ("/.svn/*"); + writer.WriteLine ("*/.svn/*"); + + writer.Close (); + } + } +} diff --git a/SparkleLib/SparkleLib.csproj b/SparkleLib/SparkleLib.csproj index 1f437b48..1aff1ff6 100644 --- a/SparkleLib/SparkleLib.csproj +++ b/SparkleLib/SparkleLib.csproj @@ -38,7 +38,8 @@ - + + diff --git a/SparkleLib/SparkleRepo.cs b/SparkleLib/SparkleRepo.cs index 8bb5f8ec..21234680 100644 --- a/SparkleLib/SparkleRepo.cs +++ b/SparkleLib/SparkleRepo.cs @@ -324,7 +324,7 @@ namespace SparkleLib { SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Changes have settled."); this.is_buffering = false; - this.has_changed = false; + this.has_changed = false; while (AnyDifferences) { this.watcher.EnableRaisingEvents = false; @@ -770,6 +770,7 @@ namespace SparkleLib { // Gets the domain name of a given URL + // TODO: make this a regex private string GetDomain (string url) { if (url.Equals ("")) diff --git a/SparkleShare/SparkleController.cs b/SparkleShare/SparkleController.cs index 691c7ee2..84b78ed0 100644 --- a/SparkleShare/SparkleController.cs +++ b/SparkleShare/SparkleController.cs @@ -1001,8 +1001,10 @@ namespace SparkleShare { // Strip the '.git' from the name string canonical_name = Path.GetFileNameWithoutExtension (name); string tmp_folder = Path.Combine (SparklePaths.SparkleTmpPath, canonical_name); - SparkleFetcher fetcher = new SparkleFetcher (url, tmp_folder); - bool folder_exists = Directory.Exists (Path.Combine (SparklePaths.SparklePath, canonical_name)); + + // TODO: backend detection + SparkleFetcherBase fetcher = new SparkleFetcherGit (url, tmp_folder); + bool folder_exists = Directory.Exists (Path.Combine (SparklePaths.SparklePath, canonical_name)); // Add a numbered suffix to the nameif a folder with the same name // already exists. Example: "Folder (2)"