+ "{data.Items.Count} #Targets={data.Targets.Count}")]
public class Project
{
- public Project (XmlReader xml)
- : this (ProjectRootElement.Create (xml))
+ public Project (XmlReader xmlReader)
+ : this (ProjectRootElement.Create (xmlReader))
{
}
- public Project (XmlReader xml, IDictionary<string, string> globalProperties,
+ public Project (XmlReader xmlReader, IDictionary<string, string> globalProperties,
string toolsVersion)
- : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
+ : this (ProjectRootElement.Create (xmlReader), globalProperties, toolsVersion)
{
}
- public Project (XmlReader xml, IDictionary<string, string> globalProperties,
+ public Project (XmlReader xmlReader, IDictionary<string, string> globalProperties,
string toolsVersion, ProjectCollection projectCollection)
- : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
+ : this (ProjectRootElement.Create (xmlReader), globalProperties, toolsVersion, projectCollection)
{
}
- public Project (XmlReader xml, IDictionary<string, string> globalProperties,
+ public Project (XmlReader xmlReader, IDictionary<string, string> globalProperties,
string toolsVersion, ProjectCollection projectCollection,
ProjectLoadSettings loadSettings)
- : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection, loadSettings)
+ : this (ProjectRootElement.Create (xmlReader), globalProperties, toolsVersion, projectCollection, loadSettings)
{
}
this.ProjectCollection = projectCollection;
this.load_settings = loadSettings;
- Initialize (null);
- }
-
- Project (ProjectRootElement imported, Project parent)
- {
- this.Xml = imported;
- this.GlobalProperties = parent.GlobalProperties;
- this.ToolsVersion = parent.ToolsVersion;
- this.ProjectCollection = parent.ProjectCollection;
- this.load_settings = parent.load_settings;
-
- Initialize (parent);
+ Initialize ();
}
public Project (string projectFile)
List<ProjectProperty> properties;
Dictionary<string, ProjectTargetInstance> targets;
- void Initialize (Project parent)
+ void Initialize ()
{
dir_path = Directory.GetCurrentDirectory ();
raw_imports = new List<ResolvedImport> ();
targets = new Dictionary<string, ProjectTargetInstance> ();
raw_items = new List<ProjectItem> ();
- // FIXME: this is likely hack. Test ImportedProject.Properties to see what exactly should happen.
- if (parent != null) {
- properties = parent.properties;
- } else {
- properties = new List<ProjectProperty> ();
-
- foreach (DictionaryEntry p in Environment.GetEnvironmentVariables ())
- // FIXME: this is kind of workaround for unavoidable issue that PLATFORM=* is actually given
- // on some platforms and that prevents setting default "PLATFORM=AnyCPU" property.
- if (!string.Equals ("PLATFORM", (string) p.Key, StringComparison.OrdinalIgnoreCase))
- this.properties.Add (new EnvironmentProjectProperty (this, (string)p.Key, (string)p.Value));
- foreach (var p in GlobalProperties)
- this.properties.Add (new GlobalProjectProperty (this, p.Key, p.Value));
- var tools = ProjectCollection.GetToolset (this.ToolsVersion) ?? ProjectCollection.GetToolset (this.ProjectCollection.DefaultToolsVersion);
- foreach (var p in ProjectCollection.GetReservedProperties (tools, this))
- this.properties.Add (p);
- foreach (var p in ProjectCollection.GetWellKnownProperties (this))
- this.properties.Add (p);
- }
-
- ProcessXml (parent);
+ properties = new List<ProjectProperty> ();
+
+ foreach (DictionaryEntry p in Environment.GetEnvironmentVariables ())
+ // FIXME: this is kind of workaround for unavoidable issue that PLATFORM=* is actually given
+ // on some platforms and that prevents setting default "PLATFORM=AnyCPU" property.
+ if (!string.Equals ("PLATFORM", (string) p.Key, StringComparison.OrdinalIgnoreCase))
+ this.properties.Add (new EnvironmentProjectProperty (this, (string)p.Key, (string)p.Value));
+ foreach (var p in GlobalProperties)
+ this.properties.Add (new GlobalProjectProperty (this, p.Key, p.Value));
+ var tools = ProjectCollection.GetToolset (this.ToolsVersion) ?? ProjectCollection.GetToolset (this.ProjectCollection.DefaultToolsVersion);
+ foreach (var p in ProjectCollection.GetReservedProperties (tools, this))
+ this.properties.Add (p);
+ foreach (var p in ProjectCollection.GetWellKnownProperties (this))
+ this.properties.Add (p);
+
+ ProcessXml ();
ProjectCollection.AddProject (this);
}
- void ProcessXml (Project parent)
+ void ProcessXml ()
{
// this needs to be initialized here (regardless of that items won't be evaluated at property evaluation;
// Conditions could incorrectly reference items and lack of this list causes NRE.
// At first step, all non-imported properties are evaluated TOO, WHILE those properties are being evaluated.
// This means, Include and IncludeGroup elements with Condition attribute MAY contain references to
// properties and they will be expanded.
- var elements = EvaluatePropertiesAndImports (Xml.Children).ToArray (); // ToArray(): to not lazily evaluate elements.
+ var elements = EvaluatePropertiesAndImportsAndChooses (Xml.Children).ToArray (); // ToArray(): to not lazily evaluate elements.
// next, evaluate items
EvaluateItems (elements);
EvaluateTargets (elements);
}
- IEnumerable<ProjectElement> EvaluatePropertiesAndImports (IEnumerable<ProjectElement> elements)
+ IEnumerable<ProjectElement> EvaluatePropertiesAndImportsAndChooses (IEnumerable<ProjectElement> elements)
{
// First step: evaluate Properties
foreach (var child in elements) {
if (inc != null && Evaluate (inc.Condition))
foreach (var e in Import (inc))
yield return e;
+ var choose = child as ProjectChooseElement;
+ if (choose != null && Evaluate (choose.Condition)) {
+ bool done = false;
+ foreach (ProjectWhenElement when in choose.WhenElements)
+ if (Evaluate (when.Condition)) {
+ foreach (var e in EvaluatePropertiesAndImportsAndChooses (when.Children))
+ yield return e;
+ done = true;
+ break;
+ }
+ if (!done && choose.OtherwiseElement != null)
+ foreach (var e in EvaluatePropertiesAndImportsAndChooses (choose.OtherwiseElement.Children))
+ yield return e;
+ }
}
}
foreach (var child in elements) {
var te = child as ProjectTargetElement;
if (te != null)
- this.targets.Add (te.Name, new ProjectTargetInstance (te));
+ // It overwrites same name target.
+ this.targets [te.Name] = new ProjectTargetInstance (te);
}
}
-
+
IEnumerable<ProjectElement> Import (ProjectImportElement import)
{
string dir = ProjectCollection.GetEvaluationTimeThisFileDirectory (() => FullPath);
- string path = WindowsCompatibilityExtensions.NormalizeFilePath (ExpandString (import.Project));
+ // FIXME: use appropriate logger (but cannot be instantiated here...?)
+ string path = ProjectCollection.FindFileInSeveralExtensionsPath (ref extensions_path_override, ExpandString, import.Project, TextWriter.Null.WriteLine);
path = Path.IsPathRooted (path) ? path : dir != null ? Path.Combine (dir, path) : Path.GetFullPath (path);
if (ProjectCollection.OngoingImports.Contains (path)) {
switch (load_settings) {
using (var reader = XmlReader.Create (path)) {
var root = ProjectRootElement.Create (reader, ProjectCollection);
raw_imports.Add (new ResolvedImport (import, root, true));
- return this.EvaluatePropertiesAndImports (root.Children).ToArray ();
+ return this.EvaluatePropertiesAndImportsAndChooses (root.Children).ToArray ();
}
} finally {
ProjectCollection.OngoingImports.Pop ();
public bool Build ()
{
- return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries));
+ return Build (GetDefaultTargets (Xml));
}
public bool Build (IEnumerable<ILogger> loggers)
{
- return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), loggers);
+ return Build (GetDefaultTargets (Xml), loggers);
}
public bool Build (string target)
public bool Build (ILogger logger)
{
- return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), new ILogger [] {logger});
+ return Build (GetDefaultTargets (Xml), new ILogger [] {logger});
}
public bool Build (string[] targets, IEnumerable<ILogger> loggers)
public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
{
- return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), loggers, remoteLoggers);
+ return Build (GetDefaultTargets (Xml), loggers, remoteLoggers);
}
public bool Build (string target, IEnumerable<ILogger> loggers)
return Build (new string [] { target }, loggers, remoteLoggers);
}
+ // FIXME: this is a duplicate code between Project and ProjectInstance
+ static readonly char [] item_target_sep = {';'};
+
+ string [] GetDefaultTargets (ProjectRootElement xml)
+ {
+ var ret = GetDefaultTargets (xml, true, true);
+ return ret.Any () ? ret : GetDefaultTargets (xml, false, true);
+ }
+
+ string [] GetDefaultTargets (ProjectRootElement xml, bool fromAttribute, bool checkImports)
+ {
+ if (fromAttribute) {
+ var ret = xml.DefaultTargets.Split (item_target_sep, StringSplitOptions.RemoveEmptyEntries).Select (s => s.Trim ()).ToArray ();
+ if (checkImports && ret.Length == 0) {
+ foreach (var imp in this.raw_imports) {
+ ret = GetDefaultTargets (imp.ImportedProject, true, false);
+ if (ret.Any ())
+ break;
+ }
+ }
+ return ret;
+ } else {
+ if (xml.Targets.Any ())
+ return new String [] { xml.Targets.First ().Name };
+ if (checkImports) {
+ foreach (var imp in this.raw_imports) {
+ var ret = GetDefaultTargets (imp.ImportedProject, false, false);
+ if (ret.Any ())
+ return ret;
+ }
+ }
+ return new string [0];
+ }
+ }
+
public ProjectInstance CreateProjectInstance ()
{
var ret = new ProjectInstance (Xml, GlobalProperties, ToolsVersion, ProjectCollection);
bool Evaluate (string unexpandedValue)
{
- return string.IsNullOrWhiteSpace (unexpandedValue) || new ExpressionEvaluator (this, null).EvaluateAsBoolean (unexpandedValue);
+ return string.IsNullOrWhiteSpace (unexpandedValue) || new ExpressionEvaluator (this).EvaluateAsBoolean (unexpandedValue);
}
public string ExpandString (string unexpandedValue)
{
- return ExpandString (unexpandedValue, null);
- }
-
- string ExpandString (string unexpandedValue, string replacementForMissingStuff)
- {
- return new ExpressionEvaluator (this, replacementForMissingStuff).Evaluate (unexpandedValue);
+ return WindowsCompatibilityExtensions.NormalizeFilePath (new ExpressionEvaluator (this).Evaluate (unexpandedValue));
}
public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
return property.EvaluatedValue;
}
+ string extensions_path_override;
+
public ProjectProperty GetProperty (string name)
{
+ if (extensions_path_override != null && (name.Equals ("MSBuildExtensionsPath") || name.Equals ("MSBuildExtensionsPath32") || name.Equals ("MSBuildExtensionsPath64")))
+ return new ReservedProjectProperty (this, name, () => extensions_path_override);
return properties.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
}
public bool SkipEvaluation { get; set; }
- #if NET_4_5
public
- #else
- internal
- #endif
IDictionary<string, ProjectTargetInstance> Targets {
get { return targets; }
}