1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
+ using System . Text . RegularExpressions ;
4
5
5
6
namespace WebCompiler
6
7
{
@@ -29,7 +30,7 @@ public override void UpdateFileDependencies(string path)
29
30
if ( this . Dependencies != null )
30
31
{
31
32
path = path . ToLowerInvariant ( ) ;
32
-
33
+
33
34
if ( ! Dependencies . ContainsKey ( path ) )
34
35
Dependencies [ path ] = new Dependencies ( ) ;
35
36
@@ -49,60 +50,72 @@ public override void UpdateFileDependencies(string path)
49
50
string content = File . ReadAllText ( info . FullName ) ;
50
51
51
52
//match both <@import "myFile.scss";> and <@import url("myFile.scss");> syntax
52
- var matches = System . Text . RegularExpressions . Regex . Matches ( content , " @import([ \\ s]+)( \\ ([ \\ S]+ \\ )([ \\ s]+ ))?(url \\ ( )?('| \" |)( ?<url>[^' \" \\ ):?:]+)('| \" | \\ ))" ) ;
53
- foreach ( System . Text . RegularExpressions . Match match in matches )
53
+ var matches = Regex . Matches ( content , @"(?<=^ @import(?:[\ s]+))(?:(?:\(\w+\) ))?\s*(?:url )?(?<url>.+?)$" , RegexOptions . Multiline ) ;
54
+ foreach ( Match match in matches )
54
55
{
55
- FileInfo importedfile = GetFileInfo ( info , match ) ;
56
-
57
- if ( importedfile == null )
58
- continue ;
56
+ var importedfiles = GetFileInfos ( info , match ) ;
59
57
60
- //if the file doesn't end with the correct extension, an import statement without extension is probably used, to re-add the extension (#175)
61
- if ( string . Compare ( importedfile . Extension , FileExtension , StringComparison . OrdinalIgnoreCase ) != 0 )
58
+ foreach ( FileInfo importedfile in importedfiles )
62
59
{
63
- importedfile = new FileInfo ( importedfile . FullName + this . FileExtension ) ;
64
- }
60
+ if ( importedfile == null )
61
+ continue ;
65
62
66
- var dependencyFilePath = importedfile . FullName . ToLowerInvariant ( ) ;
63
+ var theFile = importedfile ;
67
64
68
- if ( ! File . Exists ( dependencyFilePath ) )
69
- {
70
- // Trim leading underscore to support Sass partials
71
- var dir = Path . GetDirectoryName ( dependencyFilePath ) ;
72
- var fileName = Path . GetFileName ( dependencyFilePath ) ;
73
- var cleanPath = Path . Combine ( dir , "_" + fileName ) ;
65
+ //if the file doesn't end with the correct extension, an import statement without extension is probably used, to re-add the extension (#175)
66
+ if ( string . Compare ( importedfile . Extension , FileExtension , StringComparison . OrdinalIgnoreCase ) != 0 )
67
+ {
68
+ theFile = new FileInfo ( importedfile . FullName + this . FileExtension ) ;
69
+ }
74
70
75
- if ( ! File . Exists ( cleanPath ) )
76
- continue ;
71
+ var dependencyFilePath = theFile . FullName . ToLowerInvariant ( ) ;
77
72
78
- dependencyFilePath = cleanPath ;
79
- }
73
+ if ( ! File . Exists ( dependencyFilePath ) )
74
+ {
75
+ // Trim leading underscore to support Sass partials
76
+ var dir = Path . GetDirectoryName ( dependencyFilePath ) ;
77
+ var fileName = Path . GetFileName ( dependencyFilePath ) ;
78
+ var cleanPath = Path . Combine ( dir , "_" + fileName ) ;
79
+
80
+ if ( ! File . Exists ( cleanPath ) )
81
+ continue ;
82
+
83
+ dependencyFilePath = cleanPath ;
84
+ }
80
85
81
- if ( ! Dependencies [ path ] . DependentOn . Contains ( dependencyFilePath ) )
82
- Dependencies [ path ] . DependentOn . Add ( dependencyFilePath ) ;
86
+ if ( ! Dependencies [ path ] . DependentOn . Contains ( dependencyFilePath ) )
87
+ Dependencies [ path ] . DependentOn . Add ( dependencyFilePath ) ;
83
88
84
- if ( ! Dependencies . ContainsKey ( dependencyFilePath ) )
85
- Dependencies [ dependencyFilePath ] = new Dependencies ( ) ;
89
+ if ( ! Dependencies . ContainsKey ( dependencyFilePath ) )
90
+ Dependencies [ dependencyFilePath ] = new Dependencies ( ) ;
86
91
87
- if ( ! Dependencies [ dependencyFilePath ] . DependentFiles . Contains ( path ) )
88
- Dependencies [ dependencyFilePath ] . DependentFiles . Add ( path ) ;
92
+ if ( ! Dependencies [ dependencyFilePath ] . DependentFiles . Contains ( path ) )
93
+ Dependencies [ dependencyFilePath ] . DependentFiles . Add ( path ) ;
94
+ }
89
95
}
90
96
}
91
97
}
92
98
93
- private static FileInfo GetFileInfo ( FileInfo info , System . Text . RegularExpressions . Match match )
99
+ private static IEnumerable < FileInfo > GetFileInfos ( FileInfo info , System . Text . RegularExpressions . Match match )
94
100
{
95
- string url = match . Groups [ "url" ] . Value ;
101
+ string url = match . Groups [ "url" ] . Value . Replace ( "'" , "\" " ) . Replace ( "(" , "" ) . Replace ( ")" , "" ) . Replace ( ";" , "" ) . Trim ( ) ;
102
+ var list = new List < FileInfo > ( ) ;
96
103
97
- try
98
- {
99
- return new FileInfo ( Path . Combine ( info . DirectoryName , match . Groups [ "url" ] . Value ) ) ;
100
- }
101
- catch ( Exception )
104
+ foreach ( string name in url . Split ( new [ ] { "\" ," } , StringSplitOptions . RemoveEmptyEntries ) )
102
105
{
103
- // Not a valid file name
104
- return null ;
106
+ try
107
+ {
108
+ string value = name . Replace ( "\" " , "" ) . Replace ( "/" , "\\ " ) . Trim ( ) ;
109
+ list . Add ( new FileInfo ( Path . Combine ( info . DirectoryName , value ) ) ) ;
110
+ }
111
+ catch ( Exception ex )
112
+ {
113
+ // Not a valid file name
114
+ System . Diagnostics . Debug . Write ( ex ) ;
115
+ }
105
116
}
117
+
118
+ return list ;
106
119
}
107
120
}
108
121
}
0 commit comments