2 // Packs a bunch of files as a resource
4 // Used to bundle all the xaml files into the APP.g.resources file
6 // Copyrigh 2008 Novell, Inc.
9 using System
.Reflection
;
10 using System
.Collections
;
11 using System
.Collections
.Generic
;
12 using System
.Text
.RegularExpressions
;
13 using System
.Resources
;
18 class ResourcePacker
{
20 static void ShowHelp (OptionSet os
)
22 Console
.WriteLine ("Usage: respack outputfile [file1[,name] .. [fileN]]");
24 os
.WriteOptionDescriptions (Console
.Out
);
28 static bool verbose
= false;
30 public static int Main (string [] args
)
33 bool decompress
= false;
34 string pattern
= @"^.+\.xaml?";
36 var p
= new OptionSet () {
37 { "h|?|help", v => help = v != null }
,
38 { "d|decomress", "Decompress the supplied assembly.", v => decompress = v != null }
,
39 { "p|pattern=", "Only decompress the resources that match supplied pattern. By default only .xaml files will be decompressed.", v => pattern = v }
,
40 { "v|verbose", v=> verbose = v != null }
43 List
<string> files
= null;
45 files
= p
.Parse(args
);
46 } catch (OptionException e
){
47 Console
.WriteLine ("Try `respack --help' for more information.");
55 return Decompress (files
[0], pattern
);
57 if (files
== null || files
.Count
== 0)
60 return Compress (files
);
63 public static int Compress (List
<string> files
)
65 ResourceWriter output
= null;
67 output
= new ResourceWriter (files
[0]);
69 Console
.WriteLine ("Error creating {0} file", files
[0]);
73 foreach (string f
in files
.Skip(1)){
76 int comma
= file
.IndexOf (',');
78 key
= file
.Substring (comma
+ 1);
79 file
= file
.Substring (0, comma
);
81 key
= Path
.GetFileName (file
).ToLower ();
85 using (FileStream source
= File
.OpenRead (file
)){
86 byte [] buffer
= new byte [source
.Length
];
87 source
.Read (buffer
, 0, (int) source
.Length
);
89 // Sadly, we cant just pass byte arrays to resourcewriter, we need
90 // to wrap this on a MemoryStream
92 MemoryStream
value = new MemoryStream (buffer
);
94 output
.AddResource (key
.ToLowerInvariant (), value);
102 public static int Decompress (string assembly
, string pattern
)
106 asm
= Assembly
.LoadFile (assembly
);
107 } catch (Exception e
) {
108 Console
.Error
.WriteLine ("Unable to load assembly: {0}.", assembly
);
109 Console
.Error
.WriteLine (e
);
113 string [] resources
= asm
.GetManifestResourceNames ();
115 foreach (string resource
in resources
) {
117 Console
.WriteLine ("decompressing '{0}'", resource
);
119 ResourceReader reader
= null;
122 using (reader
= new ResourceReader (asm
.GetManifestResourceStream (resource
))) {
124 IDictionaryEnumerator id
= reader
.GetEnumerator ();
126 while (id
.MoveNext ()) {
127 string key
= (string) id
.Key
;
128 if (!Regex
.IsMatch (key
, pattern
))
131 MemoryStream stream
= id
.Value
as MemoryStream
;
132 if (stream
== null) {
133 Console
.Error
.WriteLine ("Item not stored as a MemoryStream. {0}", key
);
137 byte [] data
= new byte [stream
.Length
];
138 stream
.Read (data
, 0, data
.Length
);
140 string dir
= Path
.GetDirectoryName (key
);
141 if (!String
.IsNullOrEmpty (dir
))
142 Directory
.CreateDirectory (dir
);
144 using (FileStream fs
= File
.OpenWrite (key
)) {
145 fs
.Write (data
, 0, data
.Length
);
150 } catch (Exception e
) {
151 Console
.WriteLine ("failed to decompress {0}, exception '{1}'.. skipping", resource
, e
.Message
);