Esempio n. 1
0
        private void GenerateClick(object sender, RoutedEventArgs e)
        {
            var originalCursor = Cursor;

            try
            {
                Cursor = Cursors.Wait;
                _packageInstaller.InstallPackage(_project, "WAQS.Server", _packageInstallerServices);

                var edmxPath   = edmx.SelectedValue as string;
                var appKind    = _project.Properties.Cast <EnvDTE.Property>().Any(p => p.Name.StartsWith("WebApplication")) ? "Web" : "App";
                var netVersion = _project.GetNetVersion();
                var kind       = (GenerationOptions.KindViewModel)generationOptions.SelectedItem;

                var                projectDirectoryPath = Path.GetDirectoryName(_project.FullName);
                string             waqsDirectory;
                string             edmxName        = null;
                EnvDTE.ProjectItem edmxProjectItem = null;
                string             edmxProjectPath = null;
                if (kind.Kind == GenerationOptions.Kind.FrameworkOnly)
                {
                    waqsDirectory = Path.Combine(projectDirectoryPath, "WAQS.Framework");
                }
                else
                {
                    if (!(edmxPath.EndsWith(".edmx") && File.Exists(edmxPath)))
                    {
                        ShowError("Edmx path is not correct");
                        return;
                    }
                    edmxName        = Path.GetFileNameWithoutExtension(edmxPath);
                    waqsDirectory   = Path.Combine(projectDirectoryPath, "WAQS." + edmxName);
                    edmxProjectItem = _dte.Solution.FindProjectItem(edmxPath);
                    edmxProjectPath = edmxProjectItem.ContainingProject.FullName;
                }
                if (Directory.Exists(waqsDirectory))
                {
                    ShowError(waqsDirectory + "already exists");
                    return;
                }

                var projectUIHierarchyItems    = _dte.GetProjectsUIHierarchyItems().First(uihi => ((EnvDTE.Project)uihi.Object).FullName == _project.FullName).UIHierarchyItems;
                var referencesUIHierarchyItems = projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().First(uihi => uihi.Name == "References").UIHierarchyItems;
                var referencesExpanded         = referencesUIHierarchyItems.Expanded;

                var            toolsPath        = Path.Combine(_packageInstallerServices.GetPackageLocation("WAQS.Server"), "tools");
                var            toolsPathServer  = Path.Combine(toolsPath, "Server");
                var            defaultNamespace = _project.GetDefaultNamespace();
                EnvDTE.Project fxProject;
                if (kind.Kind == GenerationOptions.Kind.GlobalOnly || kind.Kind == GenerationOptions.Kind.WithoutGlobalWithoutFramework)
                {
                    fxProject = _dte.Solution.FindProjectItem("ExpressionExtension.cs").ContainingProject;
                }
                else
                {
                    fxProject = _project;
                }
                var assemblyName    = (string)fxProject.Properties.Cast <EnvDTE.Property>().First(p => p.Name == "AssemblyName").Value;
                var assemblyVersion = (string)fxProject.Properties.Cast <EnvDTE.Property>().First(p => p.Name == "AssemblyVersion").Value;
                var references      = ((VSProject)_project.Object).References;
                references.Add("System");
                references.Add("System.Configuration");
                references.Add("System.Core");
                references.Add("System.Data");
                references.Add("System.Runtime.Serialization");
                references.Add("System.ServiceModel");
                references.Add("System.ServiceModel.Activation");
                references.Add("System.ServiceModel.Channels");
                references.Add("System.Transactions");
                references.Add("System.Web");
                references.Add("System.Xml");
                _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "Unity", "3.5.1404", false);
                _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "CommonServiceLocator", "1.3.0", false);
                _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "EntityFramework", "6.1.3", false);
                try
                {
                    referencesUIHierarchyItems.Expanded = referencesExpanded;
                }
                catch
                {
                }
                bool   withGlobal       = (kind.Kind & GenerationOptions.Kind.GlobalOnly) != 0;
                var    globalDirectory  = Path.Combine(projectDirectoryPath, "Global");
                string webConfigPath    = null;
                string globalAsaxPath   = null;
                string globalAsaxCsPath = null;
                string globalWCFService = null;
                if (withGlobal)
                {
                    webConfigPath = Path.Combine(projectDirectoryPath, "Web.config");
                    if (File.Exists(webConfigPath))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(webConfigPath);
                        }
                        catch
                        {
                        }
                    }
                    globalAsaxPath = Path.Combine(projectDirectoryPath, "Global.asax");
                    if (File.Exists(globalAsaxPath))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(globalAsaxPath);
                        }
                        catch
                        {
                        }
                    }
                    globalAsaxCsPath = Path.Combine(projectDirectoryPath, "Global.asax.cs");
                    if (File.Exists(globalAsaxCsPath))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(globalAsaxCsPath);
                        }
                        catch
                        {
                        }
                    }
                    globalWCFService = Path.Combine(projectDirectoryPath, "GlobalWCFService.cs");
                    if (File.Exists(globalWCFService))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(globalWCFService);
                        }
                        catch
                        {
                        }
                    }
                }
                string vsVersion;
                switch (_dte.Version)
                {
                case "12.0":
                    vsVersion = "VS12";
                    break;

                case "14.0":
                default:
                    vsVersion = "VS14";
                    break;
                }
                var    exePath = Path.Combine(toolsPathServer, "InitWAQSServer.exe");
                string specificationsFolder = null;
                string dtoFolder            = null;
                var    exeArgs = new StringBuilder("\"" + edmxPath + "\" \"" + edmxProjectPath + "\" \"" + projectDirectoryPath + "\" \"" + toolsPathServer + "\" \"" + defaultNamespace + "\" \"" + assemblyName + "\" \"" + assemblyVersion + "\" \"" + netVersion + "\" \"" + vsVersion + "\" \"" + kind.Key + "\" \"" + appKind + "\" \"" + waqsDirectory + "\" \"" + (edmxPath == null ? "" : _dte.Solution.FindProjectItem(edmxPath).ContainingProject.ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name == "App.Config")?.GetFilePath()) + "\" " + (copyTemplates.IsChecked == true ? "WithSourceControl" : "WithoutSourceControl") + " \"" + _dte.Solution.FullName + "\" WCF");
                if (kind.Kind == GenerationOptions.Kind.GlobalOnly)
                {
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem(edmxName + ".Server.DAL.Interfaces.tt").GetFirstCsFilePath() + "\"");
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem(edmxName + ".Server.DAL.tt").GetFirstCsFilePath() + "\"");
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem("I" + edmxName + "Service.cs").GetFilePath() + "\"");
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem(edmxName + "Service.cs").GetFilePath() + "\"");
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem("I" + edmxName + "WCFService.cs").GetFilePath() + "\"");
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem(edmxName + "WCFService.cs").GetFilePath() + "\"");
                    exeArgs.Append(" \"" + edmxProjectPath + "\"");
                    var edmxProjectFolderPath = Path.GetDirectoryName(edmxProjectPath);
                    exeArgs.Append(" \"" + Path.Combine(edmxProjectFolderPath, "Specifications") + "\"");
                    exeArgs.Append(" \"" + Path.Combine(edmxProjectFolderPath, "DTO") + "\"");
                }
                else if (kind.Kind != GenerationOptions.Kind.FrameworkOnly)
                {
                    exeArgs.Append(" \"" + _project.FullName + "\"");
                    specificationsFolder = Path.Combine(projectDirectoryPath, "Specifications");
                    exeArgs.Append(" \"" + specificationsFolder + "\"");
                    dtoFolder = Path.Combine(projectDirectoryPath, "DTO");
                    exeArgs.Append(" \"" + dtoFolder + "\"");
                }
                var process = new Process();
                process.StartInfo.FileName    = exePath;
                process.StartInfo.Arguments   = exeArgs.ToString();
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                process.WaitForExit();

                if (copyTemplates.IsChecked == true)
                {
                    string              templatesFolder;
                    HashSet <string>    existingTTIncludes;
                    EnvDTE.ProjectItems templatesProjectItems;
                    TemplatesCopying.CopyTemplates(_dte, "ServerTemplates", netVersion, toolsPath, vsVersion, out templatesFolder, out existingTTIncludes, out templatesProjectItems);
                    var ttInclude     = @"%AppData%\WAQS\Templates\Includes\WAQS.Roslyn.Assemblies.ttinclude";
                    var ttIncludeName = Path.GetFileName(ttInclude);
                    TemplatesCopying.AddItem(ttInclude, vsVersion, netVersion, ttIncludeName, templatesFolder, existingTTIncludes, templatesProjectItems);
                }

                if ((kind.Kind & GenerationOptions.Kind.WithoutGlobalWithoutFramework) != 0)
                {
                    var edmxFileProperties = edmxProjectItem.Properties;
                    edmxFileProperties.Item("CustomTool").Value  = "";
                    edmxFileProperties.Item("BuildAction").Value = 0;
                    foreach (var ttPath in edmxProjectItem.ProjectItems.Cast <EnvDTE.ProjectItem>().Where(pi => pi.Name.EndsWith(".tt")).Select(pi => pi.GetFilePath()))
                    {
                        _dte.Solution.FindProjectItem(ttPath).Delete();
                    }
                }
                if (kind.Kind == GenerationOptions.Kind.FrameworkOnly)
                {
                    edmxName = "Framework";
                }
                _project.ProjectItems.AddFromFile(Path.Combine(waqsDirectory, edmxName + ".Server.waqs"));
                _project.ProjectItems.AddFromFile(Path.Combine(waqsDirectory, edmxName + ".Server.tt"));
                var dalItem = _dte.Solution.FindProjectItem(Path.Combine(waqsDirectory, edmxName + ".Server.DAL.tt"));
                if (dalItem != null && dalItem.ProjectItems.Count < 2) // strange bug: sometimes code is not generated for this T4 only
                {
                    ((VSProjectItem)dalItem.Object).RunCustomTool();
                }
                try
                {
                    projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().First(uihi => uihi.Name == "WAQS." + edmxName).UIHierarchyItems.Expanded = false;
                }
                catch
                {
                }
                if (withGlobal && appKind == "Web")
                {
                    _project.ProjectItems.AddFromFile(webConfigPath);
                    _project.ProjectItems.AddFromFile(globalAsaxPath);
                    _project.ProjectItems.AddFromFile(globalAsaxCsPath);
                    _project.ProjectItems.AddFromFile(Path.Combine(projectDirectoryPath, edmxName + ".svc"));
                    if (kind.Kind == GenerationOptions.Kind.GlobalOnly)
                    {
                        var globalUIHierarchyItem = projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().FirstOrDefault(uihi => uihi.Name == "Global");
                        EnvDTE.UIHierarchyItems globalUIHierarchyItems = null;
                        bool globalUIHierarchyItemsExpanded            = false;
                        if (globalUIHierarchyItem != null)
                        {
                            globalUIHierarchyItems         = globalUIHierarchyItem.UIHierarchyItems;
                            globalUIHierarchyItemsExpanded = globalUIHierarchyItems.Expanded;
                        }
                        _project.ProjectItems.AddFromFile(Path.Combine(globalDirectory, "GlobalWCFServiceContract.tt"));
                        _project.ProjectItems.AddFromFile(Path.Combine(globalDirectory, "GlobalWCFService.cs"));
                        _project.ProjectItems.AddFromFile(Path.Combine(projectDirectoryPath, "Global.svc"));
                        if (globalUIHierarchyItem == null)
                        {
                            try
                            {
                                globalUIHierarchyItems = projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().FirstOrDefault(uihi => uihi.Name == "Global")?.UIHierarchyItems;
                            }
                            catch
                            {
                            }
                        }
                        if (globalUIHierarchyItems != null)
                        {
                            globalUIHierarchyItems.Expanded = globalUIHierarchyItemsExpanded;
                        }
                    }
                }
                if (specificationsFolder != null)
                {
                    _project.ProjectItems.AddFromDirectory(specificationsFolder);
                }
                if (dtoFolder != null)
                {
                    _project.ProjectItems.AddFromDirectory(dtoFolder);
                }
                try
                {
                    _dte.ExecuteCommand("File.TfsRefreshStatus");
                }
                catch
                {
                }
                _dte.ItemOperations.Navigate("https://github.com/MatthieuMEZIL/waqs/blob/master/README.md");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType().ToString() + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
            finally
            {
                Cursor = originalCursor;
            }
        }
