Skip to content

Commit

Permalink
DotnetCore Runtime: Use startup file name from build manifest file if…
Browse files Browse the repository at this point in the history
… present (#72)
  • Loading branch information
kichalla authored Apr 16, 2019
1 parent 8548f18 commit 4fec936
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/BuildScriptGenerator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public static class Constants
{
public const string OryxEnvironmentSettingNamePrefix = "ORYX_";
public const string BuildEnvironmentFileName = "build.env";
internal const string ManifestFileName = "oryx-manifest.toml";
public const string ManifestFileName = "oryx-manifest.toml";
}
}
1 change: 1 addition & 0 deletions src/BuildScriptGenerator/DotNetCore/DotnetCoreConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public static class DotnetCoreConstants
public const string AspNetCoreAppPackageReference = "Microsoft.AspNetCore.App";

public const string ProjectFileLanguageDetectorProperty = "ProjectFile";
public const string StartupFileName = "startupFileName";
}
}
25 changes: 22 additions & 3 deletions src/BuildScriptGenerator/DotNetCore/DotnetCorePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Extensions.Logging;

namespace Microsoft.Oryx.BuildScriptGenerator.DotNetCore
Expand Down Expand Up @@ -42,21 +44,38 @@ public LanguageDetectorResult Detect(ISourceRepo sourceRepo)
return _detector.Detect(sourceRepo);
}

public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext scriptGeneratorContext)
public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
{
(string projectFile, string publishDir) = GetProjectFileAndPublishDir(scriptGeneratorContext.SourceRepo);
var buildProperties = new Dictionary<string, string>();
(string projectFile, string publishDir) = GetProjectFileAndPublishDir(context.SourceRepo);
if (string.IsNullOrEmpty(projectFile) || string.IsNullOrEmpty(publishDir))
{
return null;
}

string startupFileName = null;
var projectFileContent = context.SourceRepo.ReadFile(projectFile);
var projFileDoc = XDocument.Load(new StringReader(projectFileContent));
var assemblyNameElement = projFileDoc.XPathSelectElement("/Project/PropertyGroup/AssemblyName");
if (assemblyNameElement == null)
{
var name = Path.GetFileNameWithoutExtension(projectFile);
startupFileName = $"{name}.dll";
}
else
{
startupFileName = $"{assemblyNameElement.Value}.dll";
}

buildProperties[DotnetCoreConstants.StartupFileName] = startupFileName;

var props = new DotNetCoreBashBuildSnippetProperties
{
ProjectFile = projectFile,
PublishDirectory = publishDir
};
string script = TemplateHelpers.Render(TemplateHelpers.TemplateResource.DotNetCoreSnippet, props, _logger);
return new BuildScriptSnippet { BashBuildScriptSnippet = script };
return new BuildScriptSnippet { BashBuildScriptSnippet = script, BuildProperties = buildProperties };
}

public bool IsCleanRepo(ISourceRepo repo)
Expand Down
36 changes: 36 additions & 0 deletions src/startupscriptgenerator/common/buildManifestHelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// --------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// --------------------------------------------------------------------------------------------

package common

import (
"github.com/BurntSushi/toml"
"log"
"path/filepath"
)

type BuildManifest struct {
StartupFileName string
}

const ManifestFileName = "oryx-manifest.toml"

func DeserializeBuildManifest(manifestFile string) BuildManifest {
var manifest BuildManifest
if _, err := toml.DecodeFile(manifestFile, &manifest); err != nil {
log.Fatal(err)
}
return manifest
}

func GetBuildManifest(appPath string) BuildManifest {
buildManifest := BuildManifest{}

tomlFile := filepath.Join(appPath, ManifestFileName)
if FileExists(tomlFile) {
buildManifest = DeserializeBuildManifest(tomlFile)
}
return buildManifest
}
44 changes: 20 additions & 24 deletions src/startupscriptgenerator/dotnetcore/scriptgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
)

type DotnetCoreStartupScriptGenerator struct {
SourcePath string
AppPath string
UserStartupCommand string
DefaultAppFilePath string
BindPort string
SourcePath string
AppPath string
UserStartupCommand string
DefaultAppFilePath string
BindPort string
}

