Esempio n. 1
0
        void AddAsyncInterface(object sender, EventArgs e)
        {
            var cached = new CachedItems(".Tasks.cs");

            if (!cached.HasInterface)
            {
                MessageBox(Resources.AddAsyncInterface, Resources.CannotGenerateAsyncInterface);
                return;
            }

            // fire-up the T4 engine and generate the text
            var t4 = ServiceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;

            if (t4 == null)
            {
                return;
            }

            var t4SessionHost = t4 as ITextTemplatingSessionHost;

            t4SessionHost.Session = t4SessionHost.CreateSession();
            t4SessionHost.Session["sourcePathName"] = cached.SourcePathName;
            t4SessionHost.Session["targetPathName"] = cached.TargetPathName;

            var generatedText = ProcessTemplate(t4, "InterfaceTasks.tt");

            // create the new file with the generated text and add it to the project:
            File.WriteAllText(cached.TargetPathName, generatedText);

            cached.SourceProjectItem
            .ContainingProject
            .ProjectItems
            .AddFromFile(cached.TargetPathName);
        }
Esempio n. 2
0
        void AddClient(object sender, EventArgs e)
        {
            var cached = new CachedItems(".Client.cs");

            if (!cached.HasInterface)
            {
                MessageBox(Resources.AddClient, Resources.CannotGenerateClient);
                return;
            }

            string baseName;

            if (cached.SourceInterface.Name.StartsWith("I", StringComparison.Ordinal))
            {
                baseName = cached.SourceInterface.Name.Substring(1);
            }
            else
            {
                baseName = cached.SourceInterface.Name;
            }

            // fire-up the T4 engine and generate the text
            var t4 = ServiceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;

            if (t4 == null)
            {
                return;
            }

            var t4SessionHost = t4 as ITextTemplatingSessionHost;

            t4SessionHost.Session                   = t4SessionHost.CreateSession();
            t4SessionHost.Session["baseName"]       = baseName;
            t4SessionHost.Session["sourcePathName"] = cached.SourcePathName;
            t4SessionHost.Session["targetPathName"] = Path.Combine(
                Path.GetDirectoryName(cached.SourcePathName),
                baseName + ".Client.cs");

            var generatedText = ProcessTemplate(t4, "LightClient.tt");

            // create the new file with the generated text and add it to the project:
            File.WriteAllText(cached.TargetPathName, generatedText);

            cached.SourceProjectItem
            .ContainingProject
            .ProjectItems
            .AddFromFile(cached.TargetPathName);
        }
Esempio n. 3
0
        void AddDto(object sender, EventArgs e)
        {
            var cached = new CachedItems(".Dto.cs");

            if (!cached.HasClass)
            {
                MessageBox(Resources.AddMetadataType, Resources.CannotGenerateDto);
                return;
            }

            var project = cached.SourceProjectItem.ContainingProject;

            // add reference to the System.Runtime.Serialization.dll
            var vsProject = project.Object as VSProject;

            if (!vsProject.References.OfType <Reference>().Any(r => r.Name == "System.Runtime.Serialization"))
            {
                vsProject.References.Add("System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            }

            // fire-up the T4 engine and generate the text
            var t4 = ServiceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;

            if (t4 == null)
            {
                return;
            }

            var t4SessionHost = t4 as ITextTemplatingSessionHost;

            t4SessionHost.Session = t4SessionHost.CreateSession();
            t4SessionHost.Session["sourcePathName"] = cached.SourcePathName;
            t4SessionHost.Session["targetPathName"] = cached.TargetPathName;

            var generatedText = ProcessTemplate(t4, "ClassDataContract.tt");

            // create the new file with the generated text and add it to the project:
            File.WriteAllText(cached.TargetPathName, generatedText);

            project.ProjectItems.AddFromFile(cached.TargetPathName);
        }
Esempio n. 4
0
        void AddMetadataType(object sender, EventArgs e)
        {
            var cached = new CachedItems(".Metadata.cs");

            if (!cached.HasClass)
            {
                MessageBox(Resources.AddMetadataType, Resources.CannotGenerateMetadataType);
                return;
            }

            var project = cached.SourceProjectItem.ContainingProject;

            // edit the source file:
            // 1. add reference to the System.ComponentModel.DataAnnotations.dll
            var vsProject = project.Object as VSProject;

            if (!vsProject.References.OfType <Reference>().Any(r => r.Name == "System.ComponentModel.DataAnnotations"))
            {
                vsProject.References.Add("System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            }

            // 2. add using System.ComponentModel.DataAnnotation
            if (!cached.SourceCodeModel.CodeElements.OfType <CodeImport>().Any(i => i.Namespace == "System.ComponentModel.DataAnnotations"))
            {
                cached.SourceCodeModel.AddImport("System.ComponentModel.DataAnnotations");
            }

            // 3. add a MetadataAttribute to the source class
            if (!cached.SourceClass.Attributes.OfType <CodeAttribute2>().Any(a => a.Name == "MetadataType"))
            {
                cached.SourceClass.AddAttribute("MetadataType", string.Format(CultureInfo.InvariantCulture, "typeof({0}Metadata)", cached.SourceClass.Name));
            }

            if (cached.SourceClass.ClassKind != vsCMClassKind.vsCMClassKindPartialClass)
            {
                // make the source class partial - required by classes with metadata types:
                cached.SourceProjectItem.Open(EnvDTE.Constants.vsViewKindPrimary);

                var point = cached.SourceClass
                            .GetStartPoint(vsCMPart.vsCMPartHeader)
                            .CreateEditPoint();

                point.FindPattern("class ", 0, ref point);
                point.CharLeft("class ".Length);
                point.Insert("partial ");
            }

            // fire-up the T4 engine and generate the text
            var t4 = ServiceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;

            if (t4 == null)
            {
                return;
            }

            var t4SessionHost = t4 as ITextTemplatingSessionHost;

            t4SessionHost.Session = t4SessionHost.CreateSession();
            t4SessionHost.Session["sourcePathName"] = cached.SourcePathName;
            t4SessionHost.Session["targetPathName"] = cached.TargetPathName;

            var generatedText = ProcessTemplate(t4, "ClassMetadata.tt");

            // create the new file with the generated text and add it to the project:
            File.WriteAllText(cached.TargetPathName, generatedText);

            project.ProjectItems.AddFromFile(cached.TargetPathName);
        }