Skip to content

Commit 8b779df

Browse files
committed
Implement the UserFormRequiresBinaryFileNameExtractor
1 parent 88fba12 commit 8b779df

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

Rubberduck.VBEEditor/Utility/ModuleNameFromFileExtractor.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34
using System.Text;
5+
using Rubberduck.JunkDrawer.Extensions;
46
using Rubberduck.VBEditor.Extensions;
7+
using Rubberduck.VBEditor.SafeComWrappers;
58

69
namespace Rubberduck.VBEditor.Utility
710
{
@@ -19,10 +22,9 @@ public string ModuleName(string filename)
1922
return null;
2023
}
2124

22-
//We cannot read binary files.
23-
if(ComponentTypeExtensions.FormBinaryExtension.Equals(Path.GetExtension(filename)))
25+
if (!SupportedExtensions.Contains(Path.GetExtension(filename)))
2426
{
25-
return Path.GetFileNameWithoutExtension(filename);
27+
return null;
2628
}
2729

2830
var contents = File.ReadLines(filename, Encoding.Default);
@@ -35,5 +37,10 @@ public string ModuleName(string filename)
3537
//The format is Attribute VB_Name = "ModuleName"
3638
return nameLine.Substring("Attribute VB_Name = ".Length + 1, nameLine.Length - "Attribute VB_Name = ".Length - 2);
3739
}
40+
41+
private static ICollection<string> SupportedExtensions =>
42+
ComponentTypeExtensions.ComponentTypesForExtension(VBEKind.Hosted).Keys
43+
.Concat(ComponentTypeExtensions.ComponentTypesForExtension(VBEKind.Standalone).Keys)
44+
.ToHashSet();
3845
}
3946
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
5+
using Rubberduck.VBEditor.Extensions;
6+
using Rubberduck.VBEditor.SafeComWrappers;
7+
using Path = System.IO.Path;
8+
9+
namespace Rubberduck.VBEditor.Utility
10+
{
11+
public class UserFormRequiredBinaryFileNameExtractor : IRequiredBinaryFilesFromFileNameExtractor
12+
{
13+
public ICollection<ComponentType> SupportedComponentTypes => new List<ComponentType>{ComponentType.UserForm};
14+
15+
public ICollection<string> RequiredBinaryFiles(string fileName, ComponentType componentType)
16+
{
17+
if (!File.Exists(fileName))
18+
{
19+
return null;
20+
}
21+
22+
if (!SupportedComponentTypes.Contains(componentType))
23+
{
24+
return null;
25+
}
26+
27+
if (componentType.FileExtension() != Path.GetExtension(fileName))
28+
{
29+
return null;
30+
}
31+
32+
var regExPattern = "OleObjectBlob\\s+=\\s+\"([^\"]+)\":";
33+
var regEx = new Regex(regExPattern);
34+
var contents = File.ReadLines(fileName, Encoding.Default);
35+
36+
foreach(var codeLine in contents)
37+
{
38+
var match = regEx.Match(codeLine);
39+
if (match.Success)
40+
{
41+
return new List<string>{match.Groups[1].Value};
42+
}
43+
}
44+
45+
var fallbackBinaryName = Path.GetFileNameWithoutExtension(fileName) + componentType.BinaryFileExtension();
46+
return new List<string>{fallbackBinaryName};
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)