This repository was archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAcPreferences.cs
More file actions
209 lines (186 loc) · 9.29 KB
/
AcPreferences.cs
File metadata and controls
209 lines (186 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*! \file
Copyright (C) 2016-2018 Verizon. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.IO;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AcUtils
{
/// <summary>
/// Get user preferences retrieved by way of the \c getpref command.
/// </summary>
[Serializable]
public static class AcPreferences
{
private static string _acHomeFolder; // The AccuRev home directory
/// <summary>
/// Get the user's Diff/Merge and Ignore Options (AccuRev Diff only) preferences.
/// </summary>
/// <remarks>
/// <em>Ignore Whitespace</em> is \e true if spaces, tabs and empty lines should be ignored, \e false otherwise.
/// A setting of \e true overrides <em>Ignore Changes in Whitespace</em> setting.<br>
/// <em>Ignore Changes in Whitespace</em> is \e true if a change in the amount of whitespace should be considered
/// a change to that line, \e false otherwise.<br>
/// <em>Ignore Case</em> is \e true if uppercase and lowercase characters should be considered the same when
/// comparing text, \e false otherwise.
/// </remarks>
/// <returns>An array initialized as <em>bool[]={ignoreWhitespace, ignoreWhitespaceChanges, ignoreCase}</em>
/// on success, otherwise \e null.</returns>
/*! \accunote_ Selecting "Ignore Whitespace" and "Ignore Changes in Whitespace" applies to \c diff only.
The \c merge command does not use these settings and will show all conflicts. */
/// <exception cref="Exception">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on failure to handle a range of exceptions.</exception>
public static async Task<bool[]> getIgnoreOptionsAsync()
{
string tmpFile = await getPreferencesAsync().ConfigureAwait(false);
if (String.IsNullOrEmpty(tmpFile)) // unlikely
return null; // error already logged
bool[] arr = null; // ignoreWhitespace, ignoreWhitespaceChanges, ignoreCase
try
{
using (StreamReader reader = new StreamReader(tmpFile))
{
XElement doc = XElement.Load(reader);
bool ignoreWhitespace = (bool)doc.Element("diffIgnoreWhitespace");
bool ignoreWhitespaceChanges = (bool)doc.Element("diffIgnoreWhitespaceChanges");
bool ignoreCase = (bool)doc.Element("diffIgnoreCase");
arr = new bool[] { ignoreWhitespace, ignoreWhitespaceChanges, ignoreCase };
}
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcPreferences.getIgnoreOptionsAsync{Environment.NewLine}{ecx.Message}");
}
finally
{
if (!String.IsNullOrEmpty(tmpFile))
File.Delete(tmpFile);
}
return arr;
}
/// <summary>
/// Get the user's USE_IGNORE_ELEMS_OPTIMIZATION setting.
/// </summary>
/// <returns>A bool set to \e true or \e false on operation success, otherwise \e null on error.
/// <exception cref="Exception">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on failure to handle a range of exceptions.</exception>
public static async Task<bool?> getUseIgnoreElemsOptimizationAsync()
{
string tmpFile = await getPreferencesAsync().ConfigureAwait(false);
if (String.IsNullOrEmpty(tmpFile)) // unlikely
return null; // error already logged
bool? useIgnoreElemsOptimization = null;
try
{
using (StreamReader reader = new StreamReader(tmpFile))
{
XElement doc = XElement.Load(reader);
useIgnoreElemsOptimization = (bool)doc.Element("USE_IGNORE_ELEMS_OPTIMIZATION");
}
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcPreferences.getUseIgnoreElemsOptimizationAsync{Environment.NewLine}{ecx.Message}");
}
finally
{
if (!String.IsNullOrEmpty(tmpFile))
File.Delete(tmpFile);
}
return useIgnoreElemsOptimization;
}
/// <summary>
/// Get the AccuRev home folder for the current user.
/// </summary>
/// <returns>The full path to the user's AccuRev home folder on success, otherwise \e null on error.</returns>
/// <exception cref="Exception">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on failure to handle a range of exceptions.</exception>
public static async Task<string> getAcHomeFolderAsync()
{
if (_acHomeFolder == null) // first time initialization only
{
string tmpFile = await getPreferencesAsync().ConfigureAwait(false);
if (String.IsNullOrEmpty(tmpFile)) // unlikely
return null; // error already logged
try
{
using (StreamReader reader = new StreamReader(tmpFile))
{
XElement doc = XElement.Load(reader);
string home = (string) doc.Element("HOME");
_acHomeFolder = Path.Combine(home, ".accurev");
}
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in AcPreferences.getAcHomeFolderAsync{Environment.NewLine}{ecx.Message}");
}
finally
{
if (!String.IsNullOrEmpty(tmpFile))
File.Delete(tmpFile);
}
}
return _acHomeFolder;
}
/// <summary>
/// Get user preferences retrieved by way of the \c getpref command.
/// </summary>
/// <returns>The full path to a temp file with the XML results from the \e getpref command, otherwise \e null on error.
/// The caller is responsible for deleting the file.</returns>
/*! \getpref_ \c getpref */
/*! \accunote_ When using the AccuRev GUI client, changes in <em>User Preferences</em> do not always persist.
As a workaround, use [setpref](https://www.microfocus.com/documentation/accurev/72/WebHelp/wwhelp/wwhimpl/js/html/wwhelp.htm#href=AccuRev_User_CLI/cli_ref_setpref.html):
-# Exit the AccuRev client.
-# Put the setting\(s\) needed in a file named <tt>set_pref.xml</tt>
-# Open a command window and cd to the folder where the file is located.
-# Run the command <tt>accurev setpref -l set_pref.xml</tt> \(-l switch is a lowercase L\)
-# Run the AccuRev client and verify the setting\(s\).
\verbatim
<AcRequest>
<USE_IGNORE_ELEMS_OPTIMIZATION>true</USE_IGNORE_ELEMS_OPTIMIZATION>
...
</AcRequest>
\endverbatim */
/// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
/// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c getpref command failure.</exception>
/// <exception cref="Exception">caught and logged in same on failure to handle a range of exceptions.</exception>
private static async Task<string> getPreferencesAsync()
{
// Putting the XML into a file as opposed to using it directly avoids exceptions thrown
// due to illegal characters. Needs further investigation.
string tempFile = null;
try
{
AcResult r = await AcCommand.runAsync("getpref").ConfigureAwait(false);
if (r != null && r.RetVal == 0) // if command succeeded
{
tempFile = Path.GetTempFileName();
using (StreamWriter writer = new StreamWriter(tempFile))
{
writer.Write(r.CmdResult);
}
}
}
catch (AcUtilsException ecx)
{
AcDebug.Log($"AcUtilsException caught and logged in AcPreferences.getPreferencesAsync{Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx) // IOException, DirectoryNotFoundException, PathTooLongException, SecurityException... others
{
AcDebug.Log($"Exception caught and logged in AcPreferences.getPreferencesAsync{Environment.NewLine}{ecx.Message}");
}
return tempFile;
}
}
}