Skip to content
This repository has been archived by the owner on May 21, 2018. It is now read-only.

Commit

Permalink
New NuGet package contents
Browse files Browse the repository at this point in the history
Using a separate assembly for the attributes
  • Loading branch information
cskeppstedt committed Nov 21, 2012
1 parent 99ab1d3 commit 7e66d47
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 281 deletions.
Binary file added build/T4TS.Attributes.dll
Binary file not shown.
53 changes: 0 additions & 53 deletions build/T4TS.cs

This file was deleted.

232 changes: 4 additions & 228 deletions build/T4TS.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#@ template language="C#" debug="true" hostspecific="true"
#><#@ output extension=".cs"
<#@ template language="C#" debug="false" hostspecific="true"
#><#@ output extension=".d.ts"
#><#@ assembly name="System.Core"
#><#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0"
#><#@ assembly name="EnvDTE"
Expand All @@ -11,8 +11,7 @@
#><#@ import namespace="Microsoft.VisualStudio.Shell.Interop"
#><#@ import namespace="Microsoft.VisualStudio.TextTemplating"
#><#@ import namespace="EnvDTE"
#><#@ Include File="T4TS.tt.settings.t4"
#><# var manager = Manager.Create(Host, GenerationEnvironment); manager.StartNewFile("T4TS.d.ts"); #>
#>
/****************************************************************************
Generated by T4TS.tt - don't make any changes in this file
****************************************************************************/
Expand All @@ -31,63 +30,7 @@
}
<# } #>
<#= module.IsGlobal ? "// -- End global interfaces" : "}" #>
<# } #>
<# manager.EndBlock(); #>

namespace T4TS
{
using System;
using System.CodeDom;
using System.CodeDom.Compiler;

/// <summary>
/// Add this attribute to a class to generate a corresponding TypeScript interface.
/// </summary>
[GeneratedCode("T4TS", "1.0")]
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public class TypeScriptInterfaceAttribute: Attribute
{
/// <summary>
/// Specifies which module the interface should be placed.
/// The default module will be used if not specified.
/// </summary>
public string Module { get; set; }

/// <summary>
/// The name of the interface.
/// If not specified, the name of the class will be used.
/// </summary>
public string Name { get; set; }
}

/// <summary>
/// Add this attribute to a property to customize the generated interface member
/// </summary>
[GeneratedCode("T4TS", "1.0")]
[AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
public class TypeScriptMemberAttribute: Attribute
{
/// <summary>
/// The member name in the interface.
/// If not specified, the property name will be used.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Specify if the member should be optional, ie. "name?: type".
/// If not specified, the default value will be used.
/// </summary>
public bool Optional { get; set; }

/// <summary>
/// Specify which type the interface member will have.
/// If not specified, a suitable type will be determined.
/// </summary>
public string Type { get; set; }
}
}
<#
manager.Process(true);
<# } #><#@ Include File="T4TS.tt.settings.t4"
#><#+

List<TypeScriptModule> GetDataToRender() {
Expand Down Expand Up @@ -681,171 +624,4 @@ class CodeGenerator
return values;
}
}

// ------------------------------------------------------------------------------------------

// https://raw.github.com/damieng/DamienGKit
// http://damieng.com/blog/2009/11/06/multiple-outputs-from-t4-made-easy-revisited

// Manager class records the various blocks so it can split them up
class Manager {
private class Block {
public String Name;
public int Start, Length;
}

private Block currentBlock;
private List<Block> files = new List<Block>();
private Block footer = new Block();
private Block header = new Block();
private ITextTemplatingEngineHost host;
private StringBuilder template;
protected List<String> generatedFileNames = new List<String>();

public static Manager Create(ITextTemplatingEngineHost host, StringBuilder template) {
return (host is IServiceProvider) ? new VSManager(host, template) : new Manager(host, template);
}

public void StartNewFile(String name) {
if (name == null)
throw new ArgumentNullException("name");
CurrentBlock = new Block { Name = name };
}

public void StartFooter() {
CurrentBlock = footer;
}

public void StartHeader() {
CurrentBlock = header;
}

public void EndBlock() {
if (CurrentBlock == null)
return;
CurrentBlock.Length = template.Length - CurrentBlock.Start;
if (CurrentBlock != header && CurrentBlock != footer)
files.Add(CurrentBlock);
currentBlock = null;
}

public virtual void Process(bool split, bool sync = true) {
if (split) {
EndBlock();
String headerText = template.ToString(header.Start, header.Length);
String footerText = template.ToString(footer.Start, footer.Length);
String outputPath = Path.GetDirectoryName(host.TemplateFile);
files.Reverse();
foreach(Block block in files) {
String fileName = Path.Combine(outputPath, block.Name);
String content = headerText + template.ToString(block.Start, block.Length) + footerText;
generatedFileNames.Add(fileName);
CreateFile(fileName, content);
template.Remove(block.Start, block.Length);
}
}
}

protected virtual void CreateFile(String fileName, String content) {
if (IsFileContentDifferent(fileName, content))
File.WriteAllText(fileName, content);
}

public virtual String GetCustomToolNamespace(String fileName) {
return null;
}

public virtual String DefaultProjectNamespace {
get { return null; }
}

protected bool IsFileContentDifferent(String fileName, String newContent) {
return !(File.Exists(fileName) && File.ReadAllText(fileName) == newContent);
}

private Manager(ITextTemplatingEngineHost host, StringBuilder template) {
this.host = host;
this.template = template;
}

private Block CurrentBlock {
get { return currentBlock; }
set {
if (CurrentBlock != null)
EndBlock();
if (value != null)
value.Start = template.Length;
currentBlock = value;
}
}

private class VSManager: Manager {
private EnvDTE.ProjectItem templateProjectItem;
private EnvDTE.DTE dte;
private Action<String> checkOutAction;
private Action<IEnumerable<String>> projectSyncAction;

public override String DefaultProjectNamespace {
get {
return templateProjectItem.ContainingProject.Properties.Item("DefaultNamespace").Value.ToString();
}
}

public override String GetCustomToolNamespace(string fileName) {
return dte.Solution.FindProjectItem(fileName).Properties.Item("CustomToolNamespace").Value.ToString();
}

public override void Process(bool split, bool sync) {
if (templateProjectItem.ProjectItems == null)
return;
base.Process(split, sync);
if (sync)
projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(generatedFileNames, null, null));
}

protected override void CreateFile(String fileName, String content) {
if (IsFileContentDifferent(fileName, content)) {
CheckoutFileIfRequired(fileName);
File.WriteAllText(fileName, content);
}
}

internal VSManager(ITextTemplatingEngineHost host, StringBuilder template)
: base(host, template) {
var hostServiceProvider = (IServiceProvider) host;
if (hostServiceProvider == null)
throw new ArgumentNullException("Could not obtain IServiceProvider");
dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new ArgumentNullException("Could not obtain DTE from host");
templateProjectItem = dte.Solution.FindProjectItem(host.TemplateFile);
checkOutAction = (String fileName) => dte.SourceControl.CheckOutItem(fileName);
projectSyncAction = (IEnumerable<String> keepFileNames) => ProjectSync(templateProjectItem, keepFileNames);
}

private static void ProjectSync(EnvDTE.ProjectItem templateProjectItem, IEnumerable<String> keepFileNames) {
var keepFileNameSet = new HashSet<String>(keepFileNames);
var projectFiles = new Dictionary<String, EnvDTE.ProjectItem>();
var originalFilePrefix = Path.GetFileNameWithoutExtension(templateProjectItem.get_FileNames(0)) + ".";
foreach(EnvDTE.ProjectItem projectItem in templateProjectItem.ProjectItems)
projectFiles.Add(projectItem.get_FileNames(0), projectItem);

// Remove unused items from the project
foreach(var pair in projectFiles)
if (!keepFileNames.Contains(pair.Key) && !(Path.GetFileNameWithoutExtension(pair.Key) + ".").StartsWith(originalFilePrefix))
pair.Value.Delete();

// Add missing files to the project
foreach(String fileName in keepFileNameSet)
if (!projectFiles.ContainsKey(fileName))
templateProjectItem.ProjectItems.AddFromFile(fileName);
}

private void CheckoutFileIfRequired(String fileName) {
var sc = dte.SourceControl;
if (sc != null && sc.IsItemUnderSCC(fileName) && !sc.IsItemCheckedOut(fileName))
checkOutAction.EndInvoke(checkOutAction.BeginInvoke(fileName, null, null));
}
}
}
#>

0 comments on commit 7e66d47

Please sign in to comment.