Exemplo n.º 1
0
        private async void ExecuteAsync(object sender, EventArgs e)
        {
            var item   = ProjectHelpers.GetSelectedItem();
            var folder = FindFolder(item);

            if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
            {
                return;
            }

            var selectedItem    = item as ProjectItem;
            var selectedProject = item as Project;
            var project         = selectedItem?.ContainingProject ?? selectedProject ?? ProjectHelpers.GetActiveProject();

            if (project == null)
            {
                return;
            }

            var input     = PromptForFileName(folder, project.UniqueName);
            var inputName = input.Input.TrimStart('/', '\\').Replace("/", "\\");

            if (string.IsNullOrEmpty(inputName))
            {
                return;
            }

            var parsedInputs = GetParsedInput(inputName);

            foreach (var inputItem in parsedInputs)
            {
                inputName = inputItem;

                if (inputName.EndsWith("\\", StringComparison.Ordinal))
                {
                    inputName = inputName + "__dummy__";
                }

                var file = new FileInfo(Path.Combine(folder, inputName));
                var dir  = file.DirectoryName;

                PackageUtilities.EnsureOutputPath(dir);

                if (!file.Exists)
                {
                    var position = await WriteFileAsync(project, file.FullName, input);

                    try
                    {
                        ProjectItem projectItem = null;
                        if (item is ProjectItem projItem)
                        {
                            if ("{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}" == projItem.Kind) // Constants.vsProjectItemKindVirtualFolder
                            {
                                projectItem = projItem.ProjectItems.AddFromFile(file.FullName);
                            }
                        }
                        if (projectItem == null)
                        {
                            projectItem = project.AddFileToProject(file);
                        }

                        if (file.FullName.EndsWith("__dummy__"))
                        {
                            projectItem?.Delete();
                            continue;
                        }

                        VsShellUtilities.OpenDocument(this, file.FullName);

                        // Move cursor into position
                        if (position > 0)
                        {
                            var view = ProjectHelpers.GetCurentTextView();

                            if (view != null)
                            {
                                view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position));
                            }
                        }

                        _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                        _dte.ActiveDocument.Activate();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist.");
                }
            }
        }
Exemplo n.º 2
0
        private async void ExecuteAsync(object sender, EventArgs e)
        {
            object item   = ProjectHelpers.GetSelectedItem();
            string folder = FindFolder(item);

            if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
            {
                return;
            }

            ProjectItem selectedItem    = item as ProjectItem;
            Project     selectedProject = item as Project;
            Project     project         = selectedItem?.ContainingProject ?? selectedProject ?? ProjectHelpers.GetActiveProject();

            if (project == null)
            {
                return;
            }

            string input = PromptForFileName(folder).TrimStart('/', '\\').Replace("/", "\\");

            if (string.IsNullOrEmpty(input))
            {
                return;
            }

            string[] parsedInputs = GetParsedInput(input);

            foreach (string inputItem in parsedInputs)
            {
                input = inputItem;

                if (input.EndsWith("\\", StringComparison.Ordinal))
                {
                    input = input + "__dummy__";
                }

                FileInfo file = null;

                try
                {
                    file = new FileInfo(Path.Combine(folder, input));
                }
                catch (PathTooLongException ex)
                {
                    MessageBox.Show("The file name is too long 😢", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Error);
                    Logger.Log(ex);
                    continue;
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                    continue;
                }

                if (!IsFileNameValid(file.Name))
                {
                    MessageBox.Show($"The file name '{file.Name}' is a system reserved name.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Error);
                    continue;
                }

                string dir = file.DirectoryName;

                PackageUtilities.EnsureOutputPath(dir);

                if (!file.Exists)
                {
                    try
                    {
                        int position = await WriteFileAsync(project, file.FullName);

                        ProjectItem projectItem = null;

                        if (item is ProjectItem projItem)
                        {
                            if ("{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}" == projItem.Kind)                             // Constants.vsProjectItemKindVirtualFolder
                            {
                                projectItem = projItem.ProjectItems.AddFromFile(file.FullName);
                            }
                        }
                        if (projectItem == null)
                        {
                            projectItem = project.AddFileToProject(file);
                        }

                        if (file.FullName.EndsWith("__dummy__"))
                        {
                            projectItem?.Delete();
                            continue;
                        }

                        VsShellUtilities.OpenDocument(this, file.FullName);

                        // Move cursor into position
                        if (position > 0)
                        {
                            Microsoft.VisualStudio.Text.Editor.IWpfTextView view = ProjectHelpers.GetCurentTextView();

                            if (view != null)
                            {
                                view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position));
                            }
                        }

                        _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                        _dte.ActiveDocument.Activate();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist.");
                }
            }
        }
        private async void ExecuteAsync(object sender, EventArgs e)
        {
            //var assmbly = System.Reflection.Assembly.LoadFile(@"D:\sample\abp-master\abp-master\samples\BookStore\src\Acme.BookStore.Domain\bin\Debug\netcoreapp2.2\Acme.BookStore.Domain.dll");
            //if (assmbly != null) {
            //    var fullname = assmbly.FullName;
            //    var types = assmbly.GetTypes();
            //    foreach (var ty in types) {
            //        var typename = ty.FullName;
            //    }
            //}

            var    entities = GetEntities(AddAnyFilePackage._dte.Solution).ToArray();
            object item     = ProjectHelpers.GetSelectedItem();

            string folder = FindFolder(item);

            if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
            {
                return;
            }

            var     selectedItem    = item as ProjectItem;
            var     selectedProject = item as Project;
            Project project         = selectedItem?.ContainingProject ?? selectedProject ?? ProjectHelpers.GetActiveProject();

            if (project == null)
            {
                return;
            }
            var dir = new DirectoryInfo(folder);

            this.rootNamespace = project.GetRootNamespace();
            var viewmodel = new AppServiceDialogViewModel()
            {
                Entities      = new System.Windows.Data.CollectionView(entities),
                SelectFolder  = dir.Name + "/",
                RootNamespace = this.rootNamespace
            };

            string input = PromptForFileName(folder, viewmodel).TrimStart('/', '\\').Replace("/", "\\");

            if (string.IsNullOrEmpty(input))
            {
                return;
            }

            //string[] parsedInputs = GetParsedInput(input);
            string[] parsedInputs = new string[] {
                viewmodel.DtoClass + ".cs",
                viewmodel.CudtoClass + ".cs",
                viewmodel.ServiceClass + ".cs",
                viewmodel.IServiceClass + ".cs"
            };
            foreach (string inputItem in parsedInputs)
            {
                input = inputItem;

                if (input.EndsWith("\\", StringComparison.Ordinal))
                {
                    input = input + "__dummy__";
                }

                var    file = new FileInfo(Path.Combine(folder, viewmodel.SubFolder, input));
                string path = file.DirectoryName;

                PackageUtilities.EnsureOutputPath(path);

                if (!file.Exists)
                {
                    int position = await WriteFileAsync(project, file.FullName);

                    try
                    {
                        ProjectItem projectItem = null;
                        if (item is ProjectItem projItem)
                        {
                            if ("{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}" == projItem.Kind) // Constants.vsProjectItemKindVirtualFolder
                            {
                                projectItem = projItem.ProjectItems.AddFromFile(file.FullName);
                            }
                        }
                        if (projectItem == null)
                        {
                            projectItem = project.AddFileToProject(file);
                        }

                        if (file.FullName.EndsWith("__dummy__"))
                        {
                            projectItem?.Delete();
                            continue;
                        }

                        VsShellUtilities.OpenDocument(this, file.FullName);

                        // Move cursor into position
                        if (position > 0)
                        {
                            Microsoft.VisualStudio.Text.Editor.IWpfTextView view = ProjectHelpers.GetCurentTextView();

                            if (view != null)
                            {
                                view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position));
                            }
                        }

                        _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                        _dte.ActiveDocument.Activate();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist.");
                }
            }
        }