Skip to content

Commit

Permalink
Added ability to reference scripts for inclusion within a script temp…
Browse files Browse the repository at this point in the history
…late.
  • Loading branch information
nikhilk committed Dec 20, 2012
1 parent 220b5c8 commit 7f183fa
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 10 deletions.
28 changes: 28 additions & 0 deletions src/Core/App/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ssc [/nologo] [/?]
[/res:<resource file>]
/ref:<assembly path>
/out:<script file>
[/inc:<include base path>]
<C# source files>
/nologo Hides the about information.
Expand All @@ -50,6 +51,7 @@ .resx files. You should pass in both the language-neutral
resources and the locale specific resources for the locale
associated with the output script.
/out The resulting script file. Typically this is a .js file.
/inc The base path against which includes are resolved.
";

private static CompilerOptions CreateCompilerOptions(CommandLine commandLine) {
Expand All @@ -62,6 +64,7 @@ private static CompilerOptions CreateCompilerOptions(CommandLine commandLine) {
bool includeTests = false;
bool minimize = true;
IStreamSource scriptFile = null;
IStreamSourceResolver includeResolver = null;

foreach (string fileName in commandLine.Arguments) {
// TODO: This is a hack... something in the .net 4 build system causes
Expand Down Expand Up @@ -132,6 +135,11 @@ private static CompilerOptions CreateCompilerOptions(CommandLine commandLine) {
includeTests = commandLine.Options.Contains("tests");
minimize = commandLine.Options.Contains("minimize");

if (commandLine.Options.Contains("inc")) {
string basePath = (string)commandLine.Options["inc"];
includeResolver = new IncludeResolver(basePath);
}

CompilerOptions compilerOptions = new CompilerOptions();
compilerOptions.IncludeTests = includeTests;
compilerOptions.Defines = defines;
Expand All @@ -140,6 +148,7 @@ private static CompilerOptions CreateCompilerOptions(CommandLine commandLine) {
compilerOptions.Sources = sources;
compilerOptions.Resources = resources;
compilerOptions.ScriptFile = scriptFile;
compilerOptions.IncludeResolver = includeResolver;

compilerOptions.InternalTestMode = commandLine.Options.Contains("test");
if (compilerOptions.InternalTestMode) {
Expand Down Expand Up @@ -207,5 +216,24 @@ private static void ShowUsage(string message) {
}
Console.WriteLine(UsageText);
}


private sealed class IncludeResolver : IStreamSourceResolver {

private string _basePath;

public IncludeResolver(string basePath) {
_basePath = basePath;
}

public IStreamSource Resolve(string name) {
string path = Path.Combine(_basePath, name);
if (File.Exists(path)) {
return new FileInputStreamSource(path, name);
}

return null;
}
}
}
}
16 changes: 15 additions & 1 deletion src/Core/Build/Tasks/ScriptCompilerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ScriptSharp.Tasks {
/// <summary>
/// The Script# MSBuild task.
/// </summary>
public sealed class ScriptCompilerTask : Task, IErrorHandler {
public sealed class ScriptCompilerTask : Task, IErrorHandler, IStreamSourceResolver {

private string _projectPath;
private ITaskItem[] _references;
Expand Down Expand Up @@ -266,6 +266,7 @@ private CompilerOptions CreateOptions(IEnumerable<ITaskItem> sourceItems, IEnume
options.References = GetReferences();
options.Sources = GetSources(sourceItems);
options.Resources = GetResources(resourceItems, locale);
options.IncludeResolver = this;

string scriptFilePath = GetScriptFilePath(locale, minimize, includeTests);
outputScriptItem = new TaskItem(scriptFilePath);
Expand Down Expand Up @@ -579,6 +580,19 @@ void IErrorHandler.ReportError(string errorMessage, string location) {
}
#endregion

#region Implementation of IStreamSourceResolver

IStreamSource IStreamSourceResolver.Resolve(string name) {
string path = Path.Combine(Path.GetDirectoryName(_projectPath), name);
if (File.Exists(path)) {
return new FileInputStreamSource(path, name);
}

return null;
}

#endregion


private sealed class TaskItemInputStreamSource : FileInputStreamSource {

Expand Down
1 change: 1 addition & 0 deletions src/Core/Compiler/Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="..\ResXParser.cs">
<Link>ResXParser.cs</Link>
</Compile>
<Compile Include="IStreamSourceResolver.cs" />
<Compile Include="ScriptReference.cs" />
<Compile Include="ScriptInfo.cs" />
<Compile Include="Importer\IL\ArrayType.cs" />
Expand Down
10 changes: 10 additions & 0 deletions src/Core/Compiler/CompilerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class CompilerOptions {
private ICollection<IStreamSource> _resources;
private IStreamSource _scriptFile;
private IStreamSource _docCommentFile;
private IStreamSourceResolver _includeResolver;
private bool _includeTests;
private bool _minimize;
private ScriptInfo _scriptInfo;
Expand Down Expand Up @@ -69,6 +70,15 @@ public bool HasTestTypes {
}
}

public IStreamSourceResolver IncludeResolver {
get {
return _includeResolver;
}
set {
_includeResolver = value;
}
}

public bool IncludeTests {
get {
return _includeTests;
Expand Down
14 changes: 14 additions & 0 deletions src/Core/Compiler/IStreamSourceResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// IStreamSourceResolver.cs
// Script#/Core/Compiler
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//

using System;

namespace ScriptSharp {

public interface IStreamSourceResolver {

IStreamSource Resolve(string name);
}
}
29 changes: 29 additions & 0 deletions src/Core/Compiler/ScriptCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using ScriptSharp.CodeModel;
using ScriptSharp.Compiler;
using ScriptSharp.Generator;
Expand Down Expand Up @@ -288,6 +289,8 @@ private string GenerateScriptWithTemplate() {
return script;
}

template = PreprocessTemplate(template);

StringBuilder requiresBuilder = new StringBuilder();
StringBuilder dependenciesBuilder = new StringBuilder();
StringBuilder depLookupBuilder = new StringBuilder();
Expand Down Expand Up @@ -337,6 +340,32 @@ private void ImportMetadata() {
_importedSymbols = mdImporter.ImportMetadata(_options.References, _symbols);
}

private string PreprocessTemplate(string template) {
if (_options.IncludeResolver == null) {
return template;
}

Regex includePattern = new Regex("\\{include:([^\\}]+)\\}", RegexOptions.Multiline | RegexOptions.CultureInvariant);
return includePattern.Replace(template, delegate(Match include) {
string includedScript = String.Empty;

if (include.Groups.Count == 2) {
string includePath = include.Groups[1].Value;

IStreamSource includeSource = _options.IncludeResolver.Resolve(includePath);
if (includeSource != null) {
Stream includeStream = includeSource.GetStream();
StreamReader reader = new StreamReader(includeStream);

includedScript = reader.ReadToEnd();
includeSource.CloseStream(includeStream);
}
}

return includedScript;
});
}

#region Implementation of IErrorHandler
void IErrorHandler.ReportError(string errorMessage, string location) {
_hasErrors = true;
Expand Down
34 changes: 26 additions & 8 deletions tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,21 @@ public void TestFlags() {
}

[TestMethod]
public void TestSimple() {
public void TestIncludes() {
RunTest((c) => {
c.AddSource("Code.cs");
}, "DefaultBaseline.txt");
}, "ZeroIncBaseline.txt");

RunTest((c) => {
c.AddSource("Code.cs");
c.Options.Defines = new string[] { "SIMPLE_TEMPLATE" };
}, "SimpleBaseline.txt");
c.AddSource("Code.cs").
AddIncludeResolver();
}, "SingleIncBaseline.txt");

RunTest((c) => {
c.AddSource("Code.cs");
c.Options.Defines = new string[] { "AMD_TEMPLATE" };
}, "AMDBaseline.txt");
c.AddSource("Code.cs").
AddIncludeResolver();
c.Options.Defines = new string[] { "MULTIPLE_INCLUDE" };
}, "MultipleIncBaseline.txt");
}

[TestMethod]
Expand Down Expand Up @@ -145,6 +146,23 @@ public void TestResources() {
});
}

[TestMethod]
public void TestSimple() {
RunTest((c) => {
c.AddSource("Code.cs");
}, "DefaultBaseline.txt");

RunTest((c) => {
c.AddSource("Code.cs");
c.Options.Defines = new string[] { "SIMPLE_TEMPLATE" };
}, "SimpleBaseline.txt");

RunTest((c) => {
c.AddSource("Code.cs");
c.Options.Defines = new string[] { "AMD_TEMPLATE" };
}, "AMDBaseline.txt");
}

[TestMethod]
public void TestUnitTest() {
RunTest((c) => {
Expand Down
17 changes: 16 additions & 1 deletion tests/Core/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace ScriptSharp.Tests.Core {

public sealed class Compilation : IErrorHandler {
public sealed class Compilation : IErrorHandler, IStreamSourceResolver {

private CompilationTest _test;

Expand All @@ -20,6 +20,7 @@ public sealed class Compilation : IErrorHandler {
private List<CompilationInput> _resources;
private CompilationInput _comments;
private CompilationOutput _output;
private IStreamSourceResolver _includeResolver;

private List<string> _errors;

Expand Down Expand Up @@ -84,6 +85,10 @@ public Compilation AddComments(string name) {
return this;
}

public void AddIncludeResolver() {
_includeResolver = this;
}

public Compilation AddReference(string name) {
string assemblyPath = _test.GetAssemblyFilePath(name);
if (File.Exists(assemblyPath)) {
Expand Down Expand Up @@ -125,6 +130,7 @@ public bool Execute() {
_options.Resources = _resources.Cast<IStreamSource>().ToList();
_options.DocCommentFile = _comments;
_options.ScriptFile = _output;
_options.IncludeResolver = _includeResolver;

ScriptCompiler compiler = new ScriptCompiler(this);
return compiler.Compile(_options);
Expand All @@ -144,5 +150,14 @@ void IErrorHandler.ReportError(string errorMessage, string location) {
}

#endregion

#region Implementation of IStreamSourceResolver

IStreamSource IStreamSourceResolver.Resolve(string name) {
string path = _test.GetTestFilePath(name);
return new CompilationInput(path, name);
}

#endregion
}
}
36 changes: 36 additions & 0 deletions tests/TestCases/Basic/Includes/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Code.cs
//

using System;
using System.Collections;
using System.Runtime.CompilerServices;

[assembly: ScriptAssembly("basic")]

#if MULTIPLE_INCLUDE
[assembly: ScriptTemplate(@"
// {name}.js
""use strict"";
(function($global) {
{include:pre.js}
{script}
{include:post.js}
})(this);
")]
#else
[assembly: ScriptTemplate(@"
// {name}.js {version}
//
{include:pre.js}
{script}
")]
#endif


namespace Basic {

public class App {
}
}
24 changes: 24 additions & 0 deletions tests/TestCases/Basic/Includes/MultipleIncBaseline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// basic.js
"use strict";

(function($global) {
// pre script

// Basic.App

function Basic$App() {
}
var Basic$App$ = {

};


var $exports = ss.module('basic', null,
{
App: [ Basic$App, Basic$App$, null ]
});


// post script

})(this);
1 change: 1 addition & 0 deletions tests/TestCases/Basic/Includes/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// post script
1 change: 1 addition & 0 deletions tests/TestCases/Basic/Includes/Pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// pre script
20 changes: 20 additions & 0 deletions tests/TestCases/Basic/Includes/SingleIncBaseline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// basic.js
//

// pre script

// Basic.App

function Basic$App() {
}
var Basic$App$ = {

};


var $exports = ss.module('basic', null,
{
App: [ Basic$App, Basic$App$, null ]
});


Loading

0 comments on commit 7f183fa

Please sign in to comment.