2 // Import.cs: Represents a single Import element in an MSBuild project.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using Microsoft.Build.Framework;
35 using Mono.XBuild.Utilities;
37 namespace Microsoft.Build.BuildEngine {
39 XmlElement importElement;
41 ImportedProject originalProject;
42 string evaluatedProjectPath;
44 static string DotConfigExtensionsPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
45 Path.Combine ("xbuild", "tasks"));
46 const string MacOSXExternalXBuildDir = "/Library/Frameworks/Mono.framework/External/xbuild";
48 internal Import (XmlElement importElement, Project project, ImportedProject originalProject)
50 if (importElement == null)
51 throw new ArgumentNullException ("importElement");
53 throw new ArgumentNullException ("project");
55 this.project = project;
56 this.importElement = importElement;
57 this.originalProject = originalProject;
59 if (ProjectPath == String.Empty)
60 throw new InvalidProjectFileException ("The required attribute \"Project\" is missing from element <Import>.");
62 if (ConditionParser.ParseAndEvaluate (Condition, project)) {
63 evaluatedProjectPath = EvaluateProjectPath (ProjectPath);
64 evaluatedProjectPath = GetFullPath ();
65 if (EvaluatedProjectPath == String.Empty)
66 throw new InvalidProjectFileException ("The required attribute \"Project\" is missing from element <Import>.");
71 internal void Evaluate (bool ignoreMissingImports)
73 string filename = evaluatedProjectPath;
74 // NOTE: it's a hack to transform Microsoft.CSharp.Targets to Microsoft.CSharp.targets
75 if (Path.HasExtension (filename))
76 filename = Path.ChangeExtension (filename, Path.GetExtension (filename));
78 if (!File.Exists (filename)) {
79 if (ignoreMissingImports) {
80 project.LogWarning (project.FullFileName, "Could not find project file {0}, to import. Ignoring.", filename);
83 throw new InvalidProjectFileException (String.Format ("Imported project: \"{0}\" does not exist.", filename));
87 ImportedProject importedProject = new ImportedProject ();
88 importedProject.Load (filename);
90 project.ProcessElements (importedProject.XmlDocument.DocumentElement, importedProject);
93 string EvaluateProjectPath (string file)
96 if (EvaluateAsMSBuildExtensionsPath (file, "MSBuildExtensionsPath", out ret) ||
97 EvaluateAsMSBuildExtensionsPath (file, "MSBuildExtensionsPath32", out ret) ||
98 EvaluateAsMSBuildExtensionsPath (file, "MSBuildExtensionsPath64", out ret))
101 return EvaluatePath (file);
104 bool EvaluateAsMSBuildExtensionsPath (string file, string property_name, out string epath)
107 string property_ref = String.Format ("$({0})", property_name);
108 if (file.IndexOf (property_ref) < 0)
111 // This is a *HACK* to support multiple paths for
112 // MSBuildExtensionsPath property. Normally it would
113 // get resolved to a single value, but here we special
114 // case it and try ~/.config/xbuild/tasks and any
115 // paths specified in the env var $MSBuildExtensionsPath .
117 // The property itself will resolve to the default
118 // location though, so you get in any other part of the
121 string envvar = Environment.GetEnvironmentVariable (property_name);
122 envvar = String.Join (":", new string [] {
123 // For mac osx, look in the 'External' dir on macosx,
125 MSBuildUtils.RunningOnMac ? MacOSXExternalXBuildDir : String.Empty,
126 (envvar ?? String.Empty),
127 DotConfigExtensionsPath});
129 string [] paths = envvar.Split (new char [] {':'}, StringSplitOptions.RemoveEmptyEntries);
130 foreach (string path in paths) {
131 if (!Directory.Exists (path)) {
132 project.ParentEngine.LogMessage (MessageImportance.Low, "Extension path '{0}' not found, ignoring.", path);
136 string pfile = Path.GetFullPath (file.Replace ("\\", "/").Replace (
137 property_ref, path + Path.DirectorySeparatorChar));
139 var evaluated_path = EvaluatePath (pfile);
140 if (File.Exists (evaluated_path)) {
141 project.ParentEngine.LogMessage (MessageImportance.Low,
142 "{0}: Importing project {1} from extension path {2}", project.FullFileName, evaluated_path, path);
146 project.ParentEngine.LogMessage (MessageImportance.Low,
147 "{0}: Couldn't find project {1} for extension path {2}", project.FullFileName, evaluated_path, path);
153 string EvaluatePath (string path)
155 var exp = new Expression ();
156 exp.Parse (path, ParseOptions.Split);
157 return (string) exp.ConvertTo (project, typeof (string));
160 string GetFullPath ()
162 string file = EvaluatedProjectPath;
164 if (!Path.IsPathRooted (EvaluatedProjectPath)) {
166 if (originalProject == null) {
167 if (project.FullFileName != String.Empty) // Path.GetDirectoryName throws exception on String.Empty
168 dir = Path.GetDirectoryName (project.FullFileName);
170 if (originalProject.FullFileName != String.Empty)
171 dir = Path.GetDirectoryName (originalProject.FullFileName);
174 file = Path.Combine (dir, EvaluatedProjectPath);
177 return MSBuildUtils.FromMSBuildPath (file);
180 public string Condition {
182 string s = importElement.GetAttribute ("Condition");
183 return s == String.Empty ? null : s;
187 public string EvaluatedProjectPath {
188 get { return evaluatedProjectPath; }
191 public bool IsImported {
192 get { return originalProject != null; }
195 public string ProjectPath {
196 get { return importElement.GetAttribute ("Project"); }