9f6b235dc413d6d373732979ef174013888638b2
[mono.git] / mcs / nunit20 / util / ProjectConfig.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright  2000-2002 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright  2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 using System;
31 using System.Text;
32 using System.Collections;
33 using System.IO;
34 using NUnit.Core;
35
36 namespace NUnit.Util
37 {
38         public enum BinPathType
39         {
40                 Auto,
41                 Manual,
42                 None
43         }
44
45         public class ProjectConfig
46         {
47                 #region Instance Variables
48
49                 /// <summary>
50                 /// The name of this config
51                 /// </summary>
52                 private string name;
53
54                 /// <summary>
55                 /// IProject interface of containing project
56                 /// </summary>
57                 protected NUnitProject project = null;
58
59                 /// <summary>
60                 /// Mark this config as changed
61                 /// </summary>
62                 private bool isDirty = false;
63
64                 /// <summary>
65                 /// List of the names of the assemblies
66                 /// </summary>
67                 private AssemblyList assemblies;
68
69                 /// <summary>
70                 /// Base path specific to this configuration
71                 /// </summary>
72                 private string basePath;
73
74                 /// <summary>
75                 /// Our configuration file, if specified
76                 /// </summary>
77                 private string configFile;
78
79                 /// <summary>
80                 /// Private bin path, if specified
81                 /// </summary>
82                 private string binPath;
83
84                 /// <summary>
85                 /// True if assembly paths should be added to bin path
86                 /// </summary>
87                 private BinPathType binPathType = BinPathType.Auto;
88
89                 #endregion
90
91                 #region Construction
92
93                 public ProjectConfig()
94                 {
95                         this.assemblies = new AssemblyList( this );
96                 }
97
98                 public ProjectConfig( string name )
99                 {
100                         this.name = name;
101                         this.assemblies = new AssemblyList( this );
102                 }
103
104                 #endregion
105
106                 #region Properties and Events
107
108                 public NUnitProject Project
109                 {
110                         get { return project; }
111                         set { project = value; }
112                 }
113
114                 public bool IsDirty
115                 {
116                         get { return isDirty; }
117                         set 
118                         { 
119                                 isDirty = value;
120
121                                 if ( isDirty )
122                                 {
123                                         if ( Changed != null )
124                                                 Changed( this, EventArgs.Empty );
125                                 }
126                         }
127                 }
128
129                 public string Name
130                 {
131                         get { return name; }
132                         set 
133                         {
134                                 if ( name != value )
135                                 {
136                                         name = value; 
137                                         IsDirty = true;
138                                 }
139                         }
140                 }
141
142                 public event EventHandler Changed;
143                 
144                 /// <summary>
145                 /// The base directory for this config - used
146                 /// as the application base for loading tests.
147                 /// </summary>
148                 public string BasePath
149                 {
150                         get
151                         { 
152                                 if ( project == null || project.BasePath == null )
153                                         return basePath;
154
155                                 if ( basePath == null )
156                                         return project.BasePath;
157
158                                 return Path.Combine( project.BasePath, basePath );
159                         }
160                         set 
161                         {
162                                 if ( BasePath != value )
163                                 {
164                                         basePath = value;
165                                         IsDirty = true;
166                                 }
167                         }
168                 }
169
170                 /// <summary>
171                 /// The base path relative to the project base
172                 /// </summary>
173                 public string RelativeBasePath
174                 {
175                         get
176                         {
177                                 if ( project == null || basePath == null || !Path.IsPathRooted( basePath ) )
178                                         return basePath;
179
180                                 return ProjectPath.RelativePath( project.BasePath, basePath );
181                         }
182                 }
183
184                 public string ConfigurationFile
185                 {
186                         get 
187                         { 
188                                 return configFile == null && project != null
189                                         ? project.ConfigurationFile 
190                                         : configFile;
191                         }
192                         set
193                         {
194                                 if ( ConfigurationFile != value )
195                                 {
196                                         configFile = value;
197                                         IsDirty = true;
198                                 }
199                         }
200                 }
201
202                 public string ConfigurationFilePath
203                 {
204                         get
205                         {               
206                                 return BasePath != null && ConfigurationFile != null
207                                         ? Path.Combine( BasePath, ConfigurationFile )
208                                         : ConfigurationFile;
209                         }
210                 }
211
212                 /// <summary>
213                 /// The semicolon-separated path containing all the
214                 /// assemblies in the list.
215                 /// </summary>
216                 public string PrivateBinPath
217                 {
218                         get
219                         {
220                                 switch( binPathType )
221                                 {
222                                         case BinPathType.Manual:
223                                                 return binPath;
224
225                                         case BinPathType.Auto:
226                                                 StringBuilder sb = new StringBuilder(200);
227                                                 ArrayList dirList = new ArrayList();
228
229                                                 foreach( AssemblyListItem assembly in Assemblies )
230                                                 {
231                                                         string dir = ProjectPath.RelativePath( BasePath, Path.GetDirectoryName( assembly.FullPath ) );
232                                                         if ( dir != null && dir != "." && !dirList.Contains( dir ) )
233                                                         {
234                                                                 dirList.Add( dir );
235                                                                 if ( sb.Length > 0 )
236                                                                         sb.Append( ';' );
237                                                                 sb.Append( dir );
238                                                         }
239                                                 }
240
241                                                 return sb.Length == 0 ? null : sb.ToString();
242
243                                         default:
244                                                 return null;
245                                 }
246                         }
247
248                         set
249                         {
250                                 if ( binPath != value )
251                                 {
252                                         binPath = value;
253                                         binPathType = binPath == null ? BinPathType.Auto : BinPathType.Manual;
254                                         IsDirty = true;
255                                 }
256                         }
257                 }
258
259                 /// <summary>
260                 /// How our PrivateBinPath is generated
261                 /// </summary>
262                 public BinPathType BinPathType
263                 {
264                         get { return binPathType; }
265                         set 
266                         {
267                                 if ( binPathType != value )
268                                 {
269                                         binPathType = value;
270                                         IsDirty = true;
271                                 }
272                         }
273                 }
274
275                 /// <summary>
276                 /// Return our AssemblyList
277                 /// </summary>
278                 public AssemblyList Assemblies
279                 {
280                         get { return assemblies; }
281                 }
282
283                 /// <summary>
284                 /// Return an ArrayList with the absolute paths of all assemblies
285                 /// </summary>
286                 public IList AbsolutePaths
287                 {
288                         get
289                         {
290                                 ArrayList paths = new ArrayList();
291                                 foreach( AssemblyListItem assembly in assemblies )
292                                         paths.Add( assembly.FullPath );
293                                 return paths;
294                         }
295                 }
296
297                 /// <summary>
298                 /// Return an ArrayList with the relative paths of all
299                 /// assemblies from the configuration BasePath.
300                 /// </summary>
301                 public IList RelativePaths
302                 {
303                         get
304                         {
305                                 ArrayList paths = new ArrayList();
306                                 foreach( AssemblyListItem assembly in Assemblies )
307                                         paths.Add( ProjectPath.RelativePath( BasePath, assembly.FullPath ) );
308                                 return paths;
309                         }
310                 }
311
312                 /// <summary>
313                 /// Return an ArrayList with the absolute paths of all
314                 /// assemblies that have tests
315                 /// </summary>
316                 public IList TestAssemblies
317                 {
318                         get
319                         {
320                                 ArrayList paths = new ArrayList();
321                                 foreach( AssemblyListItem assembly in Assemblies )
322                                         if ( assembly.HasTests )
323                                                 paths.Add( assembly.FullPath );
324                                 return paths;
325                         }
326                 }
327
328                 #endregion
329
330                 #region Methods
331
332                 public string RelativePathTo( string path )
333                 {
334                         return ProjectPath.RelativePath( BasePath, path );
335                 }
336
337                 #endregion
338         }
339 }