5 // Michael Hutchinson <mhutchinson@novell.com>
6 // Ankit Jain <jankit@novell.com>
8 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
9 // Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Generic;
35 using System.CodeDom.Compiler;
38 using Microsoft.CSharp;
40 using Microsoft.Build.Framework;
41 using Microsoft.Build.Utilities;
43 namespace Moonlight.Build.Tasks {
44 public class GenerateXap : Task {
46 public override bool Execute ()
48 if (InputFiles.Length == 0)
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) {
67 Log.LogMessage (MessageImportance.Low, "Skipping xap file {0} generation, its up-to date");
72 Log.LogMessage (MessageImportance.Normal, "Generating compressed xap file {0}", xapName);
74 using (FileStream fs = new FileStream (xapName, FileMode.Create)) {
75 var zip_stream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream (fs);
76 zip_stream.SetLevel (9);
78 AddFilesToZip (InputFiles, zip_stream);
79 AddFilesToZip (LocalCopyReferences, zip_stream);
84 } catch (IOException ex) {
85 Log.LogError ("Error writing xap file.", ex);
86 Log.LogMessage (MessageImportance.Low, "Error writing xap file:" + ex.ToString ());
89 if (File.Exists (xapName))
90 File.Delete (xapName);
99 void AddFilesToZip (ITaskItem [] files, ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream)
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);
109 zipStream.PutNextEntry (new ICSharpCode.SharpZipLib.Zip.ZipEntry (target_path));
110 using (FileStream inStream = File.OpenRead (item.ItemSpec)) {
112 byte[] buffer = new byte[4096];
115 readCount = inStream.Read (buffer, 0, buffer.Length);
116 zipStream.Write (buffer, 0, readCount);
117 } while (readCount > 0);
124 public ITaskItem XapFilename {
129 public ITaskItem[] InputFiles {
133 public ITaskItem[] LocalCopyReferences {