1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.TypeSystem;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
namespace ICSharpCode.NRefactory.ConsistencyCheck
{
/// <summary>
/// Represents a C# project (.csproj file)
/// </summary>
public class CSharpProject
{
/// <summary>
/// Parent solution.
/// </summary>
public readonly Solution Solution;
/// <summary>
/// Title is the project name as specified in the .sln file.
/// </summary>
public readonly string Title;
/// <summary>
/// Name of the output assembly.
/// </summary>
public readonly string AssemblyName;
/// <summary>
/// Full path to the .csproj file.
/// </summary>
public readonly string FileName;
public readonly List<CSharpFile> Files = new List<CSharpFile>();
public readonly CompilerSettings CompilerSettings = new CompilerSettings();
/// <summary>
/// The unresolved type system for this project.
/// </summary>
public readonly IProjectContent ProjectContent;
/// <summary>
/// The resolved type system for this project.
/// This field is initialized once all projects have been loaded (in Solution constructor).
/// </summary>
public ICompilation Compilation;
public CSharpProject(Solution solution, string title, string fileName)
{
// Normalize the file name
fileName = Path.GetFullPath(fileName);
this.Solution = solution;
this.Title = title;
this.FileName = fileName;
// Use MSBuild to open the .csproj
var msbuildProject = new Microsoft.Build.Evaluation.Project(fileName);
// Figure out some compiler settings
this.AssemblyName = msbuildProject.GetPropertyValue("AssemblyName");
this.CompilerSettings.AllowUnsafeBlocks = GetBoolProperty(msbuildProject, "AllowUnsafeBlocks") ?? false;
this.CompilerSettings.CheckForOverflow = GetBoolProperty(msbuildProject, "CheckForOverflowUnderflow") ?? false;
string defineConstants = msbuildProject.GetPropertyValue("DefineConstants");
foreach (string symbol in defineConstants.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries))
this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim());
// Initialize the unresolved type system
IProjectContent pc = new CSharpProjectContent();
pc = pc.SetAssemblyName(this.AssemblyName);
pc = pc.SetProjectFileName(fileName);
pc = pc.SetCompilerSettings(this.CompilerSettings);
// Parse the C# code files
foreach (var item in msbuildProject.GetItems("Compile")) {
var file = new CSharpFile(this, Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude));
Files.Add(file);
}
// Add parsed files to the type system
pc = pc.AddOrUpdateFiles(Files.Select(f => f.UnresolvedTypeSystemForFile));
// Add referenced assemblies:
foreach (string assemblyFile in ResolveAssemblyReferences(msbuildProject)) {
IUnresolvedAssembly assembly = solution.LoadAssembly(assemblyFile);
pc = pc.AddAssemblyReferences(new [] { assembly });
}
// Add project references:
foreach (var item in msbuildProject.GetItems("ProjectReference")) {
string referencedFileName = Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude);
// Normalize the path; this is required to match the name with the referenced project's file name
referencedFileName = Path.GetFullPath(referencedFileName);
pc = pc.AddAssemblyReferences(new[] { new ProjectReference(referencedFileName) });
}
this.ProjectContent = pc;
}
IEnumerable<string> ResolveAssemblyReferences(Microsoft.Build.Evaluation.Project project)
{
// Use MSBuild to figure out the full path of the referenced assemblies
var projectInstance = project.CreateProjectInstance();
projectInstance.SetProperty("BuildingProject", "false");
project.SetProperty("DesignTimeBuild", "true");
projectInstance.Build("ResolveAssemblyReferences", new [] { new ConsoleLogger(LoggerVerbosity.Minimal) });
var items = projectInstance.GetItems("_ResolveAssemblyReferenceResolvedFiles");
string baseDirectory = Path.GetDirectoryName(this.FileName);
return items.Select(i => Path.Combine(baseDirectory, i.GetMetadataValue("Identity")));
}
static bool? GetBoolProperty(Microsoft.Build.Evaluation.Project p, string propertyName)
{
string val = p.GetPropertyValue(propertyName);
bool result;
if (bool.TryParse(val, out result))
return result;
else
return null;
}
public override string ToString()
{
return string.Format("[CSharpProject AssemblyName={0}]", AssemblyName);
}
}
}
|