Browse Source
Replaced most ToLower() and ToUpper() calls with ToLowerInvariant() and ToUpperInvariant(), or with string.Equals(, StringComparison.OrdinalIgnoreCase). This fixes file name comparisons in cultures with different ToLower/ToUpper rules. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@988 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
57 changed files with 501 additions and 98 deletions
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
/* |
||||
* Created by SharpDevelop. |
||||
* User: Daniel Grunwald |
||||
* Date: 14.01.2006 |
||||
* Time: 17:05 |
||||
*/ |
||||
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.SharpDevelop.Project; |
||||
using ICSharpCode.SharpDevelop.Project.Converter; |
||||
using ICSharpCode.SharpDevelop.Internal.Templates; |
||||
|
||||
using ICSharpCode.NRefactory.PrettyPrinter; |
||||
using ICSharpCode.NRefactory.Parser; |
||||
using Boo.Lang.Compiler; |
||||
using Boo.Lang.Compiler.Ast; |
||||
using NRefactoryToBooConverter; |
||||
|
||||
namespace Grunwald.BooBinding |
||||
{ |
||||
public class ProjectToBooConverter : LanguageConverter |
||||
{ |
||||
public override string TargetLanguageName { |
||||
get { |
||||
return BooLanguageBinding.LanguageName; |
||||
} |
||||
} |
||||
|
||||
CompilerErrorCollection errors = new CompilerErrorCollection(); |
||||
CompilerWarningCollection warnings = new CompilerWarningCollection(); |
||||
|
||||
protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject) |
||||
{ |
||||
errors.Clear(); |
||||
warnings.Clear(); |
||||
|
||||
ProjectCreateInformation info = new ProjectCreateInformation(); |
||||
info.ProjectBasePath = targetProjectDirectory; |
||||
info.ProjectName = sourceProject.Name; |
||||
info.OutputProjectFileName = Path.Combine(targetProjectDirectory, Path.GetFileNameWithoutExtension(sourceProject.FileName) + ".booproj"); |
||||
return new BooProject(info); |
||||
} |
||||
|
||||
protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem) |
||||
{ |
||||
string ext = Path.GetExtension(sourceItem.FileName); |
||||
if (".cs".Equals(ext, StringComparison.OrdinalIgnoreCase) || ".vb".Equals(ext, StringComparison.OrdinalIgnoreCase)) { |
||||
Module module; |
||||
IList<ICSharpCode.NRefactory.Parser.ISpecial> specials; |
||||
CompileUnit compileUnit = new CompileUnit(); |
||||
using (StringReader r = new StringReader(ParserService.GetParseableFileContent(sourceItem.FileName))) { |
||||
module = Parser.ParseModule(compileUnit, r, ConvertBuffer.ApplySettings(sourceItem.VirtualName, errors, warnings), out specials); |
||||
} |
||||
if (module == null) { |
||||
conversionLog.AppendLine("Could not parse '" + sourceItem.FileName + "', see error list for details."); |
||||
base.ConvertFile(sourceItem, targetItem); |
||||
} else { |
||||
using (StringWriter w = new StringWriter()) { |
||||
BooPrinterVisitorWithComments printer = new BooPrinterVisitorWithComments(specials, w); |
||||
printer.OnModule(module); |
||||
printer.Finish(); |
||||
|
||||
targetItem.Include = Path.ChangeExtension(targetItem.Include, ".boo"); |
||||
File.WriteAllText(targetItem.FileName, w.ToString()); |
||||
} |
||||
} |
||||
} else { |
||||
base.ConvertFile(sourceItem, targetItem); |
||||
} |
||||
} |
||||
|
||||
protected override void AfterConversion(IProject targetProject) |
||||
{ |
||||
base.AfterConversion(targetProject); |
||||
|
||||
if (errors.Count > 0) { |
||||
conversionLog.AppendLine(errors.Count + " conversion errors:"); |
||||
foreach (CompilerError error in errors) { |
||||
conversionLog.Append(" "); |
||||
conversionLog.AppendLine(error.ToString()); |
||||
} |
||||
conversionLog.AppendLine(); |
||||
} |
||||
if (warnings.Count > 0) { |
||||
conversionLog.AppendLine(warnings.Count + " warnings:"); |
||||
foreach (CompilerWarning warning in warnings) { |
||||
conversionLog.Append(" "); |
||||
conversionLog.AppendLine(warning.ToString()); |
||||
} |
||||
conversionLog.AppendLine(); |
||||
} |
||||
|
||||
|
||||
errors.Clear(); |
||||
warnings.Clear(); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* Created by SharpDevelop. |
||||
* User: Daniel Grunwald |
||||
* Date: 14.01.2006 |
||||
* Time: 14:50 |
||||
*/ |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.SharpDevelop.Project; |
||||
using ICSharpCode.SharpDevelop.Project.Converter; |
||||
using ICSharpCode.SharpDevelop.Internal.Templates; |
||||
using ICSharpCode.NRefactory.Parser; |
||||
using ICSharpCode.NRefactory.Parser.AST; |
||||
using ICSharpCode.NRefactory.PrettyPrinter; |
||||
|
||||
namespace CSharpBinding |
||||
{ |
||||
public class VBToCSharpConverter : NRefactoryLanguageConverter |
||||
{ |
||||
public override string TargetLanguageName { |
||||
get { |
||||
return CSharpLanguageBinding.LanguageName; |
||||
} |
||||
} |
||||
|
||||
protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem) |
||||
{ |
||||
ConvertFile(sourceItem, targetItem, ".vb", ".cs", SupportedLanguage.VBNet, new CSharpOutputVisitor()); |
||||
} |
||||
|
||||
protected override void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials) |
||||
{ |
||||
PreProcessingDirective.VBToCSharp(specials); |
||||
new VBNetToCSharpConvertVisitor().Visit(compilationUnit, null); |
||||
} |
||||
|
||||
protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject) |
||||
{ |
||||
ProjectCreateInformation info = new ProjectCreateInformation(); |
||||
info.ProjectBasePath = targetProjectDirectory; |
||||
info.ProjectName = sourceProject.Name; |
||||
info.OutputProjectFileName = Path.Combine(targetProjectDirectory, Path.GetFileNameWithoutExtension(sourceProject.FileName) + ".csproj"); |
||||
return new CSharpProject(info); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* Created by SharpDevelop. |
||||
* User: Daniel Grunwald |
||||
* Date: 14.01.2006 |
||||
* Time: 14:50 |
||||
*/ |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.SharpDevelop.Project; |
||||
using ICSharpCode.SharpDevelop.Project.Converter; |
||||
using ICSharpCode.SharpDevelop.Internal.Templates; |
||||
using ICSharpCode.NRefactory.Parser; |
||||
using ICSharpCode.NRefactory.Parser.AST; |
||||
using ICSharpCode.NRefactory.PrettyPrinter; |
||||
|
||||
namespace VBNetBinding |
||||
{ |
||||
public class CSharpToVBConverter : NRefactoryLanguageConverter |
||||
{ |
||||
public override string TargetLanguageName { |
||||
get { |
||||
return VBNetLanguageBinding.LanguageName; |
||||
} |
||||
} |
||||
|
||||
protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem) |
||||
{ |
||||
ConvertFile(sourceItem, targetItem, ".cs", ".vb", SupportedLanguage.CSharp, new VBNetOutputVisitor()); |
||||
} |
||||
|
||||
protected override void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials) |
||||
{ |
||||
PreProcessingDirective.CSharpToVB(specials); |
||||
new CSharpToVBNetConvertVisitor().Visit(compilationUnit, null); |
||||
} |
||||
|
||||
protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject) |
||||
{ |
||||
ProjectCreateInformation info = new ProjectCreateInformation(); |
||||
info.ProjectBasePath = targetProjectDirectory; |
||||
info.ProjectName = sourceProject.Name; |
||||
info.OutputProjectFileName = Path.Combine(targetProjectDirectory, Path.GetFileNameWithoutExtension(sourceProject.FileName) + ".vbproj"); |
||||
return new VBNetProject(info); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,152 @@
@@ -0,0 +1,152 @@
|
||||
/* |
||||
* Created by SharpDevelop. |
||||
* User: Daniel Grunwald |
||||
* Date: 14.01.2006 |
||||
* Time: 14:10 |
||||
*/ |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Text; |
||||
using System.Windows.Forms; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.SharpDevelop.Project.Commands; |
||||
using ICSharpCode.NRefactory.Parser; |
||||
using ICSharpCode.NRefactory.Parser.AST; |
||||
using ICSharpCode.NRefactory.PrettyPrinter; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Project.Converter |
||||
{ |
||||
/// <summary>
|
||||
/// Converts projects from one language to another, for example C# <-> VB
|
||||
/// </summary>
|
||||
public abstract class LanguageConverter : AbstractMenuCommand |
||||
{ |
||||
protected abstract IProject CreateProject(string targetProjectDirectory, IProject sourceProject); |
||||
protected virtual void AfterConversion(IProject targetProject) {} |
||||
|
||||
public abstract string TargetLanguageName { get; } |
||||
|
||||
protected virtual void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem) |
||||
{ |
||||
if (!File.Exists(targetItem.FileName)) { |
||||
File.Copy(sourceItem.FileName, targetItem.FileName); |
||||
} |
||||
} |
||||
|
||||
protected virtual void CopyProperties(IProject sourceProject, IProject targetProject) |
||||
{ |
||||
|
||||
} |
||||
|
||||
protected virtual void CopyItems(IProject sourceProject, IProject targetProject) |
||||
{ |
||||
foreach (ProjectItem item in sourceProject.Items) { |
||||
FileProjectItem fileItem = item as FileProjectItem; |
||||
if (fileItem != null && FileUtility.IsBaseDirectory(sourceProject.Directory, fileItem.FileName)) { |
||||
FileProjectItem targetItem = new FileProjectItem(targetProject, fileItem.ItemType); |
||||
fileItem.CopyExtraPropertiesTo(targetItem); |
||||
targetItem.Include = fileItem.Include; |
||||
if (!Directory.Exists(Path.GetDirectoryName(targetItem.FileName))) { |
||||
Directory.CreateDirectory(Path.GetDirectoryName(targetItem.FileName)); |
||||
} |
||||
ConvertFile(fileItem, targetItem); |
||||
targetProject.Items.Add(targetItem); |
||||
} else { |
||||
// Adding the same item to two projects is only allowed because we will save and reload
|
||||
// the target project.
|
||||
targetProject.Items.Add(item); |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected StringBuilder conversionLog; |
||||
|
||||
public override void Run() |
||||
{ |
||||
conversionLog = new StringBuilder(); |
||||
conversionLog.AppendLine("SharpDevelop Project Converter"); |
||||
conversionLog.AppendLine("=============================="); |
||||
conversionLog.AppendLine(""); |
||||
IProject sourceProject = ProjectService.CurrentProject; |
||||
string targetProjectDirectory = sourceProject.Directory + ".ConvertedTo" + TargetLanguageName; |
||||
if (Directory.Exists(targetProjectDirectory)) { |
||||
MessageService.ShowMessage(targetProjectDirectory + " already exists, cannot convert."); |
||||
return; |
||||
} |
||||
conversionLog.AppendLine("Source: " + sourceProject.Directory); |
||||
conversionLog.AppendLine("Target: " + targetProjectDirectory); |
||||
|
||||
Directory.CreateDirectory(targetProjectDirectory); |
||||
IProject targetProject = CreateProject(targetProjectDirectory, sourceProject); |
||||
CopyProperties(sourceProject, targetProject); |
||||
conversionLog.AppendLine(); |
||||
CopyItems(sourceProject, targetProject); |
||||
conversionLog.AppendLine(); |
||||
AfterConversion(targetProject); |
||||
conversionLog.AppendLine("Conversion complete."); |
||||
targetProject.Save(); |
||||
targetProject.Dispose(); |
||||
TreeNode node = ProjectBrowserPad.Instance.SelectedNode; |
||||
while (node != null) { |
||||
if (node is ISolutionFolderNode) { |
||||
AddExitingProjectToSolution.AddProject((ISolutionFolderNode)node, targetProject.FileName); |
||||
ProjectService.SaveSolution(); |
||||
break; |
||||
} |
||||
node = node.Parent; |
||||
} |
||||
FileService.NewFile("Conversion Results", "Text", conversionLog.ToString()); |
||||
} |
||||
} |
||||
|
||||
public abstract class NRefactoryLanguageConverter : LanguageConverter |
||||
{ |
||||
protected abstract void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials); |
||||
|
||||
protected void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem, |
||||
string sourceExtension, string targetExtension, |
||||
SupportedLanguage sourceLanguage, IOutputASTVisitor outputVisitor) |
||||
{ |
||||
if (sourceExtension.Equals(Path.GetExtension(sourceItem.FileName), StringComparison.OrdinalIgnoreCase)) { |
||||
string code = ParserService.GetParseableFileContent(sourceItem.FileName); |
||||
IParser p = ParserFactory.CreateParser(sourceLanguage, new StringReader(code)); |
||||
p.Parse(); |
||||
if (p.Errors.count > 0) { |
||||
conversionLog.AppendLine(); |
||||
conversionLog.AppendLine(sourceItem.FileName + " is not converted:"); |
||||
conversionLog.AppendLine("Parser found " + p.Errors.count + " error(s)"); |
||||
conversionLog.AppendLine(p.Errors.ErrorOutput); |
||||
base.ConvertFile(sourceItem, targetItem); |
||||
return; |
||||
} |
||||
|
||||
List<ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials; |
||||
|
||||
ConvertAst(p.CompilationUnit, specials); |
||||
|
||||
SpecialNodesInserter sni = new SpecialNodesInserter(specials, |
||||
new SpecialOutputVisitor(outputVisitor.OutputFormatter)); |
||||
outputVisitor.NodeTracker.NodeVisiting += sni.AcceptNodeStart; |
||||
outputVisitor.NodeTracker.NodeVisited += sni.AcceptNodeEnd; |
||||
outputVisitor.NodeTracker.NodeChildrenVisited += sni.AcceptNodeEnd; |
||||
outputVisitor.Visit(p.CompilationUnit, null); |
||||
sni.Finish(); |
||||
|
||||
p.Dispose(); |
||||
|
||||
if (outputVisitor.Errors.count > 0) { |
||||
conversionLog.AppendLine(); |
||||
conversionLog.AppendLine(outputVisitor.Errors.count + " error(s) converting " + sourceItem.FileName + ":"); |
||||
conversionLog.AppendLine(outputVisitor.Errors.ErrorOutput); |
||||
} |
||||
|
||||
targetItem.Include = Path.ChangeExtension(targetItem.Include, targetExtension); |
||||
File.WriteAllText(targetItem.FileName, outputVisitor.Text); |
||||
} else { |
||||
base.ConvertFile(sourceItem, targetItem); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue