1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
//
// Author: Geoff Norton
// de-MonoOptionification: miguel.
//
// Copyright (C) 2004-2005 Geoff Norton.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Mac {
public class PackOptions {
public string appname;
public string output;
public string assembly;
public string icon;
public string[] resource;
public int mode;
}
public class Pack {
private PackOptions opts;
public Pack () {}
public Pack (PackOptions opts) {
this.opts = opts;
}
public bool Generate () {
if (opts.output == null){
opts.output = ".";
}
if (opts.assembly == null){
Console.Error.WriteLine ("Error: No assembly to macpack was specified");
Usage ();
return false;
}
if (opts.appname == null){
string t = Path.ChangeExtension (opts.assembly, null);
int p = t.IndexOf (Path.DirectorySeparatorChar);
if (p != -1)
t = t.Substring (p+1);
opts.appname = t;
}
if (Directory.Exists (Path.Combine (opts.output, String.Format ("{0}.app", opts.appname)))) {
Console.WriteLine ("ERROR: That application already exists. Please delete it first");
return false;
}
Directory.CreateDirectory (Path.Combine (opts.output, String.Format ("{0}.app", opts.appname)));
Directory.CreateDirectory (Path.Combine (opts.output, String.Format ("{0}.app/Contents", opts.appname)));
Directory.CreateDirectory (Path.Combine (opts.output, String.Format ("{0}.app/Contents/MacOS", opts.appname)));
Directory.CreateDirectory (Path.Combine (opts.output, String.Format ("{0}.app/Contents/Resources", opts.appname)));
if (opts.resource != null) {
foreach (string res in opts.resource) {
try {
if (Directory.Exists (res)) {
CopyDirectory (res, Path.Combine (opts.output, String.Format ("{0}.app/Contents/Resources/{1}", opts.appname, Path.GetFileName (res))));
} else {
File.Copy (res, Path.Combine (opts.output, String.Format ("{0}.app/Contents/Resources/{1}", opts.appname, Path.GetFileName (res))));
}
} catch (Exception e){
Console.Error.WriteLine ("Error while processing {0} (Details: {1})", res, e.GetType ());
}
}
}
if (opts.icon != null)
File.Copy (opts.icon, Path.Combine (opts.output, String.Format ("{0}.app/Contents/Resources/{1}", opts.appname, Path.GetFileName (opts.icon))));
if (opts.mode <= 2) {
File.Copy (opts.assembly, Path.Combine (opts.output, String.Format ("{0}.app/Contents/Resources/{0}.exe", opts.appname)));
} else {
File.Copy (opts.assembly, Path.Combine (opts.output, String.Format ("{0}.app/Contents/Resources/{0}", opts.appname)));
}
Stream s = Assembly.GetEntryAssembly ().GetManifestResourceStream ("LOADER");
BinaryReader reader = new BinaryReader (s);
byte[] data = reader.ReadBytes ((int)s.Length);
reader.Close ();
BinaryWriter writer = new BinaryWriter (File.Create (Path.Combine (opts.output, String.Format ("{0}.app/Contents/MacOS/{0}", opts.appname))));
string script = Encoding.ASCII.GetString (data);
switch (opts.mode) {
default:
case 0:
script = script.Replace ("%MWF_MODE%", "0");
script = script.Replace ("%COCOASHARP_MODE%", "0");
script = script.Replace ("%X11_MODE%", "0");
break;
case 1:
script = script.Replace ("%MWF_MODE%", "1");
script = script.Replace ("%COCOASHARP_MODE%", "0");
script = script.Replace ("%X11_MODE%", "0");
break;
case 2:
script = script.Replace ("%MWF_MODE%", "0");
script = script.Replace ("%COCOASHARP_MODE%", "1");
script = script.Replace ("%X11_MODE%", "0");
break;
case 3:
script = script.Replace ("%MWF_MODE%", "0");
script = script.Replace ("%COCOASHARP_MODE%", "0");
script = script.Replace ("%X11_MODE%", "1");
break;
}
data = Encoding.ASCII.GetBytes (script);
writer.Write (data, 0, data.Length);
writer.Close ();
try {
chmod (Path.Combine (opts.output,
String.Format ("{0}.app/Contents/MacOS/{0}", opts.appname)),
Convert.ToUInt32 ("755", 8));
} catch {
Console.WriteLine ("WARNING: It was not possible to set the executable permissions on\n" +
"the file {0}.app/Contents/MacOS/{0}, the bundle might not work", opts.appname);
}
s = Assembly.GetEntryAssembly ().GetManifestResourceStream ("PLIST");
reader = new BinaryReader (s);
data = reader.ReadBytes ((int)s.Length);
reader.Close ();
writer = new BinaryWriter (File.Create (Path.Combine (opts.output, String.Format ("{0}.app/Contents/Info.plist", opts.appname))));
string plist = Encoding.UTF8.GetString (data);
plist = plist.Replace ("%APPNAME%", opts.appname);
plist = plist.Replace ("%ICONFILE%", Path.GetFileName (opts.icon));
data = Encoding.UTF8.GetBytes (plist);
writer.Write (data, 0, data.Length);
writer.Close ();
return true;
}
public static void CopyDirectory (string src, string dest) {
string [] files;
if (dest [dest.Length-1] != Path.DirectorySeparatorChar) {
dest += Path.DirectorySeparatorChar;
}
if (!Directory.Exists (dest)) {
Directory.CreateDirectory (dest);
}
files = Directory.GetFileSystemEntries (src);
foreach (string file in files) {
if (Directory.Exists (file)) {
CopyDirectory (file, dest + Path.GetFileName (file));
} else {
File.Copy (file, dest + Path.GetFileName (file), true);
}
}
}
static void Usage ()
{
Console.WriteLine ("\n" +
"Usage is:\n" +
"macpack [options] assembly\n" +
" -n appname -appname:appname Application Name\n" +
" -o output -output:OUTPUT Output directory\n" +
" -a assembly Assembly to pack\n" +
" -i file -icon file Icon filename\n" +
" -r resource1,resource2 Additional files to bundle\n" +
" -m [winforms|cocoa|x11|console] The mode for the application");
}
static int Main (string [] args) {
PackOptions options = new PackOptions ();
ArrayList resources = new ArrayList ();
for (int i = 0; i < args.Length; i++){
string s = args [i];
string key, value;
if (s.Length > 2){
int p = s.IndexOf (':');
if (p != -1){
key = s.Substring (0, p);
value = s.Substring (p + 1);
} else {
key = s;
value = null;
}
} else {
key = s;
if (i+1 < args.Length)
value = args [i+1];
else
value = null;
}
switch (key){
case "-n": case "-appname":
options.appname = value;
break;
case "-o": case "-output":
options.output = value;
break;
case "-a": case "-assembly":
options.assembly = value;
break;
case "-i": case "-icon":
options.icon = value;
break;
case "-r": case "-resource":
foreach (string ss in value.Split (new char [] {','}))
resources.Add (ss);
break;
case "-about":
Console.WriteLine ("MacPack 1.0 by Geoff Norton\n");
break;
case "-m": case "-mode":
switch (value){
case "winforms":
options.mode = 1;
break;
case "x11":
options.mode = 3;
break;
case "console":
options.mode = 0;
break;
case "cocoa":
options.mode = 2;
break;
default:
try {
options.mode = Int32.Parse (value);
} catch {
Console.Error.WriteLine ("Could not recognize option {0} as the mode", value);
}
break;
}
break;
case "-h": case "-help":
Usage ();
break;
default:
options.assembly = key;
break;
}
}
options.resource = (string [])resources.ToArray (typeof (string));
Pack pack = new Pack (options);
if (pack.Generate ())
return 0;
return -1;
}
[DllImport ("libc")]
static extern int chmod (string path, uint mode);
}
}
|