Skip to content

Commit ac637e0

Browse files
committed
feat: switch to composer versions
1 parent 9b9610d commit ac637e0

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

bin/leaf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ if (file_exists(dirname(__DIR__, 3) . "/autoload.php")) {
77
require dirname(__DIR__) . "/vendor/autoload.php";
88
}
99

10-
$leafCli = json_decode(file_get_contents(dirname(__DIR__) . "/composer.json"));
11-
$currentVersion = $leafCli->version;
10+
// composer.json carries no version key (packagist derives it from tags),
11+
// so ask composer's runtime for the installed version instead
12+
$currentVersion = \Composer\InstalledVersions::isInstalled('leafs/cli')
13+
? \Composer\InstalledVersions::getPrettyVersion('leafs/cli')
14+
: 'dev';
1215

1316
$app = sprout()->createApp([
1417
'name' => "
@@ -17,7 +20,7 @@ $app = sprout()->createApp([
1720
| |__/ -_) _` | _| | (__| |__ | |
1821
|____\___\__,_|_| \___|____|___|
1922
",
20-
'version' => ''
23+
'version' => $currentVersion
2124
]);
2225

2326
$app->register(Leaf\Console\CreateCommand::class);

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "leafs/cli",
33
"description": "A simple command line tool for installing and interacting with your leaf apps",
44
"homepage": "https://cli.leafphp.dev",
5-
"version": "v5.0-dev",
65
"keywords": [
76
"leaf",
87
"php",

src/Utils/Package.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public static function info()
2626
*/
2727
public static function version()
2828
{
29-
$meta = static::info();
29+
if (\Composer\InstalledVersions::isInstalled('leafs/cli')) {
30+
return \Composer\InstalledVersions::getPrettyVersion('leafs/cli');
31+
}
3032

31-
return $meta->version;
33+
return static::info()->version ?? 'dev';
3234
}
3335

3436
/**
@@ -64,8 +66,15 @@ public static function ltsVersion()
6466
*/
6567
public static function updateAvailable()
6668
{
67-
$currentVersion = ltrim(static::version(), 'v');
68-
$latestVersion = ltrim(static::ltsVersion(), 'v');
69+
$currentVersion = ltrim((string) static::version(), 'v');
70+
71+
// dev checkouts (contributors, CI) never self-update — this also
72+
// keeps the check off the network for test runs
73+
if ($currentVersion === '' || strpos($currentVersion, 'dev') !== false) {
74+
return false;
75+
}
76+
77+
$latestVersion = ltrim((string) static::ltsVersion(), 'v');
6978

7079
return version_compare($currentVersion, $latestVersion, '<');
7180
}

tests/commands.test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,18 @@
7777
->and($output)->toContain('github.com/leafsphp/cli/issues')
7878
->and($output)->toContain('No composer.json found');
7979
});
80+
81+
test('--version prints the version and nothing else', function () {
82+
[$exit, $output] = leaf('--version');
83+
84+
expect($exit)->toBe(0)
85+
->and(trim($output))->not->toBe('')
86+
->and($output)->not->toContain('Available commands');
87+
});
88+
89+
test('the banner prints the version exactly once', function () {
90+
[, $version] = leaf('--version');
91+
[, $banner] = leaf('');
92+
93+
expect(substr_count($banner, trim($version)))->toBe(1);
94+
});

0 commit comments

Comments
 (0)