Esempio n. 2
0
        private void GenerateClick(object sender, RoutedEventArgs e)
        {
            var originalCursor = Cursor;

            try
            {
                Cursor = Cursors.Wait;
                _packageInstaller.InstallPackage(_project, "WAQS.Server.Mock", _packageInstallerServices);

                var edmxPath   = edmx.SelectedValue as string;
                var appKind    = _project.Properties.Cast <EnvDTE.Property>().Any(p => p.Name.StartsWith("WebApplication")) ? "Web" : "App";
                var netVersion = _project.GetNetVersion();
                var kind       = (GenerationOptions.KindViewModel)generationOptions.SelectedItem;

                var                projectDirectoryPath = Path.GetDirectoryName(_project.FullName);
                string             waqsDirectory;
                string             waqsGeneralDirectory = null;
                string             edmxName             = null;
                EnvDTE.ProjectItem edmxProjectItem      = null;
                string             edmxProjectPath      = null;
                if (kind.Kind == GenerationOptions.Kind.FrameworkOnly)
                {
                    waqsDirectory = Path.Combine(projectDirectoryPath, "WAQS.Framework");
                }
                else
                {
                    if (!(edmxPath.EndsWith(".edmx") && File.Exists(edmxPath)))
                    {
                        ShowError("Edmx path is not correct");
                        return;
                    }
                    edmxName        = Path.GetFileNameWithoutExtension(edmxPath);
                    waqsDirectory   = Path.Combine(projectDirectoryPath, "WAQS." + edmxName);
                    edmxProjectItem = _dte.Solution.FindProjectItem(edmxPath);
                    edmxProjectPath = edmxProjectItem.ContainingProject.FullName;
                }
                if (Directory.Exists(waqsDirectory))
                {
                    ShowError(waqsDirectory + "already exists");
                    return;
                }

                var projectUIHierarchyItems    = _dte.GetProjectsUIHierarchyItems().First(uihi => ((EnvDTE.Project)uihi.Object).FullName == _project.FullName).UIHierarchyItems;
                var referencesUIHierarchyItems = projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().First(uihi => uihi.Name == "References").UIHierarchyItems;
                var referencesExpanded         = referencesUIHierarchyItems.Expanded;

                var toolsPath           = Path.Combine(_packageInstallerServices.GetPackageLocation("WAQS.Server.Mock"), "tools");
                var toolsPathServerMock = Path.Combine(toolsPath, "Server.Mock");
                var defaultNamespace    = _project.GetDefaultNamespace();
                var references          = ((VSProject)_project.Object).References;
                references.Add("System");
                references.Add("System.Core");
                references.Add("System.Data");
                references.Add("Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll");
                var dalInterfacesProjectItem = _dte.Solution.FindProjectItem(edmxName + ".Server.DAL.Interfaces.tt");
                var dalInterfacesProject     = dalInterfacesProjectItem?.ContainingProject;
                if (dalInterfacesProject != null)
                {
                    references.AddProject(dalInterfacesProject);
                }
                var dalProjectItem = _dte.Solution.FindProjectItem(edmxName + ".Server.DAL.tt");
                var dalProject     = dalProjectItem?.ContainingProject;
                if (dalProject != null)
                {
                    references.AddProject(dalProject);
                }
                var dtoProject = _dte.Solution.FindProjectItem(edmxName + ".Server.DTO.tt")?.ContainingProject;
                if (dtoProject != null)
                {
                    references.AddProject(dtoProject);
                }
                var entitiesProjectItem = _dte.Solution.FindProjectItem(edmxName + ".Server.Entities.tt");
                var entitiesProject     = entitiesProjectItem?.ContainingProject;
                if (entitiesProject != null)
                {
                    references.AddProject(entitiesProject);
                }
                var serviceProject = _dte.Solution.FindProjectItem(edmxName + ".Server.Service.tt")?.ContainingProject;
                if (serviceProject != null)
                {
                    references.AddProject(serviceProject);
                }
                EnvDTE.Project fxProject = null;
                if (kind.Kind == GenerationOptions.Kind.WithoutGlobalWithoutFramework)
                {
                    fxProject = _dte.Solution.FindProjectItem("WAQS.Server.Fx.DAL.Mock.tt")?.ContainingProject;
                    if (fxProject != null)
                    {
                        references.AddProject(fxProject);
                    }
                }
                _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "EntityFramework", "6.1.2", false);
                try
                {
                    referencesUIHierarchyItems.Expanded = referencesExpanded;
                }
                catch
                {
                }
                string vsVersion;
                switch (_dte.Version)
                {
                case "12.0":
                    vsVersion = "VS12";
                    break;

                case "14.0":
                default:
                    vsVersion = "VS14";
                    break;
                }

                var entitiesProjectPath  = entitiesProject.FullName;
                var entitiesSolutionPath = entitiesProject == null ? null : _dte.Solution.FileName;

                var exePath = Path.Combine(toolsPathServerMock, "InitWAQSServerMock.exe");
                var exeArgs = new StringBuilder("\"" + edmxPath + "\" \"" + projectDirectoryPath + "\" \"" + toolsPathServerMock + "\" \"" + defaultNamespace + "\" \"" + waqsDirectory + "\" \"" + waqsGeneralDirectory + "\" \"" + entitiesSolutionPath + "\" \"" + entitiesProjectPath + "\" \"" + netVersion + "\" \"" + vsVersion + "\" \"" + kind.Key + "\" \"" + (copyTemplates.IsChecked == true ? "WithSourceControl" : "WithoutSourceControl") + "\" \"" + _dte.Solution.FullName + "\"");
                if ((kind.Kind & GenerationOptions.Kind.WithoutGlobalWithoutFramework) != 0)
                {
                    var specificationsProjectItem = entitiesProject?.GetAllProjectItems().FirstOrDefault(pi => ((string)pi.Properties.Cast <EnvDTE.Property>().First(p => p.Name == "FullPath").Value).EndsWith("\\Specifications\\"));
                    exeArgs.Append(" \"" + specificationsProjectItem?.ContainingProject.FullName + "\"");
                    exeArgs.Append(" \"" + specificationsProjectItem?.GetFilePath() + "\"");
                    var dtoProjectItem = (dtoProject ?? entitiesProject).GetAllProjectItems().FirstOrDefault(pi => ((string)pi.Properties.Cast <EnvDTE.Property>().First(p => p.Name == "FullPath").Value).EndsWith("\\DTO\\"));
                    exeArgs.Append(" \"" + dtoProjectItem?.ContainingProject.FullName + "\"");
                    exeArgs.Append(" \"" + dtoProjectItem?.GetFilePath() + "\"");
                    var entityCsProjectItem = entitiesProjectItem?.ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name.EndsWith(".cs"));
                    exeArgs.Append(" \"" + entityCsProjectItem?.GetFilePath() + "\"");
                    var dalInterfaceCsProjectItem = dalInterfacesProjectItem?.ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name.EndsWith(".cs"));
                    exeArgs.Append(" \"" + dalInterfaceCsProjectItem?.GetFilePath() + "\"");
                    var dalCsProjectItem = dalProjectItem?.ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name.EndsWith(".cs"));
                    exeArgs.Append(" \"" + dalCsProjectItem?.GetFilePath() + "\"");
                    exeArgs.Append(" \"" + edmxProjectPath + "\"");
                    var configProjectItem = edmxProjectItem.ContainingProject.ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => string.Equals(pi.Name, "App.config", StringComparison.CurrentCultureIgnoreCase) || string.Equals(pi.Name, "Web.config", StringComparison.CurrentCultureIgnoreCase));
                    exeArgs.Append(" \"" + configProjectItem?.GetFilePath() + "\"");
                }
                var process = new Process();
                process.StartInfo.FileName    = exePath;
                process.StartInfo.Arguments   = exeArgs.ToString();
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                process.WaitForExit();

                if (copyTemplates.IsChecked == true)
                {
                    string              templatesFolder;
                    HashSet <string>    existingTTIncludes;
                    EnvDTE.ProjectItems templatesProjectItems;
                    TemplatesCopying.CopyTemplates(_dte, "ServerMockTemplates", netVersion, toolsPath, vsVersion, out templatesFolder, out existingTTIncludes, out templatesProjectItems);
                    var ttInclude     = @"%AppData%\WAQS\Templates\Includes\WAQS.Roslyn.Assemblies.ttinclude";
                    var ttIncludeName = Path.GetFileName(ttInclude);
                    TemplatesCopying.AddItem(ttInclude, vsVersion, netVersion, ttIncludeName, templatesFolder, existingTTIncludes, templatesProjectItems);
                }

                if (kind.Kind == GenerationOptions.Kind.FrameworkOnly)
                {
                    edmxName = "Framework";
                }
                _project.ProjectItems.AddFromFile(Path.Combine(waqsDirectory, edmxName + ".Server.Mock.waqs"));
                _project.ProjectItems.AddFromFile(Path.Combine(waqsDirectory, edmxName + ".Server.Mock.tt"));
                try
                {
                    projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().First(uihi => uihi.Name == "WAQS." + edmxName).UIHierarchyItems.Expanded = false;
                }
                catch
                {
                }
                try
                {
                    _dte.ExecuteCommand("File.TfsRefreshStatus");
                }
                catch
                {
                }
                _dte.ItemOperations.Navigate("https://github.com/MatthieuMEZIL/waqs/blob/master/README.md");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType().ToString() + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
            finally
            {
                Cursor = originalCursor;
            }
        }
