spm-swift-package 0.13.2

Command Line Tools for macOS to create project in Swift Package Manager with desirable files.
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::core::spm_builder::*;
use crate::ui::theme_colors::*;
use crate::utils::xcode;
use iced::widget::{center_x, checkbox, column, text};
use iced::{Color, Element};

pub type AppElement<'a> = Element<'a, Message>;

#[derive(Debug, Clone, PartialEq)]
pub enum Platform {
	Ios,
	MacOs,
	TvOs,
	WatchOs,
	VisionOs,
}

impl Platform {
	fn as_str(&self) -> &'static str {
		match self {
			Platform::Ios => "iOS",
			Platform::MacOs => "macOS",
			Platform::TvOs => "tvOS",
			Platform::WatchOs => "watchOS",
			Platform::VisionOs => "visionOS",
		}
	}
}

#[derive(Debug, Clone, PartialEq)]
pub enum TestFramework {
	XCTest,
	SwiftTesting,
}

impl TestFramework {
	fn as_str(&self) -> &'static str {
		match self {
			TestFramework::XCTest => "XCTest",
			TestFramework::SwiftTesting => "Swift Testing",
		}
	}
}

#[derive(Debug, Clone)]
pub enum Message {
	ChangelogToggled(bool),
	ReadmeToggled(bool),
	SwiftPackageIndexToggled(bool),
	SwiftLintToggled(bool),
	IosToggled(bool),
	MacOsToggled(bool),
	TvOsToggled(bool),
	WatchOsToggled(bool),
	VisionOsToggled(bool),
	XCTestToggled(bool),
	SwiftTestingToggled(bool),
	InputChanged(String),
	GenerateSPM,
}

#[derive(Default)]
pub struct SpmView {
	changelog: bool,
	readme: bool,
	swift_package_index: bool,
	swiftlint: bool,
	selected_platform: Option<Platform>,
	selected_framework: Option<TestFramework>,
	input_content: String,
	platform_error: bool,
	test_framework_error: bool,
}

/// Entry point to launch the iced application for the SPM GUI
pub fn run() -> iced::Result {
	iced::application(SpmView::default, SpmView::update, SpmView::view).run()
}

impl SpmView {
	/// Handles state updates for each message received from the UI
	pub fn update(&mut self, message: Message) {
		match message {
			Message::ChangelogToggled(val) => self.changelog = val,
			Message::ReadmeToggled(val) => self.readme = val,
			Message::SwiftPackageIndexToggled(val) => self.swift_package_index = val,
			Message::SwiftLintToggled(val) => self.swiftlint = val,

			Message::IosToggled(val) => self.select_platform(val, Platform::Ios),
			Message::MacOsToggled(val) => self.select_platform(val, Platform::MacOs),
			Message::TvOsToggled(val) => self.select_platform(val, Platform::TvOs),
			Message::WatchOsToggled(val) => self.select_platform(val, Platform::WatchOs),
			Message::VisionOsToggled(val) => self.select_platform(val, Platform::VisionOs),

			Message::XCTestToggled(val) => self.select_framework(val, TestFramework::XCTest),
			Message::SwiftTestingToggled(val) => self.select_framework(val, TestFramework::SwiftTesting),

			Message::InputChanged(s) => self.input_content = s,
			Message::GenerateSPM => self.generate(),
		}
	}

	/// Selects the given platform exclusively (radio-button behavior) or deselects if val is false
	fn select_platform(&mut self, val: bool, platform: Platform) {
		if val {
			self.selected_platform = Some(platform);
			self.platform_error = false;
		} else if self.selected_platform.as_ref() == Some(&platform) {
			self.selected_platform = None;
		}
	}

	/// Selects the given test framework exclusively (radio-button behavior) or deselects if val is false
	fn select_framework(&mut self, val: bool, framework: TestFramework) {
		if val {
			self.selected_framework = Some(framework);
			self.test_framework_error = false;
		} else if self.selected_framework.as_ref() == Some(&framework) {
			self.selected_framework = None;
		}
	}

