/
ExportAllCommand.cs
106 lines (92 loc) · 3.21 KB
/
ExportAllCommand.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
using System.IO;
using System.Windows.Forms;
using NLog;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.Navigation.CodeExplorer;
using Rubberduck.Resources;
using Rubberduck.VBEditor.SafeComWrappers;
namespace Rubberduck.UI.Command
{
public class ExportAllCommand : CommandBase
{
private readonly IVBE _vbe;
private readonly IFileSystemBrowserFactory _factory;
public ExportAllCommand(IVBE vbe, IFileSystemBrowserFactory folderBrowserFactory) : base(LogManager.GetCurrentClassLogger())
{
_vbe = vbe;
_factory = folderBrowserFactory;
}
protected override bool EvaluateCanExecute(object parameter)
{
if (_vbe.Kind == VBEKind.Standalone)
{
return false;
}
if (!(parameter is CodeExplorerProjectViewModel) && parameter is CodeExplorerItemViewModel)
{
return false;
}
switch (parameter)
{
case CodeExplorerProjectViewModel projectNode:
return Evaluate(projectNode.Declaration.Project);
case IVBProject project:
return Evaluate(project);
}
using (var activeProject = _vbe.ActiveVBProject)
{
return Evaluate(activeProject);
}
}
private bool Evaluate(IVBProject project)
{
if (project == null || project.IsWrappingNullReference)
{
return false;
}
using (var compontents = project.VBComponents)
{
return compontents.Count > 0;
}
}
protected override void OnExecute(object parameter)
{
switch (parameter)
{
case CodeExplorerProjectViewModel projectNode when projectNode.Declaration.Project != null:
Export(projectNode.Declaration.Project);
break;
case IVBProject vbproject:
Export(vbproject);
break;
default:
{
using (var project = _vbe.ActiveVBProject)
{
Export(project);
}
break;
}
}
}
private void Export(IVBProject project)
{
var desc = string.Format(RubberduckUI.ExportAllCommand_SaveAsDialog_Title, project.Name);
// If .GetDirectoryName is passed an empty string for a RootFolder,
// it defaults to the Documents library (Win 7+) or equivalent.
var path = string.Empty;
if (!string.IsNullOrWhiteSpace(project.FileName))
{
path = Path.GetDirectoryName(project.FileName);
}
using (var _folderBrowser = _factory.CreateFolderBrowser(desc, true, path))
{
var result = _folderBrowser.ShowDialog();
if (result == DialogResult.OK)
{
project.ExportSourceFiles(_folderBrowser.SelectedPath);
}
}
}
}
}