示例#1
0
        private void AddFileToProject(Project project, AddTemplate m, string templatePath, bool runCustomTool = false)
        {
            if (File.Exists(templatePath))
            {
                var results = VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider, "'" + templatePath + "' already exists, are you sure you want to overwrite?", "Overwrite", OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNOCANCEL, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                if (results != 6)
                {
                    return;
                }

                //if the window is open we have to close it before we overwrite it.
                ProjectItem pi = project.GetProjectItem(m.Props.Template);
                pi?.Document?.Close(vsSaveChanges.vsSaveChangesNo);
            }

            var templateSamplesPath = Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates");
            var defaultTemplatePath = Path.Combine(templateSamplesPath, m.DefaultTemplate.SelectedValue.ToString());

            if (!File.Exists(defaultTemplatePath))
            {
                throw new UserException("T4Path: " + defaultTemplatePath + " is missing or you can access it.");
            }

            var dir = Path.GetDirectoryName(templatePath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            Status.Update("Adding " + templatePath + " to project");
            // When you add a TT file to visual studio, it will try to automatically compile it,
            // if there is error (and there will be error because we have custom generator)
            // the error will persit until you close Visual Studio. The solution is to add
            // a blank file, then overwrite it
            // http://stackoverflow.com/questions/17993874/add-template-file-without-custom-tool-to-project-programmatically
            var blankTemplatePath = Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates\Blank.tt");

            File.Copy(blankTemplatePath, templatePath, true);

            var p = project.ProjectItems.AddFromFile(templatePath);

            p.Properties.SetValue("CustomTool", "");

            File.Copy(defaultTemplatePath, templatePath, true);

            if (runCustomTool)
            {
                p.Properties.SetValue("CustomTool", typeof(CrmCodeGenerator2011).Name);
            }
        }
 public Configuration()
 {
     Settings                         = new CrmCodeGenerator.VSPackage.Model.Settings();
     Settings.CrmSdkUrl               = @"https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc";
     Settings.ProjectName             = "";
     Settings.Domain                  = "";
     Settings.T4Path                  = System.IO.Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates\CrmSvcUtil.tt");
     Settings.Template                = "";
     Settings.CrmOrg                  = "DEV-CRM";
     Settings.EntitiesToIncludeString = "account,contact,lead,opportunity,systemuser";
     Settings.OutputPath              = "";
     Settings.Username                = "******";
     Settings.Password                = "";
     Settings.Namespace               = "";
     Settings.Dirty                   = false;
 }
 public Configuration()
 {
     Settings = new Model.Settings
     {
         CrmSdkUrl               = @"https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc",
         ProjectName             = "",
         Domain                  = "",
         T4Path                  = System.IO.Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates\CrmSvcUtil.tt"),
         Template                = "",
         CrmOrg                  = "DEV-CRM",
         EntitiesToIncludeString = "account,contact,lead,opportunity,systemuser",
         OutputPath              = "",
         Username                = "******",
         Password                = "",
         Namespace               = "",
         Dirty = false
     };
 }
        public AddTemplate(EnvDTE80.DTE2 dte, Project project)
        {
            InitializeComponent();

            var main = dte.GetMainWindow();

            this.Owner = main;
            //Loaded += delegate { this.CenterWindow(main); };

            _Props           = new AddTemplateProp();
            this.DataContext = Props;

            var samplesPath = System.IO.Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates");
            var dir         = new DirectoryInfo(samplesPath);

            Props.TemplateList = new ObservableCollection <String>(dir.GetFiles().Select(x => x.Name).Where(x => !x.Equals("_Blank.tt")).ToArray());
            Props.Template     = "CrmSchema.tt";
            Props.Folder       = project.GetProjectDirectory();
        }
示例#5
0
        private void AddTemplate()
        {
            var dte2 = this.GetService(typeof(SDTE)) as EnvDTE80.DTE2;

            var project = dte2.GetSelectedProject();

            if (project == null || string.IsNullOrWhiteSpace(project.FullName))
            {
                throw new UserException("Please select a project first");
            }

            var m = new AddTemplate(dte2, project);

            m.Closed += (sender, e) =>
            {
                // logic here Will be called after the child window is closed
                if (((AddTemplate)sender).Canceled == true)
                {
                    return;
                }

                var templatePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(project.GetProjectDirectory(), m.Props.Template));  //GetFullpath removes un-needed relative paths  (ie if you are putting something in the solution directory)

                if (System.IO.File.Exists(templatePath))
                {
                    var results = VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider, "'" + templatePath + "' already exists, are you sure you want to overwrite?", "Overwrite", OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNOCANCEL, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    if (results != 6)
                    {
                        return;
                    }

                    //if the window is open we have to close it before we overwrite it.
                    var pi = project.GetProjectItem(m.Props.Template);
                    if (pi != null && pi.Document != null)
                    {
                        pi.Document.Close(vsSaveChanges.vsSaveChangesNo);
                    }
                }

                var templateSamplesPath = System.IO.Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates");
                var defaultTemplatePath = System.IO.Path.Combine(templateSamplesPath, m.DefaultTemplate.SelectedValue.ToString());
                if (!System.IO.File.Exists(defaultTemplatePath))
                {
                    throw new UserException("T4Path: " + defaultTemplatePath + " is missing or you can access it.");
                }

                var dir = System.IO.Path.GetDirectoryName(templatePath);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }

                Status.Update("Adding " + templatePath + " to project");
                // When you add a TT file to visual studio, it will try to automatically compile it,
                // if there is error (and there will be error because we have custom generator)
                // the error will persit until you close Visual Studio. The solution is to add
                // a blank file, then overwrite it
                // http://stackoverflow.com/questions/17993874/add-template-file-without-custom-tool-to-project-programmatically
                var blankTemplatePath = System.IO.Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates\Blank.tt");
                System.IO.File.Copy(blankTemplatePath, templatePath, true);

                var p = project.ProjectItems.AddFromFile(templatePath);
                p.Properties.SetValue("CustomTool", "");

                System.IO.File.Copy(defaultTemplatePath, templatePath, true);
                p.Properties.SetValue("CustomTool", typeof(CrmCodeGenerator2011).Name);
            };
            m.ShowModal();
        }
