// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace AWS.Deploy.Common
{
///
/// Models metadata about a parsed .csproj or .fsproj project.
/// Use to build
///
public class ProjectDefinition
{
///
/// The name of the project
///
public string ProjectName => GetProjectName();
///
/// Xml file contents of the Project file.
///
public XmlDocument Contents { get; set; }
///
/// Full path to the project file
///
public string ProjectPath { get; set; }
///
/// The Solution file path of the project.
///
public string ProjectSolutionPath { get;set; }
///
/// Value of the Sdk property of the root project element in a .csproj
///
public string SdkType { get; set; }
///
/// Value of the TargetFramework property of the project
///
public string? TargetFramework { get; set; }
///
/// Value of the AssemblyName property of the project
///
public string? AssemblyName { get; set; }
///
/// True if we found a docker file corresponding to the .csproj
///
public bool HasDockerFile => CheckIfDockerFileExists(ProjectPath);
public ProjectDefinition(
XmlDocument contents,
string projectPath,
string projectSolutionPath,
string sdkType)
{
Contents = contents;
ProjectPath = projectPath;
ProjectSolutionPath = projectSolutionPath;
SdkType = sdkType;
}
public string? GetMSPropertyValue(string? propertyName)
{
if (string.IsNullOrEmpty(propertyName))
return null;
var propertyValue = Contents.SelectSingleNode($"//PropertyGroup/{propertyName}")?.InnerText;
return propertyValue;
}
public string? GetPackageReferenceVersion(string? packageName)
{
if (string.IsNullOrEmpty(packageName))
return null;
var packageReference = Contents.SelectSingleNode($"//ItemGroup/PackageReference[@Include='{packageName}']") as XmlElement;
return packageReference?.GetAttribute("Version");
}
private bool CheckIfDockerFileExists(string projectPath)
{
var dir = Directory.GetFiles(new FileInfo(projectPath).DirectoryName ??
throw new InvalidProjectPathException(DeployToolErrorCode.ProjectPathNotFound, "The project path is invalid."), Constants.Docker.DefaultDockerfileName);
return dir.Length == 1;
}
private string GetProjectName()
{
if (string.IsNullOrEmpty(ProjectPath))
return string.Empty;
return Path.GetFileNameWithoutExtension(ProjectPath);
}
}
}