1. roundcrisis
  2. CodeShape

Commits

roundcrisis  committed 860d998

adding the process aspect of the renderer
not complete

  • Participants
  • Parent commits d95e077
  • Branches default

Comments (0)

Files changed (14)

File libs/graphviz/dot.exe

Binary file added.

File src/CodeShapes/ClassAnalyzer.cs

View file
 
 	public class ClassAnalyzer
 	{
-		public IEnumerable<ICodeShape> Analyze<T>()
+		public IEnumerable<CodeShape> Analyze<T>()
 		{
 			var shapes = FindMethods(typeof(T))
-//				.Concat(FindMethods(typeof(T)))
 				.Concat(FindFields(typeof(T))).ToArray();
 
 			var methods = shapes.Where(s => s is MethodCodeShape).Cast<MethodCodeShape>();
 			return shapes;
 		}
 
-		public AdjacencyGraph<string, SEdge<string>> TransformToGraph(IEnumerable<ICodeShape> shapes)
+		public AdjacencyGraph<string, SEdge<string>> TransformToGraph(IEnumerable<CodeShape> shapes)
 		{
 			throw new NotImplementedException();	
 		}
 	
-		private IDictionary<int, ICodeShape> BuildAdjacencyLists(IList<Instruction> instructions, IEnumerable<ICodeShape> shapes)
+		private IDictionary<int, CodeShape> BuildAdjacencyLists(IList<Instruction> instructions, IEnumerable<CodeShape> shapes)
 		{
-			var result = new Dictionary<int, ICodeShape>();
+			var result = new Dictionary<int, CodeShape>();
 			foreach (var instruction in instructions)
 			{
 				if(instruction.OpCode.OperandType == OperandType.InlineMethod)
 			}
 			return fieldShapes.ToArray();
 		}
-		//todo: properties
-		private IEnumerable<ICodeShape> FindMethods(Type type)
+		// todo: properties
+		private IEnumerable<CodeShape> FindMethods(Type type)
 		{
 			var privateProtectedInternalMethods = type.GetMethods( BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
 			var publicMethods = type.GetMethods();
 
-			List<MethodCodeShape> result = privateProtectedInternalMethods.Select(methodInfo => new MethodCodeShape(methodInfo, methodInfo.Name, Visibility.NonPublic)).ToList();
+			var result = privateProtectedInternalMethods.Select(methodInfo => new MethodCodeShape(methodInfo, methodInfo.Name, Visibility.NonPublic)).ToList();
 			result.AddRange(publicMethods.Select(methodInfo => new MethodCodeShape(methodInfo, methodInfo.Name, Visibility.Public)));
 			return result;
 		}

File src/CodeShapes/CodeShapes.csproj

View file
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
-    <RunCodeAnalysis>true</RunCodeAnalysis>
+    <RunCodeAnalysis>false</RunCodeAnalysis>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
     <Compile Include="Disassembler\Instruction.cs" />
     <Compile Include="Disassembler\MethodBodyReader.cs" />
     <Compile Include="Disassembler\MethodBaseRocks.cs" />
+    <Compile Include="Renderer.cs" />
     <Compile Include="Shapes\CodeShape.cs" />
     <Compile Include="Shapes\FieldCodeShape.cs" />
-    <Compile Include="Shapes\ICodeShape.cs" />
+    <Compile Include="Shapes\Visibility.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Shapes\MethodCodeShape.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <PropertyGroup>
+    <PostBuildEvent>rd  $(TargetDir)graphviz /Q ;
+xcopy $(ProjectDir)..\..\libs\graphviz $(TargetDir)graphviz /S /Y
+</PostBuildEvent>
+  </PropertyGroup>
   <!-- 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.
   <Target Name="BeforeBuild">

File src/CodeShapes/Renderer.cs

View file
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Reflection;
+
+namespace CodeShapes
+{
+
+
+	public class Renderer
+	{
+		static public string AssemblyDirectory
+		{
+			get
+			{
+				string codeBase = typeof(ClassAnalyzer).Assembly.CodeBase;
+				var uri = new UriBuilder(codeBase);
+				string path = Uri.UnescapeDataString(uri.Path);
+				return Path.GetDirectoryName(path);
+			}
+		}
+
+		private readonly string _pathToDotRenderer;
+		
+		private string _dotFilesLocation;
+		
+		private readonly Status _status;
+
+		public Renderer()
+		{
+			_status = new Status();
+			_pathToDotRenderer = Path.Combine(AssemblyDirectory, "graphviz");
+			if(!Directory.Exists(_pathToDotRenderer))
+			{
+				_status = new Status("The dot.exe path was not found. Please check configuration options and set the Renderer path. ");
+			}
+			_dotFilesLocation = DefaultDotFilesLocation;
+		}
+
+		public string DefaultDotFilesLocation
+		{
+			get {
+				return Path.Combine(AssemblyDirectory, "DotFiles");
+			}
+		}
+
+		public string PathToDotRenderer
+		{
+			get { return _pathToDotRenderer; }
+		}
+
+		public Status Status
+		{
+			get { return _status; }
+		}
+
+
+		public void RenderPng(string[] filesToRender)
+		{
+			var graphviz = new Process {EnableRaisingEvents = false, StartInfo = new ProcessStartInfo(PathToDotRenderer, " -Tpng -O " + string.Join(" ", filesToRender))};
+		}
+	}
+
+	public class Status
+	{
+		public Status()
+		{
+			Message = "All Clear";
+			Ready = true;
+		}
+
+		public Status(string errorMessage)
+		{
+			Message = errorMessage;
+			Ready = false;
+		}
+
+		public bool Ready { get; set; }
+
+		public string Message { get; set; }
+	}
+}

File src/CodeShapes/Shapes/CodeShape.cs

View file
 
 namespace CodeShapes.Shapes
 {
-	public abstract class CodeShape : ICodeShape
+	public abstract class CodeShape
 	{
 		protected CodeShape(string methodName, Visibility visibility)
 		{
 			Name = methodName;
 			Visibility = visibility;
-			AdjacencyList = new Dictionary<int, ICodeShape>();
+			AdjacencyList = new Dictionary<int, CodeShape>();
 		}
 		public string Name { get; protected set; }
 
 		public Visibility Visibility { get; protected set; }
 
-		public IDictionary<int, ICodeShape> AdjacencyList { get; private set; }
+		public IDictionary<int, CodeShape> AdjacencyList { get; private set; }
 	}
 
 	public enum ShapeType

File src/CodeShapes/Shapes/ICodeShape.cs

-using System.Collections.Generic;
-
-namespace CodeShapes.Shapes
-{
-	public interface ICodeShape
-	{
-		string Name { get; }
-		Visibility Visibility { get; }
-		IDictionary<int, ICodeShape> AdjacencyList { get;  }
-	}
-
-	public enum Visibility
-	{
-		Public,
-		NonPublic
-	}
-}

File src/CodeShapes/Shapes/NonPublicMethodCodeShape.cs

View file
+using System.Reflection;
+
+namespace CodeShapes.Shapes
+{
+	public class NonPublicMethodCodeShape : MethodCodeShape
+	{
+		public NonPublicMethodCodeShape(MethodBase methodBase, string methodName)
+			: base(methodBase, methodName, Visibility.NonPublic)
+		{
+		}
+	}
+}

File src/CodeShapes/Shapes/Visibility.cs

View file
+using System.Collections.Generic;
+
+namespace CodeShapes.Shapes
+{
+	public enum Visibility
+	{
+		Public,
+		NonPublic
+	}
+}

File src/CodeShapesTests/ClassAnalyzerTests.cs

View file
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Linq;
 using CodeShapes;
 using CodeShapes.Shapes;
-using Xunit;
+using NUnit.Framework;
 
 namespace CodeShapesTests
 {
+	[TestFixture]
 	public class ClassAnalyzerTests
 	{
 		private readonly ClassAnalyzer _classAnalyzer;
-
+		
 		public ClassAnalyzerTests()
 		{
 			_classAnalyzer = new ClassAnalyzer();
 		}
 
-		[Fact]
+		[Test]
 		public void When_class_exists_then_it_doesnt_throw()
 		{
 			Assert.DoesNotThrow(() => _classAnalyzer.Analyze<SampleClass>());
 		}
 
 
-		[Fact]
+		[Test]
 		public void When_there_is_fields_present_then_return()
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 
-			IEnumerable<ICodeShape> fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));
+			IEnumerable<CodeShape> fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));
 
-			Assert.Contains(typeof(SampleClass).GetFields().First().Name, fields.Select(x=>x.Name));
+			Assert.Contains(typeof(SampleClass).GetFields().First().Name, fields.Select(x=>x.Name).ToList());
 		}
 
-		[Fact]
-		public void When_protected_method_then_return()
+		[Test]
+		public void When_protected_method_then_return_non_public()
 		{
-			throw new NotImplementedException();
+			var classShapes = _classAnalyzer.Analyze<SampleClass>();
+
+			var methods = classShapes.Where(x => x.GetType().Equals(typeof(MethodCodeShape)) && x.Name.Contains("Protected"));
+
+			Assert.True(methods.All(x=>x.Visibility == Visibility.NonPublic));
 		}
 
-		[Fact]
-		public void When_internal_method_then_return()
+		[Test]
+		public void When_internal_method_then_return_non_public()
 		{
-			throw new NotImplementedException();
+			var classShapes = _classAnalyzer.Analyze<SampleClass>();
+
+			var methods = classShapes.Where(x => x.GetType().Equals(typeof(MethodCodeShape)) && x.Name.Contains("MyPrivateMethod"));
+
+			Assert.True(methods.All(x => x.Visibility == Visibility.NonPublic));
 		}
 
 
-		[Fact]
+		[Test]
 		public void When_there_is_property_present_then_return()
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClassWithProperty>();
 
 			var fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));
 
-			Assert.Contains(typeof(SampleClassWithProperty).GetFields().First().Name, fields.Select(x => x.Name));
+			Assert.Contains(typeof(SampleClassWithProperty).GetFields().First().Name, fields.Select(x => x.Name).ToList());
 		}
 		
-		[Fact]
+		[Test]
 		public void When_field_is_readonly_then_its_counted_anyway()
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 
 			var fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));
-			Assert.Equal(2, fields.Count());
+			Assert.AreEqual(2, fields.Count());
 			
 		}
 
-		[Fact]
+		[Test]
 		public void When_there_is_a_public_method_on_the_class_Then_finds_it()
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 			Assert.True(collection.Any(x => x.Name == "SampleMethod"));
 		}
 
-		[Fact]
+		[Test]
 		public void When_there_is_a_public_method_present_the_must_return_publicMethodShape()
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(MethodCodeShape))));
 		}
 
-		[Fact]
+		[Test]
 		public void When_there_is_a_private_method_present_the_must_return_privateMethodShape()
 		{
 			var classShapes = _classAnalyzer.Analyze<SampleClass>();
 			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(MethodCodeShape))));
 		}
 
-		[Fact]
+		[Test]
 		public void When_there_is_a_recursive_method_the_adjacency_list_contains_itself_as_a_dependency()
 		{
 			var shapes = _classAnalyzer.Analyze<ClassWithRecursiveMethod>();
 			Assert.NotNull(methodShape);
 
 			Assert.True(methodShape.AdjacencyList.Count == 1);
-			Assert.Same(methodShape, methodShape.AdjacencyList.Values.First());
+			Assert.AreSame(methodShape, methodShape.AdjacencyList.Values.First());
 		}
 
-		[Fact]
+		[Test]
 		public void Can_find_adjacency_from_private_to_public()
 		{
 			var shapes = _classAnalyzer.Analyze<ClassWithBiDirectionalDependency>();
 
-			ICodeShape methodTwoCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodTwo");
+			var methodTwoCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodTwo");
 
-			Assert.NotEmpty(methodTwoCodeShape.AdjacencyList.Values);
+			Assert.IsNotEmpty(methodTwoCodeShape.AdjacencyList.Values.ToArray());
 			var methodTwoAdjacency = methodTwoCodeShape.AdjacencyList.Values;
 
 
-			Assert.Equal("MethodOne", methodTwoAdjacency.First().Name);
+			Assert.AreEqual("MethodOne", methodTwoAdjacency.First().Name);
 		}
 
 
-		[Fact]
+		[Test]
 		public void When_there_is_a_bidirectional_dependency_between_methods_both_methods_adjacency_lists_contain_the_other()
 		{
 			var shapes = _classAnalyzer.Analyze<ClassWithBiDirectionalDependency>();
 
-			ICodeShape methodOneCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodOne");
-			ICodeShape methodTwoCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodTwo");
+			var methodOneCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodOne");
+			var methodTwoCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodTwo");
 
 			var methodOneAdjacency = methodOneCodeShape.AdjacencyList.Values;
 			var methodTwoAdjacency = methodTwoCodeShape.AdjacencyList.Values;
 
-			Assert.Equal(1, methodOneAdjacency.Count);
-			Assert.Same(methodOneAdjacency.First().Name, methodTwoCodeShape.Name);
-			Assert.Same(methodTwoAdjacency.First().Name, methodOneCodeShape.Name);
+			Assert.Equals(1, methodOneAdjacency.Count);
+			Assert.AreSame(methodOneAdjacency.First().Name, methodTwoCodeShape.Name);
+			Assert.AreSame(methodTwoAdjacency.First().Name, methodOneCodeShape.Name);
 		}
 
 		private class ClassWithBiDirectionalDependency

File src/CodeShapesTests/CodeShapeAcceptanceTests.cs

View file
 using System;
 using CodeShapes;
+using NUnit.Framework;
 using StoryQ;
-using Xunit;
 
 namespace CodeShapesTests
 {
+	[TestFixture]
 	public class CodeShapeAcceptanceTests
 	{
 		private Feature _feature;
 			_classAnalyzer = new ClassAnalyzer();
 		}
 
-		[Fact]
+		[Test]
 		public void When_something_then_something()
 		{
 			_feature.WithScenario("submitting a class type returns one graph")
 				.ExecuteWithReport();
 		}
 
-		[Fact]
+		[Test]
 		public void When_something_then_something2()
 		{
 		}

File src/CodeShapesTests/CodeShapesTests.csproj

View file
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
+      <HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll</HintPath>
+    </Reference>
+    <Reference Include="nunit.mocks, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
+      <HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.mocks.dll</HintPath>
+    </Reference>
+    <Reference Include="pnunit.framework">
+      <HintPath>..\packages\NUnit.2.5.10.11092\lib\pnunit.framework.dll</HintPath>
+    </Reference>
     <Reference Include="QuickGraph">
       <HintPath>..\packages\QuickGraph.3.3.51106.0\lib\net35\QuickGraph.dll</HintPath>
     </Reference>
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="System.Data" />
     <Reference Include="System.Xml" />
-    <Reference Include="xunit">
-      <HintPath>..\packages\xunit.1.8.0.1545\lib\xunit.dll</HintPath>
-    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="CodeShapeAcceptanceTests.cs" />
     <Compile Include="InstructionTests.cs" />
     <Compile Include="ClassAnalyzerTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RedererTests.cs" />
     <Compile Include="SampleClass.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\CodeShapes\CodeShapes.csproj">
       <Project>{DC16AEA9-2695-4AEA-B1E9-0B152BD86B4E}</Project>
       <Name>CodeShapes</Name>
+      <Private>True</Private>
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <PropertyGroup>
+    <PostBuildEvent>rd  $(TargetDir)graphviz /Q ;
+xcopy $(ProjectDir)..\..\libs\graphviz $(TargetDir)graphviz /S /Y
+</PostBuildEvent>
+  </PropertyGroup>
   <!-- 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.
   <Target Name="BeforeBuild">

File src/CodeShapesTests/InstructionTests.cs

View file
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using CodeShapes.Shapes;
+using NUnit.Framework;
 using QuickGraph;
 using QuickGraph.Graphviz;
 using QuickGraph.Graphviz.Dot;
-using Xunit;
 
 namespace CodeShapesTests
 {
+	[TestFixture]
 	public class Given_a_collection_Shapes
 	{
-		[Fact]
+		[Test]
 		public void When_transforming_to_graph_It_does_not_throw()
 		{
-			IEnumerable<ICodeShape> shapes = new List<ICodeShape>();
+			IEnumerable<CodeShape> shapes = new List<CodeShape>();
 			var transformToGraph = new TransformToGraph(shapes);
-			Assert.DoesNotThrow(() =>  transformToGraph.Transform());
+			Assert.DoesNotThrow(() => transformToGraph.Transform());
 		}
 
-		[Fact]
+		[Test]
 		public void when_shape_collection_contains_a_method_referencing_a_property_Then_result()
 		{
 			var name = "SampleMethod";
 			var shapes = CreateShapes(name);
 			var transformToGraph = new TransformToGraph(shapes);
-			transformToGraph.Transform();
 
-//			var generate = new GraphvizAlgorithm<ICodeShape, SEdge<ICodeShape>>(adjacencyGraph);
+
+			var generate = new GraphvizAlgorithm<CodeShape, SEdge<CodeShape>>(transformToGraph.Transform());
+
 			
+
 			
 		}
 
-		private IEnumerable<ICodeShape> CreateShapes(string name)
+		private IEnumerable<CodeShape> CreateShapes(string name)
 		{
 			var methodInfo = typeof (SampleClass).GetMethod(name);
 			var fieldCodeShape = new FieldCodeShape("PublicField", Visibility.Public);
 			var publicMethodCodeShape = new MethodCodeShape(methodInfo, name, Visibility.Public);
 			publicMethodCodeShape.AdjacencyList.Add(1, fieldCodeShape);
-			return new List<ICodeShape>
+			return new List<CodeShape>
 			       	{
 			       		publicMethodCodeShape,
 			       		fieldCodeShape
 
 	public class TransformToGraph
 	{
-		private readonly IEnumerable<ICodeShape> _codeShapes;
-		AdjacencyGraph<ICodeShape, SEdge<ICodeShape>> _adjacencyGraph;
+		private readonly IEnumerable<CodeShape> _codeShapes;
+		AdjacencyGraph<CodeShape, SEdge<CodeShape>> _adjacencyGraph;
 
-		public TransformToGraph(IEnumerable<ICodeShape> codeShapes)
+		public TransformToGraph(IEnumerable<CodeShape> codeShapes)
 		{
 			_codeShapes = codeShapes;
 		}
 
+		public AdjacencyGraph<CodeShape, SEdge<CodeShape>> Transform()
+		{
+			ConvertToAdjacency();
+			FormatVertex();
+			return _adjacencyGraph;
+		}
+
 		public void FormatVertex()
 		{
-			var graphviz = new GraphvizAlgorithm<ICodeShape, SEdge<ICodeShape>>(_adjacencyGraph as IEdgeListGraph<ICodeShape, SEdge<ICodeShape>>);
+			var graphviz = new GraphvizAlgorithm<CodeShape, SEdge<CodeShape>>(_adjacencyGraph);
 			graphviz.FormatVertex += FormatS;
 		}
 
-		private void FormatS(object sender, FormatVertexEventArgs<ICodeShape> e)
+		private void FormatS(object sender, FormatVertexEventArgs<CodeShape> e)
 		{
 			var codeShape = e.Vertex;
 			e.VertexFormatter.Label = codeShape.Name;
 			}
 		}
 
-		public void Transform()
+		public void ConvertToAdjacency()
 		{
-			_adjacencyGraph = new AdjacencyGraph<ICodeShape, SEdge<ICodeShape>>();
+			_adjacencyGraph = new AdjacencyGraph<CodeShape, SEdge<CodeShape>>();
 			foreach (var shape in _codeShapes)
 			{
 				_adjacencyGraph.AddVertex(shape);
 			{
 				foreach (var shape in codeShape.AdjacencyList)
 				{
-					_adjacencyGraph.AddEdge(new SEdge<ICodeShape>(codeShape, shape.Value));
+					_adjacencyGraph.AddEdge(new SEdge<CodeShape>(codeShape, shape.Value));
 				}
 			}
 			
 //			_assemblyAnalizer = new AssemblyAnalizer();
 //		}
 
-		[Fact]
+		[Test]
 		public void When_something_then_something()
 		{
 			Assert.True(true);

File src/CodeShapesTests/RedererTests.cs

View file
+using CodeShapes;
+using NUnit.Framework;
+
+namespace CodeShapesTests
+{
+	[TestFixture]
+	public class RedererTests
+	{
+		[Test]
+		public void When_dot_exe_found_Then_renderer_status_is_ready()
+		{
+			var renderer = new Renderer();
+			Assert.True(renderer.Status.Ready);
+		}
+
+		[Test]
+		public void When_something_then_something()
+		{
+		}
+
+	}
+}

File src/CodeShapesTests/packages.config

View file
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
-  <package id="xunit" version="1.8.0.1545" />
   <package id="StoryQ" version="2.0.5" />
   <package id="QuickGraph" version="3.3.51106.0" />
+  <package id="NUnit" version="2.5.10.11092" />
 </packages>