Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorkstromm committed Jul 21, 2020
2 parents cb3b5fe + 538a077 commit 637eeba
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 22 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ branches:
# Build Cache #
#---------------------------------#
cache:
- tools -> setup.cake, tools/packages.config
- tools -> recipe.cake, tools/packages.config
8 changes: 4 additions & 4 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ https://cakebuild.net

[CmdletBinding()]
Param(
[string]$Script = "setup.cake",
[string]$Script = "recipe.cake",
[string]$Target,
[string]$Configuration,
[ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
Expand Down Expand Up @@ -85,7 +85,7 @@ function GetProxyEnabledWebClient
{
$wc = New-Object System.Net.WebClient
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$wc.Proxy = $proxy
return $wc
}
Expand Down Expand Up @@ -157,8 +157,8 @@ if ((Test-Path $PSScriptRoot) -and !(Test-Path $TOOLS_DIR)) {

# Make sure that packages.config exist.
if (!(Test-Path $PACKAGES_CONFIG)) {
Write-Verbose -Message "Downloading packages.config..."
try {
Write-Verbose -Message "Downloading packages.config..."
try {
$wc = GetProxyEnabledWebClient
$wc.DownloadFile("https://cakebuild.net/download/bootstrapper/packages", $PACKAGES_CONFIG) } catch {
Throw "Could not download packages.config."
Expand Down
117 changes: 117 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env bash

##########################################################################
# This is the Cake bootstrapper script for Linux and OS X.
# This file was downloaded from https://github.com/cake-build/resources
# Feel free to change this file to fit your needs.
##########################################################################

# Define directories.
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TOOLS_DIR=$SCRIPT_DIR/tools
ADDINS_DIR=$TOOLS_DIR/Addins
MODULES_DIR=$TOOLS_DIR/Modules
NUGET_EXE=$TOOLS_DIR/nuget.exe
CAKE_EXE=$TOOLS_DIR/Cake/Cake.exe
PACKAGES_CONFIG=$TOOLS_DIR/packages.config
PACKAGES_CONFIG_MD5=$TOOLS_DIR/packages.config.md5sum
ADDINS_PACKAGES_CONFIG=$ADDINS_DIR/packages.config
MODULES_PACKAGES_CONFIG=$MODULES_DIR/packages.config

# Define md5sum or md5 depending on Linux/OSX
MD5_EXE=
if [[ "$(uname -s)" == "Darwin" ]]; then
MD5_EXE="md5 -r"
else
MD5_EXE="md5sum"
fi

# Define default arguments.
SCRIPT="recipe.cake"
CAKE_ARGUMENTS=()

# Parse arguments.
for i in "$@"; do
case $1 in
-s|--script) SCRIPT="$2"; shift ;;
--) shift; CAKE_ARGUMENTS+=("$@"); break ;;
*) CAKE_ARGUMENTS+=("$1") ;;
esac
shift
done

# Make sure the tools folder exist.
if [ ! -d "$TOOLS_DIR" ]; then
mkdir "$TOOLS_DIR"
fi

# Make sure that packages.config exist.
if [ ! -f "$TOOLS_DIR/packages.config" ]; then
echo "Downloading packages.config..."
curl -Lsfo "$TOOLS_DIR/packages.config" https://cakebuild.net/download/bootstrapper/packages
if [ $? -ne 0 ]; then
echo "An error occurred while downloading packages.config."
exit 1
fi
fi

# Download NuGet if it does not exist.
if [ ! -f "$NUGET_EXE" ]; then
echo "Downloading NuGet..."
curl -Lsfo "$NUGET_EXE" https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
if [ $? -ne 0 ]; then
echo "An error occurred while downloading nuget.exe."
exit 1
fi
fi

# Restore tools from NuGet.
pushd "$TOOLS_DIR" >/dev/null
if [ ! -f "$PACKAGES_CONFIG_MD5" ] || [ "$( cat "$PACKAGES_CONFIG_MD5" | sed 's/\r$//' )" != "$( $MD5_EXE "$PACKAGES_CONFIG" | awk '{ print $1 }' )" ]; then
find . -type d ! -name . ! -name 'Cake.Bakery' | xargs rm -rf
fi

mono "$NUGET_EXE" install -ExcludeVersion
if [ $? -ne 0 ]; then
echo "Could not restore NuGet tools."
exit 1
fi

$MD5_EXE "$PACKAGES_CONFIG" | awk '{ print $1 }' >| "$PACKAGES_CONFIG_MD5"

popd >/dev/null

# Restore addins from NuGet.
if [ -f "$ADDINS_PACKAGES_CONFIG" ]; then
pushd "$ADDINS_DIR" >/dev/null

mono "$NUGET_EXE" install -ExcludeVersion
if [ $? -ne 0 ]; then
echo "Could not restore NuGet addins."
exit 1
fi

popd >/dev/null
fi

# Restore modules from NuGet.
if [ -f "$MODULES_PACKAGES_CONFIG" ]; then
pushd "$MODULES_DIR" >/dev/null

mono "$NUGET_EXE" install -ExcludeVersion
if [ $? -ne 0 ]; then
echo "Could not restore NuGet modules."
exit 1
fi

popd >/dev/null
fi

# Make sure that Cake has been installed.
if [ ! -f "$CAKE_EXE" ]; then
echo "Could not find Cake.exe at '$CAKE_EXE'."
exit 1
fi

# Start Cake
exec mono "$CAKE_EXE" $SCRIPT "${CAKE_ARGUMENTS[@]}"
9 changes: 5 additions & 4 deletions setup.cake → recipe.cake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Environment.SetVariableNames();

BuildParameters.SetParameters(context: Context,
BuildParameters.SetParameters(context: Context,
buildSystem: BuildSystem,
sourceDirectoryPath: "./src",
solutionFilePath: "./Gazorator.sln",
Expand All @@ -12,12 +12,13 @@ BuildParameters.SetParameters(context: Context,
appVeyorAccountName: "mholo65",
shouldRunDupFinder: false,
shouldRunInspectCode: false,
shouldRunDotNetCorePack: true);
shouldRunDotNetCorePack: true,
shouldRunGitVersion: true);

BuildParameters.PrintParameters(Context);

ToolSettings.SetToolSettings(context: Context,
dupFinderExcludePattern: new string[] {
dupFinderExcludePattern: new string[] {
BuildParameters.RootDirectoryPath + "/src/Gazorator/**/*.AssemblyInfo.cs"});

Build.RunDotNetCore();
Build.RunDotNetCore();
8 changes: 6 additions & 2 deletions sample/Gazorator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ await Gazorator.Default
typeof(Cake.Issues.Reporting.Generic.DevExtremeTheme).Assembly,
typeof(Cake.Core.IO.FilePath).Assembly
)
.WithViewBag(ViewBag =>
// .WithViewBag(ViewBag =>
// {
// ViewBag.Title = "Foo";
// })
.WithViewBag(new Dictionary<string, object>
{
ViewBag.Title = "Foo";
["Title"] = "FooBar"
})
.ProcessAsync("./Views/CakeIssues.cshtml");

Expand Down
58 changes: 48 additions & 10 deletions src/Gazorator/Gazorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class Gazorator
protected TextWriter Output { get; }
protected IEnumerable<Assembly> References { get; } = new List<Assembly>();
protected Action<dynamic> ConfigureViewBag { get; }
protected IEnumerable<KeyValuePair<string, object>> ViewBag { get; }

protected Gazorator(TextWriter output = null, Action<dynamic> configureViewBag = null, params Assembly[] references)
{
Expand All @@ -23,7 +24,14 @@ protected Gazorator(TextWriter output = null, Action<dynamic> configureViewBag =
References = new List<Assembly>(references);
}

public static Gazorator Default => new DefaultGazorator(TextWriter.Null);
protected Gazorator(TextWriter output = null, IEnumerable<KeyValuePair<string, object>> viewBag = null, params Assembly[] references)
{
Output = output ?? TextWriter.Null;
ViewBag = viewBag;
References = new List<Assembly>(references);
}

public static Gazorator Default => new DefaultGazorator(TextWriter.Null, configureViewBag: null);

public Gazorator<TModel> WithModel<TModel>(TModel model)
{
Expand All @@ -45,12 +53,17 @@ public Gazorator WithViewBag(Action<dynamic> configureViewBag)
return new DefaultGazorator(Output, configureViewBag, References.ToArray());
}

public Gazorator WithViewBag(IEnumerable<KeyValuePair<string, object>> viewBag)
{
return new DefaultGazorator(Output, viewBag, References.ToArray());
}

public virtual Task ProcessAsync(string filePath)
{
var razorGenerator = new CSharpScriptRazorGenerator(Path.GetDirectoryName(filePath));
var csharpScript = razorGenerator.Generate(filePath);

var viewBag = new DynamicViewBag();
var viewBag = new DynamicViewBag(ViewBag);
ConfigureViewBag?.Invoke(viewBag);

var razorContentGenerator = new RazorContentGenerator(Output, References, viewBag);
Expand All @@ -60,29 +73,49 @@ public virtual Task ProcessAsync(string filePath)
public virtual async Task ProcessTemplateAsync(string template)
{
var tempFile = Path.GetTempFileName();
try
{
using (var stream = File.OpenWrite(tempFile))
using (var writer = new StreamWriter(stream))
{
await writer.WriteAsync(template);
}

using (var stream = File.OpenWrite(tempFile))
using (var writer = new StreamWriter(stream))
await ProcessAsync(tempFile);
}
finally
{
await writer.WriteAsync(template);
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}

await ProcessAsync(tempFile);
}

private sealed class DefaultGazorator : Gazorator
{
public DefaultGazorator(TextWriter output = null, Action<dynamic> configureViewBag = null, params Assembly[] references) : base(output, configureViewBag, references)
{
}

public DefaultGazorator(TextWriter output = null, IEnumerable<KeyValuePair<string, object>> viewbag = null, params Assembly[] references) : base(output, viewbag, references)
{
}
}
}

public sealed class Gazorator<TModel> : Gazorator
{
private readonly TModel _model;

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

internal Gazorator(TextWriter output, TModel model, IEnumerable<KeyValuePair<string, object>> viewBag, params Assembly[] references)
: base(output, viewBag, references)
{
_model = model;
}
Expand All @@ -97,17 +130,22 @@ internal Gazorator(TextWriter output, TModel model, Action<dynamic> configureVie
return new Gazorator<TModel>(Output, _model, ConfigureViewBag, references);
}

public Gazorator WithViewBag(Action<dynamic> configureViewBag)
public new Gazorator<TModel> WithViewBag(Action<dynamic> configureViewBag)
{
return new Gazorator<TModel>(Output, _model, configureViewBag, References.ToArray());
}

public new Gazorator<TModel> WithViewBag(IEnumerable<KeyValuePair<string, object>> viewBag)
{
return new Gazorator<TModel>(Output, _model, viewBag, References.ToArray());
}

public override Task ProcessAsync(string filePath)
{
var razorGenerator = new CSharpScriptRazorGenerator(Path.GetDirectoryName(filePath));
var csharpScript = razorGenerator.Generate(filePath);

var viewBag = new DynamicViewBag();
var viewBag = new DynamicViewBag(ViewBag);
ConfigureViewBag?.Invoke(viewBag);

var razorContentGenerator = new RazorContentGenerator<TModel>(_model, Output, References, viewBag);
Expand Down
9 changes: 8 additions & 1 deletion src/Gazorator/Scripting/DynamicViewBag.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;

namespace Gazorator.Scripting
{
public class DynamicViewBag : DynamicObject
{
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
private readonly IDictionary<string, object> _properties;

public DynamicViewBag(IEnumerable<KeyValuePair<string, object>> properties = null)
{
_properties = properties?.ToDictionary(x => x.Key, x => x.Value)
?? new Dictionary<string, object>();
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
Expand Down

0 comments on commit 637eeba

Please sign in to comment.