From 6f780fc3c160525bad5373b72ab7dafd4ced48e1 Mon Sep 17 00:00:00 2001 From: Lewis Larsen Date: Tue, 30 Jul 2024 18:06:47 +0100 Subject: [PATCH] feat: Added feature banner --- app/Console/Commands/FetchNewFeatures.php | 143 ++++++++++++++++++ app/Livewire/Other/NewFeatureBanner.php | 56 +++++++ bump_version.sh | 28 ++++ new_features.json | 8 + .../views/account/partials/sidebar.blade.php | 2 +- .../views/components/no-content.blade.php | 14 +- resources/views/layouts/app.blade.php | 1 + .../other/new-feature-banner.blade.php | 69 +++++++++ routes/console.php | 4 + .../Console/Commands/FetchNewFeaturesTest.php | 134 ++++++++++++++++ .../Livewire/Other/NewFeatureBannerTest.php | 38 +++++ 11 files changed, 493 insertions(+), 4 deletions(-) create mode 100644 app/Console/Commands/FetchNewFeatures.php create mode 100644 app/Livewire/Other/NewFeatureBanner.php create mode 100644 new_features.json create mode 100644 resources/views/livewire/other/new-feature-banner.blade.php create mode 100644 tests/Feature/Console/Commands/FetchNewFeaturesTest.php create mode 100644 tests/Feature/Livewire/Other/NewFeatureBannerTest.php diff --git a/app/Console/Commands/FetchNewFeatures.php b/app/Console/Commands/FetchNewFeatures.php new file mode 100644 index 00000000..bfa5c248 --- /dev/null +++ b/app/Console/Commands/FetchNewFeatures.php @@ -0,0 +1,143 @@ +fetchFeatures(); + + if ($features === []) { + $this->error('No new features found.'); + + return CommandAlias::FAILURE; + } + + $this->storeLatestFeature($features); + $this->info('New features fetched and stored successfully.'); + + return CommandAlias::SUCCESS; + } catch (Exception $e) { + $this->error($e->getMessage()); + + return CommandAlias::FAILURE; + } + } + + /** + * Fetch features from either local or remote source. + * + * @return array> + */ + private function fetchFeatures(): array + { + return $this->option('local') ? $this->fetchLocal() : $this->fetchRemote(); + } + + /** + * Fetch features from the remote GitHub repository. + * + * @return array> + * + * @throws Exception + */ + private function fetchRemote(): array + { + try { + $response = Http::get('https://raw.githubusercontent.com/vanguardbackup/vanguard/main/new_features.json'); + $response->throw(); + + return $response->json(); + } catch (RequestException $e) { + throw new Exception("Failed to fetch new features from remote: {$e->getMessage()}", $e->getCode(), $e); + } + } + + /** + * Fetch features from the local file. + * + * @return array> + * + * @throws Exception + */ + private function fetchLocal(): array + { + $path = base_path('new_features.json'); + + if (! File::exists($path)) { + throw new Exception('Local new_features.json file not found in project root.'); + } + + $content = File::get($path); + $features = json_decode($content, true); + + if (json_last_error() !== JSON_ERROR_NONE || ! is_array($features)) { + throw new Exception('Failed to parse local new_features.json file.'); + } + + return $features; + } + + /** + * Store the latest feature in the cache if available. + * + * @param array> $features + * + * @throws FileNotFoundException + */ + private function storeLatestFeature(array $features): void + { + if ($features === []) { + $this->info('No features to store.'); + + return; + } + + $latestFeature = end($features); + if ($latestFeature === false) { + $this->error('Failed to retrieve the latest feature.'); + + return; + } + + $latestFeature['current_version'] = $this->getCurrentVersion(); + $latestFeature['github_url'] ??= 'https://github.com/vanguardbackup/vanguard'; + + Cache::put('latest_feature', $latestFeature, now()->addDay()); + } + + /** + * Get the current version from the VERSION file. + * + * @throws FileNotFoundException + */ + private function getCurrentVersion(): string + { + $versionFile = base_path('VERSION'); + + return File::exists($versionFile) ? trim(File::get($versionFile)) : '0.0.0'; + } +} diff --git a/app/Livewire/Other/NewFeatureBanner.php b/app/Livewire/Other/NewFeatureBanner.php new file mode 100644 index 00000000..daff2504 --- /dev/null +++ b/app/Livewire/Other/NewFeatureBanner.php @@ -0,0 +1,56 @@ +|null + */ + public ?array $latestFeature = null; + + /** + * Initialize the component state. + */ + public function mount(): void + { + $cachedFeature = Cache::get('latest_feature'); + + if (is_array($cachedFeature)) { + $this->latestFeature = $cachedFeature; + } elseif ($cachedFeature !== null) { + Log::warning('Unexpected data type for latest_feature in cache', ['type' => gettype($cachedFeature)]); + $this->latestFeature = null; + } + } + + /** + * Render the component. + */ + public function render(): View + { + return view('livewire.other.new-feature-banner'); + } + + /** + * Dismiss the feature banner. + */ + public function dismiss(): void + { + $this->latestFeature = null; + $this->dispatch('featureDismissed'); + } +} diff --git a/bump_version.sh b/bump_version.sh index e3d30fce..c81150e2 100755 --- a/bump_version.sh +++ b/bump_version.sh @@ -92,6 +92,27 @@ bump_version() { echo "${parts[0]}.${parts[1]}.${parts[2]}" } +update_feature_banner() { + local version="$1" + local title + local description + + read -p "Enter the feature title: " title + read -p "Enter the feature description: " description + + local json_content="[ + { + \"title\": \"$title\", + \"description\": \"$description\", + \"version\": \"$version\", + \"github_url\": \"https://github.com/vanguardbackup/vanguard/releases/tag/$version\" + } +]" + + echo "$json_content" > new_features.json + log "INFO" "Updated new_features.json with the latest feature information." +} + show_usage() { echo "Usage: $0 [-v] " echo " -v: Enable verbose mode" @@ -170,6 +191,13 @@ esac log "INFO" "Bumping version to $NEW_VERSION ..." echo "$NEW_VERSION" > VERSION +# Ask if the user wants to update the feature banner +read -p "Do you want to update the feature banner? (y/n): " update_banner +if [[ $update_banner =~ ^[Yy]$ ]]; then + update_feature_banner "$NEW_VERSION" + git add new_features.json +fi + log "INFO" "Committing version bump..." git add VERSION git commit --no-verify -m "chore: bump version from $OLD_VERSION to $NEW_VERSION 🎉" diff --git a/new_features.json b/new_features.json new file mode 100644 index 00000000..f8a14238 --- /dev/null +++ b/new_features.json @@ -0,0 +1,8 @@ +[ + { + "title": "New Feature Banner!", + "description": "We have added a new feature banner to Vanguard.", + "version": "1.4.2", + "github_url": "https://github.com/vanguardbackup/vanguard/releases/tag/1.4.2" + } +] diff --git a/resources/views/account/partials/sidebar.blade.php b/resources/views/account/partials/sidebar.blade.php index 4f617666..88d039c5 100644 --- a/resources/views/account/partials/sidebar.blade.php +++ b/resources/views/account/partials/sidebar.blade.php @@ -1,4 +1,4 @@ -