PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/util/Services/ProjectService.cs

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