1+ #l "aspire-variables.cake"
2+
3+ using System . Xml . Linq ;
4+
5+ //-------------------------------------------------------------
6+
7+ public class AspireProcessor : ProcessorBase
8+ {
9+ public AspireProcessor ( BuildContext buildContext )
10+ : base ( buildContext )
11+ {
12+
13+ }
14+
15+ public override bool HasItems ( )
16+ {
17+ return BuildContext . Aspire . Items . Count > 0 ;
18+ }
19+
20+ public override async Task PrepareAsync ( )
21+ {
22+ if ( ! HasItems ( ) )
23+ {
24+ return ;
25+ }
26+
27+ // Nothing needed
28+ }
29+
30+ public override async Task UpdateInfoAsync ( )
31+ {
32+ if ( ! HasItems ( ) )
33+ {
34+ return ;
35+ }
36+
37+ // Nothing needed
38+ }
39+
40+ public override async Task BuildAsync ( )
41+ {
42+ if ( ! HasItems ( ) )
43+ {
44+ return ;
45+ }
46+
47+ // Nothing needed
48+ }
49+
50+ public override async Task PackageAsync ( )
51+ {
52+ if ( ! HasItems ( ) )
53+ {
54+ return ;
55+ }
56+
57+ var aspireContext = BuildContext . Aspire ;
58+
59+ if ( aspireContext . Items . Count > 1 )
60+ {
61+ throw new InvalidOperationException ( "Multiple Aspire projects found. Please ensure only one Aspire project is defined in the solution." ) ;
62+ }
63+
64+ var environmentName = GetEnvironmentName ( aspireContext ) ;
65+
66+ foreach ( var aspireProject in aspireContext . Items )
67+ {
68+ if ( BuildContext . General . SkipComponentsThatAreNotDeployable &&
69+ ! ShouldPackageProject ( BuildContext , aspireProject ) )
70+ {
71+ CakeContext . Information ( "Aspire project '{0}' should not be packaged" , aspireProject ) ;
72+ continue ;
73+ }
74+
75+ BuildContext . CakeContext . LogSeparator ( "Packaging Aspire project '{0}'" , aspireProject ) ;
76+
77+ BuildContext . CakeContext . Information ( "Setting environment variables" ) ;
78+
79+ var environmentVariables = new Dictionary < string , string >
80+ {
81+ { "AZURE_PRINCIPAL_ID" , aspireContext . AzurePrincipalId } ,
82+ { "AZURE_PRINCIPAL_TYPE" , aspireContext . AzurePrincipalType } ,
83+ { "AZURE_LOCATION" , aspireContext . AzureLocation } ,
84+ { "AZURE_RESOURCE_GROUP" , $ "rg-{ aspireContext . AzureResourceGroup } -{ aspireContext . EnvironmentName } " } ,
85+ { "AZURE_SUBSCRIPTION_ID" , aspireContext . AzureSubscriptionId } ,
86+ { "AZURE_ENV_NAME" , aspireContext . EnvironmentName } ,
87+ } ;
88+
89+ foreach ( var environmentVariable in environmentVariables )
90+ {
91+ RunAzd ( $ "env set { environmentVariable . Key } =\" { environmentVariable . Value } \" -e { environmentName } --no-prompt") ;
92+ }
93+
94+ BuildContext . CakeContext . Information ( "Generating infrastructure context" ) ;
95+
96+ RunAzd ( $ "infra gen -e { environmentName } --force") ;
97+
98+ BuildContext . CakeContext . LogSeparator ( ) ;
99+ }
100+ }
101+
102+ public override async Task DeployAsync ( )
103+ {
104+ if ( ! HasItems ( ) )
105+ {
106+ return ;
107+ }
108+
109+ var aspireContext = BuildContext . Aspire ;
110+
111+ if ( aspireContext . Items . Count > 1 )
112+ {
113+ throw new InvalidOperationException ( "Multiple Aspire projects found. Please ensure only one Aspire project is defined in the solution." ) ;
114+ }
115+
116+ var environmentName = GetEnvironmentName ( aspireContext ) ;
117+
118+ foreach ( var aspireProject in aspireContext . Items )
119+ {
120+ if ( ! ShouldDeployProject ( BuildContext , aspireProject ) )
121+ {
122+ CakeContext . Information ( "Aspire project '{0}' should not be deployed" , aspireProject ) ;
123+ continue ;
124+ }
125+
126+ BuildContext . CakeContext . LogSeparator ( "Deploying Aspire project '{0}'" , aspireProject ) ;
127+
128+ try
129+ {
130+ BuildContext . CakeContext . Information ( "Logging in to Azure" ) ;
131+
132+ RunAzd ( $ "auth login --tenant-id { aspireContext . AzureTenantId } --client-id { aspireContext . AzureClientId } --client-secret { aspireContext . AzureClientSecret } ") ;
133+
134+ // Note: got weird errors when running provision and deploy manually, so using up instead
135+
136+ BuildContext . CakeContext . Information ( "Deploying to Azure" ) ;
137+
138+ RunAzd ( $ "up -e { environmentName } ") ;
139+
140+ //BuildContext.CakeContext.Information("Provisioning infrastructure for Aspire project '{0}'", aspireProject);
141+
142+ //RunAzd($"provision -e {environmentName}");
143+
144+ //BuildContext.CakeContext.Information("Deploying Aspire project '{0}'", aspireProject);
145+
146+ // Note: this could technically be improved in the future by using
147+ // azd deploy 'componentname'
148+
149+ //RunAzd($"deploy --all -e {environmentName}");
150+
151+ await BuildContext . Notifications . NotifyAsync ( aspireProject , string . Format ( "Deployed to Azure" ) , TargetType . AspireProject ) ;
152+ }
153+ finally
154+ {
155+ BuildContext . CakeContext . Information ( "Logging out of Azure" ) ;
156+
157+ RunAzd ( $ "auth logout") ;
158+ }
159+
160+ BuildContext . CakeContext . LogSeparator ( ) ;
161+ }
162+ }
163+
164+ public override async Task FinalizeAsync ( )
165+ {
166+ // Nothing needed
167+ }
168+
169+ private string GetEnvironmentName ( AspireContext aspireContext )
170+ {
171+ // Because resource group names are set: "rg-{environmentName}" by Aspire, we automatically add
172+ // an extra name to the environment
173+
174+ var environmentName = $ "{ aspireContext . AzureResourceGroup } -{ aspireContext . EnvironmentName } ";
175+
176+ return environmentName ;
177+ }
178+
179+ private void RunAzd ( string arguments )
180+ {
181+ if ( BuildContext . CakeContext . StartProcess ( "azd" , new ProcessSettings
182+ {
183+ Arguments = arguments
184+ } ) != 0 )
185+ {
186+ throw new CakeException ( "Azd failed failed. Please check the logs for more details." ) ;
187+ }
188+ }
189+ }
0 commit comments