Skip to content

Commit 59132b3

Browse files
committed
Initial commit.
0 parents  commit 59132b3

32 files changed

+3490
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Bin
2+
obj
3+
Xinq.suo
4+
Xinq.csproj.user

Build/Build.Package.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe Scripts\Build.Package.proj /target:BuildPackage
2+
@PAUSE

Build/Scripts/Build.Package.proj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup>
5+
<ImportedBuildPackage>True</ImportedBuildPackage>
6+
</PropertyGroup>
7+
8+
<Import Project="Build.proj" Condition="'$(ImportedBuild)'==''" />
9+
10+
<PropertyGroup>
11+
<BuildPackagePath>$(BuildProjectsPath)\Package</BuildPackagePath>
12+
<BuildPackageBinPath>$(BuildPackagePath)\Bin</BuildPackageBinPath>
13+
<BuildPackageSourcesPath>$(BuildPackagePath)\Sources</BuildPackageSourcesPath>
14+
</PropertyGroup>
15+
16+
<Target Name="CompilePackage">
17+
<MSBuild Projects="$(BuildPackageSourcesPath)\Xinq\Xinq.csproj" Properties="Configuration=$(Configuration)" />
18+
</Target>
19+
20+
<Target Name="BuildPackage">
21+
<CallTarget Targets="CompilePackage" />
22+
</Target>
23+
24+
</Project>

Build/Scripts/Build.proj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup>
5+
<ImportedBuild>True</ImportedBuild>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<BuildPath>$(MSBuildProjectDirectory)\..</BuildPath>
10+
<BuildExtensionsPath>$(BuildPath)\Extensions</BuildExtensionsPath>
11+
<BuildRootPath>$(BuildPath)\..</BuildRootPath>
12+
<BuildProjectsPath>$(BuildRootPath)\Projects</BuildProjectsPath>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<Configuration>Release</Configuration>
17+
</PropertyGroup>
18+
19+
<PropertyGroup>
20+
<MSBuildCommunityTasksPath>$(BuildExtensionsPath)</MSBuildCommunityTasksPath>
21+
</PropertyGroup>
22+
23+
</Project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Xinq
5+
{
6+
internal abstract class Document
7+
{
8+
private bool _loadedAsReadOnly;
9+
private bool _isReadOnly;
10+
11+
public string Filename
12+
{
13+
get;
14+
private set;
15+
}
16+
17+
public bool IsDirty
18+
{
19+
get;
20+
set;
21+
}
22+
23+
public bool IsReadOnly
24+
{
25+
get
26+
{
27+
if (_loadedAsReadOnly)
28+
return true;
29+
30+
return _isReadOnly;
31+
}
32+
set
33+
{
34+
_isReadOnly = value;
35+
}
36+
}
37+
38+
public void DetermineReadOnlyMode()
39+
{
40+
_isReadOnly = ((File.GetAttributes(Filename) & FileAttributes.ReadOnly) != 0);
41+
}
42+
43+
public void Load(string filename, bool asReadOnly)
44+
{
45+
LoadDocument(filename);
46+
47+
Filename = filename;
48+
_loadedAsReadOnly = asReadOnly;
49+
IsDirty = false;
50+
51+
DetermineReadOnlyMode();
52+
}
53+
54+
public void Load(string filename)
55+
{
56+
Load(filename, false);
57+
}
58+
59+
public void Save(string filename, bool asCopy)
60+
{
61+
SaveDocument(filename);
62+
63+
if (!asCopy)
64+
{
65+
Filename = filename;
66+
IsDirty = false;
67+
68+
DetermineReadOnlyMode();
69+
}
70+
}
71+
72+
public void Save(string filename)
73+
{
74+
Save(filename, false);
75+
}
76+
77+
public void Save()
78+
{
79+
Save(Filename);
80+
}
81+
82+
public void Rename(string filename)
83+
{
84+
Filename = filename;
85+
86+
DetermineReadOnlyMode();
87+
}
88+
89+
protected abstract void LoadDocument(string filename);
90+
91+
protected abstract void SaveDocument(string filename);
92+
93+
public void Reload()
94+
{
95+
Load(Filename);
96+
}
97+
}
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Xinq
4+
{
5+
internal static class GuidList
6+
{
7+
public const string XinqPackageGuidString = "C2BD518B-C09A-401C-B9D0-93736786E488";
8+
public const string XinqEditorFactoryGuidString = "977D2DD7-0DFD-4B48-B6F5-329EE04FFB80";
9+
public const string XinqSingleFileGeneratorGuidString = "2252AA80-D1C4-4943-A0F2-BF9D75DC1096";
10+
public static readonly Guid XinqPackageGuid = new Guid(XinqPackageGuidString);
11+
public static readonly Guid XinqEditorFactoryGuid = new Guid(XinqEditorFactoryGuidString);
12+
};
13+
}
718 Bytes
Binary file not shown.
722 Bytes
Binary file not shown.
596 Bytes
Binary file not shown.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/***************************************************************************
2+
3+
Copyright (c) Microsoft Corporation. All rights reserved.
4+
This code is licensed under the Visual Studio SDK license terms.
5+
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6+
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7+
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8+
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9+
10+
***************************************************************************/
11+
12+
using System;
13+
using System.Diagnostics;
14+
using System.IO;
15+
using System.Runtime.InteropServices;
16+
using Microsoft.VisualStudio.Shell.Interop;
17+
using Microsoft.VisualStudio;
18+
19+
namespace Microsoft.VisualStudio.Shell
20+
{
21+
/// <summary>
22+
/// A managed wrapper for VS's concept of an IVsSingleFileGenerator which is
23+
/// a custom tool invoked at design time which can take any file as an input
24+
/// and provide any file as output.
25+
/// </summary>
26+
public abstract class BaseCodeGenerator : IVsSingleFileGenerator
27+
{
28+
private IVsGeneratorProgress codeGeneratorProgress;
29+
private string codeFileNameSpace = String.Empty;
30+
private string codeFilePath = String.Empty;
31+
32+
#region IVsSingleFileGenerator Members
33+
34+
/// <summary>
35+
/// Implements the IVsSingleFileGenerator.DefaultExtension method.
36+
/// Returns the extension of the generated file
37+
/// </summary>
38+
/// <param name="pbstrDefaultExtension">Out parameter, will hold the extension that is to be given to the output file name. The returned extension must include a leading period</param>
39+
/// <returns>S_OK if successful, E_FAIL if not</returns>
40+
int IVsSingleFileGenerator.DefaultExtension(out string pbstrDefaultExtension)
41+
{
42+
try
43+
{
44+
pbstrDefaultExtension = GetDefaultExtension();
45+
return VSConstants.S_OK;
46+
}
47+
catch (Exception e)
48+
{
49+
Trace.WriteLine(e.ToString());
50+
pbstrDefaultExtension = string.Empty;
51+
return VSConstants.E_FAIL;
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Implements the IVsSingleFileGenerator.Generate method.
57+
/// Executes the transformation and returns the newly generated output file, whenever a custom tool is loaded, or the input file is saved
58+
/// </summary>
59+
/// <param name="wszInputFilePath">The full path of the input file. May be a null reference (Nothing in Visual Basic) in future releases of Visual Studio, so generators should not rely on this value</param>
60+
/// <param name="bstrInputFileContents">The contents of the input file. This is either a UNICODE BSTR (if the input file is text) or a binary BSTR (if the input file is binary). If the input file is a text file, the project system automatically converts the BSTR to UNICODE</param>
61+
/// <param name="wszDefaultNamespace">This parameter is meaningful only for custom tools that generate code. It represents the namespace into which the generated code will be placed. If the parameter is not a null reference (Nothing in Visual Basic) and not empty, the custom tool can use the following syntax to enclose the generated code</param>
62+
/// <param name="rgbOutputFileContents">[out] Returns an array of bytes to be written to the generated file. You must include UNICODE or UTF-8 signature bytes in the returned byte array, as this is a raw stream. The memory for rgbOutputFileContents must be allocated using the .NET Framework call, System.Runtime.InteropServices.AllocCoTaskMem, or the equivalent Win32 system call, CoTaskMemAlloc. The project system is responsible for freeing this memory</param>
63+
/// <param name="pcbOutput">[out] Returns the count of bytes in the rgbOutputFileContent array</param>
64+
/// <param name="pGenerateProgress">A reference to the IVsGeneratorProgress interface through which the generator can report its progress to the project system</param>
65+
/// <returns>If the method succeeds, it returns S_OK. If it fails, it returns E_FAIL</returns>
66+
int IVsSingleFileGenerator.Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
67+
{
68+
if (bstrInputFileContents == null)
69+
{
70+
throw new ArgumentNullException(bstrInputFileContents);
71+
}
72+
73+
codeFilePath = wszInputFilePath;
74+
codeFileNameSpace = wszDefaultNamespace;
75+
codeGeneratorProgress = pGenerateProgress;
76+
77+
byte[] bytes = GenerateCode(bstrInputFileContents);
78+
79+
if (bytes == null)
80+
{
81+
// This signals that GenerateCode() has failed. Tasklist items have been put up in GenerateCode()
82+
rgbOutputFileContents = null;
83+
pcbOutput = 0;
84+
85+
// Return E_FAIL to inform Visual Studio that the generator has failed (so that no file gets generated)
86+
return VSConstants.E_FAIL;
87+
}
88+
else
89+
{
90+
// The contract between IVsSingleFileGenerator implementors and consumers is that
91+
// any output returned from IVsSingleFileGenerator.Generate() is returned through
92+
// memory allocated via CoTaskMemAlloc(). Therefore, we have to convert the
93+
// byte[] array returned from GenerateCode() into an unmanaged blob.
94+
95+
int outputLength = bytes.Length;
96+
rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(outputLength);
97+
Marshal.Copy(bytes, 0, rgbOutputFileContents[0], outputLength);
98+
pcbOutput = (uint)outputLength;
99+
return VSConstants.S_OK;
100+
}
101+
}
102+
103+
#endregion
104+
105+
/// <summary>
106+
/// Namespace for the file
107+
/// </summary>
108+
protected string FileNameSpace
109+
{
110+
get
111+
{
112+
return codeFileNameSpace;
113+
}
114+
}
115+
116+
/// <summary>
117+
/// File-path for the input file
118+
/// </summary>
119+
protected string InputFilePath
120+
{
121+
get
122+
{
123+
return codeFilePath;
124+
}
125+
}
126+
127+
/// <summary>
128+
/// Interface to the VS shell object we use to tell our progress while we are generating
129+
/// </summary>
130+
internal IVsGeneratorProgress CodeGeneratorProgress
131+
{
132+
get
133+
{
134+
return codeGeneratorProgress;
135+
}
136+
}
137+
138+
/// <summary>
139+
/// Gets the default extension for this generator
140+
/// </summary>
141+
/// <returns>String with the default extension for this generator</returns>
142+
protected abstract string GetDefaultExtension();
143+
144+
/// <summary>
145+
/// The method that does the actual work of generating code given the input file
146+
/// </summary>
147+
/// <param name="inputFileContent">File contents as a string</param>
148+
/// <returns>The generated code file as a byte-array</returns>
149+
protected abstract byte[] GenerateCode(string inputFileContent);
150+
151+
/// <summary>
152+
/// Method that will communicate an error via the shell callback mechanism
153+
/// </summary>
154+
/// <param name="level">Level or severity</param>
155+
/// <param name="message">Text displayed to the user</param>
156+
/// <param name="line">Line number of error</param>
157+
/// <param name="column">Column number of error</param>
158+
protected virtual void GeneratorError(uint level, string message, uint line, uint column)
159+
{
160+
IVsGeneratorProgress progress = CodeGeneratorProgress;
161+
if (progress != null)
162+
{
163+
progress.GeneratorError(0, level, message, line, column);
164+
}
165+
}
166+
167+
/// <summary>
168+
/// Method that will communicate a warning via the shell callback mechanism
169+
/// </summary>
170+
/// <param name="level">Level or severity</param>
171+
/// <param name="message">Text displayed to the user</param>
172+
/// <param name="line">Line number of warning</param>
173+
/// <param name="column">Column number of warning</param>
174+
protected virtual void GeneratorWarning(uint level, string message, uint line, uint column)
175+
{
176+
IVsGeneratorProgress progress = CodeGeneratorProgress;
177+
if (progress != null)
178+
{
179+
progress.GeneratorError(1, level, message, line, column);
180+
}
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)