/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/
using System;
using System.Collections;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using at.jku.ssw.ProfIt.Components;
namespace at.jku.ssw.ProfIt.Import.VS2003{
/// <summary>
/// This class is responsible for importing projects from Visual Studio .NET
/// There may be some problems with huge VS.NET projects, this module is
/// only at a beta-version
/// </summary>
public class ImportManager {
private static Hashtable foundReferences = new Hashtable();
private static Solution ReadSolution(string fileName) {
Solution solution = new Solution(FileUtility.ToShortFileName(fileName));
solution.RootPath = FileUtility.GetPath(fileName);
StreamReader sr = File.OpenText(fileName);
Regex projectLinePattern = new Regex("Project\\(.*\\)\\s+=\\s+\"(?<Title>.*)\",\\s*\"(?<Location>.*)\",", RegexOptions.Compiled);
//ArrayList projects = new ArrayList();
while (true) {
string line = sr.ReadLine();
if (line == null) {
break;
}
Match match = projectLinePattern.Match(line);
if (match.Success) {
Project project = new Project(match.Result("${Title}"),solution);
string location = match.Result("${Location}");
project.ExternalFileName = solution.RootPath + "\\" + location;
if (location.LastIndexOf("\\") > 0)
project.SetRelativeDirectory(location.Substring(0, location.LastIndexOf("\\")));
else project.SetRelativeDirectory("");
if (project.ExternalFileName.EndsWith("csproj"))
solution.AddProject(project);
}
}
sr.Close();
return solution;
}
public static Solution ImportSolution(string fileName) {
Solution solution = ReadSolution(fileName);
Errors.count = 0;
if (Errors.count == 0) {
foreach (Project project in solution.Projects) {
ImportProject(project);
if (project.OutputType == OutputType.WinExe) solution.StartProject = project;
}
}
if (Errors.count == 0) {
foreach (Project project in solution.Projects) {
IncludeReferences(solution, project);
}
}
if (Errors.count == 0) {
return solution;
} else {
return null;
}
}
private static void ImportProject(Project project) {
try {
XmlDocument doc = new XmlDocument();
doc.Load(project.ExternalFileName);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iterator = nav.Select("/VisualStudioProject/CSHARP/Files/Include/File");
while (iterator.MoveNext()) {
SourceFile file = new SourceFile(project, iterator.Current.GetAttribute("RelPath", ""));
switch (iterator.Current.GetAttribute("BuildAction", "")) {
case "Compile": file.BuildAction = BuildAction.Compile; break;
case "Content": file.BuildAction = BuildAction.Content; break;
case "EmbeddedResource": file.BuildAction = BuildAction.EmbeddedResource; break;
default: file.BuildAction = BuildAction.Content; break;
}
project.AddFile(file);
}
XPathNavigator nav3 = doc.CreateNavigator();
XPathNodeIterator iterator3 = nav3.Select("/VisualStudioProject/CSHARP");
while (iterator3.MoveNext()) {
project.ExternalId = iterator3.Current.GetAttribute("ProjectGuid","");
}
iterator = nav.Select("/VisualStudioProject/CSHARP/Build/Settings");
while (iterator.MoveNext()) {
switch (iterator.Current.GetAttribute("OutputType", "")) {
case "Library": project.OutputType = OutputType.Library; break;
case "WinExe": project.OutputType = OutputType.WinExe; break;
default: project.OutputType = OutputType.WinExe; break;
}
}
} catch (Exception e){
Errors.Error(-1,-1, "Error in parsing " + project.ExternalFileName + " due to: " +e.ToString());
}
}
private static void IncludeReferences(Solution solution, Project project) {
try {
XmlDocument doc = new XmlDocument();
doc.Load(project.ExternalFileName);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iterator = nav.Select("/VisualStudioProject/CSHARP/Build/References/Reference");
while (iterator.MoveNext()) {
IReference reference = null;
string hintPath = iterator.Current.GetAttribute("HintPath", "");
// synchronize the hintpath of the project file with the root path of the project
// e.g.: hintPath: ..\..\System.dll
// rootPath: c:\projects\test\
// assemblyPath: c:\System.dll
if (hintPath != null && hintPath != "") {
// remove the last slash
if (hintPath.StartsWith("..")) {
string rootPath = project.RootPath.Remove(project.RootPath.Length-1, 1);
hintPath = hintPath.Replace("/", "\\");
while (hintPath.StartsWith("..\\")) {
hintPath = hintPath.Substring(3);
rootPath = rootPath.Substring(0, rootPath.LastIndexOf("\\"));
}
reference = new AssemblyReference(rootPath + "\\" + hintPath);
}
else reference = new AssemblyReference(hintPath); /* absolute path */
} else {
string projectId = iterator.Current.GetAttribute("Project", "");
foreach (Project p in solution.Projects) {
if (p.ExternalId == projectId) {
reference = new ProjectReference(p);
break;
}
}
if (reference == null) {
Errors.Error(-1,-1, "Could not find project with guid " + projectId);
continue;
}
}
if (!reference.Exists && reference is AssemblyReference) {
if (foundReferences[reference.FileName] != null) {
project.AddReference(new AssemblyReference((string)foundReferences[reference.FileName]));
} else {
ReferenceCheckerDialog dialog = new ReferenceCheckerDialog();
dialog.OldFileName = reference.FileName;
switch (dialog.ShowDialog()) {
case DialogResult.OK:
project.AddReference(new AssemblyReference(dialog.NewFileName));
foundReferences.Add(dialog.OldFileName, dialog.NewFileName);
break;
case DialogResult.Ignore: project.AddReference(reference); break;
case DialogResult.Abort: break;
}
}
} else {
project.AddReference(reference);
}
}
} catch (Exception e) {
e.ToString();
//Errors.Error(-1,-1, "Error in parsing references in " + project.ExternalFileName + " due to " +e.ToString());
}
}
}
}
|