Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorkstromm committed Sep 24, 2018
2 parents 13d432b + 00a5216 commit 448829c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions sample/Gazorator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using AndroidBinderator;
using System.Linq;
using System.Xml.Linq;

namespace Gazorator.Console
{
Expand All @@ -24,6 +25,7 @@ static void Main(string[] args)
Gazorator.Default
.WithOutput(writer)
.WithModel(new BindingProjectModel())
.WithReferences(typeof(XDocument).Assembly)
.ProcessAsync("./Views/Xamarin.cshtml").Wait();

System.Console.WriteLine(writer.ToString());
Expand Down
1 change: 1 addition & 0 deletions sample/Gazorator.Console/Views/Xamarin.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@inherits Gazorator.Scripting.RazorScriptHost<AndroidBinderator.BindingProjectModel>
@using System.Linq
@using System.Xml.Linq
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
Expand Down
33 changes: 24 additions & 9 deletions src/Gazorator/Gazorator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Gazorator.Razor;
using Gazorator.Scripting;
Expand All @@ -8,35 +11,42 @@ namespace Gazorator
public abstract class Gazorator
{
protected TextWriter Output { get; }
protected IEnumerable<Assembly> References { get; } = new List<Assembly>();

protected Gazorator(TextWriter output = null)
protected Gazorator(TextWriter output = null, params Assembly[] references)
{
Output = output ?? TextWriter.Null;
References = new List<Assembly>(references);
}

public static Gazorator Default => new DefaultGazorator(TextWriter.Null);

public Gazorator<TModel> WithModel<TModel>(TModel model)
{
return new Gazorator<TModel>(Output, model);
return new Gazorator<TModel>(Output, model, References.ToArray());
}

public Gazorator WithOutput(TextWriter output)
{
return new DefaultGazorator(output);
return new DefaultGazorator(output, References.ToArray());
}

public Gazorator WithReferences(params Assembly[] references)
{
return new DefaultGazorator(Output, references);
}

public virtual Task ProcessAsync(string filePath)
{
var razorGenerator = new CSharpScriptRazorGenerator(Path.GetDirectoryName(filePath));
var csharpScript = razorGenerator.Generate(filePath);
var razorContentGenerator = new RazorContentGenerator(Output);
var razorContentGenerator = new RazorContentGenerator(Output, References);
return razorContentGenerator.Generate(csharpScript);
}

private sealed class DefaultGazorator : Gazorator
{
public DefaultGazorator(TextWriter output = null) : base(output)
public DefaultGazorator(TextWriter output = null, params Assembly[] references) : base(output, references)
{
}
}
Expand All @@ -46,21 +56,26 @@ public sealed class Gazorator<TModel> : Gazorator
{
private readonly TModel _model;

internal Gazorator(TextWriter output, TModel model) : base(output)
internal Gazorator(TextWriter output, TModel model, params Assembly[] references) : base(output, references)
{
_model = model;
}

public new Gazorator<TModel> WithOutput(TextWriter output)
{
return new Gazorator<TModel>(output, _model);
return new Gazorator<TModel>(output, _model, References.ToArray());
}

public new Gazorator<TModel> WithReferences(params Assembly[] references)
{
return new Gazorator<TModel>(Output, _model, references);
}

public override Task ProcessAsync(string filePath)
{
var razorGenerator = new CSharpScriptRazorGenerator(Path.GetDirectoryName(filePath));
var csharpScript = razorGenerator.Generate(filePath);
var razorContentGenerator = new RazorContentGenerator<TModel>(_model, Output);
var razorContentGenerator = new RazorContentGenerator<TModel>(_model, Output, References);
return razorContentGenerator.Generate(csharpScript);
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/Gazorator/Scripting/RazorContentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace Gazorator.Scripting
{
internal abstract class RazorContentGeneratorBase
{
private IReadOnlyCollection<Assembly> _references;

protected RazorContentGeneratorBase(IEnumerable<Assembly> references)
{
_references = new List<Assembly>(references);
}

public Task Generate(string csharpScript)
{
var options = ScriptOptions.Default
Expand All @@ -37,13 +44,25 @@ public Task Generate(string csharpScript)

protected virtual IEnumerable<MetadataReference> GetMetadataReferences()
{
yield return MetadataReference.CreateFromFile(typeof(Action).Assembly.Location); // mscorlib or System.Private.Core
yield return MetadataReference.CreateFromFile(typeof(IQueryable).Assembly.Location); // System.Core or System.Linq.Expressions
yield return MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location); // System
yield return MetadataReference.CreateFromFile(typeof(System.Xml.XmlReader).Assembly.Location); // System.Xml
yield return MetadataReference.CreateFromFile(typeof(System.Xml.Linq.XDocument).Assembly.Location); // System.Xml.Linq
yield return MetadataReference.CreateFromFile(typeof(System.Data.DataTable).Assembly.Location); // System.Data

var entryAssembly = Assembly.GetEntryAssembly();
yield return MetadataReference.CreateFromFile(entryAssembly.Location);

foreach (var reference in entryAssembly.GetReferencedAssemblies())
{
yield return MetadataReference.CreateFromFile(Assembly.Load(reference).Location);
}

foreach (var reference in _references)
{
yield return MetadataReference.CreateFromFile(reference.Location);
}
}

protected abstract Type GetGlobalsType();
Expand All @@ -55,7 +74,7 @@ internal sealed class RazorContentGenerator : RazorContentGeneratorBase
{
private readonly TextWriter _textWriter;

public RazorContentGenerator(TextWriter textWriter)
public RazorContentGenerator(TextWriter textWriter, IEnumerable<Assembly> references) : base(references)
{
_textWriter = textWriter ?? throw new ArgumentNullException(nameof(textWriter));
}
Expand All @@ -77,7 +96,7 @@ internal sealed class RazorContentGenerator<TModel> : RazorContentGeneratorBase
private readonly TModel _model;
private readonly bool _isDynamicAssembly;

public RazorContentGenerator(TModel model, TextWriter textWriter)
public RazorContentGenerator(TModel model, TextWriter textWriter, IEnumerable<Assembly> references) : base(references)
{
_textWriter = textWriter ?? throw new ArgumentNullException(nameof(textWriter));
if (typeof(TModel).IsNullable() && model == null)
Expand Down

0 comments on commit 448829c

Please sign in to comment.