2007-01-16 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Import.cs
index 3f41bd9c51a7aa66c7ef637ad84957b69f2447f3..34c9615c056b79ca0e0d557846d2e50d40ad8411 100644 (file)
@@ -50,42 +50,61 @@ namespace Microsoft.Build.BuildEngine {
                        this.originalProject = originalProject;
 
                        if (ProjectPath == String.Empty)
-                               throw new InvalidProjectFileException ("Project attribute must be specified.");
+                               throw new InvalidProjectFileException ("The required attribute \"Project\" is missing from element <Import>.");
                }
 
                internal void Evaluate ()
                {
-                       string file;
-                       OldExpression exp;
+                       evaluatedProjectPath = EvaluateProjectPath (ProjectPath);
+                       string filename = GetFullPath ();
 
-                       exp = new OldExpression (project);
-                       exp.ParseSource (ProjectPath);
+                       if (EvaluatedProjectPath == String.Empty)
+                               throw new InvalidProjectFileException ("The required attribute \"Project\" is missing from element <Import>.");
                        
-                       file = evaluatedProjectPath = (string) exp.ConvertTo (typeof (string));
-
-                       if (Path.IsPathRooted (EvaluatedProjectPath) == false) {
-                               string dir;
-                               if (originalProject == null)
-                                       dir = Path.GetDirectoryName (project.FullFileName);
-                               else
-                                       dir = Path.GetDirectoryName (originalProject.FullFileName);
-                               file = Path.Combine (dir, EvaluatedProjectPath);
-                       }
-
-                       // FIXME: loggers anybody?
-                       if (!File.Exists (file)) {
-                               Console.WriteLine ("Imported file {0} doesn't exist.", file);
-                               return;
+                       if (!File.Exists (filename)) {
+                               throw new InvalidProjectFileException (String.Format ("Imported project: \"{0}\" does not exist.", filename));
                        }
                        
                        ImportedProject importedProject = new ImportedProject ();
-                       importedProject.Load (file);
-                       // FIXME: UGLY HACK
+                       importedProject.Load (filename);
+
                        project.ProcessElements (importedProject.XmlDocument.DocumentElement, importedProject);
                }
+
+               string EvaluateProjectPath (string file)
+               {
+                       Expression exp;
+
+                       exp = new Expression ();
+                       exp.Parse (file);
+                       return (string) exp.ConvertTo (project, typeof (string));
+               }
+
+               string GetFullPath ()
+               {
+                       string file = EvaluatedProjectPath;
+
+                       if (!Path.IsPathRooted (EvaluatedProjectPath)) {
+                               string dir = null;
+                               if (originalProject == null) {
+                                       if (project.FullFileName != String.Empty) // Path.GetDirectoryName throws exception on String.Empty
+                                               dir = Path.GetDirectoryName (project.FullFileName);
+                               } else {
+                                       if (originalProject.FullFileName != String.Empty)
+                                               dir = Path.GetDirectoryName (originalProject.FullFileName);
+                               }
+                               if (dir != null)
+                                       file = Path.Combine (dir, EvaluatedProjectPath);
+                       }
+                       
+                       return file;
+               }
                
                public string Condition {
-                       get { return importElement.GetAttribute ("Condition"); }
+                       get {
+                               string s = importElement.GetAttribute ("Condition");
+                               return s == String.Empty ? null : s;
+                       }
                }
                
                public string EvaluatedProjectPath {