-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathTempSourceFileHandler.cs
75 lines (64 loc) · 2.1 KB
/
TempSourceFileHandler.cs
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
using System.IO.Abstractions;
using System.Text;
using Rubberduck.InternalApi.Common;
using Rubberduck.Resources;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.SourceCodeHandling;
namespace Rubberduck.VBEditor.VBA
{
public class TempSourceFileHandler : ITempSourceFileHandler
{
private IFileSystem _fileSystem = FileSystemProvider.FileSystem;
public string Export(IVBComponent component)
{
if (!_fileSystem.Directory.Exists(ApplicationConstants.RUBBERDUCK_TEMP_PATH))
{
_fileSystem.Directory.CreateDirectory(ApplicationConstants.RUBBERDUCK_TEMP_PATH);
}
var fileName = component.ExportAsSourceFile(ApplicationConstants.RUBBERDUCK_TEMP_PATH, true, false);
return _fileSystem.File.Exists(fileName)
? fileName
: null;
}
public IVBComponent ImportAndCleanUp(IVBComponent component, string fileName)
{
if (fileName == null || !_fileSystem.File.Exists(fileName))
{
return component;
}
IVBComponent newComponent = null;
using (var components = component.Collection)
{
components.Remove(component);
newComponent = components.ImportSourceFile(fileName);
}
try
{
_fileSystem.File.Delete(fileName);
}
catch
{
// Meh.
}
return newComponent;
}
public string Read(IVBComponent component)
{
var fileName = Export(component);
if (fileName == null || !_fileSystem.File.Exists(fileName))
{
return null;
}
var code = _fileSystem.File.ReadAllText(fileName, Encoding.Default);
try
{
_fileSystem.File.Delete(fileName);
}
catch
{
// Meh.
}
return code;
}
}
}