private void SetTextTemplateCodeGenerator(EnvDTE.ProjectItem projectItem)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

            projectItem.SetCustomTool(null);

            if (_solution.GetCachedProjectFiles()?.Any(file => file.RelativeFilePath.Equals(T4FileName, StringComparison.OrdinalIgnoreCase)) != true)
            {
                var fullName = Path.Combine(_solution.SolutionFolder, T4FileName);
                File.WriteAllBytes(fullName, Resources.Resources_Designer_t4);
                _solution.AddFile(fullName);
            }

            // Ensure DataAnnotations is referenced, used by TT generated code.
            const string dataAnnotations = "System.ComponentModel.DataAnnotations";

            var vsProject = projectItem.ContainingProject?.Object as VSLangProj.VSProject;

            vsProject?.References?.Add(dataAnnotations);

            var fileName = Path.ChangeExtension(FilePath, "Designer.tt");

            File.WriteAllBytes(fileName, Resources.Resources_Designer_tt);

            var item = projectItem.AddFromFile(fileName);

            if (item == null)
            {
                return;
            }

            item.SetProperty(@"BuildAction", 0);

            item.RunCustomTool();
        }
Esempio n. 2
0
        private void SetTextTemplateCodeGenerator(EnvDTE.ProjectItem projectItem)
        {
            Contract.Requires(projectItem != null);

            projectItem.SetCustomTool(null);

            const string t4FileName = "Resources.Designer.t4";

            if (!_solution.GetProjectFiles().Any(file => file.RelativeFilePath.Equals(t4FileName)))
            {
                var fullName = Path.Combine(_solution.SolutionFolder, t4FileName);
                File.WriteAllBytes(fullName, Resources.Resources_Designer_t4);
                _solution.AddFile(fullName);
            }

            // Ensure DataAnnotations is referenced, used by TT generated code.
            const string dataAnnotations = "System.ComponentModel.DataAnnotations";

            var vsProject = projectItem.ContainingProject?.Object as VSLangProj.VSProject;

            vsProject?.References?.Add(dataAnnotations);

            var fileName = Path.ChangeExtension(FilePath, "Designer.tt");

            File.WriteAllBytes(fileName, Resources.Resources_Designer_tt);

            var item = projectItem.AddFromFile(fileName);

            if (item == null)
            {
                return;
            }

            item.SetProperty(@"BuildAction", 0);

            Dispatcher.BeginInvoke(() => item.RunCustomTool());
        }