@@ -12,10 +12,12 @@ import (
1212 "path/filepath"
1313 "strings"
1414
15+ "github.com/azure/azure-dev/cli/azd/internal/names"
1516 "github.com/azure/azure-dev/cli/azd/pkg/apphost"
1617 "github.com/azure/azure-dev/cli/azd/pkg/environment"
1718 "github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
1819 "github.com/azure/azure-dev/cli/azd/pkg/osutil"
20+ "github.com/azure/azure-dev/cli/azd/pkg/project"
1921 "github.com/azure/azure-dev/cli/azd/pkg/tools/dotnet"
2022)
2123
@@ -64,32 +66,65 @@ func (s *environmentService) CreateEnvironmentAsync(
6466 // If an azure.yaml doesn't already exist, we need to create one. Creating an environment implies initializing the
6567 // azd project if it does not already exist.
6668 if _ , err := os .Stat (c .azdContext .ProjectPath ()); errors .Is (err , fs .ErrNotExist ) {
67- _ = observer .OnNext (ctx , newImportantProgressMessage ("Analyzing Aspire Application (this might take a moment...)" ))
68-
69- manifest , err := apphost .ManifestFromAppHost (ctx , rc .HostProjectPath , c .dotnetCli , dotnetEnv )
70- if err != nil {
71- return false , fmt .Errorf ("reading app host manifest: %w" , err )
72- }
73-
74- projectName := azdcontext .ProjectName (strings .TrimSuffix (c .azdContext .ProjectDirectory (), ".AppHost" ))
75-
76- // Write an azure.yaml file to the project.
77- files , err := apphost .GenerateProjectArtifacts (
78- ctx ,
79- c .azdContext .ProjectDirectory (),
80- projectName ,
81- manifest ,
82- rc .HostProjectPath ,
83- )
69+ isAspire , err := c .dotnetCli .IsAspireHostProject (ctx , rc .HostProjectPath )
8470 if err != nil {
85- return false , fmt .Errorf ("generating project artifacts : %w" , err )
71+ return false , fmt .Errorf ("checking if %s is an app host project : %w" , rc . HostProjectPath , err )
8672 }
8773
88- file := files ["azure.yaml" ]
89- projectFilePath := filepath .Join (c .azdContext .ProjectDirectory (), "azure.yaml" )
90-
91- if err := os .WriteFile (projectFilePath , []byte (file .Contents ), osutil .PermissionFile ); err != nil {
92- return false , fmt .Errorf ("writing azure.yaml: %w" , err )
74+ if isAspire {
75+ _ = observer .OnNext (ctx ,
76+ newImportantProgressMessage ("Analyzing Aspire Application (this might take a moment...)" ))
77+
78+ manifest , err := apphost .ManifestFromAppHost (ctx , rc .HostProjectPath , c .dotnetCli , dotnetEnv )
79+ if err != nil {
80+ return false , fmt .Errorf ("reading app host manifest: %w" , err )
81+ }
82+
83+ projectName := azdcontext .ProjectName (strings .TrimSuffix (c .azdContext .ProjectDirectory (), ".AppHost" ))
84+
85+ // Write an azure.yaml file to the project.
86+ files , err := apphost .GenerateProjectArtifacts (
87+ ctx ,
88+ c .azdContext .ProjectDirectory (),
89+ projectName ,
90+ manifest ,
91+ rc .HostProjectPath ,
92+ )
93+ if err != nil {
94+ return false , fmt .Errorf ("generating project artifacts: %w" , err )
95+ }
96+
97+ file := files ["azure.yaml" ]
98+ projectFilePath := filepath .Join (c .azdContext .ProjectDirectory (), "azure.yaml" )
99+
100+ if err := os .WriteFile (projectFilePath , []byte (file .Contents ), osutil .PermissionFile ); err != nil {
101+ return false , fmt .Errorf ("writing azure.yaml: %w" , err )
102+ }
103+ } else {
104+ rel , err := filepath .Rel (c .azdContext .ProjectDirectory (), rc .HostProjectPath )
105+ if err != nil {
106+ return false , fmt .Errorf ("determining relative path: %w" , err )
107+ }
108+
109+ projectName := azdcontext .ProjectName (c .azdContext .ProjectDirectory ())
110+
111+ ext := filepath .Ext (rc .HostProjectPath )
112+ serviceName := names .LabelName (strings .TrimSuffix (filepath .Base (rc .HostProjectPath ), ext ))
113+
114+ prjConfig := project.ProjectConfig {
115+ Name : projectName ,
116+ Services : map [string ]* project.ServiceConfig {
117+ serviceName : {
118+ Name : serviceName ,
119+ RelativePath : fmt .Sprintf ("./%s" , filepath .ToSlash (rel )),
120+ },
121+ },
122+ }
123+
124+ err = project .Save (ctx , & prjConfig , c .azdContext .ProjectPath ())
125+ if err != nil {
126+ return false , fmt .Errorf ("saving project config: %w" , err )
127+ }
93128 }
94129 } else if err != nil {
95130 return false , fmt .Errorf ("checking for project: %w" , err )
0 commit comments