Skip to content

Commit

Permalink
Fix .NET Core build script to build from intermediate directory if pr…
Browse files Browse the repository at this point in the history
…ovided (#162)
  • Loading branch information
kichalla authored May 21, 2019
1 parent 6b1d131 commit ffb5a35
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Extensions.Logging;
Expand All @@ -22,7 +23,7 @@ internal class DefaultAspNetCoreWebAppProjectFileProvider : IAspNetCoreWebAppPro

// Since this service is registered as a singleton, we can cache the lookup of project file.
private bool _probedForProjectFile;
private string _projectFile;
private string _projectFileRelativePath;

public DefaultAspNetCoreWebAppProjectFileProvider(
IOptions<DotnetCoreScriptGeneratorOptions> options,
Expand All @@ -32,49 +33,47 @@ public DefaultAspNetCoreWebAppProjectFileProvider(
_logger = logger;
}

public string GetProjectFile(ISourceRepo sourceRepo)
public string GetRelativePathToProjectFile(ISourceRepo sourceRepo)
{
if (_probedForProjectFile)
{
return _projectFile;
return _projectFileRelativePath;
}

var projectEnvVariablePath = _options.Project;

string projectFile = null;
if (string.IsNullOrEmpty(projectEnvVariablePath))
if (!string.IsNullOrEmpty(projectEnvVariablePath))
{
// Check if root of the repo has a .csproj or a .fsproj file
projectFile = GetProjectFileAtRoot(sourceRepo, DotnetCoreConstants.CSharpProjectFileExtension) ??
GetProjectFileAtRoot(sourceRepo, DotnetCoreConstants.FSharpProjectFileExtension);
}
else
{
projectEnvVariablePath = projectEnvVariablePath.Trim();
projectFile = Path.Combine(sourceRepo.RootPath, projectEnvVariablePath);
var projectFileWithRelativePath = projectEnvVariablePath.Trim();
projectFile = Path.Combine(sourceRepo.RootPath, projectFileWithRelativePath);
if (!sourceRepo.FileExists(projectFile))
{
_logger.LogWarning($"Could not find the project file '{projectFile}'.");
throw new InvalidUsageException(
$"Could not find the project file '{projectFile}' specified by the environment variable" +
$" '{EnvironmentSettingsKeys.Project}' with value '{projectEnvVariablePath}'. " +
$" '{EnvironmentSettingsKeys.Project}' with value '{projectFileWithRelativePath}'. " +
"Make sure the path to the project file is relative to the root of the repo. " +
"For example: PROJECT=src/Dashboard/Dashboard.csproj");
}

// NOTE: Do not check if the project file specified by the end user is a web application since this
// can be a escape hatch for end users if our logic to determine a web app is incorrect.
return projectFile;
return projectFileWithRelativePath;
}

// Check if root of the repo has a .csproj or a .fsproj file
projectFile = GetProjectFileAtRoot(sourceRepo, DotnetCoreConstants.CSharpProjectFileExtension) ??
GetProjectFileAtRoot(sourceRepo, DotnetCoreConstants.FSharpProjectFileExtension);

if (projectFile != null)
{
if (!IsAspNetCoreWebApplicationProject(sourceRepo, projectFile))
{
return null;
}

return projectFile;
return new FileInfo(projectFile).Name;
}

// Check if any of the sub-directories has a .csproj file and if that .csproj file has references
Expand Down Expand Up @@ -138,9 +137,8 @@ public string GetProjectFile(ISourceRepo sourceRepo)

// Cache the results
_probedForProjectFile = true;
_projectFile = projectFile;

return _projectFile;
_projectFileRelativePath = GetRelativePathToRoot(projectFile, sourceRepo.RootPath);
return _projectFileRelativePath;
}

// To enable unit testing
Expand Down Expand Up @@ -171,6 +169,25 @@ internal static bool IsAspNetCoreWebApplicationProject(XDocument projectFileDoc)
return string.Equals(sdkName, expectedWebSdkName, StringComparison.OrdinalIgnoreCase);
}

// To enable unit testing
internal static string GetRelativePathToRoot(string projectFilePath, string repoRoot)
{
var repoRootDir = new DirectoryInfo(repoRoot);
var projectFileInfo = new FileInfo(projectFilePath);
var currDir = projectFileInfo.Directory;
var parts = new List<string>();
parts.Add(projectFileInfo.Name);

// Since directory names are case sensitive on non-Windows OSes, try not to use ignore case
while (!string.Equals(currDir.FullName, repoRootDir.FullName, StringComparison.Ordinal))
{
parts.Insert(0, currDir.Name);
currDir = currDir.Parent;
}

return Path.Combine(parts.ToArray());
}

private static IEnumerable<string> GetAllProjectFilesInRepo(
ISourceRepo sourceRepo,
string projectFileExtension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function publishToDirectory()
{
local directoryToPublishTo="$1"
echo
echo "Publishing to directory '$directoryToPublishTo'..."
echo "Publishing '{{ ProjectFile }}' to directory '$directoryToPublishTo'..."
echo
dotnet publish "{{ ProjectFile }}" -c {{ Configuration }} -o "$directoryToPublishTo"
}
Expand Down Expand Up @@ -105,7 +105,7 @@ echo ".NET Core Version: $dotnetCoreVersion"
cd "$SOURCE_DIR"

echo
echo Restoring packages ...
echo "Restoring packages for '{{ ProjectFile }}'..."
echo
dotnet restore "{{ ProjectFile }}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DotnetCoreLanguageDetector(

public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
{
var projectFile = _aspNetCoreWebAppProjectFileProvider.GetProjectFile(sourceRepo);
var projectFile = _aspNetCoreWebAppProjectFileProvider.GetRelativePathToProjectFile(sourceRepo);
if (string.IsNullOrEmpty(projectFile))
{
return null;
Expand Down
6 changes: 2 additions & 4 deletions src/BuildScriptGenerator/DotNetCore/DotnetCorePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,13 @@ private static bool ShouldZipAllOutput(BuildScriptGeneratorContext context)

private (string projFile, string publishDir) GetProjectFileAndPublishDir(ISourceRepo repo)
{
var projectFile = _aspNetCoreWebAppProjectFileProvider.GetProjectFile(repo);
var projectFile = _aspNetCoreWebAppProjectFileProvider.GetRelativePathToProjectFile(repo);
if (string.IsNullOrEmpty(projectFile))
{
return (null, null);
}

var publishDir = Path.Combine(
new FileInfo(projectFile).Directory.FullName,
DotnetCoreConstants.OryxOutputPublishDirectory);
var publishDir = Path.Combine(repo.RootPath, DotnetCoreConstants.OryxOutputPublishDirectory);
return (projectFile, publishDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace Microsoft.Oryx.BuildScriptGenerator.DotNetCore
/// </summary>
public interface IAspNetCoreWebAppProjectFileProvider
{
string GetProjectFile(ISourceRepo sourceRepo);
string GetRelativePathToProjectFile(ISourceRepo sourceRepo);
}
}
Loading

0 comments on commit ffb5a35

Please sign in to comment.