2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / tools / respack / respack.cs
blobe399ddd3471642c06423a5909e0706ff2fda63f5
1 //
2 // Packs a bunch of files as a resource
3 //
4 // Used to bundle all the xaml files into the APP.g.resources file
5 //
6 // Copyrigh 2008 Novell, Inc.
7 //
8 using System;
9 using System.Reflection;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Text.RegularExpressions;
13 using System.Resources;
14 using System.IO;
15 using System.Linq;
16 using NDesk.Options;
18 class ResourcePacker {
20 static void ShowHelp (OptionSet os)
22 Console.WriteLine ("Usage: respack outputfile [file1[,name] .. [fileN]]");
23 Console.WriteLine ();
24 os.WriteOptionDescriptions (Console.Out);
25 Environment.Exit (0);
28 static bool verbose = false;
30 public static int Main (string [] args)
32 bool help = false;
33 bool unpack = false;
34 string pattern = @"^.+\.xaml?";
36 var p = new OptionSet () {
37 { "h|?|help", v => help = v != null },
38 { "u|unpack", "Extract resources from the supplied assembly.", v => unpack = v != null },
39 { "p|pattern=", "Only extract the resources that match supplied pattern. By default only .xaml files will be extracted.", v => pattern = v },
40 { "v|verbose", v=> verbose = v != null }
43 List<string> files = null;
44 try {
45 files = p.Parse(args);
46 } catch (OptionException e){
47 Console.WriteLine ("Try `respack --help' for more information.");
48 return 1;
51 if (help)
52 ShowHelp (p);
54 if (unpack)
55 return Unpack (files [0], pattern);
57 if (files == null || files.Count == 0)
58 ShowHelp (p);
60 return Pack (files);
63 public static int Pack (List<string> files)
65 ResourceWriter output = null;
66 try {
67 output = new ResourceWriter (files [0]);
68 } catch {
69 Console.WriteLine ("Error creating {0} file", files [0]);
73 foreach (string f in files.Skip(1)){
74 string key;
75 string file = f;
76 int comma = file.IndexOf (',');
77 if (comma != -1) {
78 key = file.Substring (comma + 1);
79 file = file.Substring (0, comma);
80 } else {
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);
98 output.Generate ();
99 return 0;
102 public static int Unpack (string assembly, string pattern)
104 Assembly asm = null;
105 try {
106 asm = Assembly.LoadFile (assembly);
107 } catch (Exception e) {
108 Console.Error.WriteLine ("Unable to load assembly: {0}.", assembly);
109 Console.Error.WriteLine (e);
110 return 1;
113 string [] resources = asm.GetManifestResourceNames ();
115 foreach (string resource in resources) {
116 if (verbose)
117 Console.WriteLine ("decompressing '{0}'", resource);
119 ResourceReader reader = null;
121 try {
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))
129 continue;
131 MemoryStream stream = id.Value as MemoryStream;
132 if (stream == null) {
133 Console.Error.WriteLine ("Item not stored as a MemoryStream. {0}", key);
134 continue;
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);
155 return 0;