Skip to content

Commit 8c8f8ab

Browse files
authored
Merge pull request #6 from creacoon/env-config-support
Add support for .env version setting
2 parents 1ebbeb3 + 4421c93 commit 8c8f8ab

4 files changed

Lines changed: 52 additions & 22 deletions

File tree

README.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,7 @@ The package will:
4545
5. Create a git tag for the new version
4646

4747
## Configuration
48-
You can customize the package behavior by modifying the `config/version.php` file:
49-
``` php
50-
return [
51-
'config_key' => 'app.version',
52-
'version_key' => 'version',
53-
'config_path' => config_path('app.php'),
54-
'changelog_path' => base_path('CHANGELOG.md'),
55-
'unreleased_pattern' => '## [Unreleased]',
56-
'commit_message' => 'chg: dev: Version set to',
57-
'files_to_commit' => [
58-
'CHANGELOG.md',
59-
'config/app.php'
60-
],
61-
];
62-
```
48+
You can customize the package behavior by modifying the `config/version-manager.php` file.
6349

6450
## License
6551
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

config/version-manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// Path to the config file
2020
'config_path' => config_path('app.php'),
2121

22+
// Instead of modifying the config file, use an environment variable (specify variable)
23+
'use_environment_variable_for_version' => null,
24+
2225
// Path to the changelog file
2326
'changelog_path' => base_path('CHANGELOG.md'),
2427

src/Commands/VersionManagerCommand.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ public function handle(): int
2424
}
2525

2626
// Update config file
27-
if (! VersionManager::updateConfigFile($new_version)) {
27+
if (! VersionManager::setVersion($new_version)) {
2828
$this->error('Could not update version in config file. Please check the format.');
2929

3030
return self::FAILURE;
3131
}
3232

33-
// Update runtime config
34-
VersionManager::updateRuntimeConfig($new_version);
35-
$this->info('Application version set to: '.VersionManager::getCurrentVersion());
36-
3733
// Update changelog
3834
VersionManager::updateChangelog($new_version);
3935

src/VersionManager.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,53 @@ public function setVersion(string $newVersion): bool
2828
return false;
2929
}
3030

31-
return $this->updateConfigFile($newVersion) &&
32-
$this->updateRuntimeConfig($newVersion);
31+
if (config('version-manager.use_environment_variable_for_version')) {
32+
return $this->updateEnvironmentFile($newVersion) &&
33+
$this->updateRuntimeConfig($newVersion);
34+
} else {
35+
return $this->updateConfigFile($newVersion) &&
36+
$this->updateRuntimeConfig($newVersion);
37+
}
38+
}
39+
40+
public function updateEnvironmentFile(string $newVersion): bool
41+
{
42+
$env_variable = config('version-manager.use_environment_variable_for_version');
43+
44+
if (empty($env_variable)) {
45+
return false;
46+
}
47+
48+
$env_path = base_path('.env');
49+
50+
if (! File::exists($env_path)) {
51+
return false;
52+
}
53+
54+
$content = File::get($env_path);
55+
56+
$key = preg_quote($env_variable, '/');
57+
$pattern = "/^($key\s*=\s*)(.*)$/m";
58+
59+
if (preg_match($pattern, $content)) {
60+
$updated_content = preg_replace(
61+
$pattern,
62+
'${1}'.$newVersion,
63+
$content
64+
);
65+
} else {
66+
$updated_content = $content;
67+
if (! str_ends_with($updated_content, "\n") && strlen($updated_content) > 0) {
68+
$updated_content .= "\n";
69+
}
70+
$updated_content .= "$env_variable=$newVersion";
71+
}
72+
73+
if ($updated_content === $content) {
74+
return true;
75+
}
76+
77+
return (bool) File::put($env_path, $updated_content);
3378
}
3479

3580
/**

0 commit comments

Comments
 (0)