-
Notifications
You must be signed in to change notification settings - Fork 818
Add VersionChecker to detect and warn about newer Bicep CLI installations #18375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
polatengin
wants to merge
9
commits into
main
Choose a base branch
from
polatengin/5070-try-to-identify-when-two-versions-of-bicep-are-installed-on-the-system
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a00074c
Add VersionChecker to detect and warn about newer Bicep CLI installat…
polatengin 67c5137
Use FileVersionInfo to get Bicep executable version instead of launch…
polatengin d14a075
Run version check asynchronously and limit scan scope
polatengin ac9e02c
Add VersionChecker tests and make internals testable
polatengin f8c3d61
Normalize path handling in VersionChecker tests for cross-platform co…
polatengin 668c7fc
Enhance Version Checking and CLI Version Output
polatengin 190c959
Merge remote-tracking branch 'origin/main' into polatengin/5070-try-t…
polatengin 0d01d95
Update version output format in PrintVersion method
polatengin e30735e
Update version output format in Bicep CLI integration tests
polatengin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.IO.Abstractions; | ||
| using System.Runtime.InteropServices; | ||
| using Bicep.Core.Utils; | ||
|
|
||
| namespace Bicep.Cli.Helpers; | ||
|
|
||
| public class VersionChecker(IEnvironment environment, IFileSystem fileSystem) | ||
| { | ||
| /// <summary> | ||
| /// Checks well-known installation locations for other Bicep CLI versions and warns if a newer version is installed. | ||
| /// </summary> | ||
| public void CheckForNewerVersions(TextWriter output) | ||
| { | ||
| try | ||
| { | ||
| var currentVersion = environment.CurrentVersion.Version; | ||
| if (!Version.TryParse(currentVersion, out var parsedCurrentVersion)) | ||
| { | ||
| return; // Cannot parse current version, skip check | ||
| } | ||
|
|
||
| var newerVersions = FindNewerVersions(parsedCurrentVersion); | ||
|
|
||
| if (newerVersions.Any()) | ||
| { | ||
| output.WriteLine($"Warning: You are running Bicep CLI version {currentVersion}, but newer version(s) are installed on this system:"); | ||
| foreach (var (version, path) in newerVersions.OrderByDescending(v => v.Version)) | ||
| { | ||
| output.WriteLine($" - Version {version} at {path}"); | ||
| } | ||
| output.WriteLine(); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Silently ignore any errors during version checking to avoid disrupting normal CLI operations | ||
| } | ||
| } | ||
|
|
||
| private List<(Version Version, string Path)> FindNewerVersions(Version currentVersion) | ||
| { | ||
| var newerVersions = new List<(Version, string)>(); | ||
| var checkedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| var currentExePath = GetNormalizedPath(System.Environment.ProcessPath); | ||
| if (currentExePath != null) | ||
| { | ||
| checkedPaths.Add(currentExePath); | ||
| } | ||
|
|
||
| foreach (var location in GetWellKnownInstallLocations()) | ||
| { | ||
| try | ||
| { | ||
| if (!fileSystem.Directory.Exists(location)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var bicepFiles = fileSystem.Directory.GetFiles( | ||
| location, | ||
| environment.CurrentPlatform == OSPlatform.Windows ? "bicep.exe" : "bicep", | ||
| SearchOption.AllDirectories); | ||
|
|
||
| foreach (var bicepPath in bicepFiles) | ||
| { | ||
| var normalizedPath = GetNormalizedPath(bicepPath); | ||
| if (normalizedPath == null || checkedPaths.Contains(normalizedPath)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| checkedPaths.Add(normalizedPath); | ||
|
|
||
| var version = GetBicepVersion(bicepPath); | ||
| if (version != null && version > currentVersion) | ||
| { | ||
| newerVersions.Add((version, bicepPath)); | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Skip locations that cannot be accessed | ||
| } | ||
| } | ||
|
|
||
| return newerVersions; | ||
| } | ||
|
|
||
| private List<string> GetWellKnownInstallLocations() | ||
| { | ||
| var locations = new List<string>(); | ||
| var homePath = environment.GetVariable("HOME") ?? environment.GetVariable("USERPROFILE"); | ||
|
|
||
| if (string.IsNullOrEmpty(homePath)) | ||
| { | ||
| return locations; | ||
| } | ||
|
|
||
| // ~/.bicep/bin (default location) | ||
| locations.Add(fileSystem.Path.Combine(homePath, ".bicep", "bin")); | ||
|
|
||
| // ~/.azure/bin (install location from Azure CLI) | ||
| locations.Add(fileSystem.Path.Combine(homePath, ".azure", "bin")); | ||
|
|
||
| if (environment.CurrentPlatform == OSPlatform.Windows) | ||
| { | ||
| // C:\Program Files\Bicep CLI | ||
| var programFiles = environment.GetVariable("ProgramFiles"); | ||
| if (!string.IsNullOrEmpty(programFiles)) | ||
| { | ||
| locations.Add(fileSystem.Path.Combine(programFiles, "Bicep CLI")); | ||
| } | ||
|
|
||
| // C:\Program Files (x86)\Bicep CLI | ||
| var programFilesX86 = environment.GetVariable("ProgramFiles(x86)"); | ||
| if (!string.IsNullOrEmpty(programFilesX86)) | ||
| { | ||
| locations.Add(fileSystem.Path.Combine(programFilesX86, "Bicep CLI")); | ||
| } | ||
|
|
||
| var pathVar = environment.GetVariable("PATH"); | ||
| if (!string.IsNullOrEmpty(pathVar)) | ||
| { | ||
| foreach (var pathEntry in pathVar.Split(';')) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(pathEntry)) | ||
| { | ||
| locations.Add(pathEntry.Trim()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // /usr/local/bin | ||
| locations.Add("/usr/local/bin"); | ||
|
|
||
| // /usr/bin | ||
| locations.Add("/usr/bin"); | ||
|
|
||
| var pathVar = environment.GetVariable("PATH"); | ||
| if (!string.IsNullOrEmpty(pathVar)) | ||
| { | ||
| foreach (var pathEntry in pathVar.Split(':')) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(pathEntry)) | ||
| { | ||
| locations.Add(pathEntry.Trim()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return locations.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); | ||
| } | ||
|
|
||
| private Version? GetBicepVersion(string bicepPath) | ||
| { | ||
| try | ||
| { | ||
| var startInfo = new ProcessStartInfo | ||
| { | ||
| FileName = bicepPath, | ||
| Arguments = "--version", | ||
| RedirectStandardOutput = true, | ||
| RedirectStandardError = true, | ||
| UseShellExecute = false, | ||
| CreateNoWindow = true | ||
| }; | ||
|
|
||
| using var process = Process.Start(startInfo); | ||
| if (process == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| process.WaitForExit(5000); | ||
|
|
||
| if (process.ExitCode != 0) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var output = process.StandardOutput.ReadToEnd(); | ||
|
|
||
| // Parse version from output like "Bicep CLI version 0.30.23 (abc123)" | ||
| // Extract the version number between "version " and the next space or parenthesis | ||
| var versionMatch = System.Text.RegularExpressions.Regex.Match( | ||
| output, | ||
| @"version\s+(\d+\.\d+\.\d+)", | ||
| System.Text.RegularExpressions.RegexOptions.IgnoreCase); | ||
|
|
||
| if (versionMatch.Success && Version.TryParse(versionMatch.Groups[1].Value, out var version)) | ||
| { | ||
| return version; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
|
polatengin marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private string? GetNormalizedPath(string? path) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| return fileSystem.Path.GetFullPath(path); | ||
| } | ||
| catch | ||
| { | ||
| return path; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.