Skip to content

Commit

Permalink
make chained dependencies work
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmeijer committed Apr 15, 2012
1 parent 9bc54cb commit 84ea5e6
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 34 deletions.
6 changes: 3 additions & 3 deletions CCompilerTask.cs
Expand Up @@ -18,18 +18,18 @@ public CCompilerTask(string targetFile, string sourceFile, string[] includePaths
_includePaths = includePaths;
}

public IEnumerable<string> GetInputFiles()
public IEnumerable<string> GetInputFiles(DependencyGraph depGraph)
{
var scanner = new RecursiveIncludeScanner(_includePaths, new IncludeScanner());
yield return _sourceFile;
foreach (var file in scanner.GetFilesIncludedBy(_sourceFile))
foreach (var file in scanner.GetFilesIncludedBy(_sourceFile, depGraph))
yield return file;
}

public void RegisterWithDepGraph(DependencyGraph depGraph)
{
var action = new SimpleAction(Execute, "todo");
var settings = new TargetGenerateSettings(action, GetInputFiles(), _targetFile);
var settings = new TargetGenerateSettings(action, GetInputFiles(depGraph), _targetFile);
depGraph.RegisterTarget(settings);
}

Expand Down
11 changes: 7 additions & 4 deletions RecursiveIncludeScanner.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace cake
{
Expand All @@ -14,20 +15,22 @@ public RecursiveIncludeScanner(string[] includePaths, IncludeScanner scanner)
_includeScanner = scanner;
}

public IEnumerable<string> GetFilesIncludedBy(string file)
public IEnumerable<string> GetFilesIncludedBy(string file, DependencyGraph depGraph)
{
foreach (var includedFile in _includeScanner.Scan(file))
{
var foundIncludeFile = FindSpecifiedIncludeFileInSearchDirs(includedFile);
var foundIncludeFile = FindSpecifiedIncludeFileInSearchDirs(depGraph,includedFile);
if (foundIncludeFile == null)
throw new InvalidOperationException("unable to find the header file: " + includedFile + " in the header search paths.");
yield return foundIncludeFile;
foreach (var file2 in GetFilesIncludedBy(foundIncludeFile))
if (!File.Exists(foundIncludeFile))
continue;
foreach (var file2 in GetFilesIncludedBy(foundIncludeFile, depGraph))
yield return file2;
}
}

private string FindSpecifiedIncludeFileInSearchDirs(string includedFile)
private string FindSpecifiedIncludeFileInSearchDirs(DependencyGraph depGraph, string includedFile)
{
return includedFile;
}
Expand Down
2 changes: 1 addition & 1 deletion SchedulableAction.cs
Expand Up @@ -8,7 +8,7 @@ public class SchedulableAction
public IEnumerable<string> InputFilesRequiringGeneration;

public SchedulableAction(TargetGenerateSettings generateSettings)
:this(generateSettings,new List<string>())
: this(generateSettings, new List<string>())
{
}

Expand Down
13 changes: 8 additions & 5 deletions SchedulableActionCollector.cs
Expand Up @@ -18,19 +18,22 @@ public IEnumerable<SchedulableAction> CollectActionsToGenerate(string targetFile
{
var result = ActionRequiredToGetTargetUpdated(targetFile);
if (result != null)
{
yield return result;
foreach(var inputFileRequiringGeneration in result.InputFilesRequiringGeneration)
{
var theirAction = ActionRequiredToGetTargetUpdated(inputFileRequiringGeneration);
if (theirAction != null)
yield return theirAction;
}
}
}

SchedulableAction ActionRequiredToGetTargetUpdated(string target)
{
TargetGenerateSettings generateSettings;
if (!_dependencyGraph._graph.TryGetValue(target, out generateSettings))
{
return null;
//if (File.Exists(target))
// return null;
//throw new MissingDependencyException();
}

var inputFilesRequiringGeneration = generateSettings.InputFiles.Where(inputfile => ActionRequiredToGetTargetUpdated(inputfile) != null).ToArray();

Expand Down
10 changes: 8 additions & 2 deletions TargetGenerateSettings.cs
Expand Up @@ -35,8 +35,14 @@ public override bool Equals(object obj)

if (ActionHash != other.ActionHash)
return false;

return other.InputFiles.SetEquals(InputFiles);

if (!other.InputFiles.SetEquals(InputFiles))
return false;

if (!other.OutputFiles.SetEquals(OutputFiles))
return false;

return true;
}

public override int GetHashCode()
Expand Down
4 changes: 3 additions & 1 deletion Tests/CCompilerTests.cs
@@ -1,4 +1,5 @@
using System.IO;
using Moq;
using NUnit.Framework;

namespace cake.Tests
Expand All @@ -13,7 +14,8 @@ public void GetInputFilesFindsRecursiveHeaderDependencies()
File.WriteAllText("test.h", "#include <shared.h>");
File.WriteAllText("shared.h", "//like a boss");
var task = new CCompilerTask("test.o", "test.c", new[] {""});
var inputfiles = task.GetInputFiles();
var depGraph = new Mock<DependencyGraph>().Object;
var inputfiles = task.GetInputFiles(depGraph);

CollectionAssert.AreEquivalent(new [] {"test.c","test.h","shared.h"}, inputfiles);
}
Expand Down
5 changes: 0 additions & 5 deletions Tests/DependencyGraphTests.cs
Expand Up @@ -5,7 +5,6 @@

