SparkleShare/SparkleLib/SparkleFetcher.cs

177 lines
4.7 KiB
C#
Raw Normal View History

2010-07-31 19:13:01 +00:00
// SparkleShare, an instant update workflow to Git.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// 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
2010-07-31 19:13:01 +00:00
// 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 <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Diagnostics;
using System.Timers;
2010-07-31 19:13:01 +00:00
namespace SparkleLib {
2010-07-31 19:13:01 +00:00
2010-10-10 22:07:07 +00:00
// A helper class that fetches and configures
// a remote repository
2010-07-31 19:13:01 +00:00
public class SparkleFetcher {
public delegate void CloningStartedEventHandler (object o, SparkleEventArgs args);
public delegate void CloningFinishedEventHandler (object o, SparkleEventArgs args);
public delegate void CloningFailedEventHandler (object o, SparkleEventArgs args);
public event CloningStartedEventHandler CloningStarted;
public event CloningFinishedEventHandler CloningFinished;
public event CloningFailedEventHandler CloningFailed;
private string TargetFolder;
2010-07-31 19:13:01 +00:00
private string RemoteOriginUrl;
2010-07-31 19:13:01 +00:00
public SparkleFetcher (string url, string folder)
{
TargetFolder = folder;
2010-07-31 19:13:01 +00:00
RemoteOriginUrl = url;
}
2010-10-10 22:07:07 +00:00
// Clones the remote repository
2010-11-20 13:52:39 +00:00
public void Start ()
2010-07-31 19:13:01 +00:00
{
if (Directory.Exists (TargetFolder))
Directory.Delete (TargetFolder, true);
2010-11-20 13:52:39 +00:00
SparkleHelpers.DebugInfo ("Git", "[" + TargetFolder + "] Cloning Repository");
2010-07-31 19:13:01 +00:00
if (CloningStarted != null)
CloningStarted (this, new SparkleEventArgs ("CloningStarted"));
2010-07-31 19:13:01 +00:00
Process process = new Process () {
EnableRaisingEvents = true
};
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
2010-07-31 19:13:01 +00:00
process.StartInfo.UseShellExecute = false;
2010-11-27 21:23:44 +00:00
process.StartInfo.FileName = SparklePaths.GitPath;
process.StartInfo.Arguments = "clone --progress " +
"\"" + RemoteOriginUrl + "\" " + "\"" + TargetFolder + "\"";
2010-07-31 19:13:01 +00:00
process.Exited += delegate {
SparkleHelpers.DebugInfo ("Git", "Exit code " + process.ExitCode.ToString ());
2010-07-31 19:13:01 +00:00
if (process.ExitCode != 0) {
2010-11-20 13:52:39 +00:00
SparkleHelpers.DebugInfo ("Git", "[" + TargetFolder + "] Cloning failed");
2010-07-31 19:13:01 +00:00
if (CloningFailed != null)
CloningFailed (this, new SparkleEventArgs ("CloningFailed"));
2010-07-31 19:13:01 +00:00
} else {
InstallUserInfo ();
2010-07-31 19:13:01 +00:00
InstallExcludeRules ();
2010-11-20 13:52:39 +00:00
SparkleHelpers.DebugInfo ("Git", "[" + TargetFolder + "] Repository cloned");
2010-07-31 19:13:01 +00:00
if (CloningFinished != null)
CloningFinished (this, new SparkleEventArgs ("CloningFinished"));
2010-07-31 19:13:01 +00:00
}
};
2010-11-27 21:23:44 +00:00
2010-07-31 19:13:01 +00:00
process.Start ();
process.BeginErrorReadLine ();
2010-07-31 19:13:01 +00:00
}
// Install the user's name and email into
// the newly cloned repository
private void InstallUserInfo ()
{
string global_config_file_path = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath, "config");
if (File.Exists (global_config_file_path)) {
StreamReader reader = new StreamReader (global_config_file_path);
string user_info = reader.ReadToEnd ();
reader.Close ();
string repo_config_file_path = SparkleHelpers.CombineMore (TargetFolder, ".git", "config");
TextWriter writer = File.AppendText (repo_config_file_path);
writer.WriteLine (user_info);
writer.Close ();
SparkleHelpers.DebugInfo ("Config", "Added user info to '" + repo_config_file_path + "'");
}
}
2010-07-31 19:13:01 +00:00
// Add a .gitignore file to the repo
private void InstallExcludeRules ()
{
string exlude_rules_file_path = SparkleHelpers.CombineMore
(TargetFolder, ".git", "info", "exclude");
2010-07-31 19:13:01 +00:00
TextWriter writer = new StreamWriter (exlude_rules_file_path);
2011-02-07 01:15:01 +00:00
// gedit and emacs
writer.WriteLine ("*~");
2011-02-07 01:15:01 +00:00
// vi(m)
writer.WriteLine (".*.sw[a-z]");
writer.WriteLine ("*.un~");
writer.WriteLine ("*.swp");
writer.WriteLine ("*.swo");
// KDE
writer.WriteLine (".directory");
2011-02-07 01:15:01 +00:00
// Mac OSX
writer.WriteLine (".DS_Store");
2011-02-07 01:15:01 +00:00
writer.WriteLine ("Icon?");
writer.WriteLine ("._*");
writer.WriteLine (".Spotlight-V100");
writer.WriteLine (".Trashes");
2011-02-07 01:15:01 +00:00
// Windows
writer.WriteLine ("Thumbs.db");
2011-02-07 01:15:01 +00:00
writer.WriteLine ("Desktop.ini");
// CVS
writer.WriteLine ("*/CVS/*");
2011-02-07 01:15:01 +00:00
writer.WriteLine (".cvsignore");
writer.WriteLine ("*/.cvsignore");
// Subversion
writer.WriteLine ("/.svn/*");
writer.WriteLine ("*/.svn/*");
2010-07-31 19:13:01 +00:00
writer.Close ();
}
}
2010-07-31 19:13:01 +00:00
}