forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemMigrator.cs
More file actions
92 lines (76 loc) · 2.99 KB
/
FileSystemMigrator.cs
File metadata and controls
92 lines (76 loc) · 2.99 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
using System.Collections.Generic;
using System.Linq;
using ScriptCs.Contracts;
using ScriptCs.Logging;
namespace ScriptCs
{
public class FileSystemMigrator : IFileSystemMigrator
{
private readonly IFileSystem _fileSystem;
private readonly ILog _logger;
private readonly Dictionary<string, string> _fileCopies;
private readonly Dictionary<string, string> _directoryMoves;
private readonly Dictionary<string, string> _directoryCopies;
public FileSystemMigrator(IFileSystem fileSystem, ILog logger)
{
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgument("logger", logger);
_fileSystem = fileSystem;
_logger = logger;
_fileCopies = new Dictionary<string, string>
{
{ "packages.config", _fileSystem.PackagesFile },
{ "nuget.config", _fileSystem.NugetFile },
};
_directoryMoves = new Dictionary<string, string>
{
{ ".cache", _fileSystem.DllCacheFolder },
};
_directoryCopies = new Dictionary<string, string>
{
{ "bin", _fileSystem.BinFolder },
{ "packages", _fileSystem.PackagesFolder },
};
}
public void Migrate()
{
foreach (var copy in _fileCopies
.Where(copy => _fileSystem.FileExists(copy.Value)))
{
_logger.DebugFormat(
"Not performing migration since file '{0}' already exists.",
copy.Value);
return;
}
foreach (var action in _directoryMoves.Concat(_directoryCopies)
.Where(action => _fileSystem.DirectoryExists(action.Value)))
{
_logger.DebugFormat(
"Not performing migration since directory '{0}' already exists.",
action.Value);
return;
}
foreach (var copy in _fileCopies
.Where(copy => _fileSystem.FileExists(copy.Key)))
{
_logger.InfoFormat(
"Copying file '{0}' to '{1}'...", copy.Key, copy.Value);
_fileSystem.Copy(copy.Key, copy.Value, false);
}
foreach (var move in _directoryMoves
.Where(move => _fileSystem.DirectoryExists(move.Key)))
{
_logger.InfoFormat(
"Moving directory '{0}' to '{1}'...", move.Key, move.Value);
_fileSystem.MoveDirectory(move.Key, move.Value);
}
foreach (var copy in _directoryCopies
.Where(copy => _fileSystem.DirectoryExists(copy.Key)))
{
_logger.InfoFormat(
"Copying directory '{0}' to '{1}'...", copy.Key, copy.Value);
_fileSystem.CopyDirectory(copy.Key, copy.Value, false);
}
}
}
}