Skip to content

Commit

Permalink
New OWIN middleware to show topics in doc-run instead of TopicChain's
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Sep 24, 2015
1 parent 8bc6058 commit 6964d53
Show file tree
Hide file tree
Showing 12 changed files with 1,275 additions and 1,296 deletions.
882 changes: 441 additions & 441 deletions client/all-spec-data.js

Large diffs are not rendered by default.

1,408 changes: 704 additions & 704 deletions client/batch-result-data.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ NUGET
HtmlTags (3.0.0.186)
ILRepack (2.0.5)
Newtonsoft.Json (4.5.9)
Nowin (0.17.1.0)
Nowin (0.18.0.0)
Owin (1.0)
RhinoMocks (3.6.1)
Selenium.RC (2.47.0)
Expand Down
31 changes: 28 additions & 3 deletions src/ST/Docs/DocProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Caching;
using FubuCore.Util;
using FubuMVC.Core;
using FubuMVC.Core.Http.Owin;
using FubuMVC.Core.Http.Owin.Middleware;
using ST.Docs.Commands;
using ST.Docs.Exporting;
using ST.Docs.Html;
Expand All @@ -17,18 +21,21 @@ namespace ST.Docs
public class DocProject : IDisposable
{
private readonly Container _container;
private readonly Topic _topic;
private Topic _topic;
private readonly DocSettings _settings;
private TopicFileWatcher _topicWatcher;
private ISampleBuilder _sampleBuilder;

private readonly Cache<string, Topic> _topicByUrl = new Cache<string, Topic>();

public DocProject(DocSettings settings)
{
_topic = TopicLoader.LoadDirectory(settings.Root);
readTopics(TopicLoader.LoadDirectory(settings.Root));
_settings = settings;

_container = new Container(_ =>
{
_.For<DocProject>().Use(this);
_.AddRegistry<SampleRegistry>();
_.AddRegistry<TransformationRegistry>();
Expand All @@ -42,6 +49,14 @@ public DocProject(DocSettings settings)
});
}

private void readTopics(Topic topic)
{
_topic = topic;

_topicByUrl.ClearAll();
_topic.AllTopicsInOrder().Each(x => _topicByUrl[x.Url] = x);
}

public void ExportTo(string directory)
{
var exporter = _container.GetInstance<Exporter>();
Expand Down Expand Up @@ -76,7 +91,12 @@ public FubuRuntime LaunchRunner()

_sampleBuilder = scanForSamples();

var registry = new TopicRegistry(_topic) {RootPath = _settings.Root};
var registry = new TopicRegistry() {RootPath = _settings.Root};
registry.AlterSettings<OwinSettings>(_ =>
{
_.Middleware.InsertFirst(new MiddlewareNode<TopicMiddleware>());
});

registry.StructureMap(_container);
return registry.ToRuntime();
}
Expand Down Expand Up @@ -121,5 +141,10 @@ public void Dispose()
if (_topicWatcher != null) _topicWatcher.Dispose();
_container.Dispose();
}

public Topic FindTopicByUrl(string url)
{
return _topicByUrl.Has(url) ? _topicByUrl[url] : null;
}
}
}
62 changes: 0 additions & 62 deletions src/ST/Docs/Runner/TopicBehavior.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/ST/Docs/Runner/TopicChain.cs

This file was deleted.

74 changes: 74 additions & 0 deletions src/ST/Docs/Runner/TopicMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using FubuCore;
using FubuMVC.Core.Http.Owin;
using FubuMVC.Core.Http.Owin.Middleware;
using ST.Docs.Topics;
using ST.Docs.Transformation;

namespace ST.Docs.Runner
{
public class TopicMiddleware : IOwinMiddleware
{
private readonly Func<IDictionary<string, object>, Task> _inner;
private readonly DocProject _project;
private readonly IHtmlGenerator _generator;
private readonly DocSettings _settings;
private readonly string _webSocketScript;


public TopicMiddleware(Func<IDictionary<string, object>, Task> inner, DocProject project, IHtmlGenerator generator, DocSettings settings)
{
_inner = inner;
_project = project;
_generator = generator;
_settings = settings;

var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(typeof(TopicMiddleware), "WebsocketsRefresh.txt");

_webSocketScript = stream.ReadAllText();
}

public Task Invoke(IDictionary<string, object> environment)
{
var path = environment[OwinConstants.RequestPathKey].As<string>().TrimStart('/');


var topic = _project.FindTopicByUrl(path);
if (topic == null)
{
return _inner(environment);
}

return Task.Factory.StartNew(() =>
{
var response = new OwinHttpResponse(environment);
response.WriteContentType("text/html");
var html = GenerateHtml(topic);
response.Write(html);
response.Flush();
});

}

public string GenerateHtml(Topic topic)
{
var html = _generator.Generate(topic);

var builder = new StringBuilder(html);
topic.Substitutions.Each((key, value) => { builder.Replace(key, value); });

var script = _webSocketScript.Replace("%WEB_SOCKET_ADDRESS%", _settings.WebsocketAddress);
builder.Replace("</head>", script + "\n</head>");

return builder.ToString();
}
}
}
29 changes: 0 additions & 29 deletions src/ST/Docs/Runner/TopicNode.cs

