/src/NUnit/util/Services/ProjectService.cs
C# | 177 lines | 110 code | 33 blank | 34 comment | 8 complexity | fbd9efd930f8b1d7b1454956aec99685 MD5 | raw file
1// **************************************************************** 2// Copyright 2008, Charlie Poole 3// This is free software licensed under the NUnit license. You may 4// obtain a copy of the license at http://nunit.org 5// **************************************************************** 6using System; 7using System.IO; 8using NUnit.Core; 9using NUnit.Util.Extensibility; 10using NUnit.Util.ProjectConverters; 11 12namespace NUnit.Util 13{ 14 /// <summary> 15 /// Summary description for ProjectService. 16 /// </summary> 17 public class ProjectService : IProjectConverter, IService 18 { 19 /// <summary> 20 /// Seed used to generate names for new projects 21 /// </summary> 22 private int projectSeed = 0; 23 24 /// <summary> 25 /// The extension used for test projects 26 /// </summary> 27 //private static readonly string nunitExtension = ".nunit"; 28 29 /// <summary> 30 /// Array of all installed ProjectConverters 31 /// </summary> 32 IProjectConverter[] converters = new IProjectConverter[] 33 { 34 new VisualStudioConverter() 35 }; 36 37 #region Instance Methods 38 public bool CanLoadProject(string path) 39 { 40 return NUnitProject.IsNUnitProjectFile(path) || CanConvertFrom(path); 41 } 42 43 public NUnitProject LoadProject(string path) 44 { 45 if ( NUnitProject.IsNUnitProjectFile(path) ) 46 { 47 NUnitProject project = new NUnitProject( path ); 48 project.Load(); 49 return project; 50 } 51 52 return ConvertFrom(path); 53 } 54 55 /// <summary> 56 /// Creates a project to wrap a list of assemblies 57 /// </summary> 58 public NUnitProject WrapAssemblies( string[] assemblies ) 59 { 60 // if only one assembly is passed in then the configuration file 61 // should follow the name of the assembly. This will only happen 62 // if the LoadAssembly method is called. Currently the console ui 63 // does not differentiate between having one or multiple assemblies 64 // passed in. 65 if ( assemblies.Length == 1) 66 return WrapAssembly(assemblies[0]); 67 68 69 NUnitProject project = Services.ProjectService.EmptyProject(); 70 ProjectConfig config = new ProjectConfig( "Default" ); 71 foreach( string assembly in assemblies ) 72 { 73 string fullPath = Path.GetFullPath( assembly ); 74 75 if ( !File.Exists( fullPath ) ) 76 throw new FileNotFoundException( string.Format( "Assembly not found: {0}", fullPath ) ); 77 78 config.Assemblies.Add( fullPath ); 79 } 80 81 project.Configs.Add( config ); 82 83 // TODO: Deduce application base, and provide a 84 // better value for loadpath and project path 85 // analagous to how new projects are handled 86 string basePath = Path.GetDirectoryName( Path.GetFullPath( assemblies[0] ) ); 87 project.ProjectPath = Path.Combine( basePath, project.Name + ".nunit" ); 88 89 project.IsDirty = true; 90 91 return project; 92 } 93 94 /// <summary> 95 /// Creates a project to wrap an assembly 96 /// </summary> 97 public NUnitProject WrapAssembly( string assemblyPath ) 98 { 99 if ( !File.Exists( assemblyPath ) ) 100 throw new FileNotFoundException( string.Format( "Assembly not found: {0}", assemblyPath ) ); 101 102 string fullPath = Path.GetFullPath( assemblyPath ); 103 104 NUnitProject project = new NUnitProject( fullPath ); 105 106 ProjectConfig config = new ProjectConfig( "Default" ); 107 config.Assemblies.Add( fullPath ); 108 project.Configs.Add( config ); 109 110 project.IsAssemblyWrapper = true; 111 project.IsDirty = false; 112 113 return project; 114 } 115 116 public string GenerateProjectName() 117 { 118 return string.Format( "Project{0}", ++projectSeed ); 119 } 120 121 public NUnitProject EmptyProject() 122 { 123 return new NUnitProject( GenerateProjectName() ); 124 } 125 126 public NUnitProject NewProject() 127 { 128 NUnitProject project = EmptyProject(); 129 130 project.Configs.Add( "Debug" ); 131 project.Configs.Add( "Release" ); 132 project.IsDirty = false; 133 134 return project; 135 } 136 137 public void SaveProject( NUnitProject project ) 138 { 139 project.Save(); 140 } 141 #endregion 142 143 #region IProjectConverter Members 144 public bool CanConvertFrom(string path) 145 { 146 foreach( IProjectConverter converter in converters ) 147 if ( converter.CanConvertFrom(path) ) 148 return true; 149 150 return false; 151 } 152 153 public NUnitProject ConvertFrom(string path) 154 { 155 foreach( IProjectConverter converter in converters ) 156 { 157 if ( converter.CanConvertFrom( path ) ) 158 return converter.ConvertFrom( path ); 159 } 160 161 return WrapAssembly(path); 162 } 163 #endregion 164 165 #region IService Members 166 public void InitializeService() 167 { 168 // TODO: Add ProjectLoader.InitializeService implementation 169 } 170 171 public void UnloadService() 172 { 173 // TODO: Add ProjectLoader.UnloadService implementation 174 } 175 #endregion 176 } 177}