Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
* Redesign CompilationResult so that it does not throw when CompiledT…
Browse files Browse the repository at this point in the history
…ype is

accessed.
* Update to use ICompilationException interface from
  Microsoft.Framework.Runtime
* Update to use RoslynCompilationException

Fixes #955
  • Loading branch information
pranavkm committed Feb 20, 2015
1 parent 6b3119e commit d6e47d4
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

using System;
using System.Collections.Generic;
using Microsoft.AspNet.Diagnostics;
using System.Linq;
using Microsoft.Framework.Runtime;

namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
/// An exception thrown when accessing the result of a failed compilation.
/// An <see cref="Exception"/> thrown when accessing the result of a failed compilation.
/// </summary>
public class CompilationFailedException : Exception, ICompilationException
{
Expand All @@ -19,12 +20,19 @@ public class CompilationFailedException : Exception, ICompilationException
/// details of the compilation failure.</param>
public CompilationFailedException(
[NotNull] ICompilationFailure compilationFailure)
: base(Resources.FormatCompilationFailed(compilationFailure.SourceFilePath))
: base(FormatMessage(compilationFailure))
{
CompilationFailures = new[] { compilationFailure };
}

/// <inheritdoc />
public IEnumerable<ICompilationFailure> CompilationFailures { get; }

private static string FormatMessage(ICompilationFailure compilationFailure)
{
return Resources.FormatCompilationFailed(compilationFailure.SourceFilePath) +
Environment.NewLine +
string.Join(Environment.NewLine, compilationFailure.Messages.Select(message => message.Message));
}
}
}
52 changes: 0 additions & 52 deletions src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationFailure.cs

This file was deleted.

57 changes: 0 additions & 57 deletions src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationMessage.cs

This file was deleted.

116 changes: 22 additions & 94 deletions src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.FileProviders;
using Microsoft.Framework.Runtime;