This file was deleted.

52 changes: 13 additions & 39 deletions src/ST/Docs/Runner/TopicRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FubuMVC.Core;
using FubuMVC.Core.Assets;
using FubuMVC.Core.Diagnostics.Packaging;
using FubuMVC.Core.Http.Hosting;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Nodes;
using ST.Docs.Samples;
using ST.Docs.Todos;
using ST.Docs.Topics;

namespace ST.Docs.Runner
{
public class TopicRegistry : FubuRegistry, IChainSource
{
private readonly Topic _top;

public TopicRegistry(Topic top)
{
_top = top;

HostWith<NOWIN>();


namespace ST.Docs.Runner
{
public class TopicRegistry : FubuRegistry
{
public TopicRegistry()
{
HostWith<NOWIN>();

Actions.FindBy(_ => _.IncludeTypesNamed(x => x.EndsWith("DocTool")));
Actions.IncludeType<SampleExplorer>();
Actions.IncludeType<SampleExplorer>();
Actions.IncludeType<TodoExplorer>();
Actions.DisableDefaultActionSource();

Policies.ChainSource(this);

AlterSettings<AssetSettings>(_ =>
{
_.AllowableExtensions.Add(".json");
});
}
Actions.DisableDefaultActionSource();

public Task<BehaviorChain[]> BuildChains(BehaviorGraph graph, IPerfTimer timer)
{
return Task.FromResult(BuildChains(graph).ToArray());
AlterSettings<AssetSettings>(_ => _.AllowableExtensions.Add(".json"));
}

public IEnumerable<BehaviorChain> BuildChains(BehaviorGraph graph)
{
return _top.AllTopicsInOrder().Select(x => new TopicChain(x));
}
}
}
}
4 changes: 1 addition & 3 deletions src/ST/ST.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@
<Compile Include="Docs\Outline\OutlineReader.cs" />
<Compile Include="Docs\Outline\OutlineWriter.cs" />
<Compile Include="Docs\Runner\BrowserRefresher.cs" />
<Compile Include="Docs\Runner\TopicBehavior.cs" />
<Compile Include="Docs\Runner\TopicChain.cs" />
<Compile Include="Docs\Runner\TopicFileWatcher.cs" />
<Compile Include="Docs\Runner\TopicNode.cs" />
<Compile Include="Docs\Runner\TopicRegistry.cs" />
<Compile Include="Docs\Samples\BlockCommentScanner.cs" />
<Compile Include="Docs\Samples\CLangSampleScanner.cs" />
Expand All @@ -140,6 +137,7 @@
<Compile Include="Docs\Todos\TodoExplorer.cs" />
<Compile Include="Docs\Todos\TodoTableTag.cs" />
<Compile Include="Docs\Todos\TodoTask.cs" />
<Compile Include="Docs\Runner\TopicMiddleware.cs" />
<Compile Include="Docs\Topics\Topic.cs" />
<Compile Include="Docs\Topics\TopicLoader.cs" />
<Compile Include="Docs\Transformation\CodeSampleTransformHandler.cs" />
Expand Down
11 changes: 11 additions & 0 deletions src/StoryTeller.Testing/StoryTeller.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,17 @@
</ItemGroup>
</When>
</Choose>
<Choose>
<When Condition="($(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3')) Or ($(TargetFrameworkIdentifier) == 'MonoAndroid') Or ($(TargetFrameworkIdentifier) == 'MonoTouch')">
<ItemGroup>
<Reference Include="Nowin">
<HintPath>..\..\packages\Nowin\lib\net45\Nowin.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
</Choose>
<Choose>
<When Condition="($(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v2.0' Or $(TargetFrameworkVersion) == 'v3.0' Or $(TargetFrameworkVersion) == 'v3.5' Or $(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3')) Or ($(TargetFrameworkIdentifier) == 'MonoAndroid') Or ($(TargetFrameworkIdentifier) == 'MonoTouch')">
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/StoryTeller.Testing/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Newtonsoft.Json
HtmlTags
Fleck
Fixie
RhinoMocks
RhinoMocks
Nowin

0 comments on commit 6964d53

Please sign in to comment.