diff --git a/umbraco.MacroEngines.Juno/Razor/CSharpRazorProvider.cs b/umbraco.MacroEngines.Juno/Razor/CSharpRazorProvider.cs deleted file mode 100644 index e9b9f8d4c9..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/CSharpRazorProvider.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace umbraco.MacroEngines.Razor -{ - using System.CodeDom.Compiler; - using System.Web.Razor; - - using Microsoft.CSharp; - - /// - /// Provides a razor provider that supports the C# syntax. - /// - public class CSharpRazorProvider : IRazorProvider - { - #region Methods - /// - /// Creates a code language service. - /// - /// Creates a language service. - public RazorCodeLanguage CreateLanguageService() - { - return new CSharpRazorCodeLanguage(); - } - - /// - /// Creates a . - /// - /// The a code dom provider. - public CodeDomProvider CreateCodeDomProvider() - { - return new CSharpCodeProvider(); - } - #endregion - } -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/IRazorProvider.cs b/umbraco.MacroEngines.Juno/Razor/IRazorProvider.cs deleted file mode 100644 index b66a602322..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/IRazorProvider.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace umbraco.MacroEngines.Razor -{ - using System.CodeDom; - using System.CodeDom.Compiler; - using System.Web.Razor; - - /// - /// Defines a provider used to create associated compiler types. - /// - public interface IRazorProvider - { - #region Methods - /// - /// Creates a code language service. - /// - /// Creates a language service. - RazorCodeLanguage CreateLanguageService(); - - /// - /// Creates a . - /// - /// The a code dom provider. - CodeDomProvider CreateCodeDomProvider(); - #endregion - } -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/ITemplate.cs b/umbraco.MacroEngines.Juno/Razor/ITemplate.cs deleted file mode 100644 index 9a549bab65..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/ITemplate.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System.Dynamic; -namespace umbraco.MacroEngines.Razor -{ - /// - /// A razor template. - /// - public interface ITemplate - { - #region Properties - /// - /// Gets the parsed result of the template. - /// - string Result { get; } - #endregion - - #region Methods - /// - /// Clears the template. - /// - void Clear(); - - /// - /// Executes the template. - /// - void Execute(); - - /// - /// Writes the specified object to the template. - /// - /// - void Write(object @object); - - /// - /// Writes a literal to the template. - /// - /// - void WriteLiteral(string literal); - #endregion - } - - /// - /// A razor template with a model. - /// - /// The model type - public interface ITemplate : ITemplate - { - #region Properties - /// - /// Gets or sets the model. - /// - TModel Model { get; set; } - #endregion - } - - public interface ITemplateDynamic : ITemplate { - dynamic Model { get; set; } - } -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/Razor.cs b/umbraco.MacroEngines.Juno/Razor/Razor.cs deleted file mode 100644 index 54b084b29d..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/Razor.cs +++ /dev/null @@ -1,87 +0,0 @@ -namespace umbraco.MacroEngines.Razor -{ - using System; - using System.Collections.Generic; - - /// - /// Process razor templates. - /// - public static class Razor - { - #region Fields - private static RazorCompiler Compiler; - private static readonly IDictionary Templates; - #endregion - - #region Constructor - /// - /// Statically initialises the type. - /// - static Razor() - { - Compiler = new RazorCompiler(new CSharpRazorProvider()); - Templates = new Dictionary(); - } - #endregion - - #region Methods - /// - /// Gets an for the specified template. - /// - /// The template to parse. - /// The model to use in the template. - /// [Optional] The name of the template. - /// - private static ITemplate GetTemplate(string template, Type modelType, string name = null) - { - if (!string.IsNullOrEmpty(name)) - { - if (Templates.ContainsKey(name)) - return Templates[name]; - } - - var instance = Compiler.CreateTemplate(template, modelType); - - if (!string.IsNullOrEmpty(name)) - { - if (!Templates.ContainsKey(name)) - Templates.Add(name, instance); - } - - return instance; - } - - /// - /// Parses the specified template using the specified model. - /// - /// The model type. - /// The template to parse. - /// The model to use in the template. - /// [Optional] A name for the template used for caching. - /// The parsed template. - public static string Parse(string template, T model, string name = null) - { - var instance = GetTemplate(template, typeof(T), name); - if (instance is ITemplate) - ((ITemplate)instance).Model = model; - else if (instance is ITemplateDynamic) - ((ITemplateDynamic)instance).Model = model; - - instance.Execute(); - return instance.Result; - } - - /// - /// Sets the razor provider used for compiling templates. - /// - /// The razor provider. - public static void SetRazorProvider(IRazorProvider provider) - { - if (provider == null) - throw new ArgumentNullException("provider"); - - Compiler = new RazorCompiler(provider); - } - #endregion - } -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/RazorCompiler.cs b/umbraco.MacroEngines.Juno/Razor/RazorCompiler.cs deleted file mode 100644 index bd139a781e..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/RazorCompiler.cs +++ /dev/null @@ -1,204 +0,0 @@ -using System.CodeDom; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; - -namespace umbraco.MacroEngines.Razor -{ - using System; - using System.CodeDom.Compiler; - using System.IO; - using System.Text; - using System.Text.RegularExpressions; - using System.Web.Razor; - using System.Web.Razor.Parser; - using System.Runtime.CompilerServices; - - /// - /// Compiles razor templates. - /// - internal class RazorCompiler - { - #region Fields - private readonly IRazorProvider provider; - private Type templateBaseType; - #endregion - - #region Constructor - /// - /// Initialises a new instance of . - /// - /// The provider used to compile templates. - public RazorCompiler(IRazorProvider provider) - { - if (provider == null) - throw new ArgumentNullException("provider"); - - this.provider = provider; - } - #endregion - - #region Methods - /// - /// Compiles the template. - /// - /// The class name of the dynamic type. - /// The template to compile. - /// [Optional] The mode type. - private CompilerResults Compile(string className, string template, Type modelType = null) - { - var languageService = provider.CreateLanguageService(); - var codeDom = provider.CreateCodeDomProvider(); - var host = new RazorEngineHost(languageService); - - var generator = languageService.CreateCodeGenerator(className, "Razor.Dynamic", null, host); - var parser = new RazorParser(languageService.CreateCodeParser(), new HtmlMarkupParser()); - - // Umbraco hack for use with DynamicNode - bool anonymousType = modelType.FullName == "umbraco.MacroEngines.DynamicNode" || (modelType.IsClass && modelType.IsSealed && modelType.BaseType == typeof(object) && modelType.Name.StartsWith("<>") && modelType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true) != null); - - //There's no simple way of determining if an object is an anonymous type - this seems like a problem - Type baseType = (modelType == null) - ? typeof(TemplateBase) - : (anonymousType - ? typeof(TemplateBaseDynamic) - : typeof(TemplateBase<>).MakeGenericType(modelType)); - - templateBaseType = baseType; - generator.GeneratedClass.BaseTypes.Add(baseType); - - using (var reader = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(template)))) - { - parser.Parse(reader, generator); - } - - var statement = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Clear"); - generator.GeneratedExecuteMethod.Statements.Insert(0, new CodeExpressionStatement(statement)); - - var builder = new StringBuilder(); - using (var writer = new StringWriter(builder)) - { - codeDom.GenerateCodeFromCompileUnit(generator.GeneratedCode, writer, new CodeGeneratorOptions()); - } - - var parameters = new CompilerParameters(); - AddReferences(parameters); - - parameters.GenerateInMemory = true; - parameters.IncludeDebugInformation = false; - parameters.GenerateExecutable = false; - parameters.CompilerOptions = "/target:library /optimize"; - - var result = codeDom.CompileAssemblyFromSource(parameters, new[] { builder.ToString() }); - return result; - } - - /// - /// Creates a from the specified template string. - /// - /// The template to compile. - /// [Optional] The model type. - /// An instance of . - public ITemplate CreateTemplate(string template, Type modelType = null) - { - string className = Regex.Replace(Guid.NewGuid().ToString("N"), @"[^A-Za-z]*", ""); - - var result = Compile(className, template, modelType); - - if (result.Errors != null && result.Errors.Count > 0) - throw new TemplateException(result.Errors); - - ITemplate instance = (ITemplate)result.CompiledAssembly.CreateInstance("Razor.Dynamic." + className); - - return instance; - } - #endregion - - /// - /// Adds any required references to the compiler parameters. - /// - /// The compiler parameters. - private void AddReferences(CompilerParameters parameters) - { - var list = new List(); - IEnumerable coreRefs = GetCoreReferences(); - foreach (string location in coreRefs) - { - list.Add(location.ToLowerInvariant()); - } - - IEnumerable baseRefs = GetBaseTypeReferencedAssemblies(); - foreach (string location in baseRefs) - { - list.Add(location.ToLowerInvariant()); - } - - foreach (string location in list) - System.Diagnostics.Debug.Print(location); - IEnumerable distinctList = list.Distinct(new AssemblyVersionComparer()); - parameters.ReferencedAssemblies.AddRange(distinctList.ToArray()); - } - - /// - /// Gets the locations of assemblies referenced by a custom base template type. - /// - /// An enumerable of reference assembly locations. - private IEnumerable GetBaseTypeReferencedAssemblies() - { - if (templateBaseType == null) - return new string[0]; - - return templateBaseType.Assembly - .GetReferencedAssemblies() - .Select(n => Assembly.ReflectionOnlyLoad(n.FullName).Location); - } - - - /// - /// Gets the locations of all core referenced assemblies. - /// - /// An enumerable of reference assembly locations. - private static IEnumerable GetCoreReferences() - { - var refs = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic).Select(a => a.Location); - - return refs.Concat(typeof(RazorCompiler) - .Assembly - .GetReferencedAssemblies().Select(n => Assembly.ReflectionOnlyLoad(n.FullName).Location)); - } - - } - - public class AssemblyVersionComparer : IEqualityComparer - { - bool IEqualityComparer.Equals(string x, string y) - { - x = findAssemblyName(x); - y = findAssemblyName(y); - return (x.Contains(y) || y.Contains(x)); - } - - int IEqualityComparer.GetHashCode(string obj) - { - // 1) find the assembly name without version number and path (xxx.yyy.dll) - obj = findAssemblyName(obj); - // 2) send det som hashcode - if (Object.ReferenceEquals(obj, null)) - return 0; - return obj.GetHashCode(); - } - - private string findAssemblyName(string fullAssemblyPath) - { - - Regex r = new Regex(@"\\([^\\]*.dll)"); - Match m = r.Match(fullAssemblyPath); - if (m.Groups.Count > 0) - { - fullAssemblyPath = m.Groups[0].Value; - } - return fullAssemblyPath; - } - } - -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/TemplateBase.cs b/umbraco.MacroEngines.Juno/Razor/TemplateBase.cs deleted file mode 100644 index 80a19f320d..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/TemplateBase.cs +++ /dev/null @@ -1,146 +0,0 @@ -namespace umbraco.MacroEngines.Razor -{ - using System.Text; - using System.IO; - using System.Globalization; - using System.Dynamic; - using System; - using System.Runtime.CompilerServices; - - /// - /// Provides a base implementation of a template. - /// - public abstract class TemplateBase : ITemplate - { - #region Fields - private TextWriter builder = new StringWriter(CultureInfo.InvariantCulture); - - #endregion - - #region Properties - /// - /// Gets the parsed result of the template. - /// - public string Result { get { return builder.ToString(); } } - #endregion - - #region Methods - /// - /// Clears the template. - /// - public void Clear() - { - builder = new StringWriter(CultureInfo.InvariantCulture); - } - - /// - /// Executes the template. - /// - public virtual void Execute() { } - - /// - /// Writes the specified object to the template. - /// - /// - public void Write(object @object) - { - if (@object == null) - return; - - builder.Write(@object); - } - - /// - /// Writes a literal to the template. - /// - /// - public void WriteLiteral(string literal) - { - if (literal == null) - return; - - builder.Write(literal); - } - #endregion - } - - /// - /// Provides a base implementation of a template. - /// - /// The model type. - public abstract class TemplateBase : TemplateBase, ITemplate - { - #region Properties - public TModel Model { get; set; } - - /// - /// Gets or sets the model. - /// - #endregion - - } - - /// - /// Inherits form TemplateBase and provides an anonymous type implementation - /// - public abstract class TemplateBaseDynamic : TemplateBase, ITemplateDynamic - { - - dynamic model; - - /// - /// Gets or sets an anonymous type model - /// - public dynamic Model - { - get - { - return model; - } - set - { - model = new RazorDynamicObject() { Model = value }; - } - } - - - /// - /// Dynamic object that we'll utilize to return anonymous type parameters - /// - class RazorDynamicObject : DynamicObject - { - internal object Model { get; set; } - public override bool TryGetMember(GetMemberBinder binder, out object result) - { - bool isDynamicNode = false; - object tempResult = null; - try - { - if (Model.GetType().FullName == "umbraco.MacroEngines.DynamicNode") - { - isDynamicNode = ((DynamicObject)Model).TryGetMember(binder, out tempResult); - } - - } - catch - { - result = ""; - } - if (!isDynamicNode) - { - tempResult = Model.GetType().InvokeMember(binder.Name, - System.Reflection.BindingFlags.GetProperty | - System.Reflection.BindingFlags.Instance | - System.Reflection.BindingFlags.Public | - System.Reflection.BindingFlags.NonPublic, - null, - Model, - null); - } - result = tempResult; - return true; - } - } - - } -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/TemplateException.cs b/umbraco.MacroEngines.Juno/Razor/TemplateException.cs deleted file mode 100644 index 27a65976d1..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/TemplateException.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace umbraco.MacroEngines.Razor -{ - using System; - using System.CodeDom.Compiler; - using System.Collections.Generic; - using System.Collections.ObjectModel; - using System.Linq; - - /// - /// Defines an exception that occurs during compilation of a template. - /// - public class TemplateException : Exception - { - #region Constructors - /// - /// Initialises a new instance of - /// - /// The collection of compilation errors. - internal TemplateException(CompilerErrorCollection errors) : base("Unable to compile template.") - { - var list = new List(); - foreach (CompilerError error in errors) - { - list.Add(error); - } - Errors = new ReadOnlyCollection(list); - } - #endregion - - #region Properties - /// - /// Gets the collection of compiler errors. - /// - public ReadOnlyCollection Errors { get; private set; } - #endregion - } -} \ No newline at end of file diff --git a/umbraco.MacroEngines.Juno/Razor/VBRazorProvider.cs b/umbraco.MacroEngines.Juno/Razor/VBRazorProvider.cs deleted file mode 100644 index a18c388fed..0000000000 --- a/umbraco.MacroEngines.Juno/Razor/VBRazorProvider.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace umbraco.MacroEngines.Razor -{ - using System.CodeDom.Compiler; - using System.Web.Razor; - - using Microsoft.VisualBasic; - - /// - /// Provides a razor provider that supports the VB syntax. - /// - public class VBRazorProvider : IRazorProvider - { - #region Methods - /// - /// Creates a code language service. - /// - /// Creates a language service. - public RazorCodeLanguage CreateLanguageService() - { - return new VBRazorCodeLanguage(); - } - - /// - /// Creates a . - /// - /// The a code dom provider. - public CodeDomProvider CreateCodeDomProvider() - { - return new VBCodeProvider(); - } - #endregion - } -} \ No newline at end of file