1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
6
using System.Collections.ObjectModel;
7
using System.Collections.Specialized;
9
using System.Windows.Input;
11
using ICSharpCode.AvalonEdit;
12
using ICSharpCode.Scripting;
13
using ICSharpCode.SharpDevelop.Project;
16
namespace ICSharpCode.PackageManagement.Scripting
18
public class PackageManagementConsoleViewModel : ViewModelBase<PackageManagementConsoleViewModel>
20
RegisteredPackageSources registeredPackageSources;
21
IPackageManagementProjectService projectService;
22
IPackageManagementConsoleHost consoleHost;
24
DelegateCommand clearConsoleCommand;
26
ObservableCollection<PackageSourceViewModel> packageSources = new ObservableCollection<PackageSourceViewModel>();
27
PackageSourceViewModel activePackageSource;
29
ObservableCollection<IProject> projects = new ObservableCollection<IProject>();
31
PackageManagementConsole packageManagementConsole;
33
public PackageManagementConsoleViewModel(
34
RegisteredPackageSources registeredPackageSources,
35
IPackageManagementProjectService projectService,
36
IPackageManagementConsoleHost consoleHost)
38
this.registeredPackageSources = registeredPackageSources;
39
this.projectService = projectService;
40
this.consoleHost = consoleHost;
48
UpdatePackageSourceViewModels();
49
ReceiveNotificationsWhenPackageSourcesUpdated();
51
ReceiveNotificationsWhenSolutionIsUpdated();
55
void InitConsoleHost()
57
packageManagementConsole = CreateConsole();
58
consoleHost.ScriptingConsole = packageManagementConsole;
62
protected virtual PackageManagementConsole CreateConsole()
64
return new PackageManagementConsole();
69
clearConsoleCommand = new DelegateCommand(param => ClearConsole());
72
public ICommand ClearConsoleCommand {
73
get { return clearConsoleCommand; }
76
public void ClearConsole()
79
consoleHost.WritePrompt();
82
void UpdatePackageSourceViewModels()
84
packageSources.Clear();
85
AddRegisteredPackageSourceViewModels();
86
AddAggregatePackageSourceViewModelIfMoreThanOnePackageSourceViewModelAdded();
87
SelectActivePackageSource();
90
void AddRegisteredPackageSourceViewModels()
92
foreach (PackageSource packageSource in registeredPackageSources) {
93
AddPackageSourceViewModel(packageSource);
97
void AddPackageSourceViewModel(PackageSource packageSource)
99
var viewModel = new PackageSourceViewModel(packageSource);
100
packageSources.Add(viewModel);
103
void AddAggregatePackageSourceViewModelIfMoreThanOnePackageSourceViewModelAdded()
105
if (packageSources.Count > 1) {
106
AddPackageSourceViewModel(RegisteredPackageSourceSettings.AggregatePackageSource);
110
void SelectActivePackageSource()
112
PackageSource activePackageSource = consoleHost.ActivePackageSource;
113
foreach (PackageSourceViewModel packageSourceViewModel in packageSources) {
114
if (packageSourceViewModel.GetPackageSource().Equals(activePackageSource)) {
115
ActivePackageSource = packageSourceViewModel;
120
SelectFirstActivePackageSource();
123
void SelectFirstActivePackageSource()
125
if (packageSources.Count > 0) {
126
ActivePackageSource = packageSources[0];
130
void ReceiveNotificationsWhenPackageSourcesUpdated()
132
registeredPackageSources.CollectionChanged += PackageSourcesChanged;
135
void PackageSourcesChanged(object sender, NotifyCollectionChangedEventArgs e)
137
UpdatePackageSourceViewModels();
142
Solution solution = projectService.OpenSolution;
143
if (solution != null) {
144
AddProjects(solution);
146
UpdateDefaultProject();
149
void UpdateDefaultProject()
151
DefaultProject = projects.FirstOrDefault();
154
void AddProjects(Solution solution)
156
foreach (IProject project in solution.Projects) {
157
projects.Add(project);
161
void ReceiveNotificationsWhenSolutionIsUpdated()
163
projectService.ProjectAdded += ProjectAdded;
164
projectService.SolutionClosed += SolutionClosed;
165
projectService.SolutionLoaded += SolutionLoaded;
166
projectService.SolutionFolderRemoved += SolutionFolderRemoved;
169
void ProjectAdded(object sender, ProjectEventArgs e)
171
projects.Add(e.Project);
172
UpdateDefaultProject();
175
void SolutionClosed(object sender, EventArgs e)
178
DefaultProject = null;
181
void SolutionLoaded(object sender, SolutionEventArgs e)
183
AddProjects(e.Solution);
184
UpdateDefaultProject();
187
void SolutionFolderRemoved(object sender, SolutionFolderEventArgs e)
189
IProject project = e.SolutionFolder as IProject;
190
projects.Remove(project);
191
UpdateDefaultProject();
194
public ObservableCollection<PackageSourceViewModel> PackageSources {
195
get { return packageSources; }
198
public PackageSourceViewModel ActivePackageSource {
199
get { return activePackageSource; }
201
activePackageSource = value;
202
consoleHost.ActivePackageSource = GetPackageSourceForViewModel(activePackageSource);
203
OnPropertyChanged(viewModel => viewModel.ActivePackageSource);
207
PackageSource GetPackageSourceForViewModel(PackageSourceViewModel activePackageSource)
209
if (activePackageSource != null) {
210
return activePackageSource.GetPackageSource();
215
public ObservableCollection<IProject> Projects {
216
get { return projects; }
219
public IProject DefaultProject {
220
get { return consoleHost.DefaultProject; }
222
consoleHost.DefaultProject = value;
223
OnPropertyChanged(viewModel => viewModel.DefaultProject);
227
public TextEditor TextEditor {
228
get { return packageManagementConsole.TextEditor; }
231
public bool ShutdownConsole()
233
consoleHost.ShutdownConsole();
234
consoleHost.Dispose();
235
return !consoleHost.IsRunning;