wip!
This commit is contained in:
14
cli/Makefile
Normal file
14
cli/Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
|
||||
all: build
|
||||
|
||||
build: project.lock.json
|
||||
dnu build
|
||||
|
||||
project.lock.json: project.json
|
||||
|
||||
bin/output/approot/run: project.lock.json
|
||||
dnu restore
|
||||
|
||||
run: bin/output/approot/run
|
||||
ASPNET_ENV=Development dnx run
|
@ -2,10 +2,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yavsc;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Server.Helpers;
|
||||
using Yavsc.Services;
|
||||
using Microsoft.Extensions.OptionsModel;
|
||||
|
||||
using System.Globalization;
|
||||
@ -25,6 +21,11 @@ using Microsoft.AspNet.Razor;
|
||||
using Microsoft.Extensions.DependencyInjection.Abstractions;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using cli.Services;
|
||||
using Yavsc;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Server.Helpers;
|
||||
using Yavsc.Services;
|
||||
using Yavsc.Templates;
|
||||
|
||||
namespace cli
|
||||
{
|
||||
@ -32,9 +33,11 @@ namespace cli
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
var host = new WebHostBuilder();
|
||||
|
||||
var hostengnine = host.UseEnvironment("Develpment")
|
||||
var hostengnine = host
|
||||
.UseEnvironment("Development")
|
||||
.UseServer("cli")
|
||||
.UseStartup<Startup>()
|
||||
|
||||
@ -45,8 +48,8 @@ namespace cli
|
||||
.Build();
|
||||
|
||||
var app = hostengnine.Start();
|
||||
app.Services.GetService<EMailer>();
|
||||
|
||||
var mailer = app.Services.GetService<EMailer>();
|
||||
mailer.AllUserGen(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -78,12 +81,13 @@ namespace cli
|
||||
// Set up configuration sources.
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
|
||||
builder.AddEnvironmentVariables();
|
||||
Configuration = builder.Build();
|
||||
|
||||
}
|
||||
|
||||
public void ConfigureServices (IServiceCollection services)
|
||||
{
|
||||
services.AddOptions();
|
||||
@ -116,7 +120,9 @@ namespace cli
|
||||
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||
loggerFactory.AddDebug();
|
||||
logger = loggerFactory.CreateLogger<Startup>();
|
||||
logger.LogInformation("Hello World from Configure ...");
|
||||
logger.LogInformation(env.EnvironmentName);
|
||||
var cxstr = Configuration["Data:DefaultConnection:ConnectionString"];
|
||||
DbHelpers.ConnectionString = cxstr;
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,35 +1,165 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.CodeDom;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.Emit;
|
||||
|
||||
using Microsoft.AspNet.Razor;
|
||||
using Microsoft.AspNet.Razor.Generator;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.CSharp;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Identity;
|
||||
using System.Reflection;
|
||||
using Yavsc.Templates;
|
||||
using Yavsc.Abstract.Templates;
|
||||
using Microsoft.AspNet.Identity.EntityFramework;
|
||||
|
||||
namespace cli.Services
|
||||
{
|
||||
public class EMailer
|
||||
|
||||
public class EMailer
|
||||
{
|
||||
RazorTemplateEngine razorEngine;
|
||||
IStringLocalizer<EMailer> stringLocalizer;
|
||||
ILogger logger;
|
||||
ApplicationDbContext dbContext;
|
||||
|
||||
const string DefaultClassName = "ATemplate";
|
||||
const string DefaultBaseClass = nameof(UserOrientedTemplate);
|
||||
const string DefaultNamespace = "CompiledRazorTemplates";
|
||||
|
||||
RazorEngineHost host;
|
||||
public EMailer(ApplicationDbContext context, IStringLocalizer<EMailer> localizer, ILoggerFactory loggerFactory)
|
||||
{
|
||||
RazorTemplateEngine razorEngine;
|
||||
IStringLocalizer<EMailer> stringLocalizer;
|
||||
ILogger logger;
|
||||
ApplicationDbContext dbContext;
|
||||
stringLocalizer = localizer;
|
||||
|
||||
logger = loggerFactory.CreateLogger<EMailer>();
|
||||
|
||||
var language = new CSharpRazorCodeLanguage();
|
||||
|
||||
host = new RazorEngineHost(language) {
|
||||
DefaultBaseClass = DefaultBaseClass,
|
||||
DefaultClassName = DefaultClassName,
|
||||
DefaultNamespace = DefaultNamespace
|
||||
};
|
||||
|
||||
|
||||
public EMailer(ApplicationDbContext context, RazorTemplateEngine razorTemplateEngine, IStringLocalizer<EMailer> localizer, ILoggerFactory loggerFactory)
|
||||
{
|
||||
logger = loggerFactory.CreateLogger<EMailer>();
|
||||
// Everyone needs the System namespace, right?
|
||||
host.NamespaceImports.Add("System");
|
||||
host.NamespaceImports.Add("Yavsc.Templates");
|
||||
host.NamespaceImports.Add("Yavsc.Models");
|
||||
host.NamespaceImports.Add("Yavsc.Models.Identity");
|
||||
host.NamespaceImports.Add("Microsoft.AspNet.Identity.EntityFramework");
|
||||
host.InstrumentedSourceFilePath = "bin/output/approot/src/";
|
||||
host.StaticHelpers=true;
|
||||
dbContext = context;
|
||||
|
||||
razorEngine = razorTemplateEngine;
|
||||
|
||||
stringLocalizer = localizer;
|
||||
|
||||
dbContext = context;
|
||||
}
|
||||
|
||||
public string Gen(long templateCode)
|
||||
{
|
||||
string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value;
|
||||
logger.LogInformation ( $"Using code: {templateCode} and subject: {subtemp} " );
|
||||
throw new NotImplementedException("razorEngine.GenerateCode");
|
||||
}
|
||||
this.razorEngine = new RazorTemplateEngine(host);
|
||||
|
||||
|
||||
}
|
||||
public void AllUserGen(long templateCode)
|
||||
{
|
||||
string className = DefaultClassName;
|
||||
string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value;
|
||||
|
||||
logger.LogInformation($"Generating {subtemp}[{className}]");
|
||||
var templateInfo = dbContext.MailingTemplate.FirstOrDefault (t => t.Id == templateCode);
|
||||
|
||||
logger.LogInformation ( $"Using code: {templateCode} and subject: {subtemp} " );
|
||||
|
||||
logger.LogInformation (templateInfo.Body);
|
||||
|
||||
using (StringReader reader = new StringReader(templateInfo.Body)) {
|
||||
|
||||
// Generate code for the template
|
||||
var razorResult =
|
||||
razorEngine.GenerateCode(reader,className,DefaultNamespace,"fakeFileName.cs");
|
||||
|
||||
logger.LogInformation("Razor exited "+(razorResult.Success?"Ok":"Ko")+".");
|
||||
logger.LogInformation(razorResult.GeneratedCode);
|
||||
|
||||
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(razorResult.GeneratedCode);
|
||||
|
||||
|
||||
string assemblyName = Path.GetRandomFileName();
|
||||
MetadataReference[] references = new MetadataReference[]
|
||||
{
|
||||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(IdentityUser).Assembly.Location),
|
||||
MetadataReference.CreateFromFile("bin/Debug/dnx451/cli.dll") ,
|
||||
MetadataReference.CreateFromFile( "../Yavsc/bin/Debug/dnx451/Yavsc.dll" ),
|
||||
MetadataReference.CreateFromFile( "../Yavsc.Abstract/bin/Debug/dnx451/Yavsc.Abstract.dll" )
|
||||
};
|
||||
//Microsoft.CodeAnalysis.SourceReferenceResolver resolver = new CliSourceReferenceResolver() ;
|
||||
|
||||
|
||||
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
// .WithModuleName("Yavsc.Absctract").WithModuleName("Yavsc")
|
||||
.WithAllowUnsafe(true).WithOptimizationLevel(OptimizationLevel.Release)
|
||||
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu);
|
||||
|
||||
CSharpCompilation compilation = CSharpCompilation.Create(
|
||||
assemblyName,
|
||||
syntaxTrees: new[] { syntaxTree },
|
||||
references: references,
|
||||
options: compilationOptions);
|
||||
|
||||
|
||||
foreach (var mref in references) logger.LogInformation($"ctor used ref to {mref.Display}[{mref.Properties.Kind}]");
|
||||
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
EmitResult result = compilation.Emit(ms);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
|
||||
diagnostic.IsWarningAsError ||
|
||||
diagnostic.Severity == DiagnosticSeverity.Error);
|
||||
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
{
|
||||
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
Assembly assembly = Assembly.Load(ms.ToArray());
|
||||
|
||||
Type type = assembly.GetType(className);
|
||||
var generatedtemplate = (UserOrientedTemplate) Activator.CreateInstance(type);
|
||||
logger.LogInformation(generatedtemplate.ToString());
|
||||
foreach (var user in dbContext.ApplicationUser) {
|
||||
logger.LogInformation(user.ToString());
|
||||
|
||||
generatedtemplate.User = user;
|
||||
generatedtemplate.ExecuteAsync();
|
||||
logger.LogInformation (generatedtemplate.GeneratedText);
|
||||
}
|
||||
|
||||
/* ... type.InvokeMember("Write",
|
||||
BindingFlags.Default | BindingFlags.InvokeMethod,
|
||||
null,
|
||||
model,
|
||||
new object[] { "Hello World" }); */
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,17 +19,31 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Hosting": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
|
||||
"Microsoft.Framework.Configuration.Json": "1.0.0-beta8",
|
||||
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
|
||||
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Logging.TraceSource": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Globalization.CultureInfoCache": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Localization": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Localization.Abstractions": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.WebEncoders.Core": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Options": "0.0.1-alpha",
|
||||
"Microsoft.Extensions.WebEncoders": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.CodeGeneration": "1.0.0-rc1-final",
|
||||
"Microsoft.AspNet.Mvc": "6.0.0-rc1-*",
|
||||
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc1-final",
|
||||
"Microsoft.Framework.Configuration.Json": "1.0.0-*",
|
||||
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
|
||||
"Yavsc": { "type": "build", "target": "project" },
|
||||
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-*",
|
||||
"Microsoft.AspNet.Identity": "3.0.0-rc1-*",
|
||||
"Yavsc": "1.0.5-rc11",
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",
|
||||
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
|
||||
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
|
||||
"Microsoft.CodeAnalysis": "1.0.0-rc1"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": {}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user