type projectDetails struct {
Expand Down Expand Up @@ -51,10 +51,7 @@ type packageReference struct {
const ProjectEnvironmentVariableName = "PROJECT"
const DefaultBindPort = "8080"

var _retrievedProjectDetails = false
var _gotStartupFileName = false
var _projDetails projectDetails = projectDetails{}
var _startupFileName = ""

func (gen *DotnetCoreStartupScriptGenerator) GenerateEntrypointScript() string {
logger := common.GetLogger("dotnetcore.scriptgenerator.GenerateEntrypointScript")
Expand Down Expand Up @@ -124,11 +121,22 @@ func (gen *DotnetCoreStartupScriptGenerator) getStartupCommand() string {
func (gen *DotnetCoreStartupScriptGenerator) getStartupDllFileName() string {
logger := common.GetLogger("dotnetcore.scriptgenerator.getStartupDllFileName")

if _gotStartupFileName {
return _startupFileName
manifestFilePath := filepath.Join(gen.AppPath, common.ManifestFileName)
if common.FileExists(manifestFilePath) {
logger.LogInformation("Found build manifest file at '%s'.", manifestFilePath)
buildManifest := common.GetBuildManifest(gen.AppPath)
if buildManifest.StartupFileName == "" {
logger.LogInformation(
"Found build manifest file at '%s', but startup file name property is empty.",
manifestFilePath)
} else {
return buildManifest.StartupFileName
}
} else {
logger.LogInformation("Cound not find build manifest file at '%s'.", manifestFilePath)
}

projDetails := gen.getProjectDetailsAndCache()
projDetails := gen.getProjectDetails()
if projDetails.FullPath == "" {
logger.LogError("Could not find the project file.")
return ""
Expand All @@ -149,19 +157,7 @@ func (gen *DotnetCoreStartupScriptGenerator) getStartupDllFileName() string {
} else {
startupFileName = assemblyName + ".dll"
}
_startupFileName = startupFileName
_gotStartupFileName = true
return _startupFileName
}
func (gen *DotnetCoreStartupScriptGenerator) getProjectDetailsAndCache() projectDetails {
if _retrievedProjectDetails {
return _projDetails
}

projDetails := gen.getProjectDetails()
_retrievedProjectDetails = true
_projDetails = projDetails
return _projDetails
return startupFileName
}

func (gen *DotnetCoreStartupScriptGenerator) getProjectDetails() projectDetails {
Expand Down
5 changes: 5 additions & 0 deletions tests/Oryx.BuildImage.Tests/DotNetCoreSampleAppsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Oryx.Tests.Common;
using Xunit;
using Xunit.Abstractions;
using ScriptGenerator=Microsoft.Oryx.BuildScriptGenerator;

namespace Microsoft.Oryx.BuildImage.Tests
{
Expand All @@ -34,6 +35,10 @@ public void Builds_NetCore11App_UsingNetCore11_DotnetSdkVersion()
var script = new ShellScriptBuilder()
.AddBuildCommand($"{appDir} -o {appOutputDir}")
.AddFileExistsCheck($"{appOutputDir}/{appName}.dll")
.AddFileExistsCheck($"{appOutputDir}/{ScriptGenerator.Constants.ManifestFileName}")
.AddStringExistsInFileCheck(
$"{DotnetCoreConstants.StartupFileName}=\"{appName}.dll\"",
$"{appOutputDir}/{ScriptGenerator.Constants.ManifestFileName}")
.ToString();

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

using System.IO;
using System.Threading.Tasks;
using Microsoft.Oryx.BuildScriptGenerator.DotNetCore;
using Microsoft.Oryx.Tests.Common;
using Xunit;
using Xunit.Abstractions;
using ScriptGenerator = Microsoft.Oryx.BuildScriptGenerator;

namespace Microsoft.Oryx.Integration.Tests.LocalDockerTests
{
Expand Down Expand Up @@ -89,6 +91,9 @@ public async Task CanBuildAndRun_NetCore11WebApp_HavingExplicitAssemblyName()
var appOutputDir = $"{appDir}/myoutputdir";
var buildImageScript = new ShellScriptBuilder()
.AddCommand($"oryx build {appDir} -l dotnet --language-version {dotnetcoreVersion} -o {appOutputDir}")
.AddStringExistsInFileCheck(
$"{DotnetCoreConstants.StartupFileName}=\"Yoyo.dll\"",
$"{appOutputDir}/{ScriptGenerator.Constants.ManifestFileName}")
.ToString();
var runtimeImageScript = new ShellScriptBuilder()
.AddCommand(
Expand Down Expand Up @@ -223,7 +228,10 @@ public async Task CanBuildAndRun_NetCore21WebApp_HavingExplicitAssemblyName()
var appOutputDir = $"{appDir}/myoutputdir";
var buildImageScript = new ShellScriptBuilder()
.AddCommand($"oryx build {appDir} -l dotnet --language-version {dotnetcoreVersion} -o {appOutputDir}")
.ToString();
.AddStringExistsInFileCheck(
$"{DotnetCoreConstants.StartupFileName}=\"Yoyo.dll\"",
$"{appOutputDir}/{ScriptGenerator.Constants.ManifestFileName}")
.ToString();
var runtimeImageScript = new ShellScriptBuilder()
.AddCommand(
$"oryx -appPath {appOutputDir} -sourcePath {appDir} -bindPort {ContainerPort}")
Expand Down Expand Up @@ -501,6 +509,9 @@ public async Task CanBuildAndRun_NetCore21WebApp_HavingNestedProjectDirectory_Wh
var buildImageScript = new ShellScriptBuilder()
.AddCommand(setProjectEnvVariable)
.AddCommand($"oryx build {repoDir} -o {appOutputDir}") // Do not specify language and version
.AddStringExistsInFileCheck(
$"{DotnetCoreConstants.StartupFileName}=\"WebApp1.dll\"",
$"{appOutputDir}/{ScriptGenerator.Constants.ManifestFileName}")
.ToString();
var runtimeImageScript = new ShellScriptBuilder()
.AddCommand(setProjectEnvVariable)
Expand Down Expand Up @@ -535,7 +546,7 @@ await EndToEndTestHelper.BuildRunAndAssertAppAsync(
}

[Fact]
public async Task CanBuildAndRun_NetCore21WebApp_HavingNestedProjectDirectory()
public async Task CanBuildAndRun_NetCore21WebApp_HavingMultipleProjects()
{
// Arrange
var appName = "MultiWebAppRepo";
Expand All @@ -549,6 +560,9 @@ public async Task CanBuildAndRun_NetCore21WebApp_HavingNestedProjectDirectory()
var buildImageScript = new ShellScriptBuilder()
.AddCommand(setProjectEnvVariable)
.AddCommand($"oryx build {repoDir} -o {appOutputDir} -l dotnet --language-version {dotnetcoreVersion}")
.AddStringExistsInFileCheck(
$"{DotnetCoreConstants.StartupFileName}=\"WebApp1.dll\"",
$"{appOutputDir}/{ScriptGenerator.Constants.ManifestFileName}")
.ToString();
var runtimeImageScript = new ShellScriptBuilder()
.AddCommand(setProjectEnvVariable)
Expand Down
Loading

0 comments on commit 4fec936

Please sign in to comment.