~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Projects/CustomCommand.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
                        set { pauseExternalConsole = value; }
86
86
                }
87
87
                
 
88
                public string GetCommandFile (IWorkspaceObject entry, ConfigurationSelector configuration)
 
89
                {
 
90
                        string exe, args;
 
91
                        StringTagModel tagSource = GetTagModel (entry, configuration);
 
92
                        ParseCommand (tagSource, out exe, out args);
 
93
                        return exe;
 
94
                }
 
95
                
 
96
                public string GetCommandArgs (IWorkspaceObject entry, ConfigurationSelector configuration)
 
97
                {
 
98
                        string exe, args;
 
99
                        StringTagModel tagSource = GetTagModel (entry, configuration);
 
100
                        ParseCommand (tagSource, out exe, out args);
 
101
                        return args;
 
102
                }
 
103
                
 
104
                public FilePath GetCommandWorkingDir (IWorkspaceObject entry, ConfigurationSelector configuration)
 
105
                {
 
106
                        StringTagModel tagSource = GetTagModel (entry, configuration);
 
107
                        return (string.IsNullOrEmpty (workingdir) ? entry.BaseDirectory : (FilePath) StringParserService.Parse (workingdir, tagSource));
 
108
                }
 
109
                
88
110
                public CustomCommand Clone ()
89
111
                {
90
112
                        CustomCommand cmd = new CustomCommand ();
97
119
                        return cmd;
98
120
                }
99
121
                
 
122
                StringTagModel GetTagModel (IWorkspaceObject entry, ConfigurationSelector configuration)
 
123
                {
 
124
                        if (entry is SolutionItem)
 
125
                                return ((SolutionItem)entry).GetStringTagModel (configuration);
 
126
                        else if (entry is WorkspaceItem)
 
127
                                return ((WorkspaceItem)entry).GetStringTagModel ();
 
128
                        else
 
129
                                return new StringTagModel ();
 
130
                }
 
131
                
 
132
                void ParseCommand (StringTagModel tagSource, out string cmd, out string args)
 
133
                {
 
134
                        if (command.Length > 0 && command [0] == '"') {
 
135
                                int n = command.IndexOf ('"', 1);
 
136
                                if (n != -1) {
 
137
                                        cmd = command.Substring (1, n - 1);
 
138
                                        args = command.Substring (n + 1).Trim ();
 
139
                                }
 
140
                                else {
 
141
                                        cmd = command;
 
142
                                        args = string.Empty;
 
143
                                }
 
144
                        }
 
145
                        else {
 
146
                                int i = command.IndexOf (' ');
 
147
                                if (i != -1) {
 
148
                                        cmd = command.Substring (0, i);
 
149
                                        args = command.Substring (i + 1).Trim ();
 
150
                                } else {
 
151
                                        cmd = command;
 
152
                                        args = string.Empty;
 
153
                                }
 
154
                        }
 
155
                        cmd = StringParserService.Parse (cmd, tagSource);
 
156
                        args = StringParserService.Parse (args, tagSource);
 
157
                }
 
158
                
100
159
                public void Execute (IProgressMonitor monitor, IWorkspaceObject entry, ConfigurationSelector configuration)
101
160
                {
102
161
                        Execute (monitor, entry, null, configuration);
103
162
                }
104
163
                
105
 
                public bool CanExecute (ExecutionContext context, ConfigurationSelector configuration)
 
164
                public bool CanExecute (IWorkspaceObject entry, ExecutionContext context, ConfigurationSelector configuration)
106
165
                {
107
 
                        string exe = command;
108
 
                        int i = exe.IndexOf (' ');
109
 
                        if (i != -1)
110
 
                                exe = command.Substring (0, i);
111
 
                        
 
166
                        string exe, args;
 
167
                        StringTagModel tagSource = GetTagModel (entry, configuration);
 
168
                        ParseCommand (tagSource, out exe, out args);
112
169
                        ExecutionCommand cmd = Runtime.ProcessService.CreateCommand (exe);
113
170
                        return context == null || context.ExecutionHandler.CanExecute (cmd);
114
171
                }
115
172
                
116
173
                public void Execute (IProgressMonitor monitor, IWorkspaceObject entry, ExecutionContext context, ConfigurationSelector configuration)
117
174
                {
118
 
                        StringTagModel tagSource;
119
 
                        
120
 
                        if (entry is SolutionItem)
121
 
                                tagSource = ((SolutionItem)entry).GetStringTagModel (configuration);
122
 
                        else if (entry is WorkspaceItem)
123
 
                                tagSource = ((WorkspaceItem)entry).GetStringTagModel ();
124
 
                        else
125
 
                                tagSource = new StringTagModel ();
126
 
                        
127
175
                        if (string.IsNullOrEmpty (command))
128
176
                                return;
129
177
                        
130
 
                        int i = command.IndexOf (' ');
131
 
                        string exe;
132
 
                        string args = string.Empty;
133
 
                        if (i == -1) {
134
 
                                exe = command;
135
 
                                args = string.Empty;
136
 
                        } else {
137
 
                                exe = command.Substring (0, i);
138
 
                                args = StringParserService.Parse (command.Substring (i + 1), tagSource);
139
 
                        }
 
178
                        StringTagModel tagSource = GetTagModel (entry, configuration);
 
179
                        
 
180
                        string exe, args;
 
181
                        ParseCommand (tagSource, out exe, out args);
140
182
                        
141
183
                        monitor.Log.WriteLine (GettextCatalog.GetString ("Executing: {0} {1}", exe, args));
142
184
 
143
 
                        FilePath dir = (string.IsNullOrEmpty (workingdir) ? entry.BaseDirectory : (FilePath) StringParserService.Parse (workingdir, tagSource));
 
185
                        FilePath dir = GetCommandWorkingDir (entry, configuration);
144
186
 
145
187
                        FilePath localPath = entry.BaseDirectory.Combine (exe);
146
188
                        if (File.Exists (localPath))
148
190
                        
149
191
                        IProcessAsyncOperation oper;
150
192
                        
151
 
                        if (context != null) {
152
 
                                IConsole console;
153
 
                                if (externalConsole)
154
 
                                        console = context.ExternalConsoleFactory.CreateConsole (!pauseExternalConsole);
155
 
                                else
156
 
                                        console = context.ConsoleFactory.CreateConsole (!pauseExternalConsole);
157
 
 
158
 
                                ExecutionCommand cmd = Runtime.ProcessService.CreateCommand (exe);
159
 
                                ProcessExecutionCommand pcmd = cmd as ProcessExecutionCommand;
160
 
                                if (pcmd != null) {
161
 
                                        pcmd.Arguments = args;
162
 
                                        pcmd.WorkingDirectory = dir;
163
 
                                }
164
 
                                oper = context.ExecutionHandler.Execute (cmd, console);
165
 
                        }
166
 
                        else {
167
 
                                if (externalConsole) {
168
 
                                        IConsole console = ExternalConsoleFactory.Instance.CreateConsole (!pauseExternalConsole);
169
 
                                        oper = Runtime.ProcessService.StartConsoleProcess (exe, args, dir, console, null);
170
 
                                } else {
171
 
                                        oper = Runtime.ProcessService.StartProcess (exe, args, dir, monitor.Log, monitor.Log, null, false);
172
 
                                }
 
193
                        try {
 
194
                                if (context != null) {
 
195
                                        IConsole console;
 
196
                                        if (externalConsole)
 
197
                                                console = context.ExternalConsoleFactory.CreateConsole (!pauseExternalConsole);
 
198
                                        else
 
199
                                                console = context.ConsoleFactory.CreateConsole (!pauseExternalConsole);
 
200
        
 
201
                                        ExecutionCommand cmd = Runtime.ProcessService.CreateCommand (exe);
 
202
                                        ProcessExecutionCommand pcmd = cmd as ProcessExecutionCommand;
 
203
                                        if (pcmd != null) {
 
204
                                                pcmd.Arguments = args;
 
205
                                                pcmd.WorkingDirectory = dir;
 
206
                                        }
 
207
                                        oper = context.ExecutionHandler.Execute (cmd, console);
 
208
                                }
 
209
                                else {
 
210
                                        if (externalConsole) {
 
211
                                                IConsole console = ExternalConsoleFactory.Instance.CreateConsole (!pauseExternalConsole);
 
212
                                                oper = Runtime.ProcessService.StartConsoleProcess (exe, args, dir, console, null);
 
213
                                        } else {
 
214
                                                oper = Runtime.ProcessService.StartProcess (exe, args, dir, monitor.Log, monitor.Log, null, false);
 
215
                                        }
 
216
                                }
 
217
                        } catch (Exception ex) {
 
218
                                throw new UserException (GettextCatalog.GetString ("Command execution failed: {0}",ex.Message));
173
219
                        }
174
220
                        
175
221
                        monitor.CancelRequested += delegate {