Skip to content

Commit

Permalink
ASP.NET Stack Trace Art Viewer added (proof of concept).
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcev committed May 29, 2014
1 parent 9c58768 commit 6abcb18
Show file tree
Hide file tree
Showing 31 changed files with 691 additions and 102 deletions.
6 changes: 6 additions & 0 deletions Source/.nuget/NuGet.Config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
Binary file added Source/.nuget/NuGet.exe
Binary file not shown.
144 changes: 144 additions & 0 deletions Source/.nuget/NuGet.targets
@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>

<!-- Property that enables building a package from a project -->
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

<!-- Download NuGet.exe if it does not already exist -->
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>

<ItemGroup Condition=" '$(PackageSources)' == '' ">
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
<!--
<PackageSource Include="https://www.nuget.org/api/v2/" />
<PackageSource Include="https://my-nuget-source/nuget/" />
-->
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
<!-- Windows specific commands -->
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
</PropertyGroup>

<PropertyGroup>
<PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
<PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
</PropertyGroup>

<PropertyGroup>
<PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
<PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
</PropertyGroup>

<PropertyGroup>
<!-- NuGet command -->
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>

<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>

<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>

<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>

<!-- Commands -->
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>

<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
$(BuildDependsOn);
BuildPackage;
</BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
<!-- Raise an error if we're unable to locate nuget.exe -->
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
<!--
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
This effectively acts as a lock that makes sure that the download operation will only happen once and all
parallel builds will have to wait for it to complete.
-->
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
</Target>

<Target Name="_DownloadNuGet">
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(RestoreCommand)"
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(BuildCommand)"
Condition=" '$(OS)' != 'Windows_NT' " />

<Exec Command="$(BuildCommand)"
LogStandardErrorAsError="true"
Condition=" '$(OS)' == 'Windows_NT' " />
</Target>

<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
OutputFilename = Path.GetFullPath(OutputFilename);
Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
1 change: 1 addition & 0 deletions Source/ProofOfConcept/Core/ProofOfConcept.Core.csproj
Expand Up @@ -40,6 +40,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StackTraceArtClassWrapperHelper.cs" />
<Compile Include="StackTraceArtGenerator.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
45 changes: 45 additions & 0 deletions Source/ProofOfConcept/Core/StackTraceArtClassWrapperHelper.cs
@@ -0,0 +1,45 @@
/*
* WARNING!
* This source file contains Intentionally Bad Code.
* WARNING!
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace StackTraceangelo.ProofOfConcept.Core
{
public static class StackTraceArtClassWrapperHelper // Well, it is static and has Helper in name. It cannot be worse :-)
{
public static Type[] FindStackTraceArtClassWrappersInDirectory(DirectoryInfo directory)
{
IEnumerable<Type> result = Enumerable.Empty<Type>();

IEnumerable<FileInfo> potentialArtLibraries = directory.EnumerateFiles("*.dll");

foreach (FileInfo potentialArtLibrary in potentialArtLibraries)
{
Assembly potentialArtLibraryAssembly = Assembly.LoadFrom(potentialArtLibrary.FullName);
result = result.Concat(potentialArtLibraryAssembly.GetTypes().Where(type => type.IsClass && type.IsPublic && type.IsAbstract && type.IsSealed && HasPaintMethod(type)));
}

return result.ToArray();
}

public static void Paint(Type stackTraceArtClassWrapperType)
{
stackTraceArtClassWrapperType.GetMethod("Paint").Invoke(null, null);
}

private static bool HasPaintMethod(Type type)
{
var paintMethod = type.GetMethod("Paint", BindingFlags.Static | BindingFlags.Public);
if (paintMethod == null) return false;

return paintMethod.GetParameters().Length == 0;
}
}
}
32 changes: 4 additions & 28 deletions Source/ProofOfConcept/DotNet.ArtViewer/Program.cs
@@ -1,22 +1,21 @@
/*B
/*
* WARNING!
* This source file contains Intentionally Bad Code.
* WARNING!
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using StackTraceangelo.ProofOfConcept.Core;

namespace StackTraceangelo.ProofOfConcept.DotNet.ArtViewer
{
class Program
{
static void Main()
{
Type[] stackTraceArtClassWrappers = FindStackTraceArtClassWrappers();
var stackTraceArtClassWrappers = StackTraceArtClassWrapperHelper.FindStackTraceArtClassWrappersInDirectory(new DirectoryInfo(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? string.Empty /* To silent ReSharper.*/));