示例#6
0
        private async Task AddTemplateAsync()
        {
            await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);

            var dte2 = await GetServiceAsync(typeof(SDTE)) as DTE2;

            Assumes.Present(dte2);

            var project = dte2.GetSelectedProject();

            if (project == null || string.IsNullOrWhiteSpace(project.FullName))
            {
                throw new UserException("Please select a project first");
            }

            var m = new AddTemplate(dte2, project);

            m.Closed +=
                (sender, e) =>
            {
                ThreadHelper.ThrowIfNotOnUIThread();

                // logic here Will be called after the child window is closed
                if (((AddTemplate)sender).Canceled)
                {
                    return;
                }

                var templatePath = Path.GetFullPath(Path.Combine(project.GetPath(), m.Props.Template));
                //GetFullpath removes un-needed relative paths  (ie if you are putting something in the solution directory)

                if (File.Exists(templatePath))
                {
                    var results = VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider,
                                                                  "'" + templatePath + "' already exists, are you sure you want to overwrite?", "Overwrite",
                                                                  OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNOCANCEL,
                                                                  OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    if (results != 6)
                    {
                        return;
                    }

                    //if the window is open we have to close it before we overwrite it.
                    var pi = project.GetProjectItem(m.Props.Template);
                    if (pi != null && pi.Document != null)
                    {
                        pi.Document.Close(vsSaveChanges.vsSaveChangesNo);
                    }
                }

                var templateSamplesPath = Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates");
                var defaultTemplatePath = Path.Combine(templateSamplesPath, m.DefaultTemplate.SelectedValue.ToString());
                if (!File.Exists(defaultTemplatePath))
                {
                    throw new UserException("T4Path: " + defaultTemplatePath + " is missing or you can't access it.");
                }

                var dir = Path.GetDirectoryName(templatePath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                Status.Update("[Template] Adding " + templatePath + " to project ... ");
                // When you add a TT file to visual studio, it will try to automatically compile it,
                // if there is error (and there will be error because we have custom generator)
                // the error will persit until you close Visual Studio. The solution is to add
                // a blank file, then overwrite it
                // http://stackoverflow.com/questions/17993874/add-template-file-without-custom-tool-to-project-programmatically
                var blankTemplatePath = Path.Combine(DteHelper.AssemblyDirectory(), @"Resources\Templates\_Blank.tt");
                // check out file if in TFS
                try
                {
                    var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(templatePath);

                    if (workspaceInfo != null)
                    {
                        var server    = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
                        var workspace = workspaceInfo.GetWorkspace(server);
                        workspace.PendEdit(templatePath);
                        Status.Update("[Template] Checked out template file from TFS' current workspace.");
                    }
                }
                catch (Exception)
                {
                    // ignored
                }

                try
                {
                    File.Copy(blankTemplatePath, templatePath, true);
                }
                catch (Exception ex)
                {
                    var error = ex.Message + "\n" + ex.StackTrace;
                    MessageBox.Show(error, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                    throw;
                }

                Status.Update("[Template] [DONE] Adding template file to project.");

                var p = project.ProjectItems.AddFromFile(templatePath);
                p.Properties.SetValue("CustomTool", "");

                File.Copy(defaultTemplatePath, templatePath, true);
                p.Properties.SetValue("CustomTool", nameof(CrmCodeGenerator2011));
            };

            m.ShowModal();
        }