forked from FortuneN/FineCodeCoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDataFolder.cs
108 lines (93 loc) · 3.18 KB
/
AppDataFolder.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
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
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
namespace FineCodeCoverage.Engine
{
[Export(typeof(IAppDataFolder))]
internal class AppDataFolder : IAppDataFolder
{
private readonly ILogger logger;
private readonly IEnvironmentVariable environmentVariable;
private readonly IAppOptionsProvider appOptionsProvider;
internal const string fccDebugCleanInstallEnvironmentVariable = "FCCDebugCleanInstall";
[ImportingConstructor]
public AppDataFolder(ILogger logger,IEnvironmentVariable environmentVariable, IAppOptionsProvider appOptionsProvider)
{
this.logger = logger;
this.environmentVariable = environmentVariable;
this.appOptionsProvider = appOptionsProvider;
}
public string DirectoryPath { get; private set; }
public void Initialize(CancellationToken camcellationToken)
{
camcellationToken.ThrowIfCancellationRequested();
CreateAppDataFolder();
camcellationToken.ThrowIfCancellationRequested();
CleanupLegacyFolders();
}
private void CreateAppDataFolder()
{
DirectoryPath = Path.Combine(GetAppDataFolder(), Vsix.Code);
if (environmentVariable.Get(fccDebugCleanInstallEnvironmentVariable) != null)
{
logger.Log("FCCDebugCleanInstall");
if (Directory.Exists(DirectoryPath))
{
try
{
Directory.Delete(DirectoryPath, true);
logger.Log("Deleted app data folder");
}
catch (Exception exc)
{
logger.Log("Error deleting app data folder", exc);
}
}
else
{
logger.Log("App data folder does not exist");
}
}
Directory.CreateDirectory(DirectoryPath);
}
private string GetAppDataFolder()
{
var dir = appOptionsProvider.Get().ToolsDirectory;
return Directory.Exists(dir) ? dir : Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
private void CleanupLegacyFolders()
{
Directory
.GetDirectories(DirectoryPath, "*", SearchOption.TopDirectoryOnly)
.Where(path =>
{
var name = Path.GetFileName(path);
if (name.Contains("__"))
{
return true;
}
if (Guid.TryParse(name, out var _))
{
return true;
}
return false;
})
.ToList()
.ForEach(path =>
{
try
{
Directory.Delete(path, true);
}
catch
{
// ignore
}
});
}
}
}