-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializeProjectAction.cs
More file actions
129 lines (114 loc) · 4.6 KB
/
InitializeProjectAction.cs
File metadata and controls
129 lines (114 loc) · 4.6 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace DirTemplateExtension
{
internal class InitializeProjectAction
{
private readonly DirectoryInfo _templateDir;
private readonly List<DirectoryInfo> _targetDirs;
private BackgroundWorker _worker;
private FormProgress _formProgress;
protected InitializeProjectAction(DirectoryInfo templateDir, List<DirectoryInfo> targetDirs)
{
_templateDir = templateDir;
_targetDirs = targetDirs;
}
public void Start()
{
_worker = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
_worker.DoWork += Worker_DoWork;
_worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
_formProgress = new FormProgress(_templateDir.Name, _worker);
_formProgress.Show();
_worker.RunWorkerAsync();
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_formProgress.Invoke(new MethodInvoker(() => _formProgress.Close()));
if (e.Error != null)
{
MessageBox.Show(Resources.InitializeProjectAction_Worker_ErrorOccurred,
Resources.InitializeProjectAction_Worker_ErrorOccurredTitle,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (e.Cancelled)
{
MessageBox.Show(Resources.InitializeProjectAction_Worker_Canceled,
Resources.InitializeProjectAction_Worker_CanceledTitle, MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
Logger.Debug("Starting worker");
InitializeDirectories(_templateDir, _targetDirs);
}
catch (WorkCancelledException)
{
Logger.Debug("Caught work cancelled signal");
e.Cancel = true;
_worker.CancelAsync();
}
}
private void InitializeDirectories(DirectoryInfo templateDir, IEnumerable<DirectoryInfo> targetDirs)
{
if (_targetDirs.Count == 0)
{
Logger.WriteLog("Received an empty list of target directories.", EventLogEntryType.Error);
}
foreach (var targetDir in targetDirs.TakeWhile(targetDir => !_worker.CancellationPending))
{
Logger.Debug($"Processing target dir '{targetDir}'");
if (!DirectoryUtils.IsDirectoryEmpty(targetDir))
{
var dlgResult = MessageBox.Show(_formProgress,
string.Format(Resources.InitializeProjectAction_NotEmptyDirWarning, targetDir.FullName),
Resources.InitializeProjectAction_NotEmptyDirWarningTitle, MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning);
switch (dlgResult)
{
case DialogResult.No:
continue;
case DialogResult.Cancel:
throw new WorkCancelledException();
}
}
try
{
DirectoryUtils.CopyAll(templateDir, targetDir, _worker);
Logger.WriteLog($"Initialized project \"{targetDir}\" from template \"{templateDir}\"");
}
catch (Exception exception) when (!(exception is WorkCancelledException))
{
Logger.WriteLog(
$"An error occurred while initializing project \"{targetDir}\" from template \"{templateDir}\".\nException:\n{exception}",
EventLogEntryType.Error);
throw;
}
}
if (_worker.CancellationPending)
{
throw new WorkCancelledException();
}
}
public static InitializeProjectAction Build(string templateDir, List<string> targetDir)
{
return new InitializeProjectAction(new DirectoryInfo(templateDir),
targetDir.ConvertAll(dirPath => new DirectoryInfo(dirPath)));
}
protected class WorkCancelledException : Exception
{
}
}
}