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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use std::collections::BTreeMap;
use std::fs;
use std::path;
use serde::{Deserialize, Serialize};
use crate::build_system::FlatpakBuildSystem;
use crate::format::FlatpakManifestFormat;
use crate::source::{FlatpakSourceItem, ARCHIVE, GIT, PATCH};
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Debug)]
#[derive(Hash)]
#[serde(untagged)]
pub enum FlatpakModuleItem {
Path(String),
Description(FlatpakModule),
}
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Debug)]
#[derive(Default)]
#[derive(Hash)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct FlatpakModule {
#[serde(skip_serializing)]
pub format: FlatpakManifestFormat,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub disabled: Option<bool>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub sources: Vec<FlatpakSourceItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub config_opts: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub make_args: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub make_install_args: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rm_configure: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_autogen: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_parallel_make: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub install_rule: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_make_install: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_python_timestamp_fix: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cmake: Option<bool>,
#[serde(deserialize_with = "crate::build_system::deserialize_from_string")]
#[serde(serialize_with = "crate::build_system::serialize_to_string")]
pub buildsystem: Option<FlatpakBuildSystem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub builddir: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub subdir: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub build_options: Option<FlatpakBuildOptions>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub build_commands: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub post_install: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub cleanup: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub ensure_writable: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub only_arches: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub skip_arches: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub cleanup_platform: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub run_tests: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub test_rule: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub test_commands: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub modules: Vec<FlatpakModuleItem>,
}
impl FlatpakModule {
pub fn uses_external_data_checker(&self) -> bool {
for source in &self.sources {
let source_description = match source {
FlatpakSourceItem::Description(d) => d,
FlatpakSourceItem::Path(_) => continue,
};
if source_description.x_checker_data.is_some() {
return true;
}
}
return false;
}
pub fn get_all_mirror_urls(&self) -> Vec<String> {
let mut all_urls = vec![];
for module in &self.modules {
if let FlatpakModuleItem::Description(module_description) = module {
all_urls.append(&mut module_description.get_all_mirror_urls());
}
}
for source in &self.sources {
for url in source.get_all_mirror_urls() {
all_urls.push(url.to_string());
}
}
all_urls
}
pub fn get_buildsystem(&self) -> Option<String> {
if self.cmake.unwrap_or(false) {
return Some(crate::build_system::CMAKE.to_string());
}
if let Some(buildsystem) = &self.buildsystem {
return Some(buildsystem.to_string());
}
None
}
pub fn is_patched(&self) -> bool {
for source in &self.sources {
if let FlatpakSourceItem::Description(sd) = source {
if let Some(t) = &sd.r#type {
if t == PATCH {
return true;
}
}
}
}
false
}
pub fn load_from_file(path: String) -> Result<FlatpakModule, String> {
let file_path = path::Path::new(&path);
if !file_path.is_file() {
return Err(format!("{} is not a file.", path));
}
let manifest_format = match FlatpakManifestFormat::from_path(&path) {
Some(f) => f,
None => return Err(format!("{} is not a Flatpak module manifest.", path)),
};
let manifest_content = match fs::read_to_string(file_path) {
Ok(content) => content,
Err(e) => {
return Err(format!("Could not read module manifest at {}: {}", &path, e));
}
};
match FlatpakModule::parse(manifest_format, &manifest_content) {
Ok(m) => Ok(m),
Err(e) => Err(format!(
"Failed to parse Flatpak module manifest at {}: {}",
path, e
)),
}
}
pub fn parse(format: FlatpakManifestFormat, manifest_content: &str) -> Result<FlatpakModule, String> {
let mut flatpak_module: FlatpakModule = match &format {
FlatpakManifestFormat::YAML => match serde_yaml::from_str(&manifest_content) {
Ok(m) => m,
Err(e) => {
return Err(format!("Failed to parse the Flatpak module manifest: {}.", e));
}
},
FlatpakManifestFormat::JSON => {
let json_content_without_comments = crate::utils::remove_comments_from_json(manifest_content);
match serde_json::from_str(&json_content_without_comments) {
Ok(m) => m,
Err(e) => {
return Err(format!("Failed to parse the Flatpak module manifest: {}.", e));
}
}
}
};
flatpak_module.format = format;
if flatpak_module.name.is_empty() {
return Err("Required top-level field name is missing from Flatpak module.".to_string());
}
if flatpak_module.sources.is_empty() {
return Err("Required sources were not found in Flatpak module.".to_string());
}
for source in &flatpak_module.sources {
let source_path = match source {
FlatpakSourceItem::Description(_) => continue,
FlatpakSourceItem::Path(p) => p,
};
if source_path.starts_with("http://") || source_path.starts_with("https://") {
return Err("Sources provided as strings cannot be URLs!".to_string());
}
}
Ok(flatpak_module)
}
pub fn dump(&self) -> Result<String, String> {
return match serde_yaml::to_string(&self) {
Ok(d) => Ok(d),
Err(e) => Err(format!("Failed to dump the Flatpak manifest: {}.", e)),
};
}
pub fn file_path_matches(path: &str) -> bool {
crate::filename::extension_is_valid(path)
}
pub fn get_all_urls(&self) -> Vec<String> {
let mut all_urls = vec![];
for module in &self.modules {
if let FlatpakModuleItem::Description(module_description) = module {
all_urls.append(&mut module_description.get_all_urls());
}
}
for source in &self.sources {
for url in source.get_all_urls() {
all_urls.push(url);
}
}
all_urls
}
pub fn get_all_archive_urls(&self) -> Vec<String> {
let mut all_archive_urls = vec![];
for source in &self.sources {
if source.get_type_name() != ARCHIVE {
continue;
}
let source_description = match &source {
FlatpakSourceItem::Path(_) => continue,
FlatpakSourceItem::Description(d) => d,
};
let archive_url = match &source_description.url {
Some(u) => u,
None => continue,
};
all_archive_urls.push(archive_url.to_string());
}
all_archive_urls
}
pub fn get_all_git_urls(&self) -> Vec<String> {
let mut all_git_urls = vec![];
for source in &self.sources {
if source.get_type_name() != GIT {
continue;
}
let source_description = match &source {
FlatpakSourceItem::Path(_) => continue,
FlatpakSourceItem::Description(d) => d,
};
let git_url = match &source_description.url {
Some(u) => u,
None => continue,
};
all_git_urls.push(git_url.to_string());
}
all_git_urls
}
pub fn get_max_depth(&self) -> i32 {
let mut max_depth: i32 = 0;
for module in &self.modules {
if let FlatpakModuleItem::Description(module_description) = module {
let module_depth = module_description.get_max_depth();
if module_depth > max_depth {
max_depth = module_depth;
}
}
}
return max_depth + 1;
}
pub fn get_main_url(&self) -> Option<String> {
if self.sources.len() < 1 {
return None;
}
let main_module_source = self.sources.first().unwrap();
let main_module_source_url: Option<String> = main_module_source.get_url();
match &main_module_source_url {
Some(s) => Some(s.to_string()),
None => None,
}
}
pub fn get_all_modules_recursively(&self) -> Vec<&FlatpakModuleItem> {
let mut all_modules: Vec<&FlatpakModuleItem> = vec![];
let mut next_modules: Vec<&FlatpakModuleItem> = vec![];
for module in &self.modules {
next_modules.push(module);
}
while !next_modules.is_empty() {
let module = next_modules.pop().unwrap();
all_modules.push(module);
let module = match module {
FlatpakModuleItem::Description(d) => d,
FlatpakModuleItem::Path(_) => continue,
};
for next_module in &module.modules {
next_modules.push(next_module);
}
}
all_modules
}
pub fn is_composite(&self) -> bool {
let mut code_sources_count = 0;
for source in &self.sources {
let source_type_name = source.get_type_name();
if crate::source::CODE_TYPES.contains(&source_type_name) {
code_sources_count += 1;
}
if code_sources_count > 1 {
return true;
}
}
false
}
}
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Debug)]
#[derive(Default)]
#[derive(Hash)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct FlatpakBuildOptions {
#[serde(skip_serializing_if = "String::is_empty")]
pub cflags: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub cflags_override: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub cppflags: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub cppflags_override: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub cxxflags: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub cxxflags_override: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub ldflags: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub ldflags_override: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub prefix: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub libdir: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub append_path: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub prepend_path: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub append_ld_library_path: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub prepend_ld_library_path: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub append_pkg_config_path: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub prepend_pkg_config_path: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub build_args: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub test_args: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub config_opts: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub make_args: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub make_install_args: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strip: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_debuginfo: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_debuginfo_compression: Option<bool>,
pub arch: BTreeMap<String, FlatpakBuildOptions>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_parse_extra_data() {
let module_manifest = r###"
name: wps
buildsystem: simple
build-commands:
- install -Dm755 apply_extra.sh /app/bin/apply_extra
- install -Dm755 wps.sh /app/bin/wps
- ln -s wps /app/bin/et
- ln -s wps /app/bin/wpp
- ln -s wps /app/bin/wpspdf
- install -Dm644 ${FLATPAK_ID}.metainfo.xml -t /app/share/metainfo/
- install -Dm755 /usr/bin/desktop-file-edit -t /app/bin/
- install -Dm755 /usr/bin/ar -t /app/bin/
- install -Dm755 /usr/lib/$(gcc -print-multiarch)/libbfd-*.so -t /app/lib/
sources:
- type: file
path: apply_extra.sh
- type: file
path: com.wps.Office.metainfo.xml
- type: file
path: wps.sh
- type: extra-data
filename: wps-office.deb
only-arches:
- x86_64
url: https://wdl1.pcfg.cache.wpscdn.com/wps-office_11.1.0.10702.XA_amd64.deb
sha256: 390a8b358aaccdfda54740d10d5306c2543c5cd42a7a8fd5c776ccff38492992
size: 275210770
installed-size: 988671247
x-checker-data:
type: html
url: https://linux.wps.com/js/meta.js
version-pattern: version\s*=\s*"([\d.-]+)"
url-pattern: download_link_deb\s*=\s*"(http[s]?://[\w\d$-_@.&+]+)"
"###;
let module = FlatpakModule::parse(FlatpakManifestFormat::YAML, module_manifest).unwrap();
assert_eq!(module.name, "wps");
}
#[test]
pub fn test_parse_helm_files() {
let helm_file = r###"
name: wps
sources:
- "https://github.com/user/repo"
"###;
assert!(FlatpakModule::parse(FlatpakManifestFormat::YAML, helm_file,).is_err())
}
#[test]
pub fn test_parse_no_buildsystem() {
let module_manifest = r###"
name: dbus-glib
sources:
- type: archive
url: "https://dbus.freedesktop.org/releases/dbus-glib/dbus-glib-0.110.tar.gz"
sha256: 7ce4760cf66c69148f6bd6c92feaabb8812dee30846b24cd0f7395c436d7e825
config-opts:
- "--disable-static"
- "--disable-gtk-doc"
cleanup:
- "*.la"
- /bin
- /etc
- /include
- /libexec
- /share/gtk-doc
- /share/man
"###;
let flatpak_application = FlatpakModule::parse(FlatpakManifestFormat::YAML, module_manifest).unwrap();
assert!(flatpak_application.buildsystem.is_none())
}
}