Skip to content

Commit ae51e91

Browse files
committed
Rebased: Added support for basic glob patterns in "inputFile"
1 parent b565092 commit ae51e91

File tree

7 files changed

+146
-18
lines changed

7 files changed

+146
-18
lines changed

src/WebCompiler/Config/Config.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using Newtonsoft.Json;
5+
using WebCompiler.Helpers;
56

67
namespace WebCompiler
78
{
@@ -186,5 +187,41 @@ private static bool DictionaryEqual<TKey, TValue>(
186187
}
187188
return true;
188189
}
190+
191+
internal Config Match(string folder, string sourceFile)
192+
{
193+
string inputFile = Path.Combine(folder, this.InputFile);
194+
195+
if (!GlobHelper.IsGlobPattern(this.InputFile))
196+
return sourceFile.Equals(inputFile.Replace('/', '\\'), System.StringComparison.OrdinalIgnoreCase)
197+
? this : null;
198+
199+
if (GlobHelper.Glob(sourceFile, inputFile))
200+
return MakeMatchedConfig(sourceFile);
201+
202+
return null;
203+
}
204+
205+
Config MakeMatchedConfig(string sourceFile)
206+
{
207+
string compileExtension = CompileHelper.GetCompiledExtension(sourceFile);
208+
return new Config()
209+
{
210+
InputFile = sourceFile,
211+
OutputFile = Path.ChangeExtension(sourceFile, compileExtension),
212+
FileName = this.FileName,
213+
IncludeInProject = this.IncludeInProject,
214+
Minify = this.Minify,
215+
Options = this.Options,
216+
Output = this.Output,
217+
SourceMap = this.SourceMap
218+
};
219+
}
220+
221+
internal IEnumerable<Config> Match(string folder)
222+
{
223+
return Directory.EnumerateFiles(folder, this.InputFile, SearchOption.AllDirectories)
224+
.Select(MakeMatchedConfig);
225+
}
189226
}
190-
}
227+
}

src/WebCompiler/Config/ConfigFileProcessor.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using WebCompiler.Helpers;
67

78
namespace WebCompiler
89
{
@@ -32,6 +33,7 @@ public IEnumerable<CompilerResult> Process(string configFile, IEnumerable<Config
3233
try
3334
{
3435
FileInfo info = new FileInfo(configFile);
36+
string directory = info.Directory.FullName;
3537
configs = configs ?? ConfigHandler.GetConfigs(configFile);
3638

3739
if (configs.Any())
@@ -41,8 +43,16 @@ public IEnumerable<CompilerResult> Process(string configFile, IEnumerable<Config
4143
{
4244
if (force || config.CompilationRequired())
4345
{
44-
var result = ProcessConfig(info.Directory.FullName, config);
45-
list.Add(result);
46+
if (GlobHelper.IsGlobPattern(config.InputFile))
47+
{
48+
foreach (Config matchedConfig in config.Match(directory))
49+
list.Add(ProcessConfig(directory, matchedConfig));
50+
}
51+
else
52+
{
53+
list.Add(ProcessConfig(directory, config));
54+
}
55+
4656
OnConfigProcessed(config, list.Count, configs.Count());
4757
}
4858
}
@@ -112,12 +122,11 @@ private IEnumerable<CompilerResult> SourceFileChanged(string configFile,
112122
// Compile if the file if it's referenced directly in compilerconfig.json
113123
foreach (Config config in configs)
114124
{
115-
string input = Path.Combine(folder, config.InputFile.Replace("/", "\\"));
116-
117-
if (input.Equals(sourceFile, StringComparison.OrdinalIgnoreCase))
125+
Config matchingConfig = config.Match(folder, sourceFile);
126+
if (matchingConfig != null)
118127
{
119-
list.Add(ProcessConfig(folder, config));
120-
compiledFiles.Add(input.ToLowerInvariant());
128+
list.Add(ProcessConfig(folder, matchingConfig));
129+
compiledFiles.Add(matchingConfig.InputFile.ToLowerInvariant());
121130
}
122131
}
123132

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace WebCompiler.Helpers
9+
{
10+
public static class CompileHelper
11+
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
/// <param name="filename"></param>
16+
/// <returns></returns>
17+
public static string GetCompiledExtension(string filename)
18+
{
19+
string extension = Path.GetExtension(filename).ToLowerInvariant();
20+
switch (extension)
21+
{
22+
case ".coffee":
23+
case ".iced":
24+
case ".litcoffee":
25+
case ".jsx":
26+
case ".es6":
27+
case ".hbs":
28+
case ".handlebars":
29+
return ".js";
30+
31+
case ".js":
32+
return ".es5.js";
33+
34+
default:
35+
return ".css";
36+
}
37+
}
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
using System.Threading.Tasks;
7+
8+
namespace WebCompiler.Helpers
9+
{
10+
/// <summary>
11+
/// Utils for glob matching
12+
/// </summary>
13+
public static class GlobHelper
14+
{
15+
/// <summary>
16+
/// Returns true if the input text contains a basic glob character: *?
17+
/// </summary>
18+
/// <param name="text"></param>
19+
/// <returns></returns>
20+
public static bool IsGlobPattern(string text)
21+
{
22+
return text != null && text.LastIndexOfAny(new char[] { '*', '?' }) >= 0;
23+
}
24+
25+
/// <summary>
26+
/// String matching including basic glob patterns: *?
27+
/// </summary>
28+
/// <param name="text">string to be matched</param>
29+
/// <param name="pattern">pattern to match against</param>
30+
/// <returns></returns>
31+
public static bool Glob(this string text, string pattern)
32+
{
33+
StringBuilder sb = new StringBuilder(pattern, pattern.Length + 10);
34+
sb.Replace('*', (char)1).Replace('?', (char)2).Replace('/', '\\');
35+
36+
pattern = Regex.Escape(sb.ToString());
37+
38+
sb.Clear().Append('^').Append(pattern).Replace("\u0001", ".*").Replace("\u0002", ".").Append('$');
39+
40+
return Regex.IsMatch(text, sb.ToString(), RegexOptions.IgnoreCase);
41+
}
42+
}
43+
}

src/WebCompiler/WebCompiler.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@
8585
<Compile Include="Dependencies\DependencyResolverBase.cs" />
8686
<Compile Include="Dependencies\LessDependencyResolver.cs" />
8787
<Compile Include="Dependencies\SassDependencyResolver.cs" />
88+
<Compile Include="Helpers\CompileHelper.cs" />
8889
<Compile Include="Helpers\FileHelpers.cs" />
90+
<Compile Include="Helpers\GlobHelper.cs" />
8991
<Compile Include="Minify\BaseMinifyOptions.cs" />
9092
<Compile Include="Minify\FileMinifier.cs" />
9193
<Compile Include="Minify\CssOptions.cs" />

src/WebCompilerVsix/Adornments/AdornmentProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.VisualStudio.Text;
1010
using Microsoft.VisualStudio.Text.Editor;
1111
using Microsoft.VisualStudio.Utilities;
12+
using WebCompiler.Helpers;
1213

1314
namespace WebCompilerVsix
1415
{
@@ -101,6 +102,11 @@ private void CreateAdornments(ITextDocument document, IWpfTextView textView)
101102

102103
foreach (Config config in configs)
103104
{
105+
if (GlobHelper.IsGlobPattern(config.InputFile))
106+
{
107+
continue;
108+
}
109+
104110
if (config.GetAbsoluteOutputFile().FullName.Equals(normalizedFilePath, StringComparison.OrdinalIgnoreCase))
105111
{
106112
GeneratedAdornment generated = new GeneratedAdornment(textView, _isVisible, _initOpacity);

src/WebCompilerVsix/Commands/CreateConfig.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using EnvDTE80;
88
using Microsoft.VisualStudio.Shell;
99
using WebCompiler;
10+
using WebCompiler.Helpers;
1011

1112
namespace WebCompilerVsix
1213
{
@@ -163,16 +164,7 @@ private static Config CreateConfigFile(string inputfile, string outputFile)
163164

164165
private static string GetOutputFileName(string inputFile)
165166
{
166-
string extension = Path.GetExtension(inputFile).ToLowerInvariant();
167-
string ext = ".css";
168-
169-
if (extension == ".coffee" || extension == ".iced" || extension == ".litcoffee" || extension == ".jsx" || extension == ".es6" || extension == ".hbs" || extension == ".handlebars")
170-
ext = ".js";
171-
172-
if (extension == ".js")
173-
ext = ".es5.js";
174-
175-
return Path.ChangeExtension(inputFile, ext);
167+
return Path.ChangeExtension(inputFile, CompileHelper.GetCompiledExtension(inputFile));
176168
}
177169
}
178170
}

0 commit comments

Comments
 (0)