namespace Microsoft.AspNet.Mvc.Razor
{
Expand All @@ -13,119 +11,49 @@ namespace Microsoft.AspNet.Mvc.Razor
/// </summary>
public class CompilationResult
{
private Type _type;

/// <summary>
/// Creates a new instance of <see cref="CompilationResult"/>.
/// Creates a new instance of <see cref="CompilationResult"/> that represents a success in compilation.
/// </summary>
protected CompilationResult()
/// <param name="type">The compiled type.</param>
public CompilationResult([NotNull] Type compiledType)
{
CompiledType = compiledType;
}

/// <summary>
/// Gets the path of the Razor file that was compiled.
/// Creates a <see cref="CompilationResult"/> that represents a failure in compilation.
/// </summary>
public string FilePath
/// <param name="compilationFailure">The <see cref="ICompilationFailure"/> produced from parsing or
/// compiling the Razor file.</param>
public CompilationResult([NotNull] ICompilationFailure compilationFailure)
{
get
{
if (File != null)
{
return File.PhysicalPath;
}
return null;
}
CompilationFailure = compilationFailure;
}

/// <summary>
/// Gets a sequence of <see cref="CompilationMessage"/> instances encountered during compilation.
/// Gets the type produced as a result of compilation.
/// </summary>
public IEnumerable<CompilationMessage> Messages { get; private set; }
/// <remarks>This property is null when compilation fails.</remarks>
public Type CompiledType { get; }

/// <summary>
/// Gets (or sets in derived types) the generated C# content that was compiled.
/// Gets the type produced as a result of compilation.
/// </summary>
public string CompiledContent { get; protected set; }
/// <remarks>This property is null when compilation succeeds.</remarks>
public ICompilationFailure CompilationFailure { get; }

/// <summary>
/// Gets (or sets in derived types) the type produced as a result of compilation.
/// Throws an exception if compilation has failed.
/// </summary>
/// <exception cref="CompilationFailedException">An error occured during compilation.</exception>
public Type CompiledType
/// <returns>The current <see cref="CompilationResult"/> instance.</returns>
public CompilationResult EnsureSuccessful()
{
get
{
if (_type == null)
{
throw CreateCompilationFailedException();
}

return _type;
}
protected set
if (CompilationFailure != null)
{
_type = value;
throw new CompilationFailedException(CompilationFailure);
}
}

private IFileInfo File { get; set; }

/// <summary>
/// Creates a <see cref="CompilationResult"/> that represents a failure in compilation.
/// </summary>
/// <param name="fileInfo">The <see cref="IFileInfo"/> for the Razor file that was compiled.</param>
/// <param name="compilationContent">The generated C# content to be compiled.</param>
/// <param name="messages">The sequence of failure messages encountered during compilation.</param>
/// <returns>A CompilationResult instance representing a failure.</returns>
public static CompilationResult Failed([NotNull] IFileInfo file,
[NotNull] string compilationContent,
[NotNull] IEnumerable<CompilationMessage> messages)
{
return new CompilationResult
{
File = file,
CompiledContent = compilationContent,
Messages = messages,
};
}

/// <summary>
/// Creates a <see cref="CompilationResult"/> that represents a success in compilation.
/// </summary>
/// <param name="type">The compiled type.</param>
/// <returns>A CompilationResult instance representing a success.</returns>
public static CompilationResult Successful([NotNull] Type type)
{
return new CompilationResult
{
CompiledType = type
};
}

private CompilationFailedException CreateCompilationFailedException()
{
var fileContent = ReadContent(File);
var compilationFailure = new CompilationFailure(FilePath, fileContent, CompiledContent, Messages);
return new CompilationFailedException(compilationFailure);
}

private static string ReadContent(IFileInfo file)
{
try
{
using (var stream = file.CreateReadStream())
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (Exception)
{
// Don't throw if reading the file fails.
return string.Empty;
}
return this;
}
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.AspNet.Mvc.Razor/Compilation/CompilerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public class CompilerCache : ICompilerCache
cacheEntry.IsValidatedPreCompiled = true;
return new GetOrAddResult
{
CompilationResult = CompilationResult.Successful(cacheEntry.CompiledType),
CompilationResult = new CompilationResult(cacheEntry.CompiledType),
CompilerCacheEntry = cacheEntry
};
}
Expand All @@ -154,7 +154,7 @@ public class CompilerCache : ICompilerCache
cacheEntry.IsValidatedPreCompiled = true;
return new GetOrAddResult
{
CompilationResult = CompilationResult.Successful(cacheEntry.CompiledType),
CompilationResult = new CompilationResult(cacheEntry.CompiledType),
CompilerCacheEntry = cacheEntry
};
}
Expand All @@ -165,7 +165,7 @@ public class CompilerCache : ICompilerCache

return new GetOrAddResult
{
CompilationResult = CompilationResult.Successful(cacheEntry.CompiledType),
CompilationResult = new CompilationResult(cacheEntry.CompiledType),
CompilerCacheEntry = cacheEntry
};
}
Expand All @@ -174,7 +174,7 @@ public class CompilerCache : ICompilerCache
string normalizedPath,
Func<RelativeFileInfo, CompilationResult> compile)
{
var compilationResult = compile(file);
var compilationResult = compile(file).EnsureSuccessful();

// Concurrent addition to MemoryCache with the same key result in safe race.
var cacheEntry = _cache.Set(normalizedPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.FileProviders;

namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
Expand All @@ -13,11 +11,11 @@ public interface ICompilationService
/// <summary>
/// Compiles content and returns the result of compilation.
/// </summary>
/// <param name="fileInfo">The <see cref="IFileInfo"/> for the Razor file that was compiled.</param>
/// <param name="fileInfo">The <see cref="RelativeFileInfo"/> for the Razor file that was compiled.</param>
/// <param name="compilationContent">The generated C# content to be compiled.</param>
/// <returns>
/// A <see cref="CompilationResult"/> representing the result of compilation.
/// </returns>
CompilationResult Compile(IFileInfo fileInfo, string compilationContent);
CompilationResult Compile(RelativeFileInfo fileInfo, string compilationContent);
}
}
Loading

0 comments on commit d6e47d4

Please sign in to comment.