Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4063 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
73 changed files with 695 additions and 196 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,30 +0,0 @@
@@ -1,30 +0,0 @@
|
||||
// <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.CodeDom; |
||||
using System.CodeDom.Compiler; |
||||
using ICSharpCode.NRefactory; |
||||
using ICSharpCode.NRefactory.Ast; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class CSharpToPythonConverter : NRefactoryToPythonConverter |
||||
{ |
||||
public CSharpToPythonConverter() |
||||
{ |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Converts the C# source code to Python.
|
||||
/// </summary>
|
||||
public string Convert(string source) |
||||
{ |
||||
return Convert(source, SupportedLanguage.CSharp); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
// <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.IO; |
||||
using System.Text; |
||||
using ICSharpCode.SharpDevelop; |
||||
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; |
||||
using ICSharpCode.SharpDevelop.Project; |
||||
using ICSharpCode.SharpDevelop.Project.Converter; |
||||
using ICSharpCode.TextEditor.Document; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
/// <summary>
|
||||
/// Converts a C# or VB.NET project to Python.
|
||||
/// </summary>
|
||||
public class ConvertProjectToPythonProjectCommand : LanguageConverter |
||||
{ |
||||
ITextEditorProperties textEditorProperties; |
||||
|
||||
public ConvertProjectToPythonProjectCommand(ITextEditorProperties textEditorProperties) |
||||
{ |
||||
this.textEditorProperties = textEditorProperties; |
||||
} |
||||
|
||||
public ConvertProjectToPythonProjectCommand() : this(SharpDevelopTextEditorProperties.Instance) |
||||
{ |
||||
} |
||||
|
||||
public override string TargetLanguageName { |
||||
get { return PythonLanguageBinding.LanguageName; } |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Converts C# and VB.NET files to Python and saves the files.
|
||||
/// </summary>
|
||||
protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem) |
||||
{ |
||||
NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(sourceItem.Include); |
||||
if (converter != null) { |
||||
targetItem.Include = Path.ChangeExtension(sourceItem.Include, ".py"); |
||||
|
||||
string code = GetParseableFileContent(sourceItem.FileName); |
||||
string pythonCode = converter.Convert(code); |
||||
SaveFile(targetItem.FileName, pythonCode, textEditorProperties.Encoding); |
||||
} else { |
||||
LanguageConverterConvertFile(sourceItem, targetItem); |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Calls the LanguageConverter class method ConvertFile which copies the source file to the target
|
||||
/// file without any modifications.
|
||||
/// </summary>
|
||||
protected virtual void LanguageConverterConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem) |
||||
{ |
||||
base.ConvertFile(sourceItem, targetItem); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Writes the specified file to disk.
|
||||
/// </summary>
|
||||
protected virtual void SaveFile(string fileName, string content, Encoding encoding) |
||||
{ |
||||
File.WriteAllText(fileName, content, encoding); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets the content of the file from the parser service.
|
||||
/// </summary>
|
||||
protected virtual string GetParseableFileContent(string fileName) |
||||
{ |
||||
return ParserService.GetParseableFileContent(fileName); |
||||
} |
||||
} |
||||
} |
@ -1,33 +0,0 @@
@@ -1,33 +0,0 @@
|
||||
// <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.CodeDom; |
||||
using System.CodeDom.Compiler; |
||||
using ICSharpCode.NRefactory; |
||||
using ICSharpCode.NRefactory.Ast; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
/// <summary>
|
||||
/// Converts VB.NET to Python.
|
||||
/// </summary>
|
||||
public class VBNetToPythonConverter : NRefactoryToPythonConverter |
||||
{ |
||||
public VBNetToPythonConverter() |
||||
{ |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Converts the VB.NET source code to Python.
|
||||
/// </summary>
|
||||
public string Convert(string source) |
||||
{ |
||||
return Convert(source, SupportedLanguage.VBNet); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,97 @@
@@ -0,0 +1,97 @@
|
||||
// <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 System.IO; |
||||
using System.Text; |
||||
using ICSharpCode.NRefactory; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Project; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Converter |
||||
{ |
||||
[TestFixture] |
||||
public class ConvertToPythonProjectCommandTestFixture |
||||
{ |
||||
DerivedConvertProjectToPythonProjectCommand convertProjectCommand; |
||||
FileProjectItem source; |
||||
FileProjectItem target; |
||||
MockProject sourceProject; |
||||
MockProject targetProject; |
||||
FileProjectItem textFileSource; |
||||
FileProjectItem textFileTarget; |
||||
MockTextEditorProperties mockTextEditorProperties; |
||||
string sourceCode = "class Foo\r\n" + |
||||
"{\r\n" + |
||||
"}"; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
mockTextEditorProperties = new MockTextEditorProperties(); |
||||
convertProjectCommand = new DerivedConvertProjectToPythonProjectCommand(mockTextEditorProperties); |
||||
mockTextEditorProperties.Encoding = Encoding.Unicode; |
||||
|
||||
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"; |
||||
target = new FileProjectItem(targetProject, source.ItemType, source.Include); |
||||
source.CopyMetadataTo(target); |
||||
|
||||
textFileSource = new FileProjectItem(sourceProject, ItemType.None, @"src\readme.txt"); |
||||
textFileTarget = new FileProjectItem(targetProject, textFileSource.ItemType, textFileSource.Include); |
||||
textFileSource.CopyMetadataTo(textFileTarget); |
||||
|
||||
convertProjectCommand.AddParseableFileContent(source.FileName, sourceCode); |
||||
|
||||
convertProjectCommand.CallConvertFile(source, target); |
||||
convertProjectCommand.CallConvertFile(textFileSource, textFileTarget); |
||||
} |
||||
|
||||
[Test] |
||||
public void CommandHasPythonTargetLanguage() |
||||
{ |
||||
Assert.AreEqual(PythonLanguageBinding.LanguageName, convertProjectCommand.TargetLanguageName); |
||||
} |
||||
|
||||
[Test] |
||||
public void TargetFileExtensionChanged() |
||||
{ |
||||
Assert.AreEqual(@"src\Program.py", target.Include); |
||||
} |
||||
|
||||
[Test] |
||||
public void TextFileTargetFileExtensionUnchanged() |
||||
{ |
||||
Assert.AreEqual(@"src\readme.txt", textFileTarget.Include); |
||||
} |
||||
|
||||
[Test] |
||||
public void FilesPassedToBaseClassForConversion() |
||||
{ |
||||
List<SourceAndTargetFile> expectedFiles = new List<SourceAndTargetFile>(); |
||||
expectedFiles.Add(new SourceAndTargetFile(textFileSource, textFileTarget)); |
||||
Assert.AreEqual(expectedFiles, convertProjectCommand.SourceAndTargetFilesPassedToBaseClass); |
||||
} |
||||
|
||||
[Test] |
||||
public void ExpectedCodeWrittenToFile() |
||||
{ |
||||
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(); |
||||
string expectedCode = converter.Convert(sourceCode, SupportedLanguage.CSharp); |
||||
|
||||
List<ConvertedFile> expectedSavedFiles = new List<ConvertedFile>(); |
||||
expectedSavedFiles.Add(new ConvertedFile(target.FileName, expectedCode, mockTextEditorProperties.Encoding)); |
||||
Assert.AreEqual(expectedSavedFiles, convertProjectCommand.SavedFiles); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
// <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 ICSharpCode.PythonBinding; |
||||
using ICSharpCode.NRefactory; |
||||
using NUnit.Framework; |
||||
|
||||
namespace PythonBinding.Tests.Converter |
||||
{ |
||||
[TestFixture] |
||||
public class ConverterSupportedLanguageTests |
||||
{ |
||||
[Test] |
||||
public void CSharpSupportedLanguage() |
||||
{ |
||||
NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(".cs"); |
||||
Assert.AreEqual(SupportedLanguage.CSharp, converter.SupportedLanguage); |
||||
} |
||||
|
||||
[Test] |
||||
public void VBNetSupportedLanguage() |
||||
{ |
||||
NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(".vb"); |
||||
Assert.AreEqual(SupportedLanguage.VBNet, converter.SupportedLanguage); |
||||
} |
||||
|
||||
[Test] |
||||
public void CSharpCaseInsensitive() |
||||
{ |
||||
NRefactoryToPythonConverter converter = NRefactoryToPythonConverter.Create(".CS"); |
||||
Assert.AreEqual(SupportedLanguage.CSharp, converter.SupportedLanguage); |
||||
} |
||||
|
||||
[Test] |
||||
public void NullFileName() |
||||
{ |
||||
Assert.IsNull(NRefactoryToPythonConverter.Create(null)); |
||||
} |
||||
|
||||
[Test] |
||||
public void TextFileCannotBeConverted() |
||||
{ |
||||
Assert.IsNull(NRefactoryToPythonConverter.Create(".txt")); |
||||
} |
||||
|
||||
[Test] |
||||
public void CanConvertCSharpFiles() |
||||
{ |
||||
Assert.IsTrue(NRefactoryToPythonConverter.CanConvert(".cs")); |
||||
} |
||||
|
||||
[Test] |
||||
public void CanConvertVBNetFiles() |
||||
{ |
||||
Assert.IsTrue(NRefactoryToPythonConverter.CanConvert(".vb")); |
||||
} |
||||
|
||||
[Test] |
||||
public void CanConvertIsCaseInsensitive() |
||||
{ |
||||
Assert.IsTrue(NRefactoryToPythonConverter.CanConvert(".CS")); |
||||
} |
||||
|
||||
[Test] |
||||
public void CannotConvertTextFile() |
||||
{ |
||||
Assert.IsFalse(NRefactoryToPythonConverter.CanConvert(".txt")); |
||||
} |
||||
|
||||
[Test] |
||||
public void CannotConvertNullFileName() |
||||
{ |
||||
Assert.IsFalse(NRefactoryToPythonConverter.CanConvert(null)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
// <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.Text; |
||||
|
||||
namespace PythonBinding.Tests.Utils |
||||
{ |
||||
/// <summary>
|
||||
/// Stores the filename and the code for the converted file.
|
||||
/// </summary>
|
||||
public class ConvertedFile |
||||
{ |
||||
public string FileName; |
||||
public string Text; |
||||
public Encoding Encoding; |
||||
|
||||
public ConvertedFile(string fileName, string text, Encoding encoding) |
||||
{ |
||||
this.FileName = fileName; |
||||
this.Text = text; |
||||
this.Encoding = encoding; |
||||
} |
||||
|
||||
public override string ToString() |
||||
{ |
||||
return "FileName: " + FileName + "\r\n" + |
||||
"Encoding: " + Encoding + "\r\n" + |
||||
"Text: " + Text; |
||||
} |
||||
|
||||
public override bool Equals(object obj) |
||||
{ |
||||
ConvertedFile convertedFile = obj as ConvertedFile; |
||||
if (convertedFile != null) { |
||||
return FileName == convertedFile.FileName && Text == convertedFile.Text && Encoding == convertedFile.Encoding; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public override int GetHashCode() |
||||
{ |
||||
return FileName.GetHashCode(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
// <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 System.Text; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.SharpDevelop.Project; |
||||
using ICSharpCode.TextEditor.Document; |
||||
|
||||
namespace PythonBinding.Tests.Utils |
||||
{ |
||||
public struct SourceAndTargetFile |
||||
{ |
||||
public FileProjectItem Source; |
||||
public FileProjectItem Target; |
||||
|
||||
public SourceAndTargetFile(FileProjectItem source, FileProjectItem target) |
||||
{ |
||||
this.Source = source; |
||||
this.Target = target; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Used to test the ConvertProjectToPythonProjectCommand class.
|
||||
/// </summary>
|
||||
public class DerivedConvertProjectToPythonProjectCommand : ConvertProjectToPythonProjectCommand |
||||
{ |
||||
List<SourceAndTargetFile> sourceAndTargetFilesPassedToBaseClass = new List<SourceAndTargetFile>(); |
||||
List<ConvertedFile> savedFiles = new List<ConvertedFile>(); |
||||
List<ConvertedFile> parseableFileContent = new List<ConvertedFile>(); |
||||
|
||||
public DerivedConvertProjectToPythonProjectCommand(ITextEditorProperties textEditorProperties) |
||||
: base(textEditorProperties) |
||||
{ |
||||
} |
||||
|
||||
public List<SourceAndTargetFile> SourceAndTargetFilesPassedToBaseClass { |
||||
get { return sourceAndTargetFilesPassedToBaseClass; } |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets the files converted and saved.
|
||||
/// </summary>
|
||||
public List<ConvertedFile> SavedFiles { |
||||
get { return savedFiles; } |
||||
} |
||||
|
||||
public void AddParseableFileContent(string fileName, string content) |
||||
{ |
||||
parseableFileContent.Add(new ConvertedFile(fileName, content, null)); |
||||
} |
||||
|
||||
public void CallConvertFile(FileProjectItem source, FileProjectItem target) |
||||
{ |
||||
ConvertFile(source, target); |
||||
} |
||||
|
||||
protected override void LanguageConverterConvertFile(FileProjectItem source, FileProjectItem target) |
||||
{ |
||||
sourceAndTargetFilesPassedToBaseClass.Add(new SourceAndTargetFile(source, target)); |
||||
} |
||||
|
||||
protected override void SaveFile(string fileName, string content, Encoding encoding) |
||||
{ |
||||
savedFiles.Add(new ConvertedFile(fileName, content, encoding)); |
||||
} |
||||
|
||||
protected override string GetParseableFileContent(string fileName) |
||||
{ |
||||
foreach (ConvertedFile file in parseableFileContent) { |
||||
if (file.FileName == fileName) { |
||||
return file.Text; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
} |
Binary file not shown.
Loading…
Reference in new issue