for (int i = 0; i < stackTraceArtClassWrappers.Length; i++)
Console.WriteLine("{0}. {1}", i + 1, stackTraceArtClassWrappers[i].Name);
Expand All @@ -25,30 +24,7 @@ static void Main()

int artIndex = int.Parse(Console.ReadLine() ?? "1") - 1;

stackTraceArtClassWrappers[artIndex].GetMethod("Paint").Invoke(null, null);
}

private static Type[] FindStackTraceArtClassWrappers()
{
IEnumerable<Type> result = Enumerable.Empty<Type>();

var potentialArtLibraries = Directory.EnumerateFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? string.Empty /* To silent ReSharper.*/, "*.dll");

foreach(string potentialArtLibrary in potentialArtLibraries)
{
Assembly potentialArtLibraryAssembly = Assembly.LoadFrom(potentialArtLibrary);
result = result.Concat(potentialArtLibraryAssembly.GetTypes().Where(type => type.IsClass && type.IsPublic && type.IsAbstract && type.IsSealed && HasPaintMethod(type)));
}

return result.ToArray();
}

private static bool HasPaintMethod(Type type)
{
var paintMethod = type.GetMethod("Paint", BindingFlags.Static | BindingFlags.Public);
if (paintMethod == null) return false;

return paintMethod.GetParameters().Length == 0;
StackTraceArtClassWrapperHelper.Paint(stackTraceArtClassWrappers[artIndex]);
}
}
}
Expand Up @@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StackTraceangelo.ProofOfConcept.DotNet.ArtViewer</RootNamespace>
<AssemblyName>StackTraceangelo.ProofOfConcept.DotNet.ArtViewer</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
Expand All @@ -23,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -32,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -46,6 +49,15 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\ProofOfConcept.Core.csproj">
<Project>{48aa202c-25cb-4928-976b-6866fed205ee}</Project>
<Name>ProofOfConcept.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
3 changes: 3 additions & 0 deletions Source/ProofOfConcept/DotNet.ArtViewer/app.config
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
@@ -0,0 +1,15 @@
using System.Web.Mvc;
using System.Web.Routing;

namespace StackTraceangelo.ProofOfConcept.DotNet.AspDotNetArtViewer
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index" });
}
}
}
@@ -0,0 +1,27 @@
using System.IO;
using System.Linq;
using System.Web.Mvc;
using StackTraceangelo.ProofOfConcept.Core;

namespace StackTraceangelo.ProofOfConcept.DotNet.AspDotNetArtViewer.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var stackTraceArtClassWrappers = StackTraceArtClassWrapperHelper.FindStackTraceArtClassWrappersInDirectory(new DirectoryInfo(Server.MapPath("~/bin")));

return View(stackTraceArtClassWrappers);
}

[HttpPost]
public ActionResult DrawStackTraceArt(string stackTraceArtClassWrapperTypeFullName)
{
var stackTraceArtClassWrapperType = StackTraceArtClassWrapperHelper
.FindStackTraceArtClassWrappersInDirectory(new DirectoryInfo(Server.MapPath("~/bin")))
.Single(type => type.FullName == stackTraceArtClassWrapperTypeFullName);
StackTraceArtClassWrapperHelper.Paint(stackTraceArtClassWrapperType);
return null; // This will never happen because the above method throws the Stack Trace Art exception.
}
}
}
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="StackTraceangelo.ProofOfConcept.DotNet.AspDotNetArtViewer.MvcApplication" Language="C#" %>
14 changes: 14 additions & 0 deletions Source/ProofOfConcept/DotNet.AspDotNetArtViewer/Global.asax.cs
@@ -0,0 +1,14 @@
using System.Web.Mvc;
using System.Web.Routing;

namespace StackTraceangelo.ProofOfConcept.DotNet.AspDotNetArtViewer
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}

0 comments on commit 6abcb18

Please sign in to comment.