Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Adding %build.Configuration% and %build.TargetFramework% script varia…
Browse files Browse the repository at this point in the history
…bles

Fixes #887
  • Loading branch information
moozzyk committed Sep 11, 2015
1 parent d3d06c7 commit f53b6bf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
32 changes: 22 additions & 10 deletions src/Microsoft.Dnx.Tooling/Building/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,17 @@ private bool BuildConfiguration(string baseOutputPath, IEnumerable<FrameworkName
InstallBuilder installBuilder = null;
SourceBuilder sourceBuilder = null;

var contextVariables = new Dictionary<string, string>
{
{ "project:BuildOutputDir", GetBuildOutputDir(_currentProject) },
{ "build:Configuration", configuration }
};

var getScriptVariable = GetScriptVariable(contextVariables);

if (_buildOptions.GeneratePackages)
{
if(!ScriptExecutor.Execute(_currentProject, "prepack", GetScriptVariable))
if(!ScriptExecutor.Execute(_currentProject, "prepack", getScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
return false;
Expand All @@ -188,7 +196,9 @@ private bool BuildConfiguration(string baseOutputPath, IEnumerable<FrameworkName
_buildOptions.Reports.Information.WriteLine("Building {0} for {1}",
_currentProject.Name, targetFramework.ToString().Yellow().Bold());

if (!ScriptExecutor.Execute(_currentProject, "prebuild", GetScriptVariable))
contextVariables["build:TargetFramework"] = VersionUtility.GetShortFrameworkName(targetFramework);

if (!ScriptExecutor.Execute(_currentProject, "prebuild", getScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
success = false;
Expand Down Expand Up @@ -220,7 +230,7 @@ private bool BuildConfiguration(string baseOutputPath, IEnumerable<FrameworkName
context.AddLibs(packageBuilder, "*.resources.dll", recursiveSearch: true);
}

if (!ScriptExecutor.Execute(_currentProject, "postbuild", GetScriptVariable))
if (!ScriptExecutor.Execute(_currentProject, "postbuild", getScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
success = false;
Expand All @@ -230,6 +240,8 @@ private bool BuildConfiguration(string baseOutputPath, IEnumerable<FrameworkName
allDiagnostics.AddRange(diagnostics);

WriteDiagnostics(diagnostics);

contextVariables.Remove("build:TargetFramework");
}

if (_buildOptions.GeneratePackages)
Expand All @@ -250,7 +262,7 @@ private bool BuildConfiguration(string baseOutputPath, IEnumerable<FrameworkName

if (success)
{
if (!ScriptExecutor.Execute(_currentProject, "postpack", GetScriptVariable))
if (!ScriptExecutor.Execute(_currentProject, "postpack", getScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
return false;
Expand Down Expand Up @@ -403,14 +415,14 @@ internal static IEnumerable<PhysicalPackageFile> CollectAdditionalFiles(Director
}
}

private string GetScriptVariable(string key)
private Func<string, string> GetScriptVariable(IDictionary<string, string> contextVariables)
{
if (string.Equals("project:BuildOutputDir", key, StringComparison.OrdinalIgnoreCase))
return key =>
{
return GetBuildOutputDir(_currentProject);
}

return null;
string value;
contextVariables.TryGetValue(key, out value);
return value;
};
}

private static void InitializeBuilder(Runtime.Project project, PackageBuilder builder)
Expand Down
39 changes: 19 additions & 20 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/DnuPackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ public void DnuPack_ExecutesScriptsForEachConfigurationAndTargetFramework(string
}
},
""scripts"": {
""prebuild"": ""echo PREBUILD_SCRIPT_OUTPUT"",
""prepack"": ""echo PREPACK_SCRIPT_OUTPUT"",
""postbuild"": ""echo POSTBUILD_SCRIPT_OUTPUT"",
""postpack"": ""echo POSTPACK_SCRIPT_OUTPUT""
""prebuild"": ""echo PREBUILD_SCRIPT_OUTPUT %build:Configuration% %build:TargetFramework%"",
""prepack"": ""echo PREPACK_SCRIPT_OUTPUT %build:Configuration% %build:TargetFramework%"",
""postbuild"": ""echo POSTBUILD_SCRIPT_OUTPUT %build:Configuration% %build:TargetFramework%"",
""postpack"": ""echo POSTPACK_SCRIPT_OUTPUT %build:Configuration% %build:TargetFramework%""
}
}")
.WriteTo(testEnv.ProjectPath);
Expand All @@ -418,27 +418,26 @@ public void DnuPack_ExecutesScriptsForEachConfigurationAndTargetFramework(string
Assert.Empty(stdErr);

var idx = 0;
for (var i = 0; i < 2; i++)
foreach (var configuration in new[] { "Debug", "Release"})
{
idx = stdOut.IndexOf("PREPACK_SCRIPT_OUTPUT", idx);
// note that %TargetFramework% is not defined outside build
idx = stdOut.IndexOf($"PREPACK_SCRIPT_OUTPUT {configuration} %build:TargetFramework%", 0);
Assert.True(idx >= 0);
idx = stdOut.IndexOf("PREBUILD_SCRIPT_OUTPUT", idx);
Assert.True(idx >= 0);
idx = stdOut.IndexOf("POSTBUILD_SCRIPT_OUTPUT", idx);
Assert.True(idx >= 0);
idx = stdOut.IndexOf("PREBUILD_SCRIPT_OUTPUT", idx);
Assert.True(idx >= 0);
idx = stdOut.IndexOf("POSTBUILD_SCRIPT_OUTPUT", idx);
Assert.True(idx >= 0);
idx = stdOut.IndexOf("POSTPACK_SCRIPT_OUTPUT", idx);
foreach (var framework in new[] { "dnx451", "dnxcore50" })
{
idx = stdOut.IndexOf($"PREBUILD_SCRIPT_OUTPUT {configuration} {framework}", idx);
Assert.True(idx >= 0);
idx = stdOut.IndexOf($"POSTBUILD_SCRIPT_OUTPUT {configuration} {framework}", idx);
Assert.True(idx >= 0);
}
idx = stdOut.IndexOf($"POSTPACK_SCRIPT_OUTPUT {configuration} %build:TargetFramework%", 0);
Assert.True(idx >= 0);
idx += "POSTPACK_SCRIPT_OUTPUT".Length;
}

Assert.Equal(-1, stdOut.IndexOf("PREPACK_SCRIPT_OUTPUT", idx));
Assert.Equal(-1, stdOut.IndexOf("PREBUILD_SCRIPT_OUTPUT", idx));
Assert.Equal(-1, stdOut.IndexOf("POSTBUILD_SCRIPT_OUTPUT", idx));
Assert.Equal(-1, stdOut.IndexOf("POSTPACK_SCRIPT_OUTPUT", idx));
Assert.Equal(-1, stdOut.IndexOf("PREPACK_SCRIPT_OUTPUT", idx + 1));
Assert.Equal(-1, stdOut.IndexOf("PREBUILD_SCRIPT_OUTPUT", idx + 1));
Assert.Equal(-1, stdOut.IndexOf("POSTBUILD_SCRIPT_OUTPUT", idx + 1));
Assert.Equal(-1, stdOut.IndexOf("POSTPACK_SCRIPT_OUTPUT", idx + 1));
}
}

Expand Down

0 comments on commit f53b6bf

Please sign in to comment.