Skip to content

Commit

Permalink
Merge pull request #319 from Sshnyari/wildcards
Browse files Browse the repository at this point in the history
adding wildcards in blacklist
  • Loading branch information
turquoiseowl authored Dec 14, 2016
2 parents 38c7380 + c0145f5 commit a955e92
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ of your application's source files:
<appSettings>
<add key="i18n.DirectoriesToScan" value=".." /> <!-- Rel to web.config file -->
<add key="i18n.WhiteList" value="*.cs;*.cshtml;*.sitemap" />
<add key="i18n.BlackList" value=".\js\kendo;.\js\angular" />
<add key="i18n.BlackList" value=".\js\kendo;.\js\angular;.\*\dist" />
</appSettings>
```

Expand Down
37 changes: 35 additions & 2 deletions src/i18n.Domain.Tests/FileNuggetFinderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using i18n.Domain.Concrete;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -33,5 +35,36 @@ public void FileNuggetFinder_disable_references()
Assert.AreEqual(1, item.References.Count());
}
}

[TestMethod]
public void FileNuggetFinder_blacklist_wildcards()
{
var settingService = new SettingService_Mock();
var root = Path.GetDirectoryName(settingService.GetConfigFileLocation());
{
//"*" wildcard
settingService.SetSetting("i18n.BlackList", "./TestDir/*");
i18nSettings settings = new i18nSettings(settingService);
//FileNuggetFinder finder = new FileNuggetFinder(settings);
List<string> expected = new List<string>()
{
Path.Combine(root, "TestDir", "Dir02"),
Path.Combine(root, "TestDir", "Dir1"),
};

CollectionAssert.AreEqual(expected, settings.BlackList.ToList());
}
{
//"?" wildcard
settingService.SetSetting("i18n.BlackList", "./TestDir/Dir?");
i18nSettings settings = new i18nSettings(settingService);
List<string> expected = new List<string>()
{
Path.Combine(root, "TestDir", "Dir1"),
};

CollectionAssert.AreEqual(expected, settings.BlackList.ToList());
}
}
}
}
}
89 changes: 83 additions & 6 deletions src/i18n.Domain/Concrete/i18nSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class i18nSettings
{
private AbstractSettingService _settingService;
private const string _prefix = "i18n.";
private const string _allToken = "*";
private const string _oneToken = "?";

public i18nSettings(AbstractSettingService settings)
{
Expand Down Expand Up @@ -42,6 +44,72 @@ private string MakePathAbsoluteAndFromConfigFile(string path)
}


/// <summary>
/// Determines whether the specified path has a windows wildcard character (* or ?)
/// </summary>
/// <param name="path">The path.</param>
/// <returns>
/// <c>true</c> if the specified path has a wildcard otherwise, <c>false</c>.
/// </returns>
private static bool HasSearchCharacter(string path)
{
return path.Contains(_allToken) || path.Contains(_oneToken);
}

/// <summary>
/// Find all the existing physical paths that corresponds to the specified path.
/// Returns a single value if there are no wildcards in the specified path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>An enumeration of corresponding paths</returns>
private IEnumerable<string> FindPaths(string path)
{
List<string> paths = new List<string>();
if (HasSearchCharacter(path))
{
string[] parts = path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
paths = GetPaths(parts).ToList();
}
else
{
paths.Add(path);
}
return paths;
}

/// <summary>
/// Recursively gets the path by moving through a directory tree (parts).
/// </summary>
/// <param name="parts">The path parts to process.</param>
/// <param name="root">The root path from where to start.</param>
/// <returns>A list of existing paths</returns>
private IEnumerable<string> GetPaths(string[] parts, string root = "")
{
if (parts == null || parts.Length == 0)
{
if (Directory.Exists(root))
return new[] { Path.GetFullPath(root) };
return Enumerable.Empty<string>();
}

List<string> paths = new List<string>();
if (HasSearchCharacter(parts[0]))
{
var rooted = MakePathAbsoluteAndFromConfigFile(root);
string[] list = Directory.GetDirectories(rooted, parts[0]);
foreach (string path in list)
{
paths.AddRange(GetPaths(parts.Skip(1).ToArray(), path));
}
}
else
{
return GetPaths(parts.Skip(1).ToArray(), Path.Combine(root, parts[0]));
}

return paths;
}

#region Locale directory

private const string _localeDirectoryDefault = "locale";
Expand Down Expand Up @@ -158,10 +226,10 @@ public virtual IEnumerable<string> WhiteList

#endregion


#region Black list

private const string _blackListDefault = "";
private IList<string> _cached_blackList;

/// <summary>
/// Describes zero or more source directory/folder paths to be ignored during nugget parsing
Expand All @@ -179,8 +247,19 @@ public virtual IEnumerable<string> BlackList
{
get
{
if(_cached_blackList != null)
{
return _cached_blackList;
}
_cached_blackList = new List<string>();
string prefixedString = GetPrefixedString("BlackList");
string setting = _settingService.GetSetting(prefixedString);
//If we find any wildcard in the setting, we replace it by the exitsing physical paths
if (setting != null && HasSearchCharacter(setting))
{
IEnumerable<string> preblacklist = setting.Split(';');
setting = string.Join(";", preblacklist.SelectMany(FindPaths));
}
List<string> list;
if (setting != null)
{
Expand All @@ -192,16 +271,15 @@ public virtual IEnumerable<string> BlackList
}
else
{
return new List<string>();
return _cached_blackList;
}

List<string> returnList = new List<string>();
foreach (var path in list.Where(x => !string.IsNullOrWhiteSpace(x)))
{
returnList.Add(MakePathAbsoluteAndFromConfigFile(path));
_cached_blackList.Add(MakePathAbsoluteAndFromConfigFile(path));
}

return returnList;
return _cached_blackList;
}
set
{
Expand All @@ -212,7 +290,6 @@ public virtual IEnumerable<string> BlackList

#endregion


#region Nugget tokens

private const string _nuggetBeginTokenDefault = "[[[";
Expand Down

0 comments on commit a955e92

Please sign in to comment.