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
// wengwengweng

use serde::Deserialize;
use serde::Serialize;
use std::path::Path;
use std::fs::File;
use clap::ErrorKind;
use crate::utils;

enum AppDir {
	Resources,
	Frameworks,
	MacOS,
}

#[derive(Serialize, Deserialize, Debug)]
struct PlistData {

	CFBundleName: String,
	CFBundleDisplayName: String,
	CFBundleIdentifier: String,
	CFBundleVersion: String,
	CFBundlePackageType: String,
	CFBundleExecutable: String,
	CFBundleIconFile: String,
	NSHighResolutionCapable: bool,

}

pub struct Bundle {

	path: String,
	data: PlistData,
	bin: Option<String>,
	icon: Option<String>,
	frameworks: Vec<String>,
	resources: Vec<String>,

}

impl Bundle {

	pub fn new(path: &str) -> Self {

		let data = PlistData {
			CFBundleName: "".to_owned(),
			CFBundleDisplayName: "".to_owned(),
			CFBundleIdentifier: "".to_owned(),
			CFBundleVersion: "".to_owned(),
			CFBundlePackageType: "APPL".to_owned(),
			CFBundleExecutable: "".to_owned(),
			CFBundleIconFile: "".to_owned(),
			NSHighResolutionCapable: true,
		};

		let bundle = Self {
			path: path.to_owned(),
			data: data,
			bin: None,
			icon: None,
			frameworks: vec![],
			resources: vec![],
		};

		return bundle;

	}

	pub fn set_bin(&mut self, bin: &str) -> &Self {

		utils::assert_exist(bin);
		self.data.CFBundleExecutable = utils::basename(bin);
		self.bin = Some(bin.to_owned());

		return self;

	}

	pub fn set_name(&mut self, name: &str) -> &Self {

		self.data.CFBundleName = String::from(name);

		return self;

	}

	pub fn set_display_name(&mut self, name: &str) -> &Self {

		self.data.CFBundleDisplayName = String::from(name);

		return self;

	}

	pub fn set_identifier(&mut self, ident: &str) -> &Self {

		self.data.CFBundleIdentifier = String::from(ident);

		return self;

	}

	pub fn set_version(&mut self, version: &str) -> &Self {

		self.data.CFBundleVersion = String::from(version);

		return self;

	}

	pub fn set_icon(&mut self, icon: &str) -> &Self {

		utils::assert_exist(icon);
		utils::assert_ext(icon, "icns");
		self.data.CFBundleIconFile = utils::basename(icon);
		self.icon = Some(icon.to_owned());

		return self;

	}

	pub fn add_res(&mut self, file: &str) -> &Self {

		utils::assert_exist(file);
		self.resources.push(file.to_owned());

		return self;

	}

	pub fn add_frameworks(&mut self, file: &str) -> &Self {

		utils::assert_exist(file);
		self.frameworks.push(file.to_owned());

		return self;

	}

	pub fn write(&self) {

		utils::mkdir(&self.path);
		utils::mkdir(&format!("{}/Contents", self.path));

		if let Some(bin) = &self.bin {
			self.copy(bin, AppDir::MacOS, "");
		}

		if let Some(icon) = &self.icon {
			self.copy(icon, AppDir::Resources, "");
		}

		self.write_plist();

	}

	fn copy(&self, file: &str, des: AppDir, subdir: &str) -> &Self {

		let dir;

		match des {
			AppDir::MacOS => dir = "MacOS",
			AppDir::Resources => dir = "Resources",
			AppDir::Frameworks => dir = "Frameworks",
		}

		let path = &format!("{}/Contents/{}", self.path, dir);

		if !utils::exists(path) {
			utils::mkdir(path);
		}

		utils::copy(&file, &format!("{}/{}", path, utils::basename(file)));

		return self;

	}

	fn write_plist(&self) -> &Self {

		let file = File::create(format!("{}/Contents/Info.plist", self.path)).unwrap();

		if plist::serde::serialize_to_xml(&file, &self.data).is_err() {
			utils::fail("failed to write plist", ErrorKind::Io);
		}

		return self;

	}

}