Esempio n. 3
0
        private void GenerateClick(object sender, RoutedEventArgs e)
        {
            var originalCursor = Cursor;

            try
            {
                Cursor = Cursors.Wait;
                _packageInstaller.InstallPackage(_project, "WAQS.Client." + _clientKind, _packageInstallerServices);

                var edmxPath    = edmx.SelectedValue as string;
                var servicePath = service.SelectedValue as string;
                var appKind     = _project.Properties.Cast <EnvDTE.Property>().Any(p => p.Name.StartsWith("WebApplication")) ? "Web" : "App";
                var netVersion  = _project.GetNetVersion();
                var kind        = (GenerationOptions.KindViewModel)generationOptions.SelectedItem;

                var    projectDirectoryPath = Path.GetDirectoryName(_project.FullName);
                string waqsDirectory;
                string edmxName             = null;
                string waqsGeneralDirectory = null;
                string contextsPath         = null;
                bool   servicePathIsUrl     = false;
                if (kind.Kind == GenerationOptions.Kind.FrameworkOnly)
                {
                    waqsDirectory = Path.Combine(projectDirectoryPath, "WAQS.Framework");
                }
                else
                {
                    if (!(edmxPath.EndsWith(".edmx") && File.Exists(edmxPath)))
                    {
                        ShowError("Edmx path is not correct");
                        return;
                    }
                    if (!((servicePathIsUrl = Regex.IsMatch(servicePath, "^http(s)?://")) || servicePath.EndsWith(".svc") && File.Exists(servicePath)))
                    {
                        ShowError("Service path is not correct");
                        return;
                    }
                    edmxName      = Path.GetFileNameWithoutExtension(edmxPath);
                    waqsDirectory = Path.Combine(projectDirectoryPath, "WAQS." + edmxName);
                }
                if (Directory.Exists(waqsDirectory))
                {
                    ShowError(waqsDirectory + "already exists");
                    return;
                }

                var projectUIHierarchyItems    = _dte.GetProjectsUIHierarchyItems().First(uihi => ((EnvDTE.Project)uihi.Object).FullName == _project.FullName).UIHierarchyItems;
                var referencesUIHierarchyItems = projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().FirstOrDefault(uihi => uihi.Name == "References")?.UIHierarchyItems;
                var referencesExpanded         = referencesUIHierarchyItems?.Expanded ?? false;

                var toolsPath        = Path.Combine(_packageInstallerServices.GetPackageLocation("WAQS.Client." + _clientKind), "tools");
                var clientToolsPath  = Path.Combine(toolsPath, "Client." + _clientKind);
                var defaultNamespace = _project.GetDefaultNamespace();
                var references       = ((VSProject)_project.Object).References;
                references.Add("System");
                references.Add("System.Core");
                references.Add("System.Runtime.Serialization");
                references.Add("System.ServiceModel");
                if (_clientKind == GenerationOptions.WPF)
                {
                    references.Add("System.ComponentModel.DataAnnotations");
                    references.Add("System.Drawing");
                    references.Add("PresentationCore");
                    references.Add("PresentationFramework");
                    references.Add("System.Xaml");
                    references.Add("System.Xml");
                    _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "System.Windows.Interactivity.WPF", "2.0.20525", false);
                    _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "Unity", "3.5.1404", false);
                    _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "Rx-WPF", "2.2.5", false);
                }
                else if (_clientKind == GenerationOptions.PCL)
                {
                    _packageInstaller.InstallPackage("http://packages.nuget.org", _project, "Microsoft.Bcl.Async", "1.0.168", false);
                }

                try
                {
                    referencesUIHierarchyItems.Expanded = referencesExpanded;
                }
                catch
                {
                }
                bool   withGlobal    = (kind.Kind & GenerationOptions.Kind.GlobalOnly) != 0;
                string appConfigPath = null;
                string appXamlPath   = null;
                string appXamlCsPath = null;
                if (withGlobal)
                {
                    appConfigPath = Path.Combine(projectDirectoryPath, "app.config");
                    if (File.Exists(appConfigPath))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(appConfigPath);
                        }
                        catch
                        {
                        }
                    }
                    appXamlPath = Path.Combine(projectDirectoryPath, "App.xaml");
                    if (File.Exists(appXamlPath))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(appXamlPath);
                        }
                        catch
                        {
                        }
                    }
                    appXamlCsPath = Path.Combine(projectDirectoryPath, "App.xaml.cs");
                    if (File.Exists(appXamlCsPath))
                    {
                        try
                        {
                            _dte.SourceControl.CheckOutItem(appXamlCsPath);
                        }
                        catch
                        {
                        }
                    }
                    if (kind.Kind == GenerationOptions.Kind.GlobalOnly)
                    {
                        waqsGeneralDirectory = Path.Combine(projectDirectoryPath, "WAQS");
                        contextsPath         = Path.Combine(waqsGeneralDirectory, "Contexts.xml");
                        if (File.Exists(contextsPath))
                        {
                            try
                            {
                                _dte.SourceControl.CheckOutItem(contextsPath);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                var    entitiesProjectPath  = _dte.Solution.FindProjectItem(edmxName + ".Server.Entities.tt")?.ContainingProject.FullName;
                string entitiesSolutionPath = null;
                if (!string.IsNullOrEmpty(entitiesProjectPath))
                {
                    entitiesSolutionPath = _dte.Solution.FileName;
                }
                string vsVersion = _dte.GetVsVersion();

                string svcUrl = null;
                if (servicePathIsUrl)
                {
                    svcUrl = servicePath;
                }
                else if (!string.IsNullOrEmpty(servicePath))
                {
                    var svcProjectProperties = _dte.Solution.FindProjectItem(servicePath)?.ContainingProject.Properties.Cast <EnvDTE.Property>();
                    if (svcProjectProperties != null && (kind.Kind != GenerationOptions.Kind.FrameworkOnly))
                    {
                        svcUrl = StartService(servicePath, vsVersion, svcProjectProperties);
                    }
                }

                var exePath = Path.Combine(clientToolsPath, "InitWAQSClient" + _clientKind + ".exe");
                var exeArgs = new StringBuilder("\"" + edmxPath + "\" \"" + (_clientKind == GenerationOptions.WPF ? projectDirectoryPath + "\" \"" : "") + clientToolsPath + "\" \"" + defaultNamespace + "\" \"" + svcUrl + "\" \"" + waqsDirectory + "\" \"" + waqsGeneralDirectory + "\" \"" + (kind.Kind == GenerationOptions.Kind.GlobalOnly ? _dte.Solution.FindProjectItem(edmxName + ".Client." + _clientKind + ".ClientContext.tt").ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name == edmxName + "ExpressionTransformer.cs")?.GetFilePath() : "") + "\" \"" + (kind.Kind == GenerationOptions.Kind.GlobalOnly ? _dte.Solution.FindProjectItem(edmxName + ".Client." + _clientKind + ".ServiceProxy.tt").ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name == "I" + edmxName + "Service.cs")?.GetFilePath() : "") + "\" \"" + (kind.Kind == GenerationOptions.Kind.GlobalOnly ? _dte.Solution.FindProjectItem(edmxName + ".Client." + _clientKind + ".Entities.tt")?.GetFirstCsFilePath() : "") + "\" \"" + (kind.Kind == GenerationOptions.Kind.GlobalOnly ? _dte.Solution.FindProjectItem(edmxName + ".Client." + _clientKind + ".ClientContext.tt").ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name == edmxName + "ClientContext.cs")?.GetFilePath() : "") + "\" \"" + (kind.Kind == GenerationOptions.Kind.GlobalOnly ? _dte.Solution.FindProjectItem(edmxName + ".Client." + _clientKind + ".ClientContext.Interfaces.tt").ProjectItems.Cast <EnvDTE.ProjectItem>().FirstOrDefault(pi => pi.Name == "I" + edmxName + "ClientContext.cs")?.GetFilePath() : "") + "\" \"" + entitiesSolutionPath + "\" \"" + entitiesProjectPath + "\" \"" + netVersion + "\" \"" + vsVersion + "\" \"" + kind.Key + "\" " + (copyTemplates.IsChecked == true ? "WithSourceControl" : "WithoutSourceControl") + " \"" + _dte.Solution.FullName + "\"");
                if ((kind.Kind & GenerationOptions.Kind.WithoutGlobalWithoutFramework) != 0)
                {
                    var projectsItems             = _dte.GetProjects().SelectMany(p => p.GetAllProjectItems());
                    var specificationsProjectItem = projectsItems.FirstOrDefault(pi => ((string)pi.Properties.Cast <EnvDTE.Property>().First(p => p.Name == "FullPath").Value).EndsWith("\\Specifications\\"));
                    exeArgs.Append(" \"" + specificationsProjectItem.ContainingProject.FullName + "\"");
                    exeArgs.Append(" \"" + Path.GetDirectoryName(specificationsProjectItem.GetFilePath()) + "\"");
                    var dtoProjectItem = projectsItems.FirstOrDefault(pi => ((string)pi.Properties.Cast <EnvDTE.Property>().First(p => p.Name == "FullPath").Value).EndsWith("\\DTO\\"));
                    exeArgs.Append(" \"" + dtoProjectItem.ContainingProject.FullName + "\"");
                    exeArgs.Append(" \"" + Path.GetDirectoryName(dtoProjectItem.GetFilePath()) + "\"");
                    exeArgs.Append(" \"" + _dte.Solution.FindProjectItem(edmxName + ".Server.Entities.tt")?.GetFirstCsFilePath() + "\"");
                }
                var process = new Process();
                process.StartInfo.FileName    = exePath;
                process.StartInfo.Arguments   = exeArgs.ToString();
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                process.WaitForExit();

                if (copyTemplates.IsChecked == true)
                {
                    string              templatesFolder;
                    HashSet <string>    existingTTIncludes;
                    EnvDTE.ProjectItems templatesProjectItems;
                    TemplatesCopying.CopyTemplates(_dte, _clientKind + "ClientTemplates", netVersion, toolsPath, vsVersion, out templatesFolder, out existingTTIncludes, out templatesProjectItems);
                    var ttInclude     = @"%AppData%\WAQS\Templates\Includes\WAQS.Roslyn.Assemblies.ttinclude";
                    var ttIncludeName = Path.GetFileName(ttInclude);
                    TemplatesCopying.AddItem(ttInclude, vsVersion, netVersion, ttIncludeName, templatesFolder, existingTTIncludes, templatesProjectItems);
                }

                if (kind.Kind == GenerationOptions.Kind.FrameworkOnly)
                {
                    edmxName = "Framework";
                }
                _project.ProjectItems.AddFromFile(Path.Combine(waqsDirectory, edmxName + ".Client." + _clientKind + ".waqs"));
                if (kind.Kind != GenerationOptions.Kind.GlobalOnly)
                {
                    _project.ProjectItems.AddFromFile(Path.Combine(waqsDirectory, edmxName + ".Client." + _clientKind + ".tt"));
                }
                try
                {
                    projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().First(uihi => uihi.Name == "WAQS." + edmxName).UIHierarchyItems.Expanded = false;
                }
                catch
                {
                }
                if (withGlobal)
                {
                    if (_clientKind == GenerationOptions.WPF)
                    {
                        _project.ProjectItems.AddFromFile(appConfigPath);
                    }
                    if (kind.Kind == GenerationOptions.Kind.GlobalOnly)
                    {
                        EnvDTE.UIHierarchyItems waqsGeneralUIHierarchyItems = null;
                        Action setWAQSGeneralUIHierarchyItems = () => waqsGeneralUIHierarchyItems = projectUIHierarchyItems.Cast <EnvDTE.UIHierarchyItem>().FirstOrDefault(uihi => uihi.Name == "WAQS")?.UIHierarchyItems;
                        setWAQSGeneralUIHierarchyItems();
                        bool waqsGeneralUIHierarchyItemsExpanded = false;
                        if (waqsGeneralUIHierarchyItems != null)
                        {
                            waqsGeneralUIHierarchyItemsExpanded = waqsGeneralUIHierarchyItems.Expanded;
                        }
                        _project.ProjectItems.AddFromFile(contextsPath);
                        if (waqsGeneralUIHierarchyItems == null)
                        {
                            try
                            {
                                setWAQSGeneralUIHierarchyItems();
                                waqsGeneralUIHierarchyItems.Expanded = false;
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                waqsGeneralUIHierarchyItems.Expanded = waqsGeneralUIHierarchyItemsExpanded;
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                try
                {
                    _dte.ExecuteCommand("File.TfsRefreshStatus");
                }
                catch
                {
                }
                _dte.ItemOperations.Navigate("https://github.com/MatthieuMEZIL/waqs/blob/master/README.md");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType().ToString() + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
            finally
            {
                Cursor = originalCursor;
            }
        }