Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mcs / class / Moonlight.Build.Tasks / Moonlight.Build.Tasks / GenerateXap.cs
1 //
2 // GenerateXap.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 GenerateXap : Task {
45
46                 public override bool Execute ()
47                 {
48                         if (InputFiles.Length == 0)
49                                 return true;
50
51                         return Zip ();
52                 }
53
54                 bool Zip ()
55                 {
56                         var xapName = XapFilename.ItemSpec;
57                         if (File.Exists (xapName)) {
58                                 DateTime lastMod = File.GetLastWriteTime (xapName);
59                                 bool needsWrite = false;
60                                 foreach (ITaskItem file_item in InputFiles) {
61                                         if (File.GetLastWriteTime (file_item.ItemSpec) > lastMod) {
62                                                 needsWrite = true;
63                                                 break;
64                                         }
65                                 }
66                                 if (!needsWrite) {
67                                         Log.LogMessage (MessageImportance.Low, "Skipping xap file {0} generation, its up-to date");
68                                         return true;
69                                 }
70                         }
71
72                         Log.LogMessage (MessageImportance.Normal, "Generating compressed xap file {0}", xapName);
73                         try {
74                                 using (FileStream fs = new FileStream (xapName, FileMode.Create)) {
75                                         var zip_stream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream (fs);
76                                         zip_stream.SetLevel (9);
77
78                                         AddFilesToZip (InputFiles, zip_stream);
79                                         AddFilesToZip (LocalCopyReferences, zip_stream);
80
81                                         zip_stream.Finish ();
82                                         zip_stream.Close ();
83                                 }
84                         } catch (IOException ex) {
85                                 Log.LogError ("Error writing xap file.", ex);
86                                 Log.LogMessage (MessageImportance.Low, "Error writing xap file:" + ex.ToString ());
87
88                                 try {
89                                         if (File.Exists (xapName))
90                                                 File.Delete (xapName);
91                                 } catch {}
92
93                                 return false;
94                         }
95
96                         return true;
97                 }
98
99                 void AddFilesToZip (ITaskItem [] files, ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream)
100                 {
101                         if (files == null)
102                                 return;
103
104                         foreach (ITaskItem item in files) {
105                                 string target_path = item.GetMetadata ("TargetPath");
106                                 if (String.IsNullOrEmpty (target_path))
107                                         target_path = Path.GetFileName (item.ItemSpec);
108
109                                 zipStream.PutNextEntry (new ICSharpCode.SharpZipLib.Zip.ZipEntry (target_path));
110                                 using (FileStream inStream = File.OpenRead (item.ItemSpec)) {
111                                         int readCount;
112                                         byte[] buffer = new byte[4096];
113
114                                         do {
115                                                 readCount = inStream.Read (buffer, 0, buffer.Length);
116                                                 zipStream.Write (buffer, 0, readCount);
117                                         } while (readCount > 0);
118                                 }
119                         }
120                 }
121
122                 [Output]
123                 [Required]
124                 public ITaskItem XapFilename {
125                         get; set;
126                 }
127
128                 [Required]
129                 public ITaskItem[] InputFiles {
130                         get; set;
131                 }
132
133                 public ITaskItem[] LocalCopyReferences {
134                         get; set;
135                 }
136
137         }
138
139
140 }