#region BSD License
/*
Copyright (c) 2004 Matthew Holmes (matthew@wildfiregames.com)
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
* The name of the author may not be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion
#region CVS Information
/*
* $Source$
* $Author: robloach $
* $Date: 2007-02-27 19:52:34 +0100 (ti, 27 feb 2007) $
* $Revision: 207 $
*/
#endregion
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using Prebuild.Core.Attributes;
using Prebuild.Core.Interfaces;
using Prebuild.Core.Nodes;
using Prebuild.Core.Utilities;
namespace Prebuild.Core.Targets
{
///
///
///
public struct ToolInfo
{
string name;
string guid;
string fileExtension;
string xmlTag;
string importProject;
///
/// Gets or sets the name.
///
/// The name.
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
///
/// Gets or sets the GUID.
///
/// The GUID.
public string Guid
{
get
{
return guid;
}
set
{
guid = value;
}
}
///
/// Gets or sets the file extension.
///
/// The file extension.
public string FileExtension
{
get
{
return fileExtension;
}
set
{
fileExtension = value;
}
}
///
/// Gets or sets the XML tag.
///
/// The XML tag.
public string XmlTag
{
get
{
return xmlTag;
}
set
{
xmlTag = value;
}
}
///
/// Gets or sets the import project property.
///
/// The ImportProject tag.
public string ImportProject
{
get
{
return importProject;
}
set
{
importProject = value;
}
}
///
/// Initializes a new instance of the class.
///
/// The name.
/// The GUID.
/// The file extension.
/// The XML.
/// The import project.
public ToolInfo(string name, string guid, string fileExtension, string xml, string importProject)
{
this.name = name;
this.guid = guid;
this.fileExtension = fileExtension;
this.xmlTag = xml;
this.importProject = importProject;
}
///
/// Initializes a new instance of the class.
///
/// The name.
/// The GUID.
/// The file extension.
/// The XML.
public ToolInfo(string name, string guid, string fileExtension, string xml)
{
this.name = name;
this.guid = guid;
this.fileExtension = fileExtension;
this.xmlTag = xml;
this.importProject = "$(MSBuildBinPath)\\Microsoft." + xml + ".Targets";
}
///
/// Equals operator
///
/// ToolInfo to compare
/// true if toolInfos are equal
public override bool Equals(object obj)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
if (obj.GetType() != typeof(ToolInfo))
return false;
ToolInfo c = (ToolInfo)obj;
return ((this.name == c.name) && (this.guid == c.guid) && (this.fileExtension == c.fileExtension) && (this.importProject == c.importProject));
}
///
/// Equals operator
///
/// ToolInfo to compare
/// ToolInfo to compare
/// True if toolInfos are equal
public static bool operator ==(ToolInfo c1, ToolInfo c2)
{
return ((c1.name == c2.name) && (c1.guid == c2.guid) && (c1.fileExtension == c2.fileExtension) && (c1.importProject == c2.importProject) && (c1.xmlTag == c2.xmlTag));
}
///
/// Not equals operator
///
/// ToolInfo to compare
/// ToolInfo to compare
/// True if toolInfos are not equal
public static bool operator !=(ToolInfo c1, ToolInfo c2)
{
return !(c1 == c2);
}
///
/// Hash Code
///
/// Hash code
public override int GetHashCode()
{
return name.GetHashCode() ^ guid.GetHashCode() ^ this.fileExtension.GetHashCode() ^ this.importProject.GetHashCode() ^ this.xmlTag.GetHashCode();
}
}
///
///
///
[Target("vs2005")]
public class VS2005Target : ITarget
{
#region Inner Classes
#endregion
#region Fields
string solutionVersion = "9.00";
string productVersion = "8.0.50727";
string schemaVersion = "2.0";
string versionName = "Visual C# 2005";
VSVersion version = VSVersion.VS80;
Hashtable tools;
Kernel kernel;
///
/// Gets or sets the solution version.
///
/// The solution version.
protected string SolutionVersion
{
get
{
return this.solutionVersion;
}
set
{
this.solutionVersion = value;
}
}
///
/// Gets or sets the product version.
///
/// The product version.
protected string ProductVersion
{
get
{
return this.productVersion;
}
set
{
this.productVersion = value;
}
}
///
/// Gets or sets the schema version.
///
/// The schema version.
protected string SchemaVersion
{
get
{
return this.schemaVersion;
}
set
{
this.schemaVersion = value;
}
}
///
/// Gets or sets the name of the version.
///
/// The name of the version.
protected string VersionName
{
get
{
return this.versionName;
}
set
{
this.versionName = value;
}
}
///
/// Gets or sets the version.
///
/// The version.
protected VSVersion Version
{
get
{
return this.version;
}
set
{
this.version = value;
}
}
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
public VS2005Target()
{
this.tools = new Hashtable();
this.tools["C#"] = new ToolInfo("C#", "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}", "csproj", "CSHARP", "$(MSBuildBinPath)\\Microsoft.CSHARP.Targets");
this.tools["Boo"] = new ToolInfo("Boo", "{45CEA7DC-C2ED-48A6-ACE0-E16144C02365}", "booproj", "Boo", "$(BooBinPath)\\Boo.Microsoft.Build.targets");
this.tools["VisualBasic"] = new ToolInfo("VisualBasic", "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}", "vbproj", "VisualBasic", "$(MSBuildBinPath)\\Microsoft.VisualBasic.Targets");
}
#endregion
#region Private Methods
private string MakeRefPath(ProjectNode project)
{
string ret = "";
foreach (ReferencePathNode node in project.ReferencePaths)
{
try
{
string fullPath = Helper.ResolvePath(node.Path);
if (ret.Length < 1)
{
ret = fullPath;
}
else
{
ret += ";" + fullPath;
}
}
catch (ArgumentException)
{
this.kernel.Log.Write(LogType.Warning, "Could not resolve reference path: {0}", node.Path);
}
}
return ret;
}
private void WriteProject(SolutionNode solution, ProjectNode project)
{
if (!tools.ContainsKey(project.Language))
{
throw new UnknownLanguageException("Unknown .NET language: " + project.Language);
}
// Hardcoded exclusion of unbuildable project
if( project.Name == "OpenSim.DataStore.MonoSqlite")
{
return;
}
ToolInfo toolInfo = (ToolInfo)tools[project.Language];
string projectFile = Helper.MakeFilePath(project.FullPath, project.Name, toolInfo.FileExtension);
StreamWriter ps = new StreamWriter(projectFile);
kernel.CurrentWorkingDirectory.Push();
Helper.SetCurrentDir(Path.GetDirectoryName(projectFile));
#region Project File
using (ps)
{
ps.WriteLine("");
//ps.WriteLine(" <{0}", toolInfo.XMLTag);
ps.WriteLine(" ");
ps.WriteLine(" Local");
ps.WriteLine(" {0}", this.ProductVersion);
ps.WriteLine(" {0}", this.SchemaVersion);
ps.WriteLine(" {{{0}}}", project.Guid.ToString().ToUpper());
ps.WriteLine(" Debug");
ps.WriteLine(" AnyCPU");
//ps.WriteLine(" ");
//ps.WriteLine(" {0}", project.AppIcon);
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" {0}", project.AssemblyName);
foreach (ConfigurationNode conf in project.Configurations)
{
if (conf.Options.KeyFile != "")
{
ps.WriteLine(" {0}", conf.Options.KeyFile);
ps.WriteLine(" true");
break;
}
}
ps.WriteLine(" JScript");
ps.WriteLine(" Grid");
ps.WriteLine(" IE50");
ps.WriteLine(" false");
//if(m_Version == VSVersion.VS70)
// ps.WriteLine(" NoStandardLibraries = \"false\"");
ps.WriteLine(" {0}", project.Type.ToString());
ps.WriteLine(" {0}", project.DesignerFolder);
ps.WriteLine(" {0}", project.RootNamespace);
ps.WriteLine(" {0}", project.StartupObject);
//ps.WriteLine(" >");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
foreach (ConfigurationNode conf in project.Configurations)
{
ps.Write(" ", conf.Name);
ps.WriteLine(" {0}", conf.Options["AllowUnsafe"]);
ps.WriteLine(" {0}", conf.Options["BaseAddress"]);
ps.WriteLine(" {0}", conf.Options["CheckUnderflowOverflow"]);
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" {0}", conf.Options["CompilerDefines"]);
ps.WriteLine(" {0}", conf.Options["XmlDocFile"]);
ps.WriteLine(" {0}", conf.Options["DebugInformation"]);
ps.WriteLine(" {0}", conf.Options["FileAlignment"]);
// ps.WriteLine(" {0}", conf.Options["OptimizeCode"]);
ps.WriteLine(" {0}",
Helper.EndPath(Helper.NormalizePath(conf.Options["OutputPath"].ToString())));
ps.WriteLine(" {0}", conf.Options["RegisterComInterop"]);
ps.WriteLine(" {0}", conf.Options["RemoveIntegerChecks"]);
ps.WriteLine(" {0}", conf.Options["WarningsAsErrors"]);
ps.WriteLine(" {0}", conf.Options["WarningLevel"]);
ps.WriteLine(" {0}", conf.Options["SuppressWarnings"]);
ps.WriteLine(" ");
}
//ps.WriteLine(" ");
// Assembly References
ps.WriteLine(" ");
string refPath = ((ReferencePathNode) project.ReferencePaths[0]).Path;
foreach (ReferenceNode refr in project.References)
{
if (!solution.ProjectsTable.ContainsKey(refr.Name))
{
ps.Write(" ");
string path;
if( refr.Name.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase ))
{
path = Helper.NormalizePath(Path.Combine( refPath, refr.Name), '\\');
}
else
{
path = refr.Name + ".dll";
}
// TODO: Allow reference to *.exe files
ps.WriteLine(" {0}", path );
ps.WriteLine(" {0}", refr.LocalCopy);
ps.WriteLine(" ");
}
}
ps.WriteLine(" ");
//Project References
ps.WriteLine(" ");
foreach (ReferenceNode refr in project.References)
{
if (solution.ProjectsTable.ContainsKey(refr.Name))
{
ProjectNode refProject = (ProjectNode)solution.ProjectsTable[refr.Name];
// TODO: Allow reference to visual basic projects
string path =
Helper.MakePathRelativeTo(project.FullPath,
Helper.MakeFilePath(refProject.FullPath, refProject.Name, "csproj"));
ps.WriteLine(" ", path );
//
ps.WriteLine(" {0}", refProject.Name);
// RealmForge.Utility
ps.WriteLine(" {{{0}}}", refProject.Guid.ToString().ToUpper());
// {6880D1D3-69EE-461B-B841-5319845B20D3}
ps.WriteLine(" {0}", toolInfo.Guid.ToString().ToUpper());
// {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
ps.WriteLine("\t\t\t{0}", refr.LocalCopy);
ps.WriteLine(" ");
//
}
else
{
}
}
ps.WriteLine(" ");
// ps.WriteLine(" ");
ps.WriteLine(" ");
// ps.WriteLine(" ");
ArrayList list = new ArrayList();
foreach (string file in project.Files)
{
// if (file == "Properties\\Bind.Designer.cs")
// {
// Console.WriteLine("Wait a minute!");
// Console.WriteLine(project.Files.GetSubType(file).ToString());
// }
if (project.Files.GetSubType(file) != SubType.Code && project.Files.GetSubType(file) != SubType.Settings && project.Files.GetSubType(file) != SubType.Designer)
{
ps.WriteLine(" ", file.Substring(0, file.LastIndexOf('.')) + ".resx");
int slash = file.LastIndexOf('\\');
if (slash == -1)
{
ps.WriteLine(" {0}", file);
}
else
{
ps.WriteLine(" {0}", file.Substring(slash + 1, file.Length - slash - 1));
}
ps.WriteLine(" Designer");
ps.WriteLine(" ");
//
}
if (project.Files.GetSubType(file) != SubType.Code && project.Files.GetSubType(file) == SubType.Designer)
{
ps.WriteLine(" ", file.Substring(0, file.LastIndexOf('.')) + ".resx");
ps.WriteLine(" " + project.Files.GetSubType(file) + "");
ps.WriteLine(" ResXFileCodeGenerator");
ps.WriteLine(" Resources.Designer.cs");
ps.WriteLine(" ");
ps.WriteLine(" ", file.Substring(0, file.LastIndexOf('.')) + ".Designer.cs");
ps.WriteLine(" True");
ps.WriteLine(" True");
ps.WriteLine(" Resources.resx");
ps.WriteLine(" ");
list.Add(file.Substring(0, file.LastIndexOf('.')) + ".Designer.cs");
}
if (project.Files.GetSubType(file).ToString() == "Settings")
{
//Console.WriteLine("File: " + file);
//Console.WriteLine("Last index: " + file.LastIndexOf('.'));
//Console.WriteLine("Length: " + file.Length);
ps.Write(" <{0} ", project.Files.GetBuildAction(file));
ps.WriteLine("Include=\"{0}\">", file);
int slash = file.LastIndexOf('\\');
string fileName = file.Substring(slash + 1, file.Length - slash - 1);
if (project.Files.GetBuildAction(file) == BuildAction.None)
{
ps.WriteLine(" SettingsSingleFileGenerator");
//Console.WriteLine("FileName: " + fileName);
//Console.WriteLine("FileNameMain: " + fileName.Substring(0, fileName.LastIndexOf('.')));
//Console.WriteLine("FileNameExt: " + fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.')));
if (slash == -1)
{
ps.WriteLine(" {0}", fileName.Substring(0, fileName.LastIndexOf('.')) + ".Designer.cs");
}
else
{
ps.WriteLine(" {0}", fileName.Substring(0, fileName.LastIndexOf('.')) + ".Designer.cs");
}
}
else
{
ps.WriteLine(" Code");
ps.WriteLine(" True");
ps.WriteLine(" True");
string fileNameShort = fileName.Substring(0, fileName.LastIndexOf('.'));
string fileNameShorter = fileNameShort.Substring(0, fileNameShort.LastIndexOf('.'));
ps.WriteLine(" {0}", fileNameShorter + ".settings");
}
ps.WriteLine(" {0}>", project.Files.GetBuildAction(file));
}
else if (project.Files.GetSubType(file) != SubType.Designer)
{
if (!list.Contains(file))
{
ps.Write(" <{0} ", project.Files.GetBuildAction(file));
ps.WriteLine("Include=\"{0}\">", file);
if (file.Contains("Designer.cs"))
{
ps.WriteLine(" {0}", file.Substring(0, file.IndexOf(".Designer.cs")) + ".cs");
}
if (project.Files.GetIsLink(file))
{
ps.WriteLine(" {0}", Path.GetFileName(file));
}
else if (project.Files.GetBuildAction(file) != BuildAction.None)
{
if (project.Files.GetBuildAction(file) != BuildAction.EmbeddedResource)
{
ps.WriteLine(" {0}", project.Files.GetSubType(file));
}
}
if (project.Files.GetCopyToOutput(file) != CopyToOutput.Never)
{
ps.WriteLine(" {0}", project.Files.GetCopyToOutput(file));
}
ps.WriteLine(" {0}>", project.Files.GetBuildAction(file));
}
}
}
// ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
ps.WriteLine(" ");
// ps.WriteLine(" {0}>", toolInfo.XMLTag);
ps.WriteLine("");
}
#endregion
#region User File
ps = new StreamWriter(projectFile + ".user");
using (ps)
{
ps.WriteLine("");
//ps.WriteLine( "" );
//ps.WriteLine(" <{0}>", toolInfo.XMLTag);
//ps.WriteLine(" ");
ps.WriteLine(" ");
//ps.WriteLine(" ", MakeRefPath(project));
ps.WriteLine(" Debug");
ps.WriteLine(" AnyCPU");
if (projectFile.Contains( "OpenSim.csproj" ))
{
ps.WriteLine(" -loginserver -sandbox -accounts");
}
ps.WriteLine(" {0}", MakeRefPath(project));
ps.WriteLine(" {0}", this.ProductVersion);
ps.WriteLine(" ProjectFiles");
ps.WriteLine(" 0");
ps.WriteLine(" ");
foreach (ConfigurationNode conf in project.Configurations)
{
ps.Write(" ");
}
ps.WriteLine("");
}
#endregion
kernel.CurrentWorkingDirectory.Pop();
}
private void WriteSolution(SolutionNode solution)
{
kernel.Log.Write("Creating {0} solution and project files", this.VersionName);
foreach (ProjectNode project in solution.Projects)
{
kernel.Log.Write("...Creating project: {0}", project.Name);
WriteProject(solution, project);
}
kernel.Log.Write("");
string solutionFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "sln");
StreamWriter ss = new StreamWriter(solutionFile);
kernel.CurrentWorkingDirectory.Push();
Helper.SetCurrentDir(Path.GetDirectoryName(solutionFile));
using (ss)
{
ss.WriteLine("Microsoft Visual Studio Solution File, Format Version {0}", this.SolutionVersion);
ss.WriteLine("# Visual Studio 2005");
foreach (ProjectNode project in solution.Projects)
{
if (!tools.ContainsKey(project.Language))
{
throw new UnknownLanguageException("Unknown .NET language: " + project.Language);
}
ToolInfo toolInfo = (ToolInfo)tools[project.Language];
string path = Helper.MakePathRelativeTo(solution.FullPath, project.FullPath);
ss.WriteLine("Project(\"{0}\") = \"{1}\", \"{2}\", \"{{{3}}}\"",
toolInfo.Guid, project.Name, Helper.MakeFilePath(path, project.Name,
toolInfo.FileExtension), project.Guid.ToString().ToUpper());
//ss.WriteLine(" ProjectSection(ProjectDependencies) = postProject");
//ss.WriteLine(" EndProjectSection");
ss.WriteLine("EndProject");
}
if (solution.Files != null)
{
ss.WriteLine("Project(\"{0}\") = \"Solution Items\", \"Solution Items\", \"{1}\"", "{2150E333-8FDC-42A3-9474-1A3956D46DE8}", "{468F1D07-AD17-4CC3-ABD0-2CA268E4E1A6}");
ss.WriteLine("\tProjectSection(SolutionItems) = preProject");
foreach (string file in solution.Files)
ss.WriteLine("\t\t{0} = {0}", file);
ss.WriteLine("\tEndProjectSection");
ss.WriteLine("EndProject");
}
ss.WriteLine("Global");
ss.WriteLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution");
foreach (ConfigurationNode conf in solution.Configurations)
{
ss.WriteLine(" {0}|Any CPU = {0}|Any CPU", conf.Name);
}
ss.WriteLine(" EndGlobalSection");
if (solution.Projects.Count > 1)
{
ss.WriteLine(" GlobalSection(ProjectDependencies) = postSolution");
}
foreach (ProjectNode project in solution.Projects)
{
for (int i = 0; i < project.References.Count; i++)
{
ReferenceNode refr = (ReferenceNode)project.References[i];
if (solution.ProjectsTable.ContainsKey(refr.Name))
{
ProjectNode refProject = (ProjectNode)solution.ProjectsTable[refr.Name];
ss.WriteLine(" ({{{0}}}).{1} = ({{{2}}})",
project.Guid.ToString().ToUpper()
, i,
refProject.Guid.ToString().ToUpper()
);
}
}
}
if (solution.Projects.Count > 1)
{
ss.WriteLine(" EndGlobalSection");
}
ss.WriteLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution");
foreach (ProjectNode project in solution.Projects)
{
foreach (ConfigurationNode conf in solution.Configurations)
{
ss.WriteLine(" {{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU",
project.Guid.ToString().ToUpper(),
conf.Name);
ss.WriteLine(" {{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU",
project.Guid.ToString().ToUpper(),
conf.Name);
}
}
ss.WriteLine(" EndGlobalSection");
ss.WriteLine(" GlobalSection(SolutionProperties) = preSolution");
ss.WriteLine(" HideSolutionNode = FALSE");
ss.WriteLine(" EndGlobalSection");
ss.WriteLine("EndGlobal");
}
kernel.CurrentWorkingDirectory.Pop();
}
private void CleanProject(ProjectNode project)
{
kernel.Log.Write("...Cleaning project: {0}", project.Name);
ToolInfo toolInfo = (ToolInfo)tools[project.Language];
string projectFile = Helper.MakeFilePath(project.FullPath, project.Name, toolInfo.FileExtension);
string userFile = projectFile + ".user";
Helper.DeleteIfExists(projectFile);
Helper.DeleteIfExists(userFile);
}
private void CleanSolution(SolutionNode solution)
{
kernel.Log.Write("Cleaning {0} solution and project files", this.VersionName, solution.Name);
string slnFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "sln");
string suoFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "suo");
Helper.DeleteIfExists(slnFile);
Helper.DeleteIfExists(suoFile);
foreach (ProjectNode project in solution.Projects)
{
CleanProject(project);
}
kernel.Log.Write("");
}
#endregion
#region ITarget Members
///
/// Writes the specified kern.
///
/// The kern.
public virtual void Write(Kernel kern)
{
if (kern == null)
{
throw new ArgumentNullException("kern");
}
kernel = kern;
foreach (SolutionNode sol in kernel.Solutions)
{
WriteSolution(sol);
}
kernel = null;
}
///
/// Cleans the specified kern.
///
/// The kern.
public virtual void Clean(Kernel kern)
{
if (kern == null)
{
throw new ArgumentNullException("kern");
}
kernel = kern;
foreach (SolutionNode sol in kernel.Solutions)
{
CleanSolution(sol);
}
kernel = null;
}
///
/// Gets the name.
///
/// The name.
public virtual string Name
{
get
{
return "vs2005";
}
}
#endregion
}
}