Should fix use in production environment,
since it make from Yavsc and Yavsc.Abstract some nuget references.
This commit is contained in:
@ -25,146 +25,147 @@ using Microsoft.AspNet.Identity.EntityFramework;
|
||||
namespace cli.Services
|
||||
{
|
||||
|
||||
public class EMailer
|
||||
{
|
||||
RazorTemplateEngine razorEngine;
|
||||
IStringLocalizer<EMailer> stringLocalizer;
|
||||
ILogger logger;
|
||||
ApplicationDbContext dbContext;
|
||||
|
||||
const string DefaultBaseClassName = "ATemplate";
|
||||
const string DefaultBaseClass = nameof(UserOrientedTemplate);
|
||||
const string DefaultNamespace = "CompiledRazorTemplates";
|
||||
|
||||
RazorEngineHost host;
|
||||
public EMailer(ApplicationDbContext context, IStringLocalizer<EMailer> localizer, ILoggerFactory loggerFactory)
|
||||
public class EMailer
|
||||
{
|
||||
stringLocalizer = localizer;
|
||||
|
||||
logger = loggerFactory.CreateLogger<EMailer>();
|
||||
RazorTemplateEngine razorEngine;
|
||||
IStringLocalizer<EMailer> stringLocalizer;
|
||||
ILogger logger;
|
||||
ApplicationDbContext dbContext;
|
||||
|
||||
var language = new CSharpRazorCodeLanguage();
|
||||
|
||||
host = new RazorEngineHost(language) {
|
||||
DefaultBaseClass = DefaultBaseClass,
|
||||
DefaultClassName = DefaultBaseClassName,
|
||||
DefaultNamespace = DefaultNamespace
|
||||
};
|
||||
const string DefaultBaseClassName = "ATemplate";
|
||||
const string DefaultBaseClass = nameof(UserOrientedTemplate);
|
||||
const string DefaultNamespace = "CompiledRazorTemplates";
|
||||
|
||||
RazorEngineHost host;
|
||||
public EMailer(ApplicationDbContext context, IStringLocalizer<EMailer> localizer, ILoggerFactory loggerFactory)
|
||||
{
|
||||
stringLocalizer = localizer;
|
||||
|
||||
// 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;
|
||||
logger = loggerFactory.CreateLogger<EMailer>();
|
||||
|
||||
this.razorEngine = new RazorTemplateEngine(host);
|
||||
|
||||
|
||||
}
|
||||
public void AllUserGen(long templateCode, string baseclassName = DefaultBaseClassName)
|
||||
{
|
||||
string className = "Generated"+baseclassName;
|
||||
var language = new CSharpRazorCodeLanguage();
|
||||
|
||||
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[]
|
||||
host = new RazorEngineHost(language)
|
||||
{
|
||||
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() ;
|
||||
DefaultBaseClass = DefaultBaseClass,
|
||||
DefaultClassName = DefaultBaseClassName,
|
||||
DefaultNamespace = DefaultNamespace
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
// 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;
|
||||
|
||||
this.razorEngine = new RazorTemplateEngine(host);
|
||||
}
|
||||
|
||||
foreach (var mref in references) logger.LogInformation($"ctor used ref to {mref.Display}[{mref.Properties.Kind}]");
|
||||
public string GenerateTemplateObject(string baseclassName = DefaultBaseClassName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void AllUserGen(long templateCode, string baseclassName = DefaultBaseClassName)
|
||||
{
|
||||
string className = "Generated" + baseclassName;
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
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} ");
|
||||
|
||||
using (StringReader reader = new StringReader(templateInfo.Body))
|
||||
{
|
||||
EmitResult result = compilation.Emit(ms);
|
||||
foreach (Diagnostic diagnostic in result.Diagnostics.Where(diagnostic =>
|
||||
diagnostic.Severity < DiagnosticSeverity.Error && !diagnostic.IsWarningAsError))
|
||||
|
||||
// Generate code for the template
|
||||
var razorResult = razorEngine.GenerateCode(reader, className, DefaultNamespace, "fakeFileName.cs");
|
||||
|
||||
logger.LogInformation("Razor exited " + (razorResult.Success ? "Ok" : "Ko") + ".");
|
||||
|
||||
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( typeof(ApplicationUser).Assembly.Location),
|
||||
MetadataReference.CreateFromFile( typeof(Template).Assembly.Location)
|
||||
};
|
||||
|
||||
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);
|
||||
foreach (Diagnostic diagnostic in result.Diagnostics.Where(diagnostic =>
|
||||
diagnostic.Severity < DiagnosticSeverity.Error && !diagnostic.IsWarningAsError))
|
||||
{
|
||||
logger.LogWarning("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
|
||||
logger.LogWarning("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
|
||||
}
|
||||
if (!result.Success)
|
||||
{
|
||||
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
|
||||
diagnostic.IsWarningAsError ||
|
||||
diagnostic.Severity == DiagnosticSeverity.Error);
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
if (!result.Success)
|
||||
{
|
||||
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
|
||||
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
|
||||
logger.LogInformation(razorResult.GeneratedCode);
|
||||
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());
|
||||
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
Assembly assembly = Assembly.Load(ms.ToArray());
|
||||
|
||||
Type type = assembly.GetType(DefaultNamespace + "." + className);
|
||||
var generatedtemplate = (UserOrientedTemplate)Activator.CreateInstance(type);
|
||||
foreach (var user in dbContext.ApplicationUser)
|
||||
{
|
||||
logger.LogInformation("Generation for " + user.UserName);
|
||||
generatedtemplate.Init();
|
||||
generatedtemplate.User = user;
|
||||
|
||||
logger.LogInformation(generatedtemplate.GeneratedText);
|
||||
}
|
||||
|
||||
/* ... type.InvokeMember("Write",
|
||||
BindingFlags.Default | BindingFlags.InvokeMethod,
|
||||
null,
|
||||
model,
|
||||
new object[] { "Hello World" }); */
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
Assembly assembly = Assembly.Load(ms.ToArray());
|
||||
|
||||
Type type = assembly.GetType(DefaultNamespace+"."+className);
|
||||
var generatedtemplate = (UserOrientedTemplate) Activator.CreateInstance(type);
|
||||
foreach (var user in dbContext.ApplicationUser) {
|
||||
logger.LogInformation ("Generation for "+user.UserName);
|
||||
generatedtemplate.Init();
|
||||
generatedtemplate.User = user;
|
||||
|
||||
logger.LogInformation (generatedtemplate.GeneratedText);
|
||||
}
|
||||
|
||||
/* ... type.InvokeMember("Write",
|
||||
BindingFlags.Default | BindingFlags.InvokeMethod,
|
||||
null,
|
||||
model,
|
||||
new object[] { "Hello World" }); */
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user