Skip to content

Commit 8002739

Browse files
committed
Add optional force parameter to func extensions install command
1 parent 063e0d0 commit 8002739

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Azure.Functions.Cli/Actions/LocalActions/InstallExtensionAction.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal class InstallExtensionAction : BaseAction
2121
public string Source { get; set; } = string.Empty;
2222
public string ConfigPath { get; set; } = string.Empty;
2323
public bool Csx { get; set; }
24+
public bool Force { get; set; } = false;
2425

2526
public InstallExtensionAction(ISecretsManager secretsManager)
2627
{
@@ -59,6 +60,11 @@ public override ICommandLineParserResult ParseArgs(string[] args)
5960
.WithDescription("use old style csx dotnet functions")
6061
.Callback(csx => Csx = csx);
6162

63+
Parser
64+
.Setup<bool>('f', "force")
65+
.WithDescription("update extensions version when running 'func extensions install'")
66+
.Callback(force => Force = force);
67+
6268
return Parser.Parse(args);
6369
}
6470

@@ -75,9 +81,14 @@ public async override Task RunAsync()
7581

7682
if (string.IsNullOrEmpty(Package) && string.IsNullOrEmpty(Version))
7783
{
84+
var project = ProjectHelpers.GetProject(extensionsProj);
7885
foreach (var extensionPackage in ExtensionsHelper.GetExtensionPackages())
7986
{
80-
await AddPackage(extensionsProj, extensionPackage.Name, extensionPackage.Version);
87+
// Only add / update package referece if it does not exist or forced update is enabled
88+
if (!ProjectHelpers.PackageReferenceExists(project, extensionPackage.Name) || Force)
89+
{
90+
await AddPackage(extensionsProj, extensionPackage.Name, extensionPackage.Version);
91+
}
8192
}
8293
}
8394
else if (!string.IsNullOrEmpty(Package) && !string.IsNullOrEmpty(Version))

src/Azure.Functions.Cli/Common/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal static class Constants
2525
public const string FunctionsExtensionVersion = "FUNCTIONS_EXTENSION_VERSION";
2626
public const string StorageEmulatorConnectionString = "UseDevelopmentStorage=true";
2727
public const string AzureWebJobsStorage = "AzureWebJobsStorage";
28+
public const string PackageReferenceElementName = "PackageReference";
2829

2930
public static string CliVersion => typeof(Constants).GetTypeInfo().Assembly.GetName().Version.ToString(3);
3031
public static string CliBetaRevision => typeof(Constants).GetTypeInfo().Assembly.GetName().Version.MinorRevision.ToString();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Linq;
2+
using Microsoft.Build.Construction;
3+
using Azure.Functions.Cli.Common;
4+
using System.Threading.Tasks;
5+
using System.IO;
6+
using System.Xml;
7+
8+
namespace Azure.Functions.Cli.Helpers
9+
{
10+
internal static class ProjectHelpers
11+
{
12+
public static bool PackageReferenceExists(this ProjectRootElement project, string packageId)
13+
{
14+
ProjectItemElement existingPackageReference = project.Items
15+
.FirstOrDefault(item => item.ItemType == Constants.PackageReferenceElementName && item.Include.ToLowerInvariant() == packageId.ToLowerInvariant());
16+
return existingPackageReference != null;
17+
}
18+
19+
public static ProjectRootElement GetProject(string path)
20+
{
21+
ProjectRootElement root = null;
22+
23+
if (File.Exists(path))
24+
{
25+
var reader = XmlTextReader.Create(new StringReader(File.ReadAllText(path)));
26+
root = ProjectRootElement.Create(reader);
27+
}
28+
29+
return root;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)