PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/util/ProjectConfig.cs

#
C# | 282 lines | 197 code | 39 blank | 46 comment | 50 complexity | 817653149dec29ca474623634299587f MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You
  3// may obtain a copy of the license as well as information regarding
  4// copyright ownership at http://nunit.org.
  5// ****************************************************************
  6
  7using System;
  8using System.Text;
  9using System.Collections;
 10using System.IO;
 11using NUnit.Core;
 12
 13namespace NUnit.Util
 14{
 15	public enum BinPathType
 16	{
 17		Auto,
 18		Manual,
 19		None
 20	}
 21
 22	public class ProjectConfig
 23	{
 24		#region Instance Variables
 25
 26		/// <summary>
 27		/// The name of this config
 28		/// </summary>
 29		private string name;
 30
 31		/// <summary>
 32		/// IProject interface of containing project
 33		/// </summary>
 34		protected NUnitProject project = null;
 35
 36		/// <summary>
 37		/// List of the names of the assemblies
 38		/// </summary>
 39		private AssemblyList assemblies;
 40
 41		/// <summary>
 42		/// Base path specific to this configuration
 43		/// </summary>
 44		private string basePath;
 45
 46		/// <summary>
 47		/// Our configuration file, if specified
 48		/// </summary>
 49		private string configFile;
 50
 51		/// <summary>
 52		/// Private bin path, if specified
 53		/// </summary>
 54		private string binPath;
 55
 56		/// <summary>
 57		/// True if assembly paths should be added to bin path
 58		/// </summary>
 59		private BinPathType binPathType = BinPathType.Auto;
 60        
 61        /// <summary>
 62        /// The CLR under which tests are to be run
 63        /// </summary>
 64        private RuntimeFramework runtimeFramework;
 65
 66		#endregion
 67
 68		#region Constructor
 69		public ProjectConfig( string name )
 70		{
 71			this.name = name;
 72			this.assemblies = new AssemblyList();
 73			assemblies.Changed += new EventHandler( assemblies_Changed );
 74		}
 75		#endregion
 76
 77		#region Properties and Events
 78
 79		public NUnitProject Project
 80		{
 81			set { project = value; }
 82		}
 83
 84		public string Name
 85		{
 86			get { return name; }
 87			set 
 88			{
 89				if ( name != value )
 90				{
 91					name = value;
 92					NotifyProjectOfChange();
 93				}
 94			}
 95		}
 96
 97		private bool BasePathSpecified
 98		{
 99			get 
100			{
101				return project.BasePathSpecified || this.basePath != null && this.basePath != "";
102			}
103		}
104
105		/// <summary>
106		/// The base directory for this config - used
107		/// as the application base for loading tests.
108		/// </summary>
109		public string BasePath
110		{
111			get
112			{ 
113				if ( project == null || project.BasePath == null )
114					return basePath;
115
116				if ( basePath == null )
117					return project.BasePath;
118
119				return Path.Combine( project.BasePath, basePath );
120			}
121			set 
122			{
123				if ( BasePath != value )
124				{
125					basePath = value;
126					NotifyProjectOfChange();
127				}
128			}
129		}
130
131		/// <summary>
132		/// The base path relative to the project base
133		/// </summary>
134		public string RelativeBasePath
135		{
136			get
137			{
138				if ( project == null || basePath == null || !Path.IsPathRooted( basePath ) )
139					return basePath;
140
141				return PathUtils.RelativePath( project.BasePath, basePath );
142			}
143		}
144
145		private bool ConfigurationFileSpecified
146		{
147			get { return configFile != null; }
148		}
149
150		public string ConfigurationFile
151		{
152			get 
153			{ 
154				return configFile == null && project != null
155					? project.ConfigurationFile 
156					: configFile;
157			}
158			set
159			{
160				if ( ConfigurationFile != value )
161				{
162					configFile = value;
163					NotifyProjectOfChange();
164				}
165			}
166		}
167
168		public string ConfigurationFilePath
169		{
170			get
171			{		
172				return BasePath != null && ConfigurationFile != null
173					? Path.Combine( BasePath, ConfigurationFile )
174					: ConfigurationFile;
175			}
176		}
177
178		private bool PrivateBinPathSpecified
179		{
180			get { return binPath != null; }
181		}
182
183		/// <summary>
184		/// The Path.PathSeparator-separated path containing all the
185		/// assemblies in the list.
186		/// </summary>
187		public string PrivateBinPath
188		{
189			get	{ return binPath; }
190			set
191			{
192				if ( binPath != value )
193				{
194					binPath = value;
195					binPathType = binPath == null ? BinPathType.Auto : BinPathType.Manual;
196					NotifyProjectOfChange();
197				}
198			}
199		}
200
201		/// <summary>
202		/// How our PrivateBinPath is generated
203		/// </summary>
204		public BinPathType BinPathType
205		{
206			get { return binPathType; }
207			set 
208			{
209				if ( binPathType != value )
210				{
211					binPathType = value;
212					NotifyProjectOfChange();
213				}
214			}
215		}
216
217		/// <summary>
218		/// Return our AssemblyList
219		/// </summary>
220		public AssemblyList Assemblies
221		{
222			get { return assemblies; }
223		}
224
225        public RuntimeFramework RuntimeFramework
226        {
227            get { return runtimeFramework; }
228            set 
229			{
230				if ( runtimeFramework != value )
231				{
232					runtimeFramework = value; 
233					NotifyProjectOfChange();
234				}
235			}
236        }
237		#endregion
238
239		public TestPackage MakeTestPackage()
240		{
241			TestPackage package = new TestPackage( project.ProjectPath );
242
243			if ( !project.IsAssemblyWrapper )
244				foreach ( string assembly in this.Assemblies )
245					package.Assemblies.Add( assembly );
246
247			if ( this.BasePathSpecified || this.PrivateBinPathSpecified || this.ConfigurationFileSpecified )
248			{
249				package.BasePath = this.BasePath;
250				package.PrivateBinPath = this.PrivateBinPath;
251				package.ConfigurationFile = this.ConfigurationFile;
252			}
253
254			package.AutoBinPath = this.BinPathType == BinPathType.Auto;
255            if (this.RuntimeFramework != null)
256                package.Settings["RuntimeFramework"] = this.RuntimeFramework;
257
258            if (project.ProcessModel != ProcessModel.Default)
259                package.Settings["ProcessModel"] = project.ProcessModel;
260
261            if (project.DomainUsage != DomainUsage.Default)
262                package.Settings["DomainUsage"] = project.DomainUsage;
263
264			return package;
265		}
266
267		private void assemblies_Changed( object sender, EventArgs e )
268		{
269			NotifyProjectOfChange();
270		}
271
272        private void NotifyProjectOfChange()
273        {
274            if (project != null)
275            {
276                project.IsDirty = true;
277                if (ReferenceEquals(this, project.ActiveConfig))
278                    project.HasChangesRequiringReload = true;
279            }
280        }
281	}
282}