Permalink
Browse files

Added ability to reference scripts for inclusion within a script temp…

…late.
  • Loading branch information...
1 parent 220b5c8 commit 7f183fa17834b38faa77b7138385c5dc5c180c0b @nikhilk committed Dec 20, 2012
@@ -30,6 +30,7 @@ internal static class Application {
[/res:<resource file>]
/ref:<assembly path>
/out:<script file>
+ [/inc:<include base path>]
<C# source files>
/nologo Hides the about information.
@@ -50,6 +51,7 @@ compile code.
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) {
@@ -62,6 +64,7 @@ compile code.
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
@@ -132,6 +135,11 @@ compile code.
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;
@@ -140,6 +148,7 @@ compile code.
compilerOptions.Sources = sources;
compilerOptions.Resources = resources;
compilerOptions.ScriptFile = scriptFile;
+ compilerOptions.IncludeResolver = includeResolver;
compilerOptions.InternalTestMode = commandLine.Options.Contains("test");
if (compilerOptions.InternalTestMode) {
@@ -207,5 +216,24 @@ compile code.
}
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;
+ }
+ }
}
}
@@ -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;
@@ -266,6 +266,7 @@ public sealed class ScriptCompilerTask : Task, IErrorHandler {
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);
@@ -579,6 +580,19 @@ public sealed class ScriptCompilerTask : Task, IErrorHandler {
}
#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 {
@@ -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" />
@@ -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;
@@ -69,6 +70,15 @@ public sealed class CompilerOptions {
}
}
+ public IStreamSourceResolver IncludeResolver {
+ get {
+ return _includeResolver;
+ }
+ set {
+ _includeResolver = value;
+ }
+ }
+
public bool IncludeTests {
get {
return _includeTests;
@@ -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);
+ }
+}
@@ -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;
@@ -288,6 +289,8 @@ public ScriptCompiler()
return script;
}
+ template = PreprocessTemplate(template);
+
StringBuilder requiresBuilder = new StringBuilder();
StringBuilder dependenciesBuilder = new StringBuilder();
StringBuilder depLookupBuilder = new StringBuilder();
@@ -337,6 +340,32 @@ public ScriptCompiler()
_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;
View
@@ -79,20 +79,21 @@ public sealed class BasicTests : CompilationTest {
}
[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]
@@ -146,6 +147,23 @@ public sealed class BasicTests : CompilationTest {
}
[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) => {
c.AddSource("Code.cs");
@@ -11,7 +11,7 @@
namespace ScriptSharp.Tests.Core {
- public sealed class Compilation : IErrorHandler {
+ public sealed class Compilation : IErrorHandler, IStreamSourceResolver {
private CompilationTest _test;
@@ -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;
@@ -84,6 +85,10 @@ public sealed class Compilation : IErrorHandler {
return this;
}
+ public void AddIncludeResolver() {
+ _includeResolver = this;
+ }
+
public Compilation AddReference(string name) {
string assemblyPath = _test.GetAssemblyFilePath(name);
if (File.Exists(assemblyPath)) {
@@ -125,6 +130,7 @@ public sealed class Compilation : IErrorHandler {
_options.Resources = _resources.Cast<IStreamSource>().ToList();
_options.DocCommentFile = _comments;
_options.ScriptFile = _output;
+ _options.IncludeResolver = _includeResolver;
ScriptCompiler compiler = new ScriptCompiler(this);
return compiler.Compile(_options);
@@ -144,5 +150,14 @@ public sealed class Compilation : IErrorHandler {
}
#endregion
+
+ #region Implementation of IStreamSourceResolver
+
+ IStreamSource IStreamSourceResolver.Resolve(string name) {
+ string path = _test.GetTestFilePath(name);
+ return new CompilationInput(path, name);
+ }
+
+ #endregion
}
}
@@ -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 {
+ }
+}
@@ -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);
@@ -0,0 +1 @@
+// post script
@@ -0,0 +1 @@
+// pre script
@@ -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 ]
+ });
+
+
Oops, something went wrong.

0 comments on commit 7f183fa

Please sign in to comment.