Skip to content

Commit

Permalink
Get sample web working with plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Feb 17, 2012
1 parent b92a68d commit 22f8fb0
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/dotless.Compiler/Program.cs
Expand Up @@ -176,7 +176,7 @@ private static IEnumerable<IPluginConfigurator> GetPluginConfigurators()
{
if (_pluginConfigurators == null)
{
_pluginConfigurators = PluginFinder.GetConfigurators(true, false);
_pluginConfigurators = PluginFinder.GetConfigurators(true);
}
return _pluginConfigurators;
}
Expand Down Expand Up @@ -230,7 +230,7 @@ private static void WriteHelp()

private static CompilerConfiguration GetConfigurationFromArguments(List<string> arguments)
{
var configuration = new CompilerConfiguration(DotlessConfiguration.Default);
var configuration = new CompilerConfiguration(DotlessConfiguration.GetDefault());

foreach (var arg in arguments)
{
Expand Down
3 changes: 3 additions & 0 deletions src/dotless.Core/ContainerFactory.cs
Expand Up @@ -13,6 +13,7 @@ namespace dotless.Core
using Stylizers;
using dotless.Core.Plugins;
using System.Collections.Generic;
using dotless.Core.Importers;

public class ContainerFactory
{
Expand Down Expand Up @@ -74,6 +75,8 @@ private void RegisterCoreServices(FluentRegistration pandora, DotlessConfigurati
pandora.Service<LogLevel>("error-level").Instance(configuration.LogLevel);
pandora.Service<IStylizer>().Implementor<PlainStylizer>();

pandora.Service<IImporter>().Implementor<Importer>();

pandora.Service<Parser.Parser>().Implementor<Parser.Parser>().Parameters("optimization").Set("default-optimization").Lifestyle.Transient();
pandora.Service<int>("default-optimization").Instance(configuration.Optimization);

Expand Down
1 change: 1 addition & 0 deletions src/dotless.Core/Engine/LessEngine.cs
Expand Up @@ -8,6 +8,7 @@ namespace dotless.Core
using Loggers;
using Parser.Infrastructure;
using Parser.Tree;
using System.IO;

public class LessEngine : ILessEngine
{
Expand Down
2 changes: 1 addition & 1 deletion src/dotless.Core/EngineFactory.cs
Expand Up @@ -10,7 +10,7 @@ public EngineFactory(DotlessConfiguration configuration)
{
Configuration = configuration;
}
public EngineFactory() : this(DotlessConfiguration.Default)
public EngineFactory() : this(DotlessConfiguration.GetDefault())
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/dotless.Core/HandlerImpl.cs
Expand Up @@ -24,7 +24,7 @@ public void Execute()
var localPath = Http.Context.Request.Url.LocalPath;

var source = FileReader.GetFileContents(localPath);

Response.WriteCss(Engine.TransformToCss(source, localPath));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotless.Core/Less.cs
Expand Up @@ -6,7 +6,7 @@ public static class Less
{
public static string Parse(string less)
{
return Parse(less, DotlessConfiguration.Default);
return Parse(less, DotlessConfiguration.GetDefault());
}

public static string Parse(string less, DotlessConfiguration config)
Expand Down
16 changes: 14 additions & 2 deletions src/dotless.Core/Plugins/GenericPluginConfigurator.cs
Expand Up @@ -40,7 +40,7 @@ public void SetParameterValues(IEnumerable<IPluginParameter> pluginParameters)
ConstructorInfo parameterConstructor;
GetConstructorInfos(out parameterConstructor, out defaultConstructor);

if (pluginParameters == null || pluginParameters.Count() == 0 || pluginParameters.Any(parameter => parameter.Value == null))
if (pluginParameters == null || pluginParameters.Count() == 0 || pluginParameters.All(parameter => parameter.Value == null))
{
if (defaultConstructor == null)
{
Expand All @@ -52,7 +52,19 @@ public void SetParameterValues(IEnumerable<IPluginParameter> pluginParameters)
{
var constructorArguments = parameterConstructor.GetParameters()
.OrderBy(parameter => parameter.Position)
.Select(parameter => pluginParameters.First(pluginParameter => pluginParameter.Name == parameter.Name).Value)
.Select(parameter =>
{
var p = pluginParameters.FirstOrDefault(pluginParameter => pluginParameter.Name == parameter.Name);
if (p == null)
{
if (parameter.ParameterType.IsValueType)
{
return Activator.CreateInstance(parameter.ParameterType);
}
return null;
}
return p.Value;
})
.ToArray();

_pluginCreator = () => (T)parameterConstructor.Invoke(constructorArguments);
Expand Down
13 changes: 3 additions & 10 deletions src/dotless.Core/Plugins/PluginFinder.cs
Expand Up @@ -61,18 +61,11 @@ public static string GetName(Type pluginType)
/// a plugins folder underneath the executing assembly
/// </summary>
/// <param name="scanPluginsFolder">Look for a plugins folder and if exists, load plugins from it</param>
/// <param name="scanLoadedDlls">Whether to look at referenced dll's and scan them for plugins</param>
/// <returns></returns>
public static IEnumerable<IPluginConfigurator> GetConfigurators(bool scanPluginsFolder, bool scanLoadedDlls)
public static IEnumerable<IPluginConfigurator> GetConfigurators(bool scanPluginsFolder)
{
List<IEnumerable<IPluginConfigurator>> pluginConfigurators = new List<IEnumerable<IPluginConfigurator>>();

if (scanLoadedDlls)
{
pluginConfigurators.AddRange(Assembly.GetEntryAssembly().GetReferencedAssemblies()
.Select(assembly => GetConfigurators(Assembly.Load(assembly))));
}

pluginConfigurators.Add(GetConfigurators(Assembly.GetAssembly(typeof(PluginFinder))));

if (scanPluginsFolder)
Expand Down Expand Up @@ -108,12 +101,12 @@ public static IEnumerable<IPluginConfigurator> GetConfigurators(Assembly assembl

IEnumerable<Type> pluginsConfigurated = pluginConfigurators.Select(pluginConfigurator => pluginConfigurator.Configurates);

Type genericPluginConfiguratorType = typeof(GenericPluginConfigurator<IPlugin>).GetGenericTypeDefinition();
Type genericPluginConfiguratorType = typeof(GenericPluginConfigurator<>);

IEnumerable<IPluginConfigurator> plugins = types
.Where(type => typeof(IPlugin).IsAssignableFrom(type))
.Where(type => !pluginsConfigurated.Contains(type))
.Select(type => (IPluginConfigurator)genericPluginConfiguratorType.MakeGenericType(type).GetConstructor(new Type[]{}).Invoke(new object[]{}));
.Select(type => (IPluginConfigurator)Activator.CreateInstance(genericPluginConfiguratorType.MakeGenericType(type)));

return plugins.Union(pluginConfigurators);
}
Expand Down
15 changes: 11 additions & 4 deletions src/dotless.Core/configuration/DotlessConfiguration.cs
Expand Up @@ -3,16 +3,23 @@ namespace dotless.Core.configuration
using System;
using Input;
using Loggers;
using dotless.Core.Plugins;
using System.Collections.Generic;
using dotless.Core.Plugins;
using System.Collections.Generic;

public class DotlessConfiguration
{
public static readonly DotlessConfiguration Default = new DotlessConfiguration();
public static readonly DotlessConfiguration DefaultWeb = new DotlessConfiguration
public static DotlessConfiguration GetDefault()
{
return new DotlessConfiguration();
}

public static DotlessConfiguration GetDefaultWeb()
{
return new DotlessConfiguration
{
Web = true
};
}

public DotlessConfiguration()
{
Expand Down
Expand Up @@ -8,7 +8,7 @@ public class DotlessConfigurationSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
var configuration = DotlessConfiguration.Default;
var configuration = DotlessConfiguration.GetDefault();

try
{
Expand Down
Expand Up @@ -9,7 +9,7 @@ public DotlessConfiguration GetConfiguration()
var webconfig = (DotlessConfiguration)ConfigurationManager.GetSection("dotless");

if (webconfig == null)
return DotlessConfiguration.DefaultWeb;
return DotlessConfiguration.GetDefaultWeb();

webconfig.Web = true;

Expand Down
65 changes: 64 additions & 1 deletion src/dotless.Core/configuration/XmlConfigurationInterpreter.cs
Expand Up @@ -3,12 +3,16 @@ namespace dotless.Core.configuration
using System;
using System.Xml;
using Loggers;
using dotless.Core.Plugins;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public class XmlConfigurationInterpreter
{
public DotlessConfiguration Process(XmlNode section)
{
var dotlessConfiguration = DotlessConfiguration.DefaultWeb;
var dotlessConfiguration = DotlessConfiguration.GetDefaultWeb();

dotlessConfiguration.MinifyOutput = GetBoolValue(section, "minifyCss") ?? dotlessConfiguration.MinifyOutput;
dotlessConfiguration.CacheEnabled = GetBoolValue(section, "cache") ?? dotlessConfiguration.CacheEnabled;
Expand Down Expand Up @@ -40,6 +44,7 @@ public DotlessConfiguration Process(XmlNode section)
dotlessConfiguration.LessSource = source;

dotlessConfiguration.Logger = GetTypeValue(section, "logger");
dotlessConfiguration.Plugins.AddRange(GetPlugins(section));

return dotlessConfiguration;
}
Expand Down Expand Up @@ -96,5 +101,63 @@ private static Type GetTypeValue(XmlNode section, string property)

return null;
}

private static IEnumerable<IPluginConfigurator> GetPlugins(XmlNode section)
{
List<IPluginConfigurator> plugins = new List<IPluginConfigurator>();
IEnumerable<IPluginConfigurator> dotlessPlugins = null; //lazy initiate incase of no plugins used
List<string> assemblies = new List<string>();

foreach (XmlNode node in section.SelectNodes("plugin"))
{
if (dotlessPlugins == null)
{
dotlessPlugins = PluginFinder.GetConfigurators(false);
}

string assembly = GetStringValue(node, "assembly");

if (assembly != null)
{
if (!assemblies.Contains(assembly)) {
dotlessPlugins = dotlessPlugins.Union(PluginFinder.GetConfigurators(Assembly.Load(assembly)));
assemblies.Add(assembly);
}
}

string name = GetStringValue(node, "name");
var plugin = dotlessPlugins.Where(p => p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
.FirstOrDefault();

if (plugin == null)
{
throw new Exception(
string.Format("Cannot find plugin called {0}. If it is an external plugin, make sure the assembly is referenced.", name));
}
var pluginParameters = plugin.GetParameters();

foreach (XmlNode pluginParameter in node.SelectNodes("pluginParameter"))
{
var pluginParameterName = GetStringValue(pluginParameter, "name");
var pluginParameterValue = GetStringValue(pluginParameter, "value");

var actualParameter = pluginParameters
.Where(p => p.Name.Equals(pluginParameterName, StringComparison.InvariantCultureIgnoreCase))
.FirstOrDefault();

if (actualParameter == null)
{
throw new Exception(
string.Format("Cannot find plugin argument {0} in plugin {1}", pluginParameterName, name));
}

actualParameter.SetValue(pluginParameterValue);
}

plugin.SetParameterValues(pluginParameters);
plugins.Add(plugin);
}
return plugins;
}
}
}
7 changes: 6 additions & 1 deletion src/dotless.SampleWeb/Content/Web.config
Expand Up @@ -4,7 +4,12 @@
<section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler,dotless.Core" />
</configSections>

<dotless minifyCss="true" cache="false" />
<dotless minifyCss="true" cache="false">
<plugin name="Rtl">
<pluginParameter name="onlyReversePrefixedRules" value="false" />
<pluginParameter name="forceRtlTransform" value="false" />
</plugin>
</dotless>

<system.web>
<httpHandlers>
Expand Down

0 comments on commit 22f8fb0

Please sign in to comment.