From 81b50dc83926cb5e1b290e91ebf57326f5dcff52 Mon Sep 17 00:00:00 2001 From: Matt Ward <ward.matt@gmail.com> Date: Sun, 10 May 2009 10:15:05 +0000 Subject: [PATCH] Add reference to IronPython when converting projects to Python. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4068 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../ConvertProjectToPythonProjectCommand.cs | 21 ++++++++- .../Test/AddInFileTestFixture.cs | 25 ++-------- ...onvertToPythonProjectCommandTestFixture.cs | 36 ++++++++++++-- .../Test/PythonBinding.Tests.csproj | 1 + .../PythonBinding/Test/Utils/AddInHelper.cs | 47 +++++++++++++++++++ ...vedConvertProjectToPythonProjectCommand.cs | 5 ++ .../PythonBinding/Test/Utils/MockProject.cs | 14 ++---- 7 files changed, 112 insertions(+), 37 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/AddInHelper.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertProjectToPythonProjectCommand.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertProjectToPythonProjectCommand.cs index 78475488f7..334e5b8a08 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertProjectToPythonProjectCommand.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ConvertProjectToPythonProjectCommand.cs @@ -36,6 +36,18 @@ namespace ICSharpCode.PythonBinding get { return PythonLanguageBinding.LanguageName; } } + /// <summary> + /// Creates an PythonProject. + /// </summary> + protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject) + { + // Add IronPython reference. + IProject targetProject = base.CreateProject(targetProjectDirectory, sourceProject); + IProjectItemListProvider targetProjectItems = targetProject as IProjectItemListProvider; + targetProjectItems.AddProjectItem(CreateIronPythonReference(targetProject)); + return targetProject; + } + /// <summary> /// Converts C# and VB.NET files to Python and saves the files. /// </summary> @@ -76,6 +88,13 @@ namespace ICSharpCode.PythonBinding protected virtual string GetParseableFileContent(string fileName) { return ParserService.GetParseableFileContent(fileName); - } + } + + ReferenceProjectItem CreateIronPythonReference(IProject project) + { + ReferenceProjectItem reference = new ReferenceProjectItem(project, "IronPython"); + reference.SetMetadata("HintPath", @"$(PythonBinPath)\IronPython.dll"); + return reference; + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/AddInFileTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/AddInFileTestFixture.cs index 6ca3608c31..5e7540afbd 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/AddInFileTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/AddInFileTestFixture.cs @@ -14,6 +14,7 @@ using ICSharpCode.Core; using ICSharpCode.SharpDevelop.DefaultEditor.Codons; using ICSharpCode.TextEditor.Document; using NUnit.Framework; +using PythonBinding.Tests.Utils; namespace PythonBinding.Tests { @@ -704,29 +705,9 @@ namespace PythonBinding.Tests Assert.AreEqual("VBNet", condition["activeproject"]); } - /// <summary> - /// Gets the codon with the specified extension path and name. - /// </summary> - Codon GetCodon(string extensionPath, string name) + Codon GetCodon(string name, string extensionPath) { - if (addin.Paths.ContainsKey(extensionPath)) { - ExtensionPath path = addin.Paths[extensionPath]; - return GetCodon(path.Codons, name); - } - return null; - } - - /// <summary> - /// Gets the codon with the specified name. - /// </summary> - Codon GetCodon(List<Codon> codons, string name) - { - foreach (Codon codon in codons) { - if (codon.Id == name) { - return codon; - } - } - return null; + return AddInHelper.GetCodon(addin, name, extensionPath); } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs index e74ef36df4..62d57f03a5 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/ConvertToPythonProjectCommandTestFixture.cs @@ -9,8 +9,11 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; +using ICSharpCode.Core; using ICSharpCode.NRefactory; using ICSharpCode.PythonBinding; +using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.DefaultEditor.Codons; using ICSharpCode.SharpDevelop.Project; using NUnit.Framework; using PythonBinding.Tests.Utils; @@ -24,10 +27,11 @@ namespace PythonBinding.Tests.Converter FileProjectItem source; FileProjectItem target; MockProject sourceProject; - MockProject targetProject; + IProject targetProject; FileProjectItem textFileSource; FileProjectItem textFileTarget; MockTextEditorProperties mockTextEditorProperties; + ReferenceProjectItem ironPythonReference; string sourceCode = "class Foo\r\n" + "{\r\n" + "}"; @@ -35,6 +39,13 @@ namespace PythonBinding.Tests.Converter [TestFixtureSetUp] public void SetUpFixture() { + List<LanguageBindingDescriptor> bindings = new List<LanguageBindingDescriptor>(); + using (TextReader reader = PythonBindingAddInFile.ReadAddInFile()) { + AddIn addin = AddIn.Load(reader, String.Empty); + bindings.Add(new LanguageBindingDescriptor(AddInHelper.GetCodon(addin, "/SharpDevelop/Workbench/LanguageBindings", "Python"))); + } + LanguageBindingService.SetBindings(bindings); + mockTextEditorProperties = new MockTextEditorProperties(); convertProjectCommand = new DerivedConvertProjectToPythonProjectCommand(mockTextEditorProperties); mockTextEditorProperties.Encoding = Encoding.Unicode; @@ -42,8 +53,7 @@ namespace PythonBinding.Tests.Converter sourceProject = new MockProject(); sourceProject.Directory = @"d:\projects\test"; source = new FileProjectItem(sourceProject, ItemType.Compile, @"src\Program.cs"); - targetProject = new MockProject(); - targetProject.Directory = @"d:\projects\test\converted"; + targetProject = convertProjectCommand.CallCreateProject(@"d:\projects\test\converted", sourceProject); target = new FileProjectItem(targetProject, source.ItemType, source.Include); source.CopyMetadataTo(target); @@ -51,6 +61,14 @@ namespace PythonBinding.Tests.Converter textFileTarget = new FileProjectItem(targetProject, textFileSource.ItemType, textFileSource.Include); textFileSource.CopyMetadataTo(textFileTarget); + foreach (ProjectItem item in targetProject.Items) { + ReferenceProjectItem reference = item as ReferenceProjectItem; + if ((reference != null) && (reference.Name == "IronPython")) { + ironPythonReference = reference; + break; + } + } + convertProjectCommand.AddParseableFileContent(source.FileName, sourceCode); convertProjectCommand.CallConvertFile(source, target); @@ -93,5 +111,17 @@ namespace PythonBinding.Tests.Converter expectedSavedFiles.Add(new ConvertedFile(target.FileName, expectedCode, mockTextEditorProperties.Encoding)); Assert.AreEqual(expectedSavedFiles, convertProjectCommand.SavedFiles); } + + [Test] + public void IronPythonReferenceAddedToProject() + { + Assert.IsNotNull(ironPythonReference, "No IronPython reference added to converted project."); + } + + [Test] + public void IronPythonReferenceHintPath() + { + Assert.AreEqual(@"$(PythonBinPath)\IronPython.dll", ironPythonReference.GetMetadata("HintPath")); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index cc7e6d6fcf..aad2aec93c 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -271,6 +271,7 @@ <Compile Include="RunPythonCommandTestFixture.cs" /> <Compile Include="StopPythonCommandTestFixture.cs" /> <Compile Include="Utils\AddedComponent.cs" /> + <Compile Include="Utils\AddInHelper.cs" /> <Compile Include="Utils\BrowseButtonInfo.cs" /> <Compile Include="Utils\BrowseFolderButtonInfo.cs" /> <Compile Include="Utils\ConvertedFile.cs" /> diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/AddInHelper.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/AddInHelper.cs new file mode 100644 index 0000000000..c19e1652ce --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/AddInHelper.cs @@ -0,0 +1,47 @@ +// <file> +// <copyright see="prj:///doc/copyright.txt"/> +// <license see="prj:///doc/license.txt"/> +// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/> +// <version>$Revision$</version> +// </file> + +using System; +using System.Collections.Generic; +using ICSharpCode.Core; +using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.DefaultEditor.Codons; + +namespace PythonBinding.Tests.Utils +{ + public class AddInHelper + { + AddInHelper() + { + } + + /// <summary> + /// Gets the codon with the specified extension path and name. + /// </summary> + public static Codon GetCodon(AddIn addin, string extensionPath, string name) + { + if (addin.Paths.ContainsKey(extensionPath)) { + ExtensionPath path = addin.Paths[extensionPath]; + return GetCodon(path.Codons, name); + } + return null; + } + + /// <summary> + /// Gets the codon with the specified name. + /// </summary> + public static Codon GetCodon(List<Codon> codons, string name) + { + foreach (Codon codon in codons) { + if (codon.Id == name) { + return codon; + } + } + return null; + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/DerivedConvertProjectToPythonProjectCommand.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/DerivedConvertProjectToPythonProjectCommand.cs index 6141a14c11..bcebe94ec0 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/DerivedConvertProjectToPythonProjectCommand.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/DerivedConvertProjectToPythonProjectCommand.cs @@ -62,6 +62,11 @@ namespace PythonBinding.Tests.Utils ConvertFile(source, target); } + public IProject CallCreateProject(string directory, IProject sourceProject) + { + return base.CreateProject(directory, sourceProject); + } + protected override void LanguageConverterConvertFile(FileProjectItem source, FileProjectItem target) { sourceAndTargetFilesPassedToBaseClass.Add(new SourceAndTargetFile(source, target)); diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProject.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProject.cs index f4e0194df8..b9561ba52c 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProject.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProject.cs @@ -95,11 +95,8 @@ namespace PythonBinding.Tests.Utils } public string RootNamespace { - get { - throw new NotImplementedException(); - } + get { return String.Empty; } set { - throw new NotImplementedException(); } } @@ -176,9 +173,7 @@ namespace PythonBinding.Tests.Utils } public Solution ParentSolution { - get { - throw new NotImplementedException(); - } + get { return new Solution(); } } public string TypeGuid { @@ -209,11 +204,8 @@ namespace PythonBinding.Tests.Utils } public string Name { - get { - throw new NotImplementedException(); - } + get { return String.Empty; } set { - throw new NotImplementedException(); } }