Add [Category ("NotWorking")] to failing test.
[mono.git] / mcs / class / Moonlight.Build.Tasks / Moonlight.Build.Tasks / GenerateMoonlightManifest.cs
1 //
2 // GenerateMoonlightManifest.cs
3 //
4 // Author:
5 //      Michael Hutchinson <mhutchinson@novell.com>
6 //      Ankit Jain <jankit@novell.com>
7 //
8 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
9 // Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.IO;
34 using System.CodeDom;
35 using System.CodeDom.Compiler;
36 using System.Xml;
37
38 using Microsoft.CSharp;
39
40 using Microsoft.Build.Framework;
41 using Microsoft.Build.Utilities;
42
43 namespace Moonlight.Build.Tasks {
44         public class GenerateMoonlightManifest : Task {
45
46                 public override bool Execute ()
47                 {
48                         return GenerateManifest ();
49                 }
50
51                 bool GenerateManifest ()
52                 {
53                         const string depNS = "http://schemas.microsoft.com/client/2007/deployment";
54
55                         string template = null;
56                         var manifest = ManifestFile.ItemSpec;
57                         Log.LogMessage (MessageImportance.Normal, "Generating manifest file {0}", manifest);
58
59                         if (SilverlightManifestTemplate != null)
60                                 template = String.IsNullOrEmpty (SilverlightManifestTemplate.ItemSpec) ?
61                                                         null :
62                                                         SilverlightManifestTemplate.GetMetadata ("FullPath");
63
64                         XmlDocument doc = new XmlDocument ();
65                         if (template != null) {
66                                 if (!File.Exists (template)) {
67                                         Log.LogError ("Could not find manifest template '" +  template + "'.");
68                                         return false;
69                                 }
70
71                                 try {
72                                         doc.Load (template);
73                                 } catch (XmlException ex) {
74                                         Log.LogError (null, null, null, template, ex.LineNumber, ex.LinePosition, 0, 0,
75                                                         "Error loading manifest template '" + ex.Source);
76                                         return false;
77                                 } catch (Exception ex) {
78                                         Log.LogError ("Could not load manifest template '" +  template + "'.");
79                                         Log.LogMessage (MessageImportance.Low, "Could not load manifest template '" +  template + "': " + ex.ToString ());
80                                         return false;
81                                 }
82
83                         } else {
84                                 doc.LoadXml (@"<Deployment xmlns=""http://schemas.microsoft.com/client/2007/deployment"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""></Deployment>");
85                         }
86
87                         try {
88                                 XmlNode deploymentNode = doc.DocumentElement;
89                                 if (deploymentNode == null || deploymentNode.Name != "Deployment" || deploymentNode.NamespaceURI != depNS) {
90                                         Log.LogError ("Missing or invalid root <Deployment> element in manifest template '" +  template + "'.");
91                                         return false;
92                                 }
93                                 if (deploymentNode.Attributes["EntryPointAssembly"] == null)
94                                         deploymentNode.Attributes.Append (doc.CreateAttribute ("EntryPointAssembly")).Value =
95                                                 EntryPointAssembly.GetMetadata ("Filename");
96
97                                 if (!String.IsNullOrEmpty (SilverlightAppEntry) && deploymentNode.Attributes["EntryPointType"] == null)
98                                         deploymentNode.Attributes.Append (doc.CreateAttribute ("EntryPointType")).Value = SilverlightAppEntry;
99
100                                 if (deploymentNode.Attributes["RuntimeVersion"] == null) {
101                                         //FIXME:
102                                         /*string fxVersion = MoonlightFrameworkBackend.GetFxVersion (proj.TargetFramework);
103
104                                         if (proj.TargetRuntime is MonoDevelop.Core.Assemblies.MonoTargetRuntime) {
105                                                 var package = proj.TargetRuntime.RuntimeAssemblyContext.GetPackage ("moonlight-web-" + fxVersion);
106                                                 if (package != null && package.IsFrameworkPackage) {
107                                                         runtimeVersion = package.Version;
108                                                 } else {
109                                                         LoggingService.LogWarning ("Moonlight core framework package not found, cannot determine " +
110                                                                 "runtime version string. Falling back to default value.");
111                                                 }
112                                         }*/
113
114                                         deploymentNode.Attributes.Append (doc.CreateAttribute ("RuntimeVersion")).Value =
115                                                         String.IsNullOrEmpty (RuntimeVersion) ? "2.0.31005.0" : RuntimeVersion;
116                                 }
117
118                                 XmlNamespaceManager mgr = new XmlNamespaceManager (doc.NameTable);
119                                 mgr.AddNamespace ("dep", depNS);
120                                 XmlNode partsNode = deploymentNode.SelectSingleNode ("dep:Deployment.Parts", mgr);
121                                 if (partsNode == null)
122                                         partsNode = deploymentNode.AppendChild (doc.CreateElement ("Deployment.Parts", depNS));
123
124                                 AddAssemblyPart (doc, partsNode, EntryPointAssembly);
125
126                                 foreach (ITaskItem ref_item in References)
127                                         AddAssemblyPart (doc, partsNode, ref_item);
128                         } catch (XmlException ex) {
129                                 Log.LogError (null, null, null, template, ex.LineNumber, ex.LinePosition, 0, 0,
130                                                 "Error processing manifest template: '" + ex.Source);
131                                 return false;
132                         }
133
134                         doc.Save (manifest);
135
136                         return true;
137                 }
138
139                 static void AddAssemblyPart (XmlDocument doc, XmlNode partsNode, ITaskItem filename)
140                 {
141                         XmlNode child = doc.CreateElement ("AssemblyPart", "http://schemas.microsoft.com/client/2007/deployment");
142                         child.Attributes.Append (doc.CreateAttribute (
143                                                 "Name", "http://schemas.microsoft.com/winfx/2006/xaml")).Value = filename.GetMetadata ("Filename");
144                         string subdir = filename.GetMetadata ("DestinationSubdirectory");
145                         child.Attributes.Append (doc.CreateAttribute ("Source")).Value = Path.Combine (subdir ?? String.Empty, Path.GetFileName (filename.ItemSpec));
146                         partsNode.AppendChild (child);
147                 }
148
149                 [Required]
150                 [Output]
151                 public ITaskItem ManifestFile {
152                         get; set;
153                 }
154
155                 [Required]
156                 // with extension
157                 public ITaskItem EntryPointAssembly {
158                         get; set;
159                 }
160
161                 [Required]
162                 public ITaskItem[] References {
163                         get; set;
164                 }
165
166                 public ITaskItem SilverlightManifestTemplate {
167                         get; set;
168                 }
169
170                 public string SilverlightAppEntry {
171                         get; set;
172                 }
173
174                 public string RuntimeVersion {
175                         get; set;
176                 }
177         }
178
179 }