Skip to content

Instantly share code, notes, and snippets.

@terurou
Last active December 14, 2015 08:29
Show Gist options
  • Save terurou/5058283 to your computer and use it in GitHub Desktop.
Save terurou/5058283 to your computer and use it in GitHub Desktop.
This tool compiles all Qt ui files that exists in the same directory.
<#
// Ui.tt - Qt ui file compilation tool for T4 Template -
// This tool compiles all Qt ui files that exists in the same directory.
// ----------------------------------------------------------------------------
// Copyright 2013 terurou (terurou at gmail.com)
//
// 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.
#>
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<# // assembly name="EnvDTE" #>
<#@ assembly name="C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\PublicAssemblies\envdte.dll" #>
<#@ assembly name="Microsoft.VisualStudio.OLE.Interop" #>
<#@ assembly name="Microsoft.VisualStudio.Shell" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="Microsoft.VisualStudio.Shell" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
var projectItem = GetTemplateProjectItem();
projectItem.ProjectItems
.Cast<EnvDTE.ProjectItem>()
.ToList()
.ForEach(x => x.Delete());
Directory.EnumerateFiles(Path.GetDirectoryName(Host.TemplateFile), "*.ui")
.ToList()
.ForEach(src => {
var dist = src + ".cs";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo()
{
FileName = "uics.exe",
Arguments = string.Format("-o \"{1}\" \"{0}\"", src, dist),
UseShellExecute = false,
CreateNoWindow = true,
};
process.Start();
process.WaitForExit();
}
projectItem.ProjectItems.AddFromFile(dist);
});
#>
<#+
EnvDTE.ProjectItem GetTemplateProjectItem()
{
var project = GetVSProject();
var found = 0;
var itemId = 0u;
var ret = 0;
ret = project.IsDocumentInProject(Host.TemplateFile, out found, new VSDOCUMENTPRIORITY[1], out itemId);
if (ret != VSConstants.S_OK || found == 0 || itemId == 0) throw new Exception();
Microsoft.VisualStudio.OLE.Interop.IServiceProvider context = null;
ret = project.GetItemContext(itemId, out context);
if (ret != VSConstants.S_OK || context == null) throw new Exception();
var provider = new ServiceProvider(context);
return provider.GetService(typeof(EnvDTE.ProjectItem)) as EnvDTE.ProjectItem;
}
IVsProject GetVSProject()
{
var dte = (Host as IServiceProvider).GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
var project = ((Array)dte.ActiveSolutionProjects).GetValue(0) as EnvDTE.Project;
var guid = XDocument.Load((project.FileName))
.Descendants()
.First(x => x.Name.LocalName == "ProjectGuid")
.Value;
return VsShellUtilities.GetHierarchy(
new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)project.DTE),
new Guid(guid)) as IVsProject;
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment