Skip to content

Commit

Permalink
Added Visual Studio Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JDemler committed Aug 18, 2017
1 parent 63ffb82 commit 2a05d2e
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CompiledHandlebars.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompiledHandlebars.ViewEngine.Core", "ViewEngine.Core\CompiledHandlebars.ViewEngine.Core.csproj", "{A023239D-139B-49E3-8875-14E763A49946}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompiledHandlebars.RuntimeUtils", "RuntimeUtils\CompiledHandlebars.RuntimeUtils.csproj", "{9AC7E389-AA9F-4E07-ABA0-2112C449CCC6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompiledHandleBars.CompilerVSIX", "CompilerVSIX\CompiledHandleBars.CompilerVSIX.csproj", "{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -111,6 +112,14 @@ Global
{9AC7E389-AA9F-4E07-ABA0-2112C449CCC6}.Release|Any CPU.Build.0 = Release|Any CPU
{9AC7E389-AA9F-4E07-ABA0-2112C449CCC6}.Release|x86.ActiveCfg = Release|Any CPU
{9AC7E389-AA9F-4E07-ABA0-2112C449CCC6}.Release|x86.Build.0 = Release|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Debug|x86.ActiveCfg = Debug|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Debug|x86.Build.0 = Debug|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Release|Any CPU.Build.0 = Release|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Release|x86.ActiveCfg = Release|Any CPU
{23FA694C-AF99-4E50-AD16-8DF60EE82DEC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
288 changes: 288 additions & 0 deletions CompilerVSIX/CompiledHandleBars.CompilerVSIX.csproj

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions CompilerVSIX/HandlebarsCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using CompiledHandlebars.Compiler;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace CompiledHandleBarsCompilerVSIX
{
[ComVisible(true)]
[Guid("F84ABD20-9386-40D5-A4A0-4443B7206EC5")]
[CodeGeneratorRegistration(
typeof(HandlebarsCompiler),
"HandlebarsCompiler",
vsContextGuidVCSProject,
GeneratesDesignTimeSource = true)]
[ProvideObject(typeof(HandlebarsCompiler),
RegisterUsing = RegistrationMethod.CodeBase)]
public class HandlebarsCompiler : IVsSingleFileGenerator
{
public const string vsContextGuidVCSProject = "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}";

#region IVsSingleFileGenerator

public int DefaultExtension(out string pbstrDefaultExtension)
{
pbstrDefaultExtension = ".hbs.cs";
return VSConstants.S_OK;
}

public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
{
#if DEBUG
var sw = new Stopwatch();
sw.Start();
#endif
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
//TODO: fix casting issue in the following line (roslyvisualstudioworkspace -> visualstudioworkspace)
var workspace = (Workspace)componentModel.GetService<VisualStudioWorkspace>();
var docIds = workspace.CurrentSolution.GetDocumentIdsWithFilePath(wszInputFilePath);
var project = FindContainingProject(workspace.CurrentSolution.Projects.ToList(), wszInputFilePath);
var compilationResult = HbsCompiler.Compile(bstrInputFileContents, wszDefaultNamespace, Path.GetFileNameWithoutExtension(wszInputFilePath), project);
#if DEBUG
sw.Stop();
#endif
if (compilationResult.Item2.Any())
{
foreach (var error in compilationResult.Item2)
pGenerateProgress.GeneratorError(0, 1, error.Message, (uint)error.Line - 1, (uint)error.Column - 1);
}
byte[] bytes = Encoding.UTF8.GetBytes(compilationResult.Item1);
rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(bytes.Length);
Marshal.Copy(bytes, 0, rgbOutputFileContents[0], bytes.Length);
pcbOutput = (uint)bytes.Length;
return compilationResult.Item2.Any() ? VSConstants.E_FAIL : VSConstants.S_OK;
}

#endregion

private Project FindContainingProject(List<Project> projects, string filePath)
{
foreach (var project in projects)
{
//Assumption: The file is in the directory of the project...
//This assumption is probably not always true. So:
//TODO: Find a way to solve this problem correctly
var dirPath = Path.GetDirectoryName(project.FilePath);
if (filePath.StartsWith(dirPath))
return project;
}
return null;
}
}
}
Binary file added CompilerVSIX/Key.snk
Binary file not shown.
Binary file not shown.
33 changes: 33 additions & 0 deletions CompilerVSIX/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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("CompiledHandleBars.CompilerVSIX")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CompiledHandleBars.CompilerVSIX")]
[assembly: AssemblyCopyright("")]
[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)]

// 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("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
63 changes: 63 additions & 0 deletions CompilerVSIX/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Thread" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.FileVersionInfo" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.VisualStudio.Threading" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-15.3.0.0" newVersion="15.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Composition.AttributedModel" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.31.0" newVersion="1.0.31.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces.Desktop" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Binary file not shown.
83 changes: 83 additions & 0 deletions CompilerVSIX/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EnvDTE" version="8.0.1" targetFramework="net46" />
<package id="ManagedEsent" version="1.9.4" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.Common" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.CSharp" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.Elfie" version="0.10.6" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.VisualBasic" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.Composition" version="1.0.31" targetFramework="net46" />
<package id="Microsoft.VisualStudio.ComponentModelHost" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.CoreUtility" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Imaging" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.LanguageServices" version="2.3.0-beta3" targetFramework="net46" />
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6070" targetFramework="net46" />
<package id="Microsoft.VisualStudio.SDK.EmbedInteropTypes" version="15.0.9" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Shell.15.0" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Shell.Framework" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Shell.Interop" version="7.10.6071" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Shell.Interop.15.3.DesignTime" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Shell.Interop.8.0" version="8.0.50727" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Shell.Interop.9.0" version="9.0.30729" targetFramework="net46" />
<package id="Microsoft.VisualStudio.TextManager.Interop" version="7.10.6070" targetFramework="net46" />
<package id="Microsoft.VisualStudio.TextManager.Interop.8.0" version="8.0.50727" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Threading" version="15.3.23" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Utilities" version="15.0.26606" targetFramework="net46" />
<package id="Microsoft.VisualStudio.Validation" version="15.3.15" targetFramework="net46" />
<package id="Microsoft.VSSDK.BuildTools" version="15.1.192" targetFramework="net46" developmentDependency="true" />
<package id="stdole" version="7.0.3301" targetFramework="net46" />
<package id="System.AppContext" version="4.3.0" targetFramework="net46" />
<package id="System.Collections" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net46" />
<package id="System.Composition" version="1.0.31" targetFramework="net46" />
<package id="System.Composition.AttributedModel" version="1.0.31" targetFramework="net46" />
<package id="System.Composition.Convention" version="1.0.31" targetFramework="net46" />
<package id="System.Composition.Hosting" version="1.0.31" targetFramework="net46" />
<package id="System.Composition.Runtime" version="1.0.31" targetFramework="net46" />
<package id="System.Composition.TypedParts" version="1.0.31" targetFramework="net46" />
<package id="System.Console" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net46" />
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net46" />
<package id="System.Globalization" version="4.3.0" targetFramework="net46" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Linq" version="4.3.0" targetFramework="net46" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net46" />
<package id="System.Reflection" version="4.3.0" targetFramework="net46" />
<package id="System.Reflection.Metadata" version="1.4.2" targetFramework="net46" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding.CodePages" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Threading" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net46" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XPath" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net46" />
<package id="VSLangProj" version="7.0.3300" targetFramework="net46" />
<package id="VSLangProj2" version="7.0.5000" targetFramework="net46" />
<package id="VSLangProj80" version="8.0.50727" targetFramework="net46" />
</packages>
26 changes: 26 additions & 0 deletions CompilerVSIX/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="HandlebarsCompiler.Jakob Demler.71222594-41cc-4ba2-b691-ad7c2783a83b" Version="0.3.2" Language="en-US" Publisher="Jakob Demler" />
<DisplayName>HandlebarsCompiler</DisplayName>
<Description xml:space="preserve">Compiles Handlebars Templates into performant and typechecked C# Code</Description>
<MoreInfo>https://github.com/Noxum/CompiledHandlebars</MoreInfo>
<Tags>Handlebars Html Rendering</Tags>
</Metadata>
<Installation>
<InstallationTarget Version="[15.0,16.0)" Id="Microsoft.VisualStudio.Pro" />
<InstallationTarget Version="[15.0,16.0)" Id="Microsoft.VisualStudio.Community" />
<InstallationTarget Version="[15.0,16.0)" Id="Microsoft.VisualStudio.Enterprise" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
</Dependencies>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="File" Path="Microsoft.CodeAnalysis.Workspaces.dll" AssemblyName="Microsoft.CodeAnalysis.Workspaces, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="File" Path="microsoft.visualstudio.languageservices.dll" AssemblyName="Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</Assets>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,16.0)" DisplayName="Visual Studio core editor" />
</Prerequisites>
</PackageManifest>

0 comments on commit 2a05d2e

Please sign in to comment.