~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/ConvertProjectToRubyProjectCommand.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.IO;
 
6
using System.Text;
 
7
 
 
8
using ICSharpCode.SharpDevelop;
 
9
using ICSharpCode.SharpDevelop.Dom;
 
10
using ICSharpCode.SharpDevelop.Project;
 
11
using ICSharpCode.SharpDevelop.Project.Converter;
 
12
 
 
13
namespace ICSharpCode.RubyBinding
 
14
{
 
15
        /// <summary>
 
16
        /// Converts a C# or VB.NET project to Ruby.
 
17
        /// </summary>
 
18
        public class ConvertProjectToRubyProjectCommand : LanguageConverter
 
19
        {
 
20
                public override string TargetLanguageName {
 
21
                        get { return RubyProjectBinding.LanguageName; }
 
22
                }
 
23
                
 
24
                /// <summary>
 
25
                /// Creates an RubyProject.
 
26
                /// </summary>
 
27
                protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject)
 
28
                {
 
29
                        RubyProject targetProject = (RubyProject)base.CreateProject(targetProjectDirectory, sourceProject);
 
30
                        return targetProject;
 
31
                }
 
32
                
 
33
                /// <summary>
 
34
                /// Converts C# and VB.NET files to Ruby and saves the files.
 
35
                /// </summary>
 
36
                protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
 
37
                {
 
38
                        ParseInformation parseInfo = GetParseInfo(sourceItem.FileName);
 
39
                        NRefactoryToRubyConverter converter = NRefactoryToRubyConverter.Create(sourceItem.Include, parseInfo);
 
40
                        if (converter != null) {
 
41
                                targetItem.Include = ChangeFileExtensionToRubyFileExtension(sourceItem.Include);
 
42
 
 
43
                                string code = GetParseableFileContent(sourceItem.FileName);
 
44
                                string rubyCode = converter.Convert(code);
 
45
                        
 
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]);
 
51
                                }
 
52
                                SaveFile(targetItem.FileName, rubyCode, GetDefaultFileEncoding());
 
53
                        } else {
 
54
                                LanguageConverterConvertFile(sourceItem, targetItem);
 
55
                        }
 
56
                }
 
57
 
 
58
                /// <summary>
 
59
                /// Adds the MainFile property since adding it in the CreateProject method would mean
 
60
                /// it gets removed via the base class CopyProperties method.
 
61
                /// </summary>
 
62
                protected override void CopyProperties(IProject sourceProject, IProject targetProject)
 
63
                {
 
64
                        base.CopyProperties(sourceProject, targetProject);
 
65
                }
 
66
                
 
67
                /// <summary>
 
68
                /// Calls the LanguageConverter class method ConvertFile which copies the source file to the target
 
69
                /// file without any modifications.
 
70
                /// </summary>
 
71
                protected virtual void LanguageConverterConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
 
72
                {
 
73
                        base.ConvertFile(sourceItem, targetItem);
 
74
                }
 
75
                
 
76
                /// <summary>
 
77
                /// Writes the specified file to disk.
 
78
                /// </summary>
 
79
                protected virtual void SaveFile(string fileName, string content, Encoding encoding)
 
80
                {
 
81
                        File.WriteAllText(fileName, content, encoding);
 
82
                }
 
83
                
 
84
                /// <summary>
 
85
                /// Gets the content of the file from the parser service.
 
86
                /// </summary>
 
87
                protected virtual string GetParseableFileContent(string fileName)
 
88
                {
 
89
                        return ParserService.GetParseableFileContent(fileName).Text;
 
90
                }
 
91
                
 
92
                /// <summary>
 
93
                /// Gets the project content for the specified project.
 
94
                /// </summary>
 
95
                protected virtual IProjectContent GetProjectContent(IProject project)
 
96
                {
 
97
                        return ParserService.GetProjectContent(project);
 
98
                }
 
99
                
 
100
                protected virtual ParseInformation GetParseInfo(string fileName)
 
101
                {
 
102
                        return ParserService.GetParseInformation(fileName);
 
103
                }
 
104
                
 
105
                protected virtual Encoding GetDefaultFileEncoding()
 
106
                {
 
107
                        return FileService.DefaultFileEncoding.GetEncoding();
 
108
                }
 
109
                
 
110
                /// <summary>
 
111
                /// Changes the extension to ".rb"
 
112
                /// </summary>
 
113
                static string ChangeFileExtensionToRubyFileExtension(string fileName)
 
114
                {
 
115
                        return Path.ChangeExtension(fileName, ".rb");   
 
116
                }
 
117
        }
 
118
}