Exemple #1
0
        public static ITataTemplate Parse(string rawSource, object model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var content   = GetTemplate(rawSource);
            var modelType = model.GetType().FullName;
            var template  = Resources.Resources.TataTemplate_Base
                            .Replace("@code", content, StringComparison.OrdinalIgnoreCase)
                            .Replace("@modelType", modelType, StringComparison.OrdinalIgnoreCase);
            var refs = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(typeof(ITataTemplate).Assembly.Location)
            }.ToArray();

            var compileResult = UtilCompile.Compile(template, refs);

            if (!compileResult.Success)
            {
                return(null);
            }
            var type     = compileResult.Assembly.GetType("Leftware.Templates.CodeGeneration.TataTemplateCustom");
            var instance = (ITataTemplate)Activator.CreateInstance(type);

            return(instance);
        }
Exemple #2
0
        public override void Execute(IDictionary <string, object> input)
        {
            var pathKey = input.Get <string>(PATH);

            var path         = _collectionProvider.GetValue(CollectionValues.TextTransformSource, pathKey);
            var content      = File.ReadAllText(path);
            var clipboard    = WindowsClipboard.GetText();
            var templateCode = @"
using System;
using Leftware.Common;

public class DynamicTransform : ITransformExecutor
{
    {{ code }}
}
";
            var code         = templateCode.FormatLiquid(new { code = content });
            //var currentAssemblyLocation = GetType().Assembly.Location;
            var compileResult = UtilCompile.Compile(code);

            if (!compileResult.Success)
            {
                _console.WriteLine("Failed to compile dynamic code");
                _console.WriteLine(compileResult.Errors[0]);
                return;
            }

            var assembly = compileResult.Assembly;
            var t        = assembly.GetType("DynamicTransform");
            var instance = (ITransformExecutor)Activator.CreateInstance(t);
            var result   = instance.Execute(clipboard);

            WindowsClipboard.SetText(result);
        }