namespace cake.Tests
{
[TestFixture]
public class DependencyGraphTests
{
[SetUp]
Expand All @@ -32,9 +31,5 @@ protected void ThrowIfDepgraphGenerates()
{
_depGraph.GenerateCallback += settings => { throw new InvalidOperationException(); };
}




}
}
3 changes: 0 additions & 3 deletions Tests/GeneratedHeader.cs
Expand Up @@ -11,7 +11,6 @@ namespace cake.Tests
public class GeneratedHeader : DependencyGraphTests
{
[Test]
[Ignore]
public void Test()
{
var generateHeaderAction = new SimpleAction(s => File.Copy(s.InputFiles.Single(), s.OutputFiles.Single(), true));
Expand All @@ -20,8 +19,6 @@ public void Test()
File.WriteAllText("file1","//theboss");
File.WriteAllText("test.c","#include <myheader.h>");

//temp:
File.WriteAllText("myheader.h","doesalreadyexisttemporary" );
var compilecppfile = new CCompilerTask("test.o", "test.c", new string[] {});

_depGraph.RegisterTarget(generateHeaderSettings);
Expand Down
42 changes: 32 additions & 10 deletions Tests/SchedulableActionCollectorTest.cs
@@ -1,28 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cake;
using cake.Tests;
using NUnit.Framework;

namespace bs.Tests
namespace cake.Tests
{
[TestFixture]
class SchedulableActionCollectorTest
{
DependencyGraph _depGraph;
SchedulableActionCollector _collector;

[SetUp]
public void Setup()
{
_depGraph = new DependencyGraph();
_collector = new SchedulableActionCollector(_depGraph);
}

[Test]
public void CanCollectSingleAction()
{
var depGraph = new DependencyGraph();
var a = new SchedulableActionCollector(depGraph);

var settings = new TargetGenerateSettings(new SimpleAction(s=> { }), new[] {"input"}, "output");
depGraph.RegisterTarget(settings);
var result = a.CollectActionsToGenerate("output").ToArray();
_depGraph.RegisterTarget(settings);
var result = _collector.CollectActionsToGenerate("output").ToArray();

Assert.AreEqual(1, result.Length);
Assert.AreEqual(settings,result[0].Settings);
Assert.AreEqual(settings, result[0].Settings);
}

[Test]
public void CanCollectMultipleActions()
{
var settings1 = new TargetGenerateSettings(new SimpleAction(s => { }), new[] { "file1" }, "file2");
_depGraph.RegisterTarget(settings1);

var settings2 = new TargetGenerateSettings(new SimpleAction(s => { }), new[] { "file2" }, "file3");
_depGraph.RegisterTarget(settings2);

var result = _collector.CollectActionsToGenerate("file3").ToArray();

Assert.AreEqual(2, result.Length);

//HALP! how to do a more reasonable equivalenec test?
CollectionAssert.AreEquivalent(result.Select(r=>r.Settings), new[] { settings1,settings2});
CollectionAssert.AreEquivalent(result.Select(r => r.InputFilesRequiringGeneration), new[] { new string[0], new[]{"file2"}});
}
}
}

0 comments on commit 84ea5e6

Please sign in to comment.