/SparkleLib/Hg/SparkleFetcherHg.cs

http://github.com/hbons/SparkleShare · 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. using System;
  17. using System.IO;
  18. using System.Diagnostics;
  19. using System.Xml;
  20. namespace SparkleLib {
  21. // Sets up a fetcher that can get remote folders
  22. public class SparkleFetcherHg : SparkleFetcherBase {
  23. public SparkleFetcherHg (string server, string remote_folder, string target_folder) :
  24. base (server, remote_folder, target_folder) { }
  25. public override bool Fetch ()
  26. {
  27. SparkleHg hg = new SparkleHg (SparklePaths.SparkleTmpPath,
  28. "clone \"" + base.remote_url + "\" " + "\"" + base.target_folder + "\"");
  29. hg.Start ();
  30. hg.WaitForExit ();
  31. SparkleHelpers.DebugInfo ("Hg", "Exit code " + hg.ExitCode.ToString ());
  32. if (hg.ExitCode != 0) {
  33. return false;
  34. } else {
  35. InstallConfiguration ();
  36. InstallExcludeRules ();
  37. return true;
  38. }
  39. }
  40. // Install the user's name and email and some config into
  41. // the newly cloned repository
  42. private void InstallConfiguration ()
  43. {
  44. string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml");
  45. if (!File.Exists (global_config_file_path))
  46. return;
  47. string repo_config_file_path = SparkleHelpers.CombineMore (base.target_folder, ".hg", "hgrc");
  48. string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path));
  49. // Add user info
  50. string n = Environment.NewLine;
  51. XmlDocument xml = new XmlDocument();
  52. xml.Load (global_config_file_path);
  53. XmlNode node_name = xml.SelectSingleNode ("//user/name/text()");
  54. XmlNode node_email = xml.SelectSingleNode ("//user/email/text()");
  55. // TODO this ignore duplicate names (FolderName (2))
  56. string ignore_file_path = base.target_folder.Replace (SparklePaths.SparkleTmpPath,
  57. SparklePaths.SparklePath);
  58. ignore_file_path = SparkleHelpers.CombineMore (ignore_file_path, ".hg", "hgignore");
  59. config += n +
  60. "[ui]" + n +
  61. "username = " + node_name.Value + " <" + node_email.Value + ">" + n +
  62. "ignore = " + ignore_file_path + n;
  63. // Write the config to the file
  64. TextWriter writer = new StreamWriter (repo_config_file_path);
  65. writer.WriteLine (config);
  66. writer.Close ();
  67. string style_file_path = SparkleHelpers.CombineMore (base.target_folder, ".hg", "log.style");
  68. string style = "changeset = \"{file_mods}{file_adds}{file_dels}\"" + n +
  69. "file_add = \"A {file_add}\\n\"" + n +
  70. "file_mod = \"M {file_mod}\\n\"" + n +
  71. "file_del = \"D {file_del}\\n\"" + n;
  72. writer = new StreamWriter (style_file_path);
  73. writer.WriteLine (style);
  74. writer.Close ();
  75. SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'");
  76. }
  77. // Add a .gitignore file to the repo
  78. private void InstallExcludeRules ()
  79. {
  80. string exlude_rules_file_path = SparkleHelpers.CombineMore (
  81. this.target_folder, ".hg", "hgignore");
  82. TextWriter writer = new StreamWriter (exlude_rules_file_path);
  83. writer.WriteLine ("syntax: glob");
  84. // gedit and emacs
  85. writer.WriteLine ("*~");
  86. // vi(m)
  87. writer.WriteLine (".*.sw[a-z]");
  88. writer.WriteLine ("*.un~");
  89. writer.WriteLine ("*.swp");
  90. writer.WriteLine ("*.swo");
  91. // KDE
  92. writer.WriteLine (".directory");
  93. // Mac OSX
  94. writer.WriteLine (".DS_Store");
  95. writer.WriteLine ("Icon?");
  96. writer.WriteLine ("._*");
  97. writer.WriteLine (".Spotlight-V100");
  98. writer.WriteLine (".Trashes");
  99. // Mac OSX
  100. writer.WriteLine ("*(Autosaved).graffle");
  101. // Windows
  102. writer.WriteLine ("Thumbs.db");
  103. writer.WriteLine ("Desktop.ini");
  104. // CVS
  105. writer.WriteLine ("*/CVS/*");
  106. writer.WriteLine (".cvsignore");
  107. writer.WriteLine ("*/.cvsignore");
  108. // Subversion
  109. writer.WriteLine ("/.svn/*");
  110. writer.WriteLine ("*/.svn/*");
  111. writer.Close ();
  112. }
  113. }
  114. public class SparkleHg : Process {
  115. public SparkleHg (string path, string args) : base ()
  116. {
  117. EnableRaisingEvents = true;
  118. StartInfo.FileName = "/opt/local/bin/hg";
  119. StartInfo.Arguments = args;
  120. StartInfo.RedirectStandardOutput = true;
  121. StartInfo.UseShellExecute = false;
  122. StartInfo.WorkingDirectory = path;
  123. }
  124. new public void Start ()
  125. {
  126. SparkleHelpers.DebugInfo ("Cmd", StartInfo.FileName + " " + StartInfo.Arguments);
  127. base.Start ();
  128. }
  129. }
  130. }