|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using Azure.Functions.Cli.Common; |
| 5 | +using Azure.Functions.Cli.Helpers; |
| 6 | +using Azure.Functions.Cli.Interfaces; |
| 7 | +using Colors.Net; |
| 8 | +using static Azure.Functions.Cli.Common.OutputTheme; |
| 9 | + |
| 10 | +namespace Azure.Functions.Cli.Actions.LocalActions.PackAction |
| 11 | +{ |
| 12 | + [Action(Name = "pack dotnet", CommandType = CommandType.SubCommand, ShowInHelp = true, HelpText = "Internal .NET runtime-specific pack command")] |
| 13 | + internal class DotNetPackSubCommand : PackSubCommandBase |
| 14 | + { |
| 15 | + public DotNetPackSubCommand(ISecretsManager secretsManager, PackAction parentAction) |
| 16 | + : base(secretsManager, parentAction) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + protected override void SetupParser() |
| 21 | + { |
| 22 | + // .NET doesn't have any runtime-specific arguments beyond the common ones |
| 23 | + Parser |
| 24 | + .Setup<bool>("build-native-deps") |
| 25 | + .SetDefault(false) |
| 26 | + .WithDescription("Build native dependencies in Docker container"); |
| 27 | + } |
| 28 | + |
| 29 | + public override async Task RunAsync() |
| 30 | + { |
| 31 | + var functionAppRoot = ParentAction.ResolveFunctionAppRoot(); |
| 32 | + string packingRoot = functionAppRoot; |
| 33 | + |
| 34 | + if (ParentAction.NoBuild) |
| 35 | + { |
| 36 | + // For --no-build, treat FolderPath as the build output directory |
| 37 | + if (string.IsNullOrEmpty(ParentAction.FolderPath)) |
| 38 | + { |
| 39 | + throw new CliException("When using --no-build for .NET projects, you must specify the path to the build output directory (e.g., ./bin/Release/net8.0/publish)"); |
| 40 | + } |
| 41 | + |
| 42 | + packingRoot = Path.IsPathRooted(ParentAction.FolderPath) |
| 43 | + ? ParentAction.FolderPath |
| 44 | + : Path.Combine(Environment.CurrentDirectory, ParentAction.FolderPath); |
| 45 | + |
| 46 | + if (!Directory.Exists(packingRoot)) |
| 47 | + { |
| 48 | + throw new CliException($"Build output directory not found: {packingRoot}"); |
| 49 | + } |
| 50 | + |
| 51 | + ValidateDotNetPublishDirectory(packingRoot); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + ParentAction.ValidateFunctionAppRoot(functionAppRoot); |
| 56 | + |
| 57 | + // Run dotnet publish |
| 58 | + ColoredConsole.WriteLine("Building .NET project..."); |
| 59 | + await RunDotNetPublish(functionAppRoot); |
| 60 | + |
| 61 | + // Update packing root to publish output |
| 62 | + packingRoot = Path.Combine(functionAppRoot, "output"); |
| 63 | + } |
| 64 | + |
| 65 | + var outputPath = ParentAction.ResolveOutputPath(functionAppRoot); |
| 66 | + ParentAction.CleanupExistingPackage(outputPath); |
| 67 | + |
| 68 | + // Install extensions if not in no-build mode |
| 69 | + if (!ParentAction.NoBuild) |
| 70 | + { |
| 71 | + var installExtensionAction = new InstallExtensionAction(SecretsManager, false); |
| 72 | + await installExtensionAction.RunAsync(); |
| 73 | + } |
| 74 | + |
| 75 | + await ParentAction.CreatePackage(packingRoot, outputPath); |
| 76 | + } |
| 77 | + |
| 78 | + private void ValidateDotNetPublishDirectory(string path) |
| 79 | + { |
| 80 | + var requiredFiles = new[] { "host.json" }; |
| 81 | + foreach (var file in requiredFiles) |
| 82 | + { |
| 83 | + if (!FileSystemHelpers.FileExists(Path.Combine(path, file))) |
| 84 | + { |
| 85 | + throw new CliException($"Required file {file} not found in build output directory: {path}"); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private async Task RunDotNetPublish(string functionAppRoot) |
| 91 | + { |
| 92 | + DotnetHelpers.EnsureDotnet(); |
| 93 | + |
| 94 | + var outputPath = Path.Combine(functionAppRoot, "output"); |
| 95 | + |
| 96 | + // Clean the output directory if it exists |
| 97 | + if (FileSystemHelpers.DirectoryExists(outputPath)) |
| 98 | + { |
| 99 | + FileSystemHelpers.DeleteDirectorySafe(outputPath); |
| 100 | + } |
| 101 | + |
| 102 | + // Run dotnet publish |
| 103 | + var exe = new Executable("dotnet", $"publish --configuration Release --output \"{outputPath}\"", workingDirectory: functionAppRoot); |
| 104 | + var exitCode = await exe.RunAsync( |
| 105 | + o => ColoredConsole.WriteLine(o), |
| 106 | + e => ColoredConsole.Error.WriteLine(ErrorColor(e))); |
| 107 | + |
| 108 | + if (exitCode != 0) |
| 109 | + { |
| 110 | + throw new CliException("Error publishing .NET project"); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments