コード例 #1
0
        private Project GetOrAddSolutionFolder(string name, NewItemTarget target)
        {
            if (target.IsSolution && string.IsNullOrEmpty(name))
            {
                // An empty solution folder name means we are not creating any solution
                // folders for that item, and the file we are adding is intended to be
                // added to the solution. Files cannot be added directly to the solution,
                // so there is a "Solution Items" folder that they are added to.
                return(_dte.Solution.FindSolutionFolder(SolutionItemsProjectName)
                       ?? ((Solution2)_dte.Solution).AddSolutionFolder(SolutionItemsProjectName));
            }

            // Even though solution folders are always virtual, if the target directory exists,
            // then we will also create the new directory on disk. This ensures that any files
            // that are added to this folder will end up in the corresponding physical directory.
            if (Directory.Exists(target.Directory))
            {
                PackageUtilities.EnsureOutputPath(Path.Combine(target.Directory, name));
            }

            Project parent = target.Project;

            foreach (string segment in SplitPath(name))
            {
                // If we don't have a parent project yet,
                // then this folder is added to the solution.
                if (parent == null)
                {
                    parent = _dte.Solution.FindSolutionFolder(segment) ?? ((Solution2)_dte.Solution).AddSolutionFolder(segment);
                }
                else
                {
                    parent = parent.FindSolutionFolder(segment) ?? ((SolutionFolder)parent.Object).AddSolutionFolder(segment);
                }
            }

            return(parent);
        }
コード例 #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.");
                }
            }
        }
コード例 #3
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.");
                }
            }
        }
コード例 #4
0
        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.");
                }
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
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.");
                }
            }
        }
コード例 #7
0
        //private void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
        //{
        //    var button = (OleMenuCommand)sender;
        //    button.Visible = button.Enabled = false;

        //    UIHierarchyItem item = GetSelectedItem();
        //    var project = item.Object as Project;

        //    if (project == null || !project.Kind.Equals(EnvDTE.Constants.vsProjectKindSolutionItems, StringComparison.OrdinalIgnoreCase))
        //        button.Visible = button.Enabled = true;
        //}

        private async void ExecuteAsync(object sender, EventArgs e)
        {
            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;
            }

            dynamic inputValues = PromptForFileName(folder);
            string  input       = inputValues.Input.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__";
                }
                else
                {
                    // only prepend the date/time onto filenames (not folders)
                    input = input.Insert(input.LastIndexOf("\\") + 1, DateTime.Now.ToString("yyyyMMdd_HHmmss_"));
                }

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

                PackageUtilities.EnsureOutputPath(dir);

                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)
                        {
                            project.AddFileToProject(file.FullName, isEmbeddedResource: (bool)inputValues.IsEmbeddedResource);
                        }

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

                        // I have no idea why but only doing Activate once after SyncwithActiveDocument does not work 100% of the time.
                        _dte.ActiveDocument.Activate();
                        _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                        _dte.ActiveDocument.Activate();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist.");
                }
            }
        }
コード例 #8
0
        private void EnsureFilePath()
        {
            var dir = File.DirectoryName;

            PackageUtilities.EnsureOutputPath(dir);
        }