Пример #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.");
                }
            }
        }
Пример #2
0
        private async System.Threading.Tasks.Task AddFileAsync(string name, NewItemTarget target)
        {
            FileInfo file;


            // If the file is being added to a solution folder, but that
            // solution folder doesn't have a corresponding directory on
            // disk, then write the file to the root of the solution instead.
            if (target.IsSolutionFolder && !Directory.Exists(target.Directory))
            {
                file = new FileInfo(Path.Combine(Path.GetDirectoryName(_dte.Solution.FullName), Path.GetFileName(name)));
            }
            else
            {
                file = new FileInfo(Path.Combine(target.Directory, name));
            }

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

            PackageUtilities.EnsureOutputPath(file.DirectoryName);

            if (!file.Exists)
            {
                try
                {
                    Project project;

                    if (target.IsSolutionOrSolutionFolder)
                    {
                        project = GetOrAddSolutionFolder(Path.GetDirectoryName(name), target);
                    }
                    else
                    {
                        project = target.Project;
                    }

                    int position = await WriteFileAsync(project, file.FullName);

                    if (target.ProjectItem != null && target.ProjectItem.IsKind(Constants.vsProjectItemKindVirtualFolder))
                    {
                        target.ProjectItem.ProjectItems.AddFromFile(file.FullName);
                    }
                    else
                    {
                        project.AddFileToProject(file);
                    }

                    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));
                        }
                    }

                    ExecuteCommandIfAvailable("SolutionExplorer.SyncWithActiveDocument");
                    _dte.ActiveDocument.Activate();
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
            else
            {
                MessageBox.Show($"The file '{file}' already exists.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        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.");
                }
            }
        }
Пример #4
0
        private async void MenuItemCallback(object sender, EventArgs e)
        {
            UIHierarchyItem item = GetSelectedItem();

            if (item == null)
            {
                return;
            }

            string folder = FindFolder(item);

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

            Project project = 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__";
                }

                string file = Path.Combine(folder, input);
                string dir  = Path.GetDirectoryName(file);

                PackageUtilities.EnsureOutputPath(dir);

                if (!File.Exists(file))
                {
                    int position = await WriteFile(project, file);

                    try
                    {
                        var projectItem = project.AddFileToProject(file);

                        if (file.EndsWith("__dummy__"))
                        {
                            Telemetry.TrackEvent("Folder added");
                            projectItem.Delete();
                            continue;
                        }

                        VsShellUtilities.OpenDocument(this, file);

                        // 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();

                        await Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                        {
                            var command = _dte.Commands.Item("Edit.FormatDocument");

                            if (command.IsAvailable)
                            {
                                _dte.ExecuteCommand(command.Name);
                            }
                        }), DispatcherPriority.SystemIdle, null);
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist.");
                }
            }
        }