/SparkleLib/Hg/SparkleFetcherHg.cs
C# | 172 lines | 100 code | 42 blank | 30 comment | 4 complexity | 2f960cbbba6ab391620399201efd56f5 MD5 | raw file
1// SparkleShare, a collaboration and sharing tool. 2// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com> 3// 4// This program is free software: you can redistribute it and/or modify 5// it under the terms of the GNU General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// This program is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU General Public License for more details. 13// 14// You should have received a copy of the GNU General Public License 15// along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 18using System; 19using System.IO; 20using System.Diagnostics; 21using System.Xml; 22 23namespace SparkleLib { 24 25 // Sets up a fetcher that can get remote folders 26 public class SparkleFetcherHg : SparkleFetcherBase { 27 28 public SparkleFetcherHg (string server, string remote_folder, string target_folder) : 29 base (server, remote_folder, target_folder) { } 30 31 32 public override bool Fetch () 33 { 34 SparkleHg hg = new SparkleHg (SparklePaths.SparkleTmpPath, 35 "clone \"" + base.remote_url + "\" " + "\"" + base.target_folder + "\""); 36 37 hg.Start (); 38 hg.WaitForExit (); 39 40 SparkleHelpers.DebugInfo ("Hg", "Exit code " + hg.ExitCode.ToString ()); 41 42 if (hg.ExitCode != 0) { 43 return false; 44 } else { 45 InstallConfiguration (); 46 InstallExcludeRules (); 47 return true; 48 } 49 } 50 51 52 // Install the user's name and email and some config into 53 // the newly cloned repository 54 private void InstallConfiguration () 55 { 56 string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); 57 58 if (!File.Exists (global_config_file_path)) 59 return; 60 61 string repo_config_file_path = SparkleHelpers.CombineMore (base.target_folder, ".hg", "hgrc"); 62 string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path)); 63 64 // Add user info 65 string n = Environment.NewLine; 66 XmlDocument xml = new XmlDocument(); 67 xml.Load (global_config_file_path); 68 69 XmlNode node_name = xml.SelectSingleNode ("//user/name/text()"); 70 XmlNode node_email = xml.SelectSingleNode ("//user/email/text()"); 71 72 // TODO this ignore duplicate names (FolderName (2)) 73 string ignore_file_path = base.target_folder.Replace (SparklePaths.SparkleTmpPath, 74 SparklePaths.SparklePath); 75 76 ignore_file_path = SparkleHelpers.CombineMore (ignore_file_path, ".hg", "hgignore"); 77 78 config += n + 79 "[ui]" + n + 80 "username = " + node_name.Value + " <" + node_email.Value + ">" + n + 81 "ignore = " + ignore_file_path + n; 82 83 // Write the config to the file 84 TextWriter writer = new StreamWriter (repo_config_file_path); 85 writer.WriteLine (config); 86 writer.Close (); 87 88 string style_file_path = SparkleHelpers.CombineMore (base.target_folder, ".hg", "log.style"); 89 90 string style = "changeset = \"{file_mods}{file_adds}{file_dels}\"" + n + 91 "file_add = \"A {file_add}\\n\"" + n + 92 "file_mod = \"M {file_mod}\\n\"" + n + 93 "file_del = \"D {file_del}\\n\"" + n; 94 95 writer = new StreamWriter (style_file_path); 96 writer.WriteLine (style); 97 writer.Close (); 98 99 SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'"); 100 } 101 102 103 // Add a .gitignore file to the repo 104 private void InstallExcludeRules () 105 { 106 string exlude_rules_file_path = SparkleHelpers.CombineMore ( 107 this.target_folder, ".hg", "hgignore"); 108 109 TextWriter writer = new StreamWriter (exlude_rules_file_path); 110 111 writer.WriteLine ("syntax: glob"); 112 113 // gedit and emacs 114 writer.WriteLine ("*~"); 115 116 // vi(m) 117 writer.WriteLine (".*.sw[a-z]"); 118 writer.WriteLine ("*.un~"); 119 writer.WriteLine ("*.swp"); 120 writer.WriteLine ("*.swo"); 121 122 // KDE 123 writer.WriteLine (".directory"); 124 125 // Mac OSX 126 writer.WriteLine (".DS_Store"); 127 writer.WriteLine ("Icon?"); 128 writer.WriteLine ("._*"); 129 writer.WriteLine (".Spotlight-V100"); 130 writer.WriteLine (".Trashes"); 131 132 // Mac OSX 133 writer.WriteLine ("*(Autosaved).graffle"); 134 135 // Windows 136 writer.WriteLine ("Thumbs.db"); 137 writer.WriteLine ("Desktop.ini"); 138 139 // CVS 140 writer.WriteLine ("*/CVS/*"); 141 writer.WriteLine (".cvsignore"); 142 writer.WriteLine ("*/.cvsignore"); 143 144 // Subversion 145 writer.WriteLine ("/.svn/*"); 146 writer.WriteLine ("*/.svn/*"); 147 148 writer.Close (); 149 } 150 } 151 152 153 public class SparkleHg : Process { 154 155 public SparkleHg (string path, string args) : base () 156 { 157 EnableRaisingEvents = true; 158 StartInfo.FileName = "/opt/local/bin/hg"; 159 StartInfo.Arguments = args; 160 StartInfo.RedirectStandardOutput = true; 161 StartInfo.UseShellExecute = false; 162 StartInfo.WorkingDirectory = path; 163 } 164 165 166 new public void Start () 167 { 168 SparkleHelpers.DebugInfo ("Cmd", StartInfo.FileName + " " + StartInfo.Arguments); 169 base.Start (); 170 } 171 } 172}