~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/MacBuildUtilities.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// BuildUtilities.cs
3
 
//  
4
 
// Author:
5
 
//       Michael Hutchinson <mhutchinson@novell.com>
6
 
// 
7
 
// Copyright (c) 2010 Novell, Inc.
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
 
27
 
using System;
28
 
using System.Collections.Generic;
29
 
using MonoDevelop.Projects;
30
 
using MonoDevelop.Core;
31
 
using MonoDevelop.Core.Execution;
32
 
using System.Diagnostics;
33
 
using MonoDevelop.MacDev.Plist;
34
 
using System.Linq;
35
 
using System.IO;
36
 
using MonoDevelop.Core.ProgressMonitoring;
37
 
using System.Xml;
38
 
 
39
 
namespace MonoDevelop.MacDev
40
 
{
41
 
        public static class MacBuildUtilities
42
 
        {
43
 
                public static bool NeedsBuilding (FilePair fp)
44
 
                {
45
 
                        return fp.NeedsBuilding ();
46
 
                }
47
 
                
48
 
                public static int ExecuteBuildCommand (IProgressMonitor monitor, System.Diagnostics.ProcessStartInfo startInfo)
49
 
                {
50
 
                        return ExecuteBuildCommand (monitor, startInfo, null, null);
51
 
                }
52
 
                
53
 
                /// <summary>Executes a build command, writing output to the monitor.</summary>
54
 
                /// <returns>Whether the command executed successfully.</returns>
55
 
                /// <param name='monitor'>Progress monitor for writing output and handling cancellation.</param>
56
 
                /// <param name='startInfo'>Process start info. Redirection will be enabled if necessary.</param>
57
 
                /// <param name='stdout'>Text writer for stdout. May be null.</param>
58
 
                /// <param name='stderr'>Text writer for stderr. May be null.</param>
59
 
                public static int ExecuteBuildCommand (IProgressMonitor monitor, ProcessStartInfo startInfo,
60
 
                        TextWriter stdout, TextWriter stderr)
61
 
                {
62
 
                        monitor.Log.WriteLine (startInfo.FileName + " " + startInfo.Arguments);
63
 
                        
64
 
                        startInfo.UseShellExecute = false;
65
 
                        startInfo.RedirectStandardError = true;
66
 
                        startInfo.RedirectStandardOutput = true;
67
 
                        
68
 
                        int exitCode = -1;
69
 
                        
70
 
                        TextWriter outWriter = monitor.Log, errWriter = monitor.Log;
71
 
                        LogTextWriter chainedOut = null, chainedErr = null;
72
 
                        
73
 
                        if (stdout != null) {
74
 
                                chainedOut = new LogTextWriter ();
75
 
                                chainedOut.ChainWriter (outWriter);
76
 
                                chainedOut.ChainWriter (stdout);
77
 
                                outWriter = chainedOut;
78
 
                        }
79
 
                        
80
 
                        if (stderr != null) {
81
 
                                chainedErr = new LogTextWriter ();
82
 
                                chainedErr.ChainWriter (errWriter);
83
 
                                chainedErr.ChainWriter (stderr);
84
 
                                errWriter = chainedErr;
85
 
                        }
86
 
                        
87
 
                        var operationMonitor = new AggregatedOperationMonitor (monitor);
88
 
                        
89
 
                        IProcessAsyncOperation p = null;
90
 
                        try {
91
 
                                p = Runtime.ProcessService.StartProcess (startInfo, outWriter, errWriter, null);
92
 
                                operationMonitor.AddOperation (p); //handles cancellation
93
 
                                p.WaitForCompleted ();
94
 
                                exitCode = p.ExitCode;
95
 
                        } finally {
96
 
                                if (chainedErr != null)
97
 
                                        chainedErr.Dispose ();
98
 
                                if (chainedOut != null)
99
 
                                        chainedOut.Dispose ();
100
 
                                if (p != null)
101
 
                                        p.Dispose ();
102
 
                                operationMonitor.Dispose ();
103
 
                        }
104
 
                        
105
 
                        if (exitCode != 0)
106
 
                                monitor.Log.WriteLine ("{0} exited with code {1}", Path.GetFileName (startInfo.FileName), exitCode);
107
 
                        
108
 
                        return exitCode;
109
 
                }
110
 
                
111
 
                public static BuildResult CreateMergedPlist (IProgressMonitor monitor, 
112
 
                        ProjectFile template, string outPath,
113
 
                        Func<PlistDocument,BuildResult> merge)
114
 
                {
115
 
                        var result = new BuildResult ();
116
 
                        
117
 
                        var doc = new PlistDocument ();
118
 
                        if (template != null) {
119
 
                                try {
120
 
                                        doc.LoadFromXmlFile (template.FilePath);
121
 
                                } catch (Exception ex) {
122
 
                                        if (ex is XmlException)
123
 
                                                result.AddError (template.FilePath, ((XmlException)ex).LineNumber,
124
 
                                                                 ((XmlException)ex).LinePosition, null, ex.Message);
125
 
                                        else
126
 
                                                result.AddError (template.FilePath, 0, 0, null, ex.Message);
127
 
                                        monitor.ReportError (GettextCatalog.GetString ("Could not load file '{0}': {1}",
128
 
                                                                                       template.FilePath, ex.Message), null);
129
 
                                        return result;
130
 
                                }
131
 
                        }
132
 
                        
133
 
                        try {
134
 
                                if (result.Append (merge (doc)).ErrorCount > 0)
135
 
                                        return result;
136
 
                        } catch (Exception ex) {
137
 
                                result.AddError ("Error merging Info.plist: " + ex.Message);
138
 
                                LoggingService.LogError ("Error merging Info.plist", ex);
139
 
                                return result;
140
 
                        }
141
 
                        
142
 
                        try {
143
 
                                EnsureDirectoryForFile (outPath);
144
 
                                doc.WriteToFile (outPath);
145
 
                        } catch (Exception ex) {
146
 
                                result.AddError (outPath, 0, 0, null, ex.Message);
147
 
                                monitor.ReportError (GettextCatalog.GetString ("Could not write file '{0}'", outPath), ex);
148
 
                        }
149
 
                        return result;
150
 
                }
151
 
                
152
 
                public static void EnsureDirectoryForFile (string filename)
153
 
                {
154
 
                        string dir = Path.GetDirectoryName (filename);
155
 
                        if (!Directory.Exists (dir))
156
 
                                Directory.CreateDirectory (dir);
157
 
                }
158
 
                
159
 
                public static ProcessStartInfo GetTool (string tool, DotNetProject project, IProgressMonitor monitor,
160
 
                                                   out BuildResult error)
161
 
                {
162
 
                        var toolPath = project.TargetRuntime.GetToolPath (project.TargetFramework, tool);
163
 
                        if (String.IsNullOrEmpty (toolPath)) {
164
 
                                var err = GettextCatalog.GetString ("Error: Unable to find '{0}' tool.", tool);
165
 
                                monitor.ReportError (err, null);
166
 
                                error = new BuildResult ();
167
 
                                error.AddError (null, 0, 0, null, err);
168
 
                                return null;
169
 
                        }
170
 
                        
171
 
                        error = null;
172
 
                        return new ProcessStartInfo (toolPath) {
173
 
                                UseShellExecute = false,
174
 
                                RedirectStandardError = true,
175
 
                                RedirectStandardOutput = true,
176
 
                        };
177
 
                }
178
 
        }
179
 
        
180
 
        public struct FilePair
181
 
        {
182
 
                public FilePair (FilePath input, FilePath output)
183
 
                {
184
 
                        this.Input = input;
185
 
                        this.Output = output;
186
 
                }
187
 
                
188
 
                public FilePath Input, Output;
189
 
                
190
 
                public bool NeedsBuilding ()
191
 
                {
192
 
                        var output = new FileInfo (Output);
193
 
                        if (!output.Exists)
194
 
                                return true;
195
 
                        
196
 
                        var input = new FileInfo (Input);
197
 
                        return input.LastWriteTimeUtc > output.LastWriteTimeUtc;
198
 
                }
199
 
                
200
 
                public void EnsureOutputDirectory ()
201
 
                {
202
 
                        if (!Directory.Exists (Output.ParentDirectory))
203
 
                                Directory.CreateDirectory (Output.ParentDirectory);
204
 
                }
205
 
        }
206
 
}
 
 
b'\\ No newline at end of file'