📄 vsprojectinputconverter.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version value="$version"/>
// </file>
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Security.Permissions;
using System.Resources;
using System.Xml;
using System.Xml.Xsl;
using ICSharpCode.Core.AddIns;
using ICSharpCode.Core.Properties;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.Dialogs;
using ICSharpCode.SharpDevelop.Commands;
using ICSharpCode.SharpDevelop.ProjectImportExporter.Converters;
using ICSharpCode.SharpDevelop.ProjectImportExporter.Dialogs;
namespace ICSharpCode.SharpDevelop.ProjectImportExporter.Converters
{
public class VSProjectInputConverter : AbstractInputConverter
{
string projectTitle;
string projectInputDirectory;
string combineOutputFile;
string projectOutputDirectory;
public override string FormatName {
get {
return "Visual Studio.NET 7 / 2003 C# and VB.NET Projects";
}
}
public override string OutputFile {
get {
return combineOutputFile;
}
}
public override bool CanConvert(string fileName)
{
string upperExtension = Path.GetExtension(fileName).ToUpper();
return upperExtension == ".VBPROJ" || upperExtension == ".CSPROJ";
}
public override bool Convert(string solutionInputFile, string outputPath)
{
this.projectOutputDirectory = outputPath;
string projectOutputFile = Path.Combine(projectOutputDirectory, Path.ChangeExtension(Path.GetFileName(solutionInputFile), ".prjx"));
projectTitle = Path.GetFileNameWithoutExtension(solutionInputFile);
projectInputDirectory = Path.GetDirectoryName(solutionInputFile);
combineOutputFile = Path.Combine(outputPath, Path.ChangeExtension(Path.GetFileName(solutionInputFile), ".prjx"));
switch (Path.GetExtension(solutionInputFile).ToUpper()) {
case ".VBPROJ":
return TryConvertProject(solutionInputFile, projectOutputFile, "VBSolutionConversion.xsl");
case ".CSPROJ":
return TryConvertProject(solutionInputFile, projectOutputFile, "CSSolutionConversion.xsl");
default:
IMessageService messageService = (IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowError("${res:ICSharpCode.SharpDevelop.ProjectImportExporter.Converters.SolutionInputConverter.CantConvertProjectFileError}");
return false;
}
}
bool TryConvertProject(string inputFile, string outputFile, string resourceStreamFile)
{
return ConvertProject(inputFile, outputFile, resourceStreamFile, projectTitle, projectInputDirectory, projectOutputDirectory);
}
public static bool ConvertProject(string inputFile, string outputFile, string resourceStreamFile,
string projectTitle, string projectInputDirectory, string projectOutputDirectory)
{
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
SolutionConversionTool solutionConversionTool = new SolutionConversionTool(projectTitle, projectInputDirectory, projectOutputDirectory);
XsltArgumentList xsltArgumentList = new XsltArgumentList();
xsltArgumentList.AddParam("ProjectTitle", "", projectTitle);
xsltArgumentList.AddExtensionObject("urn:convtool", solutionConversionTool);
FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
XmlDocument document = new XmlDocument();
bool useDefaultEncoding = false;
try {
document.Load(inputFile);
} catch (XmlException) {
// try it again with the system encoding instead of UTF-8
try {
using (StreamReader r = new StreamReader(inputFile, Encoding.Default)) {
document.Load(r);
}
// success with default encoding:
useDefaultEncoding = true;
} catch (XmlException) {
messageService.ShowError("${res:ICSharpCode.SharpDevelop.ProjectImportExporter.Converters.SolutionInputConverter.InvalidProjectFile}");
return false;
}
}
if (document.DocumentElement.Name != "VisualStudioProject") {
if (document.DocumentElement.Name == "Project") {
messageService.ShowError("${res:ICSharpCode.SharpDevelop.ProjectImportExporter.Converters.SolutionInputConverter.MSBuildProjectFile}");
} else {
messageService.ShowError("${res:ICSharpCode.SharpDevelop.ProjectImportExporter.Converters.SolutionInputConverter.InvalidProjectFile}");
}
return false;
}
if (useDefaultEncoding) {
ConvertXml.Convert(inputFile,
new XmlTextReader(new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(resourceStreamFile), Encoding.UTF8)),
outputFile,
xsltArgumentList,
Encoding.Default);
} else {
ConvertXml.Convert(inputFile,
new XmlTextReader(new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(resourceStreamFile), Encoding.UTF8)),
outputFile,
xsltArgumentList);
}
foreach (DictionaryEntry entry in solutionConversionTool.copiedFiles) {
string srcFile = entry.Key.ToString();
string dstFile = entry.Value.ToString();
if (File.Exists(srcFile)) {
if (!Directory.Exists(Path.GetDirectoryName(dstFile))) {
Directory.CreateDirectory(Path.GetDirectoryName(dstFile));
}
if (Path.GetExtension(srcFile).ToUpper() == ".RESX") {
ConvertResource(srcFile, dstFile);
} else {
if (srcFile.ToLower() == dstFile.ToLower()) continue;
try {
File.Copy(srcFile, dstFile, true);
File.SetAttributes(dstFile, FileAttributes.Normal);
} catch (Exception e) {
messageService.ShowError(e, "Can't Copy file from " + srcFile +" to " + dstFile +". Copy it manually.");
}
}
}
}
solutionConversionTool.copiedFiles = new ArrayList();
return true;
}
static void ConvertResource(string inputFile, string outputFile)
{
Hashtable resources = new Hashtable();
// read .resx file
try {
Stream s = File.OpenRead(inputFile);
ResXResourceReader rx = new ResXResourceReader(s);
IDictionaryEnumerator n = rx.GetEnumerator();
while (n.MoveNext()) {
if (!resources.ContainsKey(n.Key)) {
resources.Add(n.Key, n.Value);
}
}
rx.Close();
s.Close();
} catch (Exception e) {
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowError(e, "Can't read resource file " + inputFile +"\nCheck file existance.");
}
// write .resources file
try {
ResourceWriter rw = new ResourceWriter(outputFile);
foreach (DictionaryEntry entry in resources) {
rw.AddResource(entry.Key.ToString(), entry.Value);
}
rw.Generate();
rw.Close();
} catch (Exception e) {
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowError(e, "Can't generate resource file " + outputFile +"\nCheck for write permission.");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -