From 630c49dbff6cc85a487ec3a60cd77564f1f40806 Mon Sep 17 00:00:00 2001 From: William Wood Date: Fri, 16 Oct 2015 13:38:14 -0700 Subject: [PATCH] Initial Commit --- Angular-Template-Bundler.nuspec | 24 ++++++ LICENSE.md | 20 +++++ README.md | 33 ++++++++ wmwood.AngularTemplateBundler.sln | 22 ++++++ .../AngularTemplateBundle.cs | 12 +++ .../AngularTemplateTransform.cs | 64 ++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++ wmwood.AngularTemplateBundler/app.config | 11 +++ wmwood.AngularTemplateBundler/packages.config | 8 ++ .../wmwood.AngularTemplateBundler.csproj | 75 +++++++++++++++++++ 10 files changed, 305 insertions(+) create mode 100644 Angular-Template-Bundler.nuspec create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 wmwood.AngularTemplateBundler.sln create mode 100644 wmwood.AngularTemplateBundler/AngularTemplateBundle.cs create mode 100644 wmwood.AngularTemplateBundler/AngularTemplateTransform.cs create mode 100644 wmwood.AngularTemplateBundler/Properties/AssemblyInfo.cs create mode 100644 wmwood.AngularTemplateBundler/app.config create mode 100644 wmwood.AngularTemplateBundler/packages.config create mode 100644 wmwood.AngularTemplateBundler/wmwood.AngularTemplateBundler.csproj diff --git a/Angular-Template-Bundler.nuspec b/Angular-Template-Bundler.nuspec new file mode 100644 index 0000000..43d1889 --- /dev/null +++ b/Angular-Template-Bundler.nuspec @@ -0,0 +1,24 @@ + + + + Angular-Template-Bundler + 0.0.3.0 + Angular Template Bundler + William Wood + wwood + https://raw.githubusercontent.com/wmwood/Angular-Template-Bundler/master/LICENSE.md + https://github.com/wmwood/Angular-Template-Bundler + false + A simple ASP.NET MVC bundle that helps you combine all of your angular html templates into one file and add to your app's template cache. + + 2015 + en-US + angularjs templates bundling System.Web.Optimization + + + + + + + + \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..092ae83 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2015 William Wood + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f23e583 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# Angular Template Bundler + +A simple ASP.NET MVC bundle that helps you combine all of your angular html templates into one file and add to your app's template cache. + +## Installation + +``` Powershell +PM> Install-Package Angular-Template-Bundler +``` +[see nuget](https://www.nuget.org/packages/Angular-Template-Bundler/) + +## Usage + +#### Add this line to your BundleConfig.cs: +``` C# +bundles.Add(new AngularTemplateBundle("VIRTUAL_PATH", "ANGULAR_MODULE_NAME") + .IncludeDirectory("PATH_TO_APP_FOLDER", "SEARCH_PATTERN", SEARCH_SUBFOLDERS)); +``` + +An example usage would be: +``` C# +bundles.Add(new AngularTemplateBundle("~/bundles/app/templates", "myApp") + .IncludeDirectory("~/Assets/js/app", "*.tpl.html", true)); +``` + +#### Then, add this line to your main razor page: + +``` C# +@Scripts.Render("~/bundles/app/templates") +``` + +## License +[see LICENSE.md](LICENSE.md) \ No newline at end of file diff --git a/wmwood.AngularTemplateBundler.sln b/wmwood.AngularTemplateBundler.sln new file mode 100644 index 0000000..d7d4bfd --- /dev/null +++ b/wmwood.AngularTemplateBundler.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.40629.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "wmwood.AngularTemplateBundler", "wmwood.AngularTemplateBundler\wmwood.AngularTemplateBundler.csproj", "{5BCCD6D0-28B6-451E-918E-E2C15B2B85E7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5BCCD6D0-28B6-451E-918E-E2C15B2B85E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BCCD6D0-28B6-451E-918E-E2C15B2B85E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BCCD6D0-28B6-451E-918E-E2C15B2B85E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BCCD6D0-28B6-451E-918E-E2C15B2B85E7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/wmwood.AngularTemplateBundler/AngularTemplateBundle.cs b/wmwood.AngularTemplateBundler/AngularTemplateBundle.cs new file mode 100644 index 0000000..c0a3026 --- /dev/null +++ b/wmwood.AngularTemplateBundler/AngularTemplateBundle.cs @@ -0,0 +1,12 @@ +using System.Web.Optimization; + +namespace wmwood.AngularTemplateBundler +{ + public class AngularTemplateBundle : Bundle + { + public AngularTemplateBundle(string virtualPath, string moduleName) + : base(virtualPath, new AngularTemplateTransform(moduleName), new JsMinify()) + { + } + } +} \ No newline at end of file diff --git a/wmwood.AngularTemplateBundler/AngularTemplateTransform.cs b/wmwood.AngularTemplateBundler/AngularTemplateTransform.cs new file mode 100644 index 0000000..6a20f49 --- /dev/null +++ b/wmwood.AngularTemplateBundler/AngularTemplateTransform.cs @@ -0,0 +1,64 @@ +using System.IO; +using System.Text; +using System.Web; +using System.Web.Optimization; + +namespace wmwood.AngularTemplateBundler +{ + public class AngularTemplateTransform : IBundleTransform + { + private const string ContentType = "text/javascript"; + private const string FooterText = "}])})()"; + private readonly string _moduleName; + + public AngularTemplateTransform(string moduleName) + { + _moduleName = moduleName; + } + + public void Process(BundleContext context, BundleResponse response) + { + var responseContent = new StringBuilder(); + responseContent.Append(GetHeaderText()); + + foreach (var bundleFile in response.Files) + { + responseContent.Append(GetBundleText(bundleFile)); + } + + responseContent.Append(FooterText); + + FormatResponse(response); + response.Content = responseContent.ToString(); + } + + private string GetHeaderText() + { + return string.Format("(function() {{ \"use strict\"; angular.module(\"{0}\").run([\"$templateCache\", function($templateCache) {{", _moduleName); + } + + private static string GetBundleText(BundleFile bundleFile) + { + return string.Format("$templateCache.put(\"{0}\", \"{1}\");", GetTemplatePath(bundleFile), GetJavascriptEncodedContent(bundleFile)); + } + + private static string GetTemplatePath(BundleFile bundleFile) + { + return VirtualPathUtility.ToAbsolute(bundleFile.IncludedVirtualPath); + } + + private static string GetJavascriptEncodedContent(BundleFile bundleFile) + { + using (var streamReader = new StreamReader(BundleTable.VirtualPathProvider.GetFile(bundleFile.IncludedVirtualPath).Open())) + { + return HttpUtility.JavaScriptStringEncode(streamReader.ReadToEnd().Trim()); + } + } + + private static void FormatResponse(BundleResponse response) + { + response.Files = new BundleFile[] {}; + response.ContentType = ContentType; + } + } +} \ No newline at end of file diff --git a/wmwood.AngularTemplateBundler/Properties/AssemblyInfo.cs b/wmwood.AngularTemplateBundler/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..245c552 --- /dev/null +++ b/wmwood.AngularTemplateBundler/Properties/AssemblyInfo.cs @@ -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("wmwood.AngularTemplateBundler")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("William Wood")] +[assembly: AssemblyProduct("wmwood.AngularTemplateBundler")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[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("b7e695d1-0a83-4527-ac63-6fd214d39098")] + +// 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("0.0.3.0")] +[assembly: AssemblyFileVersion("0.0.3.0")] diff --git a/wmwood.AngularTemplateBundler/app.config b/wmwood.AngularTemplateBundler/app.config new file mode 100644 index 0000000..9d3e5a6 --- /dev/null +++ b/wmwood.AngularTemplateBundler/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/wmwood.AngularTemplateBundler/packages.config b/wmwood.AngularTemplateBundler/packages.config new file mode 100644 index 0000000..6bd773f --- /dev/null +++ b/wmwood.AngularTemplateBundler/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/wmwood.AngularTemplateBundler/wmwood.AngularTemplateBundler.csproj b/wmwood.AngularTemplateBundler/wmwood.AngularTemplateBundler.csproj new file mode 100644 index 0000000..27ca90e --- /dev/null +++ b/wmwood.AngularTemplateBundler/wmwood.AngularTemplateBundler.csproj @@ -0,0 +1,75 @@ + + + + + Debug + AnyCPU + {5BCCD6D0-28B6-451E-918E-E2C15B2B85E7} + Library + Properties + wmwood.AngularTemplateBundler + wmwood.AngularTemplateBundler + v4.0 + 512 + ..\packages\WebGrease.1.5.2\lib + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Antlr.3.4.1.9004\lib\Antlr3.Runtime.dll + True + + + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + True + + + ..\packages\Newtonsoft.Json.5.0.4\lib\net40\Newtonsoft.Json.dll + True + + + + + + ..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll + True + + + ..\packages\WebGrease.1.5.2\lib\WebGrease.dll + True + + + + + + + + + + + + + + \ No newline at end of file