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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
|
//! ref: composer/src/Composer/Package/Version/VersionGuesser.php
use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::preg::{CaptureKey, Preg};
use shirabe_external_packages::symfony::component::process::process::Process;
use shirabe_php_shim::{
PHP_INT_MAX, PhpMixed, RuntimeException, array_keys, array_map, array_merge, empty,
function_exists, implode, is_string, json_encode, preg_quote, str_replace, strlen,
strnatcasecmp, strpos, substr, trim, usort,
};
use shirabe_semver::version_parser::VersionParser as SemverVersionParser;
use crate::config::Config;
use crate::io::io_interface::IOInterface;
use crate::io::null_io::NullIO;
use crate::package::version::version_parser::VersionParser;
use crate::repository::vcs::hg_driver::HgDriver;
use crate::util::git::Git as GitUtil;
use crate::util::http_downloader::HttpDownloader;
use crate::util::platform::Platform;
use crate::util::process_executor::ProcessExecutor;
use crate::util::svn::Svn as SvnUtil;
/// Try to guess the current version number based on different VCS configuration.
///
/// @phpstan-type Version array{version: string, commit: string|null, pretty_version: string|null}|array{version: string, commit: string|null, pretty_version: string|null, feature_version: string|null, feature_pretty_version: string|null}
#[derive(Debug)]
pub struct VersionGuesser {
/// @var Config
config: std::rc::Rc<std::cell::RefCell<Config>>,
/// @var ProcessExecutor
process: std::rc::Rc<std::cell::RefCell<ProcessExecutor>>,
/// @var SemverVersionParser
version_parser: VersionParser,
/// @var IOInterface|null
io: Option<Box<dyn IOInterface>>,
}
/// PHP: @phpstan-type Version array{version, commit, pretty_version, feature_version?, feature_pretty_version?}
#[derive(Debug, Clone)]
pub struct VersionData {
pub version: Option<String>,
pub commit: Option<String>,
pub pretty_version: Option<String>,
pub feature_version: Option<String>,
pub feature_pretty_version: Option<String>,
}
impl VersionGuesser {
pub fn new(
config: std::rc::Rc<std::cell::RefCell<Config>>,
process: std::rc::Rc<std::cell::RefCell<ProcessExecutor>>,
version_parser: VersionParser,
io: Option<Box<dyn IOInterface>>,
) -> Self {
Self {
config,
process,
version_parser,
io,
}
}
/// @param array<string, mixed> $packageConfig
/// @param string $path Path to guess into
///
/// @phpstan-return Version|null
pub fn guess_version(
&mut self,
package_config: &IndexMap<String, PhpMixed>,
path: &str,
) -> Result<Option<VersionData>> {
if !function_exists("proc_open") {
return Ok(None);
}
// bypass version guessing in bash completions as it takes time to create
// new processes and the root version is usually not that important
if Platform::is_input_completion_process() {
return Ok(None);
}
let version_data = self.guess_git_version(package_config, path)?;
if version_data.version.is_some() {
return Ok(Some(self.postprocess(version_data)));
}
let version_data = self.guess_hg_version(package_config, path)?;
if let Some(vd) = version_data {
if vd.version.is_some() {
return Ok(Some(self.postprocess(vd)));
}
}
let version_data = self.guess_fossil_version(path)?;
if version_data.version.is_some() {
return Ok(Some(self.postprocess(version_data)));
}
let version_data = self.guess_svn_version(package_config, path)?;
if let Some(vd) = version_data {
if vd.version.is_some() {
return Ok(Some(self.postprocess(vd)));
}
}
Ok(None)
}
/// @phpstan-param Version $versionData
///
/// @phpstan-return Version
fn postprocess(&self, mut version_data: VersionData) -> VersionData {
// PHP: !empty($versionData['feature_version']) && $versionData['feature_version'] === $versionData['version'] && $versionData['feature_pretty_version'] === $versionData['pretty_version']
let feature_matches = version_data
.feature_version
.as_ref()
.map(|fv| !fv.is_empty())
.unwrap_or(false)
&& version_data.feature_version == version_data.version
&& version_data.feature_pretty_version == version_data.pretty_version;
if feature_matches {
version_data.feature_version = None;
version_data.feature_pretty_version = None;
}
if "-dev" == substr(version_data.version.as_deref().unwrap_or(""), -4, None)
&& Preg::is_match(r"{\.9{7}}", version_data.version.as_deref().unwrap_or(""))
.unwrap_or(false)
{
version_data.pretty_version = Some(
Preg::replace(
r"{(\.9{7})+}",
".x",
version_data.version.as_deref().unwrap_or(""),
)
.unwrap_or_default(),
);
}
let feature_non_empty = version_data
.feature_version
.as_ref()
.map(|fv| !fv.is_empty())
.unwrap_or(false);
if feature_non_empty
&& "-dev"
== substr(
version_data.feature_version.as_deref().unwrap_or(""),
-4,
None,
)
&& Preg::is_match(
r"{\.9{7}}",
version_data.feature_version.as_deref().unwrap_or(""),
)
.unwrap_or(false)
{
version_data.feature_pretty_version = Some(
Preg::replace(
r"{(\.9{7})+}",
".x",
version_data.feature_version.as_deref().unwrap_or(""),
)
.unwrap_or_default(),
);
}
version_data
}
/// @param array<string, mixed> $packageConfig
///
/// @return array{version: string|null, commit: string|null, pretty_version: string|null, feature_version?: string|null, feature_pretty_version?: string|null}
fn guess_git_version(
&mut self,
package_config: &IndexMap<String, PhpMixed>,
path: &str,
) -> Result<VersionData> {
GitUtil::clean_env(&mut self.process);
let mut commit: Option<String> = None;
let mut version: Option<String> = None;
let mut pretty_version: Option<String> = None;
let mut feature_version: Option<String> = None;
let mut feature_pretty_version: Option<String> = None;
let mut is_detached = false;
// try to fetch current version from git branch
let mut output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&[
"git".to_string(),
"branch".to_string(),
"-a".to_string(),
"--no-color".to_string(),
"--no-abbrev".to_string(),
"-v".to_string(),
],
&mut output,
Some(path.to_string()),
) {
let mut branches: Vec<String> = vec![];
let mut is_feature_branch = false;
// find current branch and collect all branch names
for branch in self.process.borrow().split_lines(&output) {
if !branch.is_empty() {
let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::is_match_strict_groups3(
r"{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}",
&branch,
Some(&mut m),
)
.unwrap_or(false)
{
let g1 = m
.get(&CaptureKey::ByIndex(1))
.cloned()
.unwrap_or_default();
let g2 = m
.get(&CaptureKey::ByIndex(2))
.cloned()
.unwrap_or_default();
if g1 == "(no branch)"
|| strpos(&g1, "(detached ") == Some(0)
|| strpos(&g1, "(HEAD detached at") == Some(0)
{
version = Some(format!("dev-{}", g2));
pretty_version = version.clone();
is_feature_branch = true;
is_detached = true;
} else {
version = Some(self.version_parser.normalize_branch(&g1)?);
pretty_version = Some(format!("dev-{}", g1));
is_feature_branch = self.is_feature_branch(package_config, Some(&g1));
}
commit = Some(g2);
}
}
if !branch.is_empty() && {
let mut tmp: IndexMap<CaptureKey, String> = IndexMap::new();
!Preg::is_match_strict_groups3(r"{^ *.+/HEAD }", &branch, Some(&mut tmp))
.unwrap_or(false)
} {
let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::is_match_strict_groups3(
r"{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}",
&branch,
Some(&mut m),
)
.unwrap_or(false)
{
branches.push(m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default());
}
}
}
if is_feature_branch {
feature_version = version.clone();
feature_pretty_version = pretty_version.clone();
// try to find the best (nearest) version branch to assume this feature's version
let result = self.guess_feature_version(
package_config,
version.clone(),
branches,
vec![
"git".to_string(),
"rev-list".to_string(),
"%candidate%..%branch%".to_string(),
],
path,
)?;
version = result.version;
pretty_version = result.pretty_version;
}
}
GitUtil::check_for_repo_ownership_error(
&self.process.borrow().get_error_output(),
path,
self.io.as_deref(),
);
if version.is_none() || is_detached {
let result = self.version_from_git_tags(path)?;
if let Some(r) = result {
version = Some(r.0);
pretty_version = Some(r.1);
feature_version = None;
feature_pretty_version = None;
}
}
if commit.is_none() {
let command = GitUtil::build_rev_list_command(
&self.process,
array_merge(
PhpMixed::List(vec![
Box::new(PhpMixed::String("--format=%H".to_string())),
Box::new(PhpMixed::String("-n1".to_string())),
Box::new(PhpMixed::String("HEAD".to_string())),
]),
PhpMixed::List(
GitUtil::get_no_show_signature_flags(&self.process)
.into_iter()
.map(|s| Box::new(PhpMixed::String(s)))
.collect(),
),
)
.as_list()
.map(|l| {
l.iter()
.filter_map(|v| v.as_string().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default(),
);
let mut command_output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&command,
&mut command_output,
Some(path.to_string()),
) {
let parsed = trim(
&GitUtil::parse_rev_list_output(&command_output, &self.process),
None,
);
commit = if parsed.is_empty() {
None
} else {
Some(parsed)
};
}
}
Ok(VersionData {
version,
commit,
pretty_version,
feature_version,
feature_pretty_version,
})
}
/// @return array{version: string, pretty_version: string}|null
fn version_from_git_tags(&mut self, path: &str) -> Result<Option<(String, String)>> {
// try to fetch current version from git tags
let mut output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&[
"git".to_string(),
"describe".to_string(),
"--exact-match".to_string(),
"--tags".to_string(),
],
&mut output,
Some(path.to_string()),
) {
// TODO(phase-b): use anyhow::Result<Result<T, E>> to model PHP try/catch
match self.version_parser.normalize(&trim(&output, None), None) {
Ok(version) => return Ok(Some((version, trim(&output, None)))),
Err(_e) => {}
}
}
Ok(None)
}
/// @param array<string, mixed> $packageConfig
///
/// @return array{version: string|null, commit: ''|null, pretty_version: string|null, feature_version?: string|null, feature_pretty_version?: string|null}|null
fn guess_hg_version(
&mut self,
package_config: &IndexMap<String, PhpMixed>,
path: &str,
) -> Result<Option<VersionData>> {
// try to fetch current version from hg branch
let mut output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&["hg".to_string(), "branch".to_string()],
&mut output,
Some(path.to_string()),
) {
let branch = trim(&output, None);
let version = self.version_parser.normalize_branch(&branch)?;
let is_feature_branch = strpos(&version, "dev-") == Some(0);
if VersionParser::DEFAULT_BRANCH_ALIAS == version {
return Ok(Some(VersionData {
version: Some(version.clone()),
commit: None,
pretty_version: Some(format!("dev-{}", branch)),
feature_version: None,
feature_pretty_version: None,
}));
}
if !is_feature_branch {
return Ok(Some(VersionData {
version: Some(version.clone()),
commit: None,
pretty_version: Some(version),
feature_version: None,
feature_pretty_version: None,
}));
}
// re-use the HgDriver to fetch branches (this properly includes bookmarks)
let _io = NullIO::new();
let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new();
repo_config.insert("url".to_string(), PhpMixed::String(path.to_string()));
// TODO(phase-b): HgDriver lacks a `new` constructor and HttpDownloader::new signature is unknown
let mut driver: HgDriver = todo!(
"HgDriver::new(repo_config, Box::new(io), self.config.clone(), HttpDownloader::new(io, config), Rc::clone(&self.process))"
);
let branches: Vec<String> =
array_map(|k: &String| k.clone(), &array_keys(&driver.get_branches()?));
// try to find the best (nearest) version branch to assume this feature's version
let mut result = self.guess_feature_version(
package_config,
Some(version.clone()),
branches,
vec![
"hg".to_string(),
"log".to_string(),
"-r".to_string(),
"not ancestors('%candidate%') and ancestors('%branch%')".to_string(),
"--template".to_string(),
"\"{node}\\n\"".to_string(),
],
path,
)?;
// PHP: $result['commit'] = '';
// TODO(phase-b): VersionData::commit modeled as Option<String>; using Some(String::new())
let commit = Some(String::new());
let feature_version = Some(version.clone());
let feature_pretty_version = Some(version);
return Ok(Some(VersionData {
version: result.version,
commit,
pretty_version: result.pretty_version,
feature_version,
feature_pretty_version,
}));
}
Ok(None)
}
/// @param array<string, mixed> $packageConfig
/// @param list<string> $branches
/// @param list<string> $scmCmdline
///
/// @return array{version: string|null, pretty_version: string|null}
fn guess_feature_version(
&mut self,
package_config: &IndexMap<String, PhpMixed>,
version: Option<String>,
mut branches: Vec<String>,
scm_cmdline: Vec<String>,
path: &str,
) -> Result<FeatureVersionResult> {
let mut pretty_version: Option<String> = version.clone();
let mut version = version;
// ignore feature branches if they have no branch-alias or self.version is used
// and find the branch they came from to use as a version instead
let has_branch_alias = package_config
.get("extra")
.and_then(|v| v.as_array())
.and_then(|m| m.get("branch-alias"))
.and_then(|v| v.as_array())
.map(|m| m.contains_key(version.as_deref().unwrap_or("")))
.unwrap_or(false);
let has_self_version = strpos(
&json_encode(&PhpMixed::Array(
package_config
.iter()
.map(|(k, v)| (k.clone(), Box::new(v.clone())))
.collect(),
))
.unwrap_or_default(),
"\"self.version\"",
)
.is_some();
if !has_branch_alias || has_self_version {
let branch =
Preg::replace(r"{^dev-}", "", version.as_deref().unwrap_or("")).unwrap_or_default();
let mut length: i64 = PHP_INT_MAX;
// return directly, if branch is configured to be non-feature branch
if !self.is_feature_branch(package_config, Some(&branch)) {
return Ok(FeatureVersionResult {
version,
pretty_version,
});
}
// sort local branches first then remote ones
// and sort numeric branches below named ones, to make sure if the branch has the same distance from main and 1.10 and 1.9 for example, 1.9 is picked
// and sort using natural sort so that 1.10 will appear before 1.9
usort(&mut branches, |a: &String, b: &String| -> i64 {
let a_remote = strpos(a, "remotes/") == Some(0);
let b_remote = strpos(b, "remotes/") == Some(0);
if a_remote != b_remote {
return if a_remote { 1 } else { -1 };
}
strnatcasecmp(b, a)
});
let mut promises: Vec<Box<dyn shirabe_external_packages::react::promise::promise_interface::PromiseInterface>> =
vec![];
self.process.borrow_mut().set_max_jobs(30);
// TODO(phase-b): try/finally with resetMaxJobs
let result: Result<()> = (|| -> Result<()> {
let mut last_index: i64 = -1;
for (index, candidate) in branches.iter().enumerate() {
let candidate_version =
Preg::replace(r"{^remotes/\S+/}", "", candidate).unwrap_or_default();
// do not compare against itself or other feature branches
if candidate == &branch
|| self.is_feature_branch(package_config, Some(&candidate_version))
{
continue;
}
let candidate_clone = candidate.clone();
let branch_clone = branch.clone();
let cmd_line: Vec<String> = array_map(
move |component: &String| -> String {
// TODO(phase-b): str_replace with array arguments — emulating
let r1 = str_replace("%candidate%", &candidate_clone, component);
str_replace("%branch%", &branch_clone, &r1)
},
&scm_cmdline,
);
let async_promise = self.process.borrow_mut().execute_async(&cmd_line, path)?;
// TODO(phase-b): closure receives Process in PHP but PromiseInterface::then expects fn(Option<PhpMixed>) -> Option<PhpMixed>;
// closure captures need shared mutable state (last_index, length, version, pretty_version, promises)
promises.push(async_promise.then(
Some(Box::new(
move |_value: Option<PhpMixed>| -> Option<PhpMixed> {
todo!(
"mutate last_index/length/version/pretty_version and possibly cancel promises"
)
},
)),
None,
));
}
self.process.borrow_mut().wait();
Ok(())
})();
self.process.borrow_mut().reset_max_jobs();
result?;
}
Ok(FeatureVersionResult {
version,
pretty_version,
})
}
/// @param array<string, mixed> $packageConfig
fn is_feature_branch(
&self,
package_config: &IndexMap<String, PhpMixed>,
branch_name: Option<&str>,
) -> bool {
let mut non_feature_branches = String::new();
let nf_value = package_config.get("non-feature-branches");
if !empty(&nf_value.cloned().unwrap_or(PhpMixed::Null)) {
let names: Vec<String> = nf_value
.and_then(|v| v.as_list())
.map(|l| {
l.iter()
.filter_map(|v| v.as_string().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default();
non_feature_branches = implode("|", &names);
}
!Preg::is_match(
&format!(
r"{{^({}|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}}",
non_feature_branches,
),
branch_name.unwrap_or(""),
)
.unwrap_or(false)
}
/// @return array{version: string|null, commit: '', pretty_version: string|null}
fn guess_fossil_version(&mut self, path: &str) -> Result<VersionData> {
let mut version: Option<String> = None;
let mut pretty_version: Option<String> = None;
// try to fetch current version from fossil
let mut output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&[
"fossil".to_string(),
"branch".to_string(),
"list".to_string(),
],
&mut output,
Some(path.to_string()),
) {
let branch = trim(&output, None);
version = Some(self.version_parser.normalize_branch(&branch)?);
pretty_version = Some(format!("dev-{}", branch));
}
// try to fetch current version from fossil tags
let mut output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&["fossil".to_string(), "tag".to_string(), "list".to_string()],
&mut output,
Some(path.to_string()),
) {
// TODO(phase-b): use anyhow::Result<Result<T, E>> to model PHP try/catch
match self.version_parser.normalize(&trim(&output, None), None) {
Ok(v) => {
version = Some(v);
pretty_version = Some(trim(&output, None));
}
Err(_e) => {}
}
}
Ok(VersionData {
version,
commit: Some(String::new()),
pretty_version,
feature_version: None,
feature_pretty_version: None,
})
}
/// @param array<string, mixed> $packageConfig
///
/// @return array{version: string, commit: '', pretty_version: string}|null
fn guess_svn_version(
&mut self,
package_config: &IndexMap<String, PhpMixed>,
path: &str,
) -> Result<Option<VersionData>> {
SvnUtil::clean_env();
// try to fetch current version from svn
let mut output = String::new();
if 0 == self.process.borrow_mut().execute_args(
&["svn".to_string(), "info".to_string(), "--xml".to_string()],
&mut output,
Some(path.to_string()),
) {
let trunk_path = package_config
.get("trunk-path")
.and_then(|v| v.as_string())
.map(|s| preg_quote(s, Some('#')))
.unwrap_or_else(|| "trunk".to_string());
let branches_path = package_config
.get("branches-path")
.and_then(|v| v.as_string())
.map(|s| preg_quote(s, Some('#')))
.unwrap_or_else(|| "branches".to_string());
let tags_path = package_config
.get("tags-path")
.and_then(|v| v.as_string())
.map(|s| preg_quote(s, Some('#')))
.unwrap_or_else(|| "tags".to_string());
let url_pattern = format!(
"#<url>.*/({}|({}|{})/(.*))</url>#",
trunk_path, branches_path, tags_path,
);
if let Some(matches) = Preg::is_match_with_indexed_captures(&url_pattern, &output)? {
let m1 = matches.get(1).cloned().unwrap_or_default();
let m2 = matches.get(2).cloned();
let m3 = matches.get(3).cloned();
if m2.is_some()
&& m3.is_some()
&& (branches_path == *m2.as_ref().unwrap()
|| tags_path == *m2.as_ref().unwrap())
{
// we are in a branches path
let version = self
.version_parser
.normalize_branch(m3.as_deref().unwrap())?;
let pretty_version = format!("dev-{}", m3.as_ref().unwrap());
return Ok(Some(VersionData {
version: Some(version),
commit: Some(String::new()),
pretty_version: Some(pretty_version),
feature_version: None,
feature_pretty_version: None,
}));
}
assert!(is_string(&PhpMixed::String(m1.clone())));
let pretty_version = trim(&m1, None);
let version = if pretty_version == "trunk" {
"dev-trunk".to_string()
} else {
self.version_parser.normalize(&pretty_version, None)?
};
return Ok(Some(VersionData {
version: Some(version),
commit: Some(String::new()),
pretty_version: Some(pretty_version),
feature_version: None,
feature_pretty_version: None,
}));
}
}
Ok(None)
}
pub fn get_root_version_from_env(&self) -> Result<String> {
let version = Platform::get_env("COMPOSER_ROOT_VERSION");
let version = match version {
Some(v) if !v.is_empty() => v,
_ => {
return Err(RuntimeException {
message: "COMPOSER_ROOT_VERSION not set or empty".to_string(),
code: 0,
}
.into());
}
};
let mut m: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::is_match_strict_groups3(r"{^(\d+(?:\.\d+)*)-dev$}i", &version, Some(&mut m))
.unwrap_or(false)
{
return Ok(format!(
"{}.x-dev",
m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default()
));
}
Ok(version)
}
}
#[derive(Debug)]
pub struct FeatureVersionResult {
pub version: Option<String>,
pub pretty_version: Option<String>,
}
|