1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
8
using ICSharpCode.SharpDevelop;
9
using ICSharpCode.SharpDevelop.Dom;
10
using ICSharpCode.SharpDevelop.Project;
11
using ICSharpCode.SharpDevelop.Project.Converter;
13
namespace ICSharpCode.RubyBinding
16
/// Converts a C# or VB.NET project to Ruby.
18
public class ConvertProjectToRubyProjectCommand : LanguageConverter
20
public override string TargetLanguageName {
21
get { return RubyProjectBinding.LanguageName; }
25
/// Creates an RubyProject.
27
protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject)
29
RubyProject targetProject = (RubyProject)base.CreateProject(targetProjectDirectory, sourceProject);
34
/// Converts C# and VB.NET files to Ruby and saves the files.
36
protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
38
ParseInformation parseInfo = GetParseInfo(sourceItem.FileName);
39
NRefactoryToRubyConverter converter = NRefactoryToRubyConverter.Create(sourceItem.Include, parseInfo);
40
if (converter != null) {
41
targetItem.Include = ChangeFileExtensionToRubyFileExtension(sourceItem.Include);
43
string code = GetParseableFileContent(sourceItem.FileName);
44
string rubyCode = converter.Convert(code);
46
RubyProject rubyTargetProject = (RubyProject)targetItem.Project;
47
if ((converter.EntryPointMethods.Count > 0) && !rubyTargetProject.HasMainFile) {
48
rubyTargetProject.AddMainFile(targetItem.Include);
49
// Add code to call main method at the end of the file.
50
rubyCode += "\r\n\r\n" + converter.GenerateMainMethodCall(converter.EntryPointMethods[0]);
52
SaveFile(targetItem.FileName, rubyCode, GetDefaultFileEncoding());
54
LanguageConverterConvertFile(sourceItem, targetItem);
59
/// Adds the MainFile property since adding it in the CreateProject method would mean
60
/// it gets removed via the base class CopyProperties method.
62
protected override void CopyProperties(IProject sourceProject, IProject targetProject)
64
base.CopyProperties(sourceProject, targetProject);
68
/// Calls the LanguageConverter class method ConvertFile which copies the source file to the target
69
/// file without any modifications.
71
protected virtual void LanguageConverterConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
73
base.ConvertFile(sourceItem, targetItem);
77
/// Writes the specified file to disk.
79
protected virtual void SaveFile(string fileName, string content, Encoding encoding)
81
File.WriteAllText(fileName, content, encoding);
85
/// Gets the content of the file from the parser service.
87
protected virtual string GetParseableFileContent(string fileName)
89
return ParserService.GetParseableFileContent(fileName).Text;
93
/// Gets the project content for the specified project.
95
protected virtual IProjectContent GetProjectContent(IProject project)
97
return ParserService.GetProjectContent(project);
100
protected virtual ParseInformation GetParseInfo(string fileName)
102
return ParserService.GetParseInformation(fileName);
105
protected virtual Encoding GetDefaultFileEncoding()
107
return FileService.DefaultFileEncoding.GetEncoding();
111
/// Changes the extension to ".rb"
113
static string ChangeFileExtensionToRubyFileExtension(string fileName)
115
return Path.ChangeExtension(fileName, ".rb");