AcUtils
A high performance abstraction layer for AccuRev
AcPreferences.cs
Go to the documentation of this file.
1 
16 using System;
17 using System.IO;
18 using System.Threading.Tasks;
19 using System.Xml.Linq;
20 
21 namespace AcUtils
22 {
26  [Serializable]
27  public static class AcPreferences
28  {
29  private static string _acHomeFolder; // The AccuRev home directory
30 
44 
46  public static async Task<bool[]> getIgnoreOptionsAsync()
49  {
50  string tmpFile = await getPreferencesAsync().ConfigureAwait(false);
51  if (String.IsNullOrEmpty(tmpFile)) // unlikely
52  return null; // error already logged
53 
54  bool[] arr = null; // ignoreWhitespace, ignoreWhitespaceChanges, ignoreCase
55  try
56  {
57  using (StreamReader reader = new StreamReader(tmpFile))
58  {
59  XElement doc = XElement.Load(reader);
60  bool ignoreWhitespace = (bool)doc.Element("diffIgnoreWhitespace");
61  bool ignoreWhitespaceChanges = (bool)doc.Element("diffIgnoreWhitespaceChanges");
62  bool ignoreCase = (bool)doc.Element("diffIgnoreCase");
63  arr = new bool[] { ignoreWhitespace, ignoreWhitespaceChanges, ignoreCase };
64  }
65  }
66 
67  catch (Exception ecx)
68  {
69  AcDebug.Log($"Exception caught and logged in AcPreferences.getIgnoreOptionsAsync{Environment.NewLine}{ecx.Message}");
70  }
71 
72  finally
73  {
74  if (!String.IsNullOrEmpty(tmpFile))
75  File.Delete(tmpFile);
76  }
77 
78  return arr;
79  }
80 
87  public static async Task<bool?> getUseIgnoreElemsOptimizationAsync()
88  {
89  string tmpFile = await getPreferencesAsync().ConfigureAwait(false);
90  if (String.IsNullOrEmpty(tmpFile)) // unlikely
91  return null; // error already logged
92 
93  bool? useIgnoreElemsOptimization = null;
94  try
95  {
96  using (StreamReader reader = new StreamReader(tmpFile))
97  {
98  XElement doc = XElement.Load(reader);
99  useIgnoreElemsOptimization = (bool)doc.Element("USE_IGNORE_ELEMS_OPTIMIZATION");
100  }
101  }
102 
103  catch (Exception ecx)
104  {
105  AcDebug.Log($"Exception caught and logged in AcPreferences.getUseIgnoreElemsOptimizationAsync{Environment.NewLine}{ecx.Message}");
106  }
107 
108  finally
109  {
110  if (!String.IsNullOrEmpty(tmpFile))
111  File.Delete(tmpFile);
112  }
113 
114  return useIgnoreElemsOptimization;
115  }
116 
123  public static async Task<string> getAcHomeFolderAsync()
124  {
125  if (_acHomeFolder == null) // first time initialization only
126  {
127  string tmpFile = await getPreferencesAsync().ConfigureAwait(false);
128  if (String.IsNullOrEmpty(tmpFile)) // unlikely
129  return null; // error already logged
130 
131  try
132  {
133  using (StreamReader reader = new StreamReader(tmpFile))
134  {
135  XElement doc = XElement.Load(reader);
136  string home = (string) doc.Element("HOME");
137  _acHomeFolder = Path.Combine(home, ".accurev");
138  }
139  }
140 
141  catch (Exception ecx)
142  {
143  AcDebug.Log($"Exception caught and logged in AcPreferences.getAcHomeFolderAsync{Environment.NewLine}{ecx.Message}");
144  }
145 
146  finally
147  {
148  if (!String.IsNullOrEmpty(tmpFile))
149  File.Delete(tmpFile);
150  }
151  }
152 
153  return _acHomeFolder;
154  }
155 
161 
175  private static async Task<string> getPreferencesAsync()
179  {
180  // Putting the XML into a file as opposed to using it directly avoids exceptions thrown
181  // due to illegal characters. Needs further investigation.
182  string tempFile = null;
183  try
184  {
185  AcResult r = await AcCommand.runAsync("getpref").ConfigureAwait(false);
186  if (r != null && r.RetVal == 0) // if command succeeded
187  {
188  tempFile = Path.GetTempFileName();
189  using (StreamWriter writer = new StreamWriter(tempFile))
190  {
191  writer.Write(r.CmdResult);
192  }
193  }
194  }
195 
196  catch (AcUtilsException ecx)
197  {
198  AcDebug.Log($"AcUtilsException caught and logged in AcPreferences.getPreferencesAsync{Environment.NewLine}{ecx.Message}");
199  }
200 
201  catch (Exception ecx) // IOException, DirectoryNotFoundException, PathTooLongException, SecurityException... others
202  {
203  AcDebug.Log($"Exception caught and logged in AcPreferences.getPreferencesAsync{Environment.NewLine}{ecx.Message}");
204  }
205 
206  return tempFile;
207  }
208  }
209 }
static async Task< bool[]> getIgnoreOptionsAsync()
Get the user's Diff/Merge and Ignore Options (AccuRev Diff only) preferences.
AccuRev program return value and command result.
Definition: AcCommand.cs:29
Get user preferences retrieved by way of the getpref command.
string CmdResult
The command result (usually XML) emitted by AccuRev.
Definition: AcCommand.cs:70
int RetVal
The AccuRev program return value for the command, otherwise minus one (-1) on error.
Definition: AcCommand.cs:61
static void Log(string message, bool formatting=true)
Write the message text to STDOUT, to weekly log files located in %LOCALAPPDATA%\AcTools\Logs, and to trigger.log in the AccuRev server's ..storage\site_slice\logs folder in the case of triggers.
Definition: AcDebug.cs:378
static async Task< string > getAcHomeFolderAsync()
Get the AccuRev home folder for the current user.
static async Task< bool?> getUseIgnoreElemsOptimizationAsync()
Get the user's USE_IGNORE_ELEMS_OPTIMIZATION setting.
AccuRev command processing.
Definition: AcCommand.cs:138
static async Task< AcResult > runAsync(string command, ICmdValidate validator=null)
Run the AccuRev command asynchronously with non-blocking I/O.
Definition: AcCommand.cs:184
Use to log and display error and general purpose text messages, and to save the XML param data sent b...
Definition: AcDebug.cs:100
static async Task< string > getPreferencesAsync()
Get user preferences retrieved by way of the getpref command.