Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
175 ResourceHelper/HtmlHelperExtensions.cs
... ... @@ -1,175 +0,0 @@
1   -using System;
2   -using System.Web.Mvc;
3   -using System.Web.WebPages;
4   -using System.Collections.Generic;
5   -using System.IO;
6   -using System.Web;
7   -using System.Globalization;
8   -using System.Configuration;
9   -using System.Linq;
10   -using System.Text.RegularExpressions;
11   -using System.Security.Cryptography;
12   -using System.Text;
13   -
14   -namespace MvcHtmlHelpers
15   -{
16   - public class HtmlResources
17   - {
18   - public List<string> Scripts;
19   - public List<string> Stylesheets;
20   -
21   - public bool Bundle = false;
22   - public bool Minify = false;
23   -
24   - public DateTime LatestScriptFile = DateTime.MinValue;
25   - public DateTime LatestCSSFile = DateTime.MinValue;
26   -
27   - public HtmlResources()
28   - {
29   - Scripts = new List<String>();
30   - Stylesheets = new List<String>();
31   - bool.TryParse(ConfigurationManager.AppSettings["ResourceBundle"], out Bundle);
32   - bool.TryParse(ConfigurationManager.AppSettings["ResourceMinify"], out Minify);
33   - }
34   - }
35   -
36   - public static class HtmlHelperExtensions
37   - {
38   - private const string scriptsFolder = "~/Scripts/"; // TODO: Get from somewhere else!
39   - private const string cssFolder = "~/Content/"; // TODO: Get from somewhere else!
40   -
41   - public static MvcHtmlString Resource(this HtmlHelper html, string value)
42   - {
43   - var server = html.ViewContext.RequestContext.HttpContext.Server;
44   - var resources = (HtmlResources)html.ViewData["Resources"];
45   - if (resources == null)
46   - {
47   - resources = new HtmlResources();
48   - html.ViewData["Resources"] = resources;
49   - }
50   -
51   - if (File.Exists(server.MapPath(value)))
52   - {
53   - FileInfo info = new FileInfo(server.MapPath(value));
54   - if (value.EndsWith(".js"))
55   - {
56   - if (!resources.Scripts.Contains(value))
57   - {
58   - // Note the latest date a file was changed.
59   - if (DateTime.Compare(resources.LatestScriptFile, info.LastWriteTime) < 0) {
60   - resources.LatestScriptFile = info.LastWriteTime;
61   - }
62   -
63   - // Minify the script file if necessary.
64   - if (resources.Minify)
65   - {
66   - string origname = info.Name.Substring(0, info.Name.LastIndexOf('.'));
67   - if (origname.EndsWith(".min"))
68   - {
69   - // The resource is pre-minified. Skip.
70   - resources.Scripts.Insert(0, value);
71   - }
72   - else if (File.Exists(server.MapPath(scriptsFolder + origname + ".min" + info.Extension)) && DateTime.Compare(File.GetLastWriteTime(server.MapPath(scriptsFolder + origname + ".min" + info.Extension)), info.LastWriteTime) >= 0)
73   - {
74   - if (DateTime.Compare(resources.LatestScriptFile, info.LastWriteTime) < 0) {
75   - resources.LatestScriptFile = File.GetLastWriteTime(server.MapPath(scriptsFolder + origname + ".min" + info.Extension));
76   - }
77   - // We have already minified the file. Skip.
78   - resources.Scripts.Insert(0, scriptsFolder + origname + ".min" + info.Extension);
79   - }
80   - else
81   - {
82   - // Minify file.
83   - string filename = scriptsFolder + origname + ".min" + info.Extension;
84   - File.WriteAllText(server.MapPath(filename), Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(File.ReadAllText(server.MapPath(value))));
85   - resources.LatestScriptFile = File.GetLastWriteTime(server.MapPath(filename));
86   -
87   - // Insert the path to the minified file.
88   - resources.Scripts.Insert(0, filename);
89   - }
90   - }
91   - else
92   - {
93   - resources.Scripts.Insert(0, value);
94   - }
95   - }
96   - }
97   - else if (value.EndsWith(".css"))
98   - {
99   - if (!resources.Stylesheets.Contains(value))
100   - {
101   - // Note the latest date a file was changed.
102   - if (DateTime.Compare(resources.LatestCSSFile, info.LastWriteTime) < 0)
103   - resources.LatestCSSFile = info.LastWriteTime;
104   -
105   - resources.Stylesheets.Insert(0, value);
106   - }
107   - }
108   - }
109   - else
110   - {
111   - throw new FileNotFoundException(String.Format("Could not find file {0}", value), value);
112   - }
113   - return null;
114   - }
115   -
116   - public static MvcHtmlString RenderResources(this HtmlHelper html)
117   - {
118   - var url = new UrlHelper(html.ViewContext.RequestContext);
119   - var server = html.ViewContext.RequestContext.HttpContext.Server;
120   - var resources = (HtmlResources)html.ViewData["Resources"];
121   - string result = "";
122   -
123   - if (resources != null)
124   - {
125   - if (resources.Bundle)
126   - {
127   - // Get a hash of the files in question and generate a path.
128   - string scriptPath = scriptsFolder + BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(string.Join(";", resources.Scripts)))) + ".js";
129   - if (File.Exists(scriptPath) && DateTime.Compare(File.GetLastWriteTime(scriptPath), resources.LatestScriptFile) >= 0)
130   - {
131   - // We have already bundled the files.
132   - }
133   - else
134   - {
135   - foreach (string script in resources.Scripts)
136   - {
137   - File.AppendAllText(server.MapPath(scriptPath), File.ReadAllText(server.MapPath(script)));
138   - }
139   - }
140   - result += "<script src=\"" + url.Content(scriptPath) + "?" + String.Format("{0:yyyyddHHss}", File.GetLastWriteTime(scriptPath)) + "\" type=\"text/javascript\"></script>\n";
141   -
142   - // Get a hash of the files in question and generate a path.
143   - string cssPath = cssFolder + BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(string.Join(";", resources.Stylesheets)))) + ".css";
144   - if (File.Exists(cssPath) && DateTime.Compare(File.GetLastWriteTime(cssPath), resources.LatestCSSFile) >= 0)
145   - {
146   - // We have already bundled the files.
147   - }
148   - else
149   - {
150   - foreach (string stylesheet in resources.Stylesheets)
151   - {
152   - File.AppendAllText(server.MapPath(cssPath), File.ReadAllText(server.MapPath(stylesheet)));
153   - }
154   - }
155   - result += "<link href=\"" + url.Content(cssPath) + "?" + String.Format("{0:yyyyddHHss}", File.GetLastWriteTime(cssPath)) + "\" rel=\"stylesheet\" type=\"text/css\" />\n";
156   - }
157   - else
158   - {
159   - foreach (string resource in resources.Stylesheets)
160   - {
161   - DateTime dt = File.GetLastWriteTime(server.MapPath(resource));
162   - result += "<link href=\"" + url.Content(resource) + "?" + String.Format("{0:yyyyddHHss}", dt) + "\" rel=\"stylesheet\" type=\"text/css\" />\n";
163   - }
164   - foreach (string resource in resources.Scripts)
165   - {
166   - DateTime dt = File.GetLastWriteTime(server.MapPath(resource));
167   - result += "<script src=\"" + url.Content(resource) + "?" + String.Format("{0:yyyyddHHss}", dt) + "\" type=\"text/javascript\"></script>\n";
168   - }
169   - }
170   - }
171   -
172   - return MvcHtmlString.Create(result);
173   - }
174   - }
175   -}
62 ResourceHelper/ResourceHelper.csproj
... ... @@ -1,62 +0,0 @@
1   -<?xml version="1.0" encoding="utf-8"?>
2   -<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3   - <PropertyGroup>
4   - <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5   - <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6   - <ProductVersion>8.0.30703</ProductVersion>
7   - <SchemaVersion>2.0</SchemaVersion>
8   - <ProjectGuid>{8BFC33B8-8A8D-458B-BFEE-692629502286}</ProjectGuid>
9   - <OutputType>Library</OutputType>
10   - <AppDesignerFolder>Properties</AppDesignerFolder>
11   - <RootNamespace>ResourceHelper</RootNamespace>
12   - <AssemblyName>ResourceHelper</AssemblyName>
13   - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14   - <FileAlignment>512</FileAlignment>
15   - </PropertyGroup>
16   - <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17   - <DebugSymbols>true</DebugSymbols>
18   - <DebugType>full</DebugType>
19   - <Optimize>false</Optimize>
20   - <OutputPath>bin\Debug\</OutputPath>
21   - <DefineConstants>DEBUG;TRACE</DefineConstants>
22   - <ErrorReport>prompt</ErrorReport>
23   - <WarningLevel>4</WarningLevel>
24   - </PropertyGroup>
25   - <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26   - <DebugType>pdbonly</DebugType>
27   - <Optimize>true</Optimize>
28   - <OutputPath>bin\Release\</OutputPath>
29   - <DefineConstants>TRACE</DefineConstants>
30   - <ErrorReport>prompt</ErrorReport>
31   - <WarningLevel>4</WarningLevel>
32   - </PropertyGroup>
33   - <ItemGroup>
34   - <Reference Include="System" />
35   - <Reference Include="System.configuration" />
36   - <Reference Include="System.Core" />
37   - <Reference Include="System.Web" />
38   - <Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
39   - <Reference Include="System.Web.Routing" />
40   - <Reference Include="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
41   - <Reference Include="System.Xml.Linq" />
42   - <Reference Include="System.Data.DataSetExtensions" />
43   - <Reference Include="Microsoft.CSharp" />
44   - <Reference Include="System.Data" />
45   - <Reference Include="System.Xml" />
46   - <Reference Include="Yahoo.Yui.Compressor">
47   - <HintPath>bin\Yahoo.Yui.Compressor.dll</HintPath>
48   - </Reference>
49   - </ItemGroup>
50   - <ItemGroup>
51   - <Compile Include="HtmlHelperExtensions.cs" />
52   - <Compile Include="Properties\AssemblyInfo.cs" />
53   - </ItemGroup>
54   - <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
55   - <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
56   - Other similar extension points exist, see Microsoft.Common.targets.
57   - <Target Name="BeforeBuild">
58   - </Target>
59   - <Target Name="AfterBuild">
60   - </Target>
61   - -->
62   -</Project>
BIN  ResourceHelper/bin/Yahoo.Yui.Compressor.dll
Binary file not shown
6 TestApp.sln
@@ -5,8 +5,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.
5 5 EndProject
6 6 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp.Tests", "TestApp.Tests\TestApp.Tests.csproj", "{976E4335-1F7C-4AAD-A42F-4F6C0E8920BD}"
7 7 EndProject
8   -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceHelper", "ResourceHelper\ResourceHelper.csproj", "{8BFC33B8-8A8D-458B-BFEE-692629502286}"
9   -EndProject
10 8 Global
11 9 GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 10 Debug|Any CPU = Debug|Any CPU
@@ -21,10 +19,6 @@ Global
21 19 {976E4335-1F7C-4AAD-A42F-4F6C0E8920BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 20 {976E4335-1F7C-4AAD-A42F-4F6C0E8920BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 21 {976E4335-1F7C-4AAD-A42F-4F6C0E8920BD}.Release|Any CPU.Build.0 = Release|Any CPU
24   - {8BFC33B8-8A8D-458B-BFEE-692629502286}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25   - {8BFC33B8-8A8D-458B-BFEE-692629502286}.Debug|Any CPU.Build.0 = Debug|Any CPU
26   - {8BFC33B8-8A8D-458B-BFEE-692629502286}.Release|Any CPU.ActiveCfg = Release|Any CPU
27   - {8BFC33B8-8A8D-458B-BFEE-692629502286}.Release|Any CPU.Build.0 = Release|Any CPU
28 22 EndGlobalSection
29 23 GlobalSection(SolutionProperties) = preSolution
30 24 HideSolutionNode = FALSE
8 TestApp/TestApp.csproj
@@ -154,12 +154,6 @@
154 154 <ItemGroup>
155 155 <Content Include="packages.config" />
156 156 </ItemGroup>
157   - <ItemGroup>
158   - <ProjectReference Include="..\ResourceHelper\ResourceHelper.csproj">
159   - <Project>{8BFC33B8-8A8D-458B-BFEE-692629502286}</Project>
160   - <Name>ResourceHelper</Name>
161   - </ProjectReference>
162   - </ItemGroup>
163 157 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
164 158 <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
165 159 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -190,4 +184,4 @@
190 184 </FlavorProperties>
191 185 </VisualStudio>
192 186 </ProjectExtensions>
193   -</Project>
  187 +</Project>

No commit comments for this range

Something went wrong with that request. Please try again.