	/// Validates selections, builds the project, and opens it in Xcode
	fn generate(&mut self) {
		let platform = match &self.selected_platform {
			Some(p) => p.as_str(),
			None => {
				self.platform_error = true;
				return;
			}
		};

		self.platform_error = false;

		let test_framework = match &self.selected_framework {
			Some(f) => f.as_str(),
			None => {
				self.test_framework_error = true;
				return;
			}
		};

		self.test_framework_error = false;

		let mut project_name = self.input_content.clone();

		if project_name.trim().is_empty() {
			project_name = "Library".to_string();
		}

		let mut files = Vec::new();

		if self.changelog {
			files.push("Changelog");
		}

		if self.readme {
			files.push("Readme");
		}

		if self.swift_package_index {
			files.push("Swift Package Index");
		}

		if self.swiftlint {
			files.push("SwiftLint");
		}

		tokio::spawn(async move {
			if SpmBuilder::create(&project_name, &files, &[platform], test_framework).is_ok() {
				let name = project_name;
				std::mem::drop(tokio::task::spawn_blocking(move || {
					xcode::open_xcode(&name)
				}));
			}
		});
	}

	/// Builds and returns the main application view layout
	pub fn view(&self) -> AppElement<'_> {
		column![
			self.header_view(),
			self.input_view(),
			self.files_title_view(),
			self.files_checkboxes_view(),
			self.platforms_title_view(),
			self.platforms_checkboxes_view(),
			self.error_platform_view(),
			self.test_framework_title_view(),
			self.test_framework_checkboxes_view(),
			self.error_test_framework_view(),
			self.generate_button_view(),
		]
		.spacing(16)
		.into()
	}

	/// Returns the header/title section view
	fn header_view(&self) -> AppElement<'_> {
		column![
			center_x(
				text("SPM Swift Package")
					.color(ThemeColors::ORANGE)
					.size(32)
			)
			.height(20),
			center_x(
				text(
					"Command Line Tools for macOS to create Swift Package Manager projects with desirable files."
				)
				.color(ThemeColors::GRAY)
				.size(14)
			)
			.padding(16),
		]
		.spacing(16)
		.into()
	}

	/// Returns the text input field view for the project name
	fn input_view(&self) -> AppElement<'_> {
		iced::widget::Container::new(
			iced::widget::text_input("Insert the name of the project", &self.input_content)
				.on_input(Message::InputChanged)
				.padding(8)
				.size(18),
		)
		.padding([0, 24])
		.into()
	}

	/// Returns the view for the file selection section's title
	fn files_title_view(&self) -> AppElement<'_> {
		let title = text("Choose the files you want to include in your project:")
			.color(Color::WHITE)
			.size(18);

		iced::widget::Container::new(title).padding([0, 24]).into()
	}

	/// Returns the view for the platform selection section's title
	fn platforms_title_view(&self) -> AppElement<'_> {
		let title = text("Choose the platforms you want to include in your project:")
			.color(Color::WHITE)
			.size(18);

		iced::widget::Container::new(title).padding([0, 24]).into()
	}

	/// Returns the view for the test framework selection section's title
	fn test_framework_title_view(&self) -> AppElement<'_> {
		let title = text("Choose the test framework:")
			.color(Color::WHITE)
			.size(18);

		iced::widget::Container::new(title).padding([0, 24]).into()
	}

	/// Returns the view containing all file checkboxes
	fn files_checkboxes_view(&self) -> AppElement<'_> {
		let mut col = column![];

		for checkbox in self.view_files_checkboxes() {
			col = col.push(checkbox);
		}

		col.spacing(8).into()
	}

	/// Returns the view containing all platform checkboxes
	fn platforms_checkboxes_view(&self) -> AppElement<'_> {
		let mut col = column![];

		for checkbox in self.view_platforms_checkboxes() {
			col = col.push(checkbox);
		}

		col.spacing(8).into()
	}

	/// Returns the view containing all test framework checkboxes
	fn test_framework_checkboxes_view(&self) -> AppElement<'_> {
		let mut col = column![];

		for checkbox in self.view_test_framework_checkboxes() {
			col = col.push(checkbox);
		}

		col.spacing(8).into()
	}

	/// Returns the view for the 'Generate SPM' button
	fn generate_button_view(&self) -> AppElement<'_> {
		iced::widget::Container::new(
			iced::widget::button(text("Generate SPM").size(16).color(Color::WHITE))
				.on_press(Message::GenerateSPM),
		)
		.padding([8, 24])
		.into()
	}

	/// Returns the view for the error message when no platform is selected
	fn error_platform_view(&self) -> AppElement<'_> {
		if self.platform_error {
			iced::widget::Container::new(
				text("Please select at least one platform.")
					.color([1.0, 0.2, 0.2])
					.size(16),
			)
			.padding([0, 24])
			.into()
		} else {
			iced::widget::Container::new("").into()
		}
	}

	/// Returns the view for the error message when no test framework is selected
	fn error_test_framework_view(&self) -> AppElement<'_> {
		if self.test_framework_error {
			iced::widget::Container::new(
				text("Please select a test framework.")
					.color([1.0, 0.2, 0.2])
					.size(16),
			)
			.padding([0, 24])
			.into()
		} else {
			iced::widget::Container::new("").into()
		}
	}

	/// Builds a vector of checkbox rows for each selectable file
	fn view_files_checkboxes(&self) -> Vec<AppElement<'_>> {
		vec![
			self.checkbox_row("Changelog", self.changelog, Message::ChangelogToggled),
			self.checkbox_row("Readme", self.readme, Message::ReadmeToggled),
			self.checkbox_row(
				"Swift Package Index",
				self.swift_package_index,
				Message::SwiftPackageIndexToggled,
			),
			self.checkbox_row("SwiftLint", self.swiftlint, Message::SwiftLintToggled),
		]
	}

	/// Builds a vector of checkbox rows for each selectable platform
	fn view_platforms_checkboxes(&self) -> Vec<AppElement<'_>> {
		vec![
			self.checkbox_row(
				"iOS",
				self.selected_platform == Some(Platform::Ios),
				Message::IosToggled,
			),
			self.checkbox_row(
				"macOS",
				self.selected_platform == Some(Platform::MacOs),
				Message::MacOsToggled,
			),
			self.checkbox_row(
				"tvOS",
				self.selected_platform == Some(Platform::TvOs),
				Message::TvOsToggled,
			),
			self.checkbox_row(
				"watchOS",
				self.selected_platform == Some(Platform::WatchOs),
				Message::WatchOsToggled,
			),
			self.checkbox_row(
				"visionOS",
				self.selected_platform == Some(Platform::VisionOs),
				Message::VisionOsToggled,
			),
		]
	}

	/// Builds a vector of checkbox rows for each test framework
	fn view_test_framework_checkboxes(&self) -> Vec<AppElement<'_>> {
		vec![
			self.checkbox_row(
				"XCTest",
				self.selected_framework == Some(TestFramework::XCTest),
				Message::XCTestToggled,
			),
			self.checkbox_row(
				"Swift Testing",
				self.selected_framework == Some(TestFramework::SwiftTesting),
				Message::SwiftTestingToggled,
			),
		]
	}

	/// Helper to create a row with a single checkbox and label
	fn checkbox_row<'a>(
		&self,
		label: &'a str,
		value: bool,
		on_toggle: fn(bool) -> Message,
	) -> AppElement<'a> {
		iced::widget::Container::new(checkbox(value).label(label).on_toggle(on_toggle))
			.padding([0, 24])
			.into()
	}
}