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 148 149 150 151 152 153 154 155 156 157
|
//
// ProjectItem.cs
//
// Author:
// Leszek Ciesielski (skolima@gmail.com)
// Rolf Bjarne Kvinge (rolf@xamarin.com)
// Atsushi Enomoto (atsushi@xamarin.com)
//
// (C) 2011 Leszek Ciesielski
// Copyright (C) 2011,2013 Xamarin Inc.
//
// 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.Diagnostics;
using System.Linq;
using Microsoft.Build.Construction;
using System.IO;
using Microsoft.Build.Framework;
namespace Microsoft.Build.Evaluation
{
[DebuggerDisplay ("{ItemType}={EvaluatedInclude} [{UnevaluatedInclude}] #DirectMetadata={DirectMetadataCount}")]
public class ProjectItem
{
internal ProjectItem (Project project, ProjectItemElement xml, string evaluatedInclude)
{
this.project = project;
this.xml = xml;
if (project.ItemDefinitions.ContainsKey (ItemType))
foreach (var md in project.ItemDefinitions [ItemType].Metadata)
metadata.Add (md);
foreach (var md in xml.Metadata)
metadata.Add (new ProjectMetadata (project, ItemType, metadata, m => metadata.Remove (m), md));
this.evaluated_include = evaluatedInclude;
is_imported = project.ProjectCollection.OngoingImports.Any ();
}
readonly Project project;
readonly ProjectItemElement xml;
readonly List<ProjectMetadata> metadata = new List<ProjectMetadata> ();
readonly bool is_imported;
readonly string evaluated_include;
internal string RecursiveDir { get; set; }
public ProjectMetadata GetMetadata (string name)
{
return metadata.FirstOrDefault (m => m.Name == name);
}
public string GetMetadataValue (string name)
{
if (name == null)
throw new ArgumentNullException ("name");
var wk = ProjectCollection.GetWellKnownMetadata (name, EvaluatedInclude, project.GetFullPath, RecursiveDir);
if (wk != null)
return wk;
var m = GetMetadata (name);
return m != null ? m.EvaluatedValue : string.Empty;
}
public bool HasMetadata (string name)
{
return GetMetadata (name) != null;
}
public bool RemoveMetadata (string name)
{
var m = GetMetadata (name);
if (m == null)
return false;
return metadata.Remove (m);
}
public void Rename (string name)
{
throw new NotImplementedException ();
}
public ProjectMetadata SetMetadataValue (string name, string unevaluatedValue)
{
// This has to do several tasks:
// - it cannot directly change Xml.Metadata because the ProjectItemElement might be shared
// among multiple ProjectItems.
// - hence it has to create another ProjectItemElement instance and add it to the project
// XML construction, with specific Include value that is assigned to this instance, and
// metadata values that are assigned to this instance.
throw new NotImplementedException ();
}
public IEnumerable<ProjectMetadata> DirectMetadata {
get {
var list = new List<ProjectMetadata> ();
foreach (var xm in xml.Metadata)
yield return new ProjectMetadata (project, ItemType, list, p => list.Remove (p), xm);
}
}
public int DirectMetadataCount {
get { return xml.Metadata.Count; }
}
public string EvaluatedInclude {
get { return evaluated_include; }
}
public bool IsImported {
get { return is_imported; }
}
public string ItemType {
get { return Xml.ItemType; }
set { Xml.ItemType = value; }
}
public ICollection<ProjectMetadata> Metadata {
get { return metadata; }
}
public int MetadataCount {
get { return metadata.Count; }
}
public Project Project {
get { return project; }
}
public string UnevaluatedInclude {
get { return Xml.Include; }
set { Xml.Include = value; }
}
public ProjectItemElement Xml {
get { return xml; }
}
}
}
|