[Ignore] //UNDONE: getting assertion public void TestPublicResxFileCodeGeneratorMethod() { string filePath = @"C:\temp\dummy.resx"; string expectedClassName = System.IO.Path.GetFileNameWithoutExtension(filePath); IVsSingleFileGenerator generator = CreatePublicResxFileCodeGenerator(); Assert.IsNotNull(generator); string generatedCode = GenerateCode(generator, filePath); Assert.IsTrue(generatedCode.Contains(String.Format("Public Class {0}", expectedClassName))); }
private static string GenerateCode(IVsSingleFileGenerator generator, string filePath) { uint pcbOutput; IntPtr[] outputFileContents = new IntPtr[1]; ((Microsoft.VisualStudio.OLE.Interop.IObjectWithSite)generator).SetSite(new ResxSingleFileGeneratorSite(EnvDTE.CodeModelLanguageConstants.vsCMLanguageVB)); generator.Generate(filePath, CreateDummyResourceFile(), "", outputFileContents, out pcbOutput, null); byte[] data = new byte[pcbOutput]; System.Runtime.InteropServices.Marshal.Copy(outputFileContents[0], data, 0, (int)pcbOutput); return(System.Text.Encoding.UTF8.GetString(data)); }
private void TestGenerate <G>(string language, out int hr, out IntPtr[] fileContents, out uint bytesOutput, out System.CodeDom.CodeCompileUnit compileUnit, out System.CodeDom.CodeTypeDeclaration typeDeclaration) where G : Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGeneratorBase, new() { SettingsSingleFileGeneratorSite site = new SettingsSingleFileGeneratorSite(language); string inputFile = site.DefaultSettingsFilePath; Mock <G> generatorMock = new Mock <G>(); System.CodeDom.CodeCompileUnit actualCompileUnit = null; System.CodeDom.CodeTypeDeclaration actualTypeDeclaration = null; generatorMock.Implement("GetProjectRootNamespace", "MyLittleNamespace"); generatorMock.Implement("AddRequiredReferences", new object[] { (Shell.Interop.IVsGeneratorProgress)null }); generatorMock.Implement("OnCompileUnitCreated", new object[] { MockConstraint.IsAnything <System.CodeDom.CodeCompileUnit>(), MockConstraint.IsAnything <System.CodeDom.CodeTypeDeclaration>() }, delegate(object obj, System.Reflection.MethodInfo method, object[] arguments) { actualCompileUnit = (System.CodeDom.CodeCompileUnit)arguments[0]; actualTypeDeclaration = (System.CodeDom.CodeTypeDeclaration)arguments[1]; return(null); }); IVsSingleFileGenerator generator = generatorMock.Instance; ((Microsoft.VisualStudio.OLE.Interop.IObjectWithSite)generator).SetSite(site); fileContents = new IntPtr[] { new IntPtr() }; bytesOutput = 0; hr = generator.Generate(inputFile, "", "", fileContents, out bytesOutput, null); compileUnit = actualCompileUnit; typeDeclaration = actualTypeDeclaration; }
/// <summary> /// Creates an instance of the single file generator requested /// </summary> /// <param name="progId">prog id of the generator to be created. For e.g HKLM\SOFTWARE\Microsoft\VisualStudio\9.0Exp\Generators\[prjfacguid]\[wszProgId]</param> /// <param name="generatesDesignTimeSource">GeneratesDesignTimeSource key value</param> /// <param name="generatesSharedDesignTimeSource">GeneratesSharedDesignTimeSource key value</param> /// <param name="useTempPEFlag">UseDesignTimeCompilationFlag key value</param> /// <param name="generate">IVsSingleFileGenerator interface</param> /// <returns>S_OK if succesful</returns> public virtual int CreateGeneratorInstance(string progId, out int generatesDesignTimeSource, out int generatesSharedDesignTimeSource, out int useTempPEFlag, out IVsSingleFileGenerator generate) { Guid genGuid; ErrorHandler.ThrowOnFailure(this.GetGeneratorInformation(progId, out generatesDesignTimeSource, out generatesSharedDesignTimeSource, out useTempPEFlag, out genGuid)); //Create the single file generator and pass it out. Check to see if it is in the cache if (!this.generatorsMap.ContainsKey(progId) || ((this.generatorsMap[progId]).Generator == null)) { Guid riid = VSConstants.IID_IUnknown; uint dwClsCtx = (uint)CLSCTX.CLSCTX_INPROC_SERVER; IntPtr genIUnknown = IntPtr.Zero; //create a new one. ErrorHandler.ThrowOnFailure(this.LocalRegistry.CreateInstance(genGuid, null, ref riid, dwClsCtx, out genIUnknown)); if (genIUnknown != IntPtr.Zero) { try { object generator = Marshal.GetObjectForIUnknown(genIUnknown); //Build the generator meta data object and cache it. GeneratorMetaData genData = new GeneratorMetaData(); genData.GeneratesDesignTimeSource = generatesDesignTimeSource; genData.GeneratesSharedDesignTimeSource = generatesSharedDesignTimeSource; genData.UseDesignTimeCompilationFlag = useTempPEFlag; genData.GeneratorClsid = genGuid; genData.Generator = generator; this.generatorsMap[progId] = genData; } finally { Marshal.Release(genIUnknown); } } } generate = (this.generatorsMap[progId]).Generator as IVsSingleFileGenerator; return(VSConstants.S_OK); }
/// <summary> /// Creates an instance of the single file generator requested /// </summary> /// <param name="progId">prog id of the generator to be created. For e.g HKLM\SOFTWARE\Microsoft\VisualStudio\9.0Exp\Generators\[prjfacguid]\[wszProgId]</param> /// <param name="generatesDesignTimeSource">GeneratesDesignTimeSource key value</param> /// <param name="generatesSharedDesignTimeSource">GeneratesSharedDesignTimeSource key value</param> /// <param name="useTempPEFlag">UseDesignTimeCompilationFlag key value</param> /// <param name="generate">IVsSingleFileGenerator interface</param> /// <returns>S_OK if succesful</returns> public virtual int CreateGeneratorInstance(string progId, out int generatesDesignTimeSource, out int generatesSharedDesignTimeSource, out int useTempPEFlag, out IVsSingleFileGenerator generate) { Guid genGuid; ErrorHandler.ThrowOnFailure(this.GetGeneratorInformation(progId, out generatesDesignTimeSource, out generatesSharedDesignTimeSource, out useTempPEFlag, out genGuid)); //Create the single file generator and pass it out. Check to see if it is in the cache if(!this.generatorsMap.ContainsKey(progId) || ((this.generatorsMap[progId]).Generator == null)) { Guid riid = VSConstants.IID_IUnknown; uint dwClsCtx = (uint)CLSCTX.CLSCTX_INPROC_SERVER; IntPtr genIUnknown = IntPtr.Zero; //create a new one. ErrorHandler.ThrowOnFailure(this.LocalRegistry.CreateInstance(genGuid, null, ref riid, dwClsCtx, out genIUnknown)); if(genIUnknown != IntPtr.Zero) { try { object generator = Marshal.GetObjectForIUnknown(genIUnknown); //Build the generator meta data object and cache it. GeneratorMetaData genData = new GeneratorMetaData(); genData.GeneratesDesignTimeSource = generatesDesignTimeSource; genData.GeneratesSharedDesignTimeSource = generatesSharedDesignTimeSource; genData.UseDesignTimeCompilationFlag = useTempPEFlag; genData.GeneratorClsid = genGuid; genData.Generator = generator; this.generatorsMap[progId] = genData; } finally { Marshal.Release(genIUnknown); } } } generate = (this.generatorsMap[progId]).Generator as IVsSingleFileGenerator; return VSConstants.S_OK; }
public static int Main(string[] args) { // Validate the argument. if (args == null) { throw new ArgumentNullException(nameof(args)); } // These are the parameters that are parsed out of the command line. string inputFilePath = string.Empty; string outputFileName = string.Empty; string targetNamespace = "DefaultNamespace"; try { // The command line parser is driven by different states that are triggered by the flags read. Unless a flag has been parsed, the command line // parser assumes that it's reading the file name from the command line. ArgumentState argumentState = ArgumentState.InputFileName; // Parse the command line for arguments. foreach (string argument in args) { // Use the dictionary to transition from one state to the next based on the input parameters. ArgumentState nextArgumentState; if (Program.ArgumentStates.TryGetValue(argument, out nextArgumentState)) { argumentState = nextArgumentState; continue; } // The parsing state will determine which variable is read next. switch (argumentState) { case ArgumentState.InputFileName: // Expand the environment variables so that paths don't need to be absolute. inputFilePath = Environment.ExpandEnvironmentVariables(argument); // The output name defaults to the input file name with a new extension. outputFileName = $"{Path.GetFileNameWithoutExtension(inputFilePath)}.cs"; break; case ArgumentState.OutputFileName: // Expand the environment variables so that paths don't need to be absolute. outputFileName = Environment.ExpandEnvironmentVariables(argument); break; case ArgumentState.TargetNamespace: // This is the namespace that is used to create the target data model. targetNamespace = argument; break; } // The default state is to look for the input file name on the command line. argumentState = ArgumentState.InputFileName; } // This will read the input XML schema into a large buffer. string fileContents; using (StreamReader streamReader = new StreamReader(inputFilePath)) { fileContents = streamReader.ReadToEnd(); } // This next step simulates the calling convention used by a custom tool, which is to say it uses unmanaged code. The progress indicator is used // to provide feedback, to either the IDE or the command line, about how far along the compilation is. Note that the calling interface is from // unmanaged-code to managed code, the way you would expect the IDE to call the generator, so we create an unmanaged buffer pointer for the results. IntPtr[] buffer = new IntPtr[1]; uint bufferSize; Generator generator = new Generator(); IVsSingleFileGenerator ivsSingleFileGenerator = generator as IVsSingleFileGenerator; ivsSingleFileGenerator.Generate(inputFilePath, fileContents, targetNamespace, buffer, out bufferSize, null); // Once the buffer of source code is generated, it is copied back out of the unmanaged buffers and written to the output file. byte[] outputBuffer = new byte[bufferSize]; Marshal.Copy(buffer[0], outputBuffer, 0, Convert.ToInt32(bufferSize)); using (StreamWriter streamWriter = new StreamWriter(outputFileName)) { streamWriter.Write(Encoding.UTF8.GetString(outputBuffer)); } } catch (ArgumentException argumentException) { // This will catch any generic errors and dump them to the console. Console.WriteLine(argumentException.Message); } // The execution at this point was a success. return(0); }
public int CreateGeneratorInstance(string wszProgId, out int pbGeneratesDesignTimeSource, out int pbGeneratesSharedDesignTimeSource, out int pbUseTempPEFlag, out IVsSingleFileGenerator ppGenerate) { // The only user in the project system does not call this method. pbGeneratesDesignTimeSource = 0; pbGeneratesSharedDesignTimeSource = 0; pbUseTempPEFlag = 0; ppGenerate = null; return(VSConstants.E_NOTIMPL); }
/// <summary> /// Initializes a new instance of the /// <see cref="GlobalDataSetGenerator"/> class. /// </summary> public GlobalDataSetGenerator() { this.msDataSetGenerator = GetGenerator(MSDataSetGeneratorGuid); this.msDataSetGeneratorRN = (IVsRefactorNotify)this.msDataSetGenerator; this.msDataSetGeneratorSite = (IObjectWithSite)this.msDataSetGenerator; }
public void CheckForIVsSingleFileGenerator() { IVsSingleFileGenerator sfg = Generator as IVsSingleFileGenerator; Assert.IsNotNull(sfg); }
int IVsSingleFileGeneratorFactory.CreateGeneratorInstance( string wszProgId, out int pbGeneratesDesignTimeSource, out int pbGeneratesSharedDesignTimeSource, out int pbUseTempPEFlag, out IVsSingleFileGenerator ppGenerate ) { int hr = Utility.COM_HResults.S_OK; //what are these used for? pbGeneratesDesignTimeSource = Utility.Boolean.TRUE; pbGeneratesSharedDesignTimeSource = Utility.Boolean.FALSE; pbUseTempPEFlag = Utility.Boolean.FALSE; ppGenerate = null; NameValueCollection coll = ParseGeneratorProgID( ref wszProgId ); switch(wszProgId) { case ResXFileCodeGenerator.c_Name: ResXFileCodeGenerator resxGenerator = new ResXFileCodeGenerator(this); ppGenerate = resxGenerator; if(coll != null) { foreach(string name in coll.AllKeys) { string value = coll[name]; bool fValue; switch(name.Trim().ToLower()) { case "nestedenums": if(bool.TryParse( value, out fValue )) resxGenerator.m_fNestedEnums = fValue; break; case "internal": if(bool.TryParse( value, out fValue )) resxGenerator.m_fInternal = fValue; break; case "mscorlib": if(bool.TryParse( value, out fValue )) resxGenerator.m_fMscorlib = fValue; break; } } } break; default: hr = Utility.COM_HResults.E_FAIL; if(m_innerSingleFileGeneratorFactory != null) { hr = m_innerSingleFileGeneratorFactory.CreateGeneratorInstance( wszProgId, out pbGeneratesDesignTimeSource, out pbGeneratesSharedDesignTimeSource, out pbUseTempPEFlag, out ppGenerate ); } break; } return hr; }