Skip to content

Commit

Permalink
fix projects with empty spaces aspire (#4472)
Browse files Browse the repository at this point in the history
* use label as package out of repository and re-use for apphost project generation

* use names

* use GetDefaultProjectName

* fix `show` not using project name as AppName

* refactor as a function

* clean up one spot

* add copyright notice

* remove panic

---------

Co-authored-by: Wei Lim <weilim@microsoft.com>
  • Loading branch information
vhvb1989 and weikanglim authored Oct 24, 2024
1 parent f27b9d6 commit 8fd80cb
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cli/azd/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}

s.console.MessageUxItem(ctx, &ux.Show{
AppName: s.azdCtx.GetDefaultProjectName(),
AppName: s.projectConfig.Name,
Services: uxServices,
Environments: uxEnvironments,
AzurePortalLink: azurePortalLink(s.portalUrlBase, subId, rgName),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package repository
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package names

import "strings"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package repository
package names

import (
"testing"
Expand Down
7 changes: 4 additions & 3 deletions cli/azd/internal/repository/app_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/internal/appdetect"
"github.com/azure/azure-dev/cli/azd/internal/names"
"github.com/azure/azure-dev/cli/azd/internal/scaffold"
"github.com/azure/azure-dev/cli/azd/internal/tracing"
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
Expand Down Expand Up @@ -176,7 +177,7 @@ func (i *Initializer) InitFromApp(
files, err := apphost.GenerateProjectArtifacts(
ctx,
azdCtx.ProjectDirectory(),
filepath.Base(azdCtx.ProjectDirectory()),
azdcontext.ProjectName(azdCtx.ProjectDirectory()),
appHostManifests[appHost.Path],
appHost.Path,
)
Expand Down Expand Up @@ -345,7 +346,7 @@ func prjConfigFromDetect(
root string,
detect detectConfirm) (project.ProjectConfig, error) {
config := project.ProjectConfig{
Name: LabelName(filepath.Base(root)),
Name: azdcontext.ProjectName(root),
Metadata: &project.ProjectMetadata{
Template: fmt.Sprintf("%s@%s", InitGenTemplateId, internal.VersionInfo().Version),
},
Expand Down Expand Up @@ -410,7 +411,7 @@ func prjConfigFromDetect(
if name == "." {
name = config.Name
}
name = LabelName(name)
name = names.LabelName(name)
config.Services[name] = &svc
}

Expand Down
3 changes: 2 additions & 1 deletion cli/azd/internal/repository/infra_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/azure/azure-dev/cli/azd/internal/appdetect"
"github.com/azure/azure-dev/cli/azd/internal/names"
"github.com/azure/azure-dev/cli/azd/internal/scaffold"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (i *Initializer) infraSpecFromDetect(
}

for _, svc := range detect.Services {
name := LabelName(filepath.Base(svc.Path))
name := names.LabelName(filepath.Base(svc.Path))
serviceSpec := scaffold.ServiceSpec{
Name: name,
Port: -1,
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/internal/repository/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (i *Initializer) writeFileSafe(
func (i *Initializer) writeCoreAssets(ctx context.Context, azdCtx *azdcontext.AzdContext) error {
// Check to see if `azure.yaml` exists, and if it doesn't, create it.
if _, err := os.Stat(azdCtx.ProjectPath()); errors.Is(err, os.ErrNotExist) {
_, err = project.New(ctx, azdCtx.ProjectPath(), azdCtx.GetDefaultProjectName())
_, err = project.New(ctx, azdCtx.ProjectPath(), azdcontext.ProjectName(azdCtx.ProjectDirectory()))
if err != nil {
return fmt.Errorf("failed to create a project file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/internal/repository/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func verifyFileContent(t *testing.T, file string, content string) {
}

func verifyProjectFile(t *testing.T, azdCtx *azdcontext.AzdContext, content string) {
content = strings.Replace(content, "<project>", azdCtx.GetDefaultProjectName(), 1)
content = strings.Replace(content, "<project>", azdcontext.ProjectName(azdCtx.ProjectDirectory()), 1)
verifyFileContent(t, azdCtx.ProjectPath(), content)

_, err := project.Load(context.Background(), azdCtx.ProjectPath())
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/internal/vsrpc/environment_service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *environmentService) CreateEnvironmentAsync(
return false, fmt.Errorf("reading app host manifest: %w", err)
}

projectName := strings.TrimSuffix(filepath.Base(c.azdContext.ProjectDirectory()), ".AppHost")
projectName := azdcontext.ProjectName(strings.TrimSuffix(c.azdContext.ProjectDirectory(), ".AppHost"))

// Write an azure.yaml file to the project.
files, err := apphost.GenerateProjectArtifacts(
Expand Down
6 changes: 4 additions & 2 deletions cli/azd/pkg/environment/azdcontext/azdcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/azure/azure-dev/cli/azd/internal/names"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
)

Expand Down Expand Up @@ -36,8 +37,9 @@ func (c *AzdContext) EnvironmentDirectory() string {
return filepath.Join(c.ProjectDirectory(), EnvironmentDirectoryName)
}

func (c *AzdContext) GetDefaultProjectName() string {
return filepath.Base(c.ProjectDirectory())
// ProjectName returns a suitable project name from the given project directory.
func ProjectName(projectDirectory string) string {
return names.LabelName(filepath.Base(projectDirectory))
}

func (c *AzdContext) EnvironmentRoot(name string) string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json

name: AspireAzdTests
name: aspire-azd-tests
services:
app:
language: dotnet
Expand Down

0 comments on commit 8fd80cb

Please sign in to comment.