Permalink
Browse files

First chunk of .net based buildsystem

  • Loading branch information...
0 parents commit 853713bd7212233e72d26515819c35784f9c6b9d Lucas Meijer committed Apr 14, 2012
Showing with 299 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +43 −0 DependencyGraph.cs
  3. +17 −0 FileAssert.cs
  4. +8 −0 MissingDependencyException.cs
  5. +14 −0 Program.cs
  6. +36 −0 Properties/AssemblyInfo.cs
  7. +93 −0 Tests/DependencyGraphTests.cs
  8. +62 −0 bs.csproj
  9. +20 −0 bs.sln
@@ -0,0 +1,6 @@
+
+_ReSharper.bs
+bin/
+bs.5.1.ReSharper.user
+bs.suo
+obj/
@@ -0,0 +1,43 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace bs
+{
+ public class DependencyGraph
+ {
+ readonly Dictionary<string, TargetBuildInstructions> graph = new Dictionary<string, TargetBuildInstructions>();
+ public Action<string, TargetBuildInstructions> GenerateCallback = (s, i) => { };
+
+ public void RequestTarget(string targetFile)
+ {
+ var instructions = graph[targetFile];
+
+ if (instructions.SourceFiles.Any(sourceFile => !File.Exists(sourceFile)))
+ throw new MissingDependencyException();
+
+ if (File.Exists(targetFile))
+ return;
+
+ Generate(targetFile, instructions);
+ }
+
+ private void Generate(string targetFile, TargetBuildInstructions instructions)
+ {
+ GenerateCallback(targetFile, instructions);
+ instructions.Action(targetFile, instructions.SourceFiles);
+ }
+
+ public void RegisterTarget(string targetFile, TargetBuildInstructions instructions)
+ {
+ graph.Add(targetFile,instructions);
+ }
+ }
+
+ public class TargetBuildInstructions
+ {
+ public Action<string, IEnumerable<string>> Action;
+ public IEnumerable<string> SourceFiles;
+ }
+}
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace bs
+{
+ static class FileAssert
+ {
+ static public void Contains(string file, string contents)
+ {
+ Assert.AreEqual(contents, File.ReadAllText(file));
+ }
+ }
+}
@@ -0,0 +1,8 @@
+using System;
+
+namespace bs
+{
+ public class MissingDependencyException : Exception
+ {
+ }
+}
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace bs
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ }
+ }
+}
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("bs")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("bs")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("6386c065-bd18-4d1d-add2-ef06f124fc5d")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,93 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace bs.Tests
+{
+ [TestFixture]
+ public class DependencyGraphTests
+ {
+ [SetUp]
+ public void Setup()
+ {
+ const string dirName = "Workspace";
+ if (Directory.Exists(dirName))
+ Directory.Delete(dirName,true);
+
+ Directory.CreateDirectory(dirName);
+ Directory.SetCurrentDirectory(dirName);
+ }
+
+ const string defaultSourceFile = "input.txt";
+ const string defaulttargetFile = "output.txt";
+
+ [Test]
+ public void WillGenerateTargetWithoutSources()
+ {
+ DependencyGraph depGraph = SetupGraphWithOneTargetWithoutSources();
+
+ depGraph.RequestTarget(defaulttargetFile);
+ FileAssert.Contains(defaulttargetFile, "Hello");
+ }
+
+ [Test]
+ public void WontGenerateTargetWithoutSourcesTwice()
+ {
+ DependencyGraph depGraph = SetupGraphWithOneTargetWithoutSources();
+
+ depGraph.RequestTarget(defaulttargetFile);
+ FileAssert.Contains(defaulttargetFile, "Hello");
+ depGraph.GenerateCallback += (target, instructions) => { throw new InvalidOperationException(); };
+ depGraph.RequestTarget(defaulttargetFile);
+ }
+
+ private static DependencyGraph SetupGraphWithOneTargetWithoutSources()
+ {
+ var depGraph = new DependencyGraph();
+ depGraph.RegisterTarget(defaulttargetFile, new TargetBuildInstructions()
+ {
+ Action = (target,sources) => File.WriteAllText(target, "Hello"),
+ SourceFiles = new string[0],
+ });
+ return depGraph;
+ }
+
+
+ [Test]
+ public void ThrowsIfDependencyDoesNotExist()
+ {
+ DependencyGraph depGraph = SetupSimpleCopyDepGraph();
+ Assert.Throws<MissingDependencyException>(() => depGraph.RequestTarget(defaulttargetFile));
+ }
+
+ [Test]
+ public void RegeneratesWhenSourceChanges()
+ {
+ DependencyGraph depGraph = SetupSimpleCopyDepGraph();
+ Assert.Throws<MissingDependencyException>(() => depGraph.RequestTarget(defaulttargetFile));
+
+ File.WriteAllText(defaultSourceFile, "One");
+ depGraph.RequestTarget(defaulttargetFile);
+ FileAssert.Contains(defaultSourceFile, "One");
+
+ File.WriteAllText(defaultSourceFile, "Two");
+ depGraph.RequestTarget(defaulttargetFile);
+ FileAssert.Contains(defaultSourceFile, "Two");
+ }
+
+ private static DependencyGraph SetupSimpleCopyDepGraph()
+ {
+ var depGraph = new DependencyGraph();
+
+ depGraph.RegisterTarget(defaulttargetFile, new TargetBuildInstructions()
+ {
+ Action = (target,sources) => File.Copy(sources.Single(), target, true),
+ SourceFiles = new[] { defaultSourceFile }
+ });
+ return depGraph;
+ }
+ }
+}
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F2546F61-0181-4E7D-AD26-5F20F9D2F888}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>bs</RootNamespace>
+ <AssemblyName>bs</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <PlatformTarget>x86</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <PlatformTarget>x86</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="FileAssert.cs" />
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Tests\DependencyGraphTests.cs" />
+ <Compile Include="DependencyGraph.cs" />
+ <Compile Include="MissingDependencyException.cs" />
+ </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.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bs", "bs.csproj", "{F2546F61-0181-4E7D-AD26-5F20F9D2F888}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {F2546F61-0181-4E7D-AD26-5F20F9D2F888}.Debug|x86.ActiveCfg = Debug|x86
+ {F2546F61-0181-4E7D-AD26-5F20F9D2F888}.Debug|x86.Build.0 = Debug|x86
+ {F2546F61-0181-4E7D-AD26-5F20F9D2F888}.Release|x86.ActiveCfg = Release|x86
+ {F2546F61-0181-4E7D-AD26-5F20F9D2F888}.Release|x86.Build.0 = Release|x86
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal

0 comments on commit 853713b

Please sign in to comment.