Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Pass the working directory through everywhere instead of using Enviro…
Browse files Browse the repository at this point in the history
…nment.CurrentDirectory (Protobuild#210)
  • Loading branch information
hach-que authored Jan 26, 2017
1 parent 1f675f0 commit 0798cce
Show file tree
Hide file tree
Showing 89 changed files with 355 additions and 324 deletions.
2 changes: 0 additions & 2 deletions Build/Projects/Protobuild.Internal.definition
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@
<Compile Include="Core\ILogger.cs" />
<Compile Include="Core\IModuleExecution.cs" />
<Compile Include="Core\IModuleUtilities.cs" />
<Compile Include="Core\IWorkingDirectoryProvider.cs" />
<Compile Include="Core\Logger.cs" />
<Compile Include="Core\ModuleExecution.cs" />
<Compile Include="Core\ModuleUtilities.cs" />
<Compile Include="Core\WorkingDirectoryProvider.cs" />
<Compile Include="DefinitionInfo.cs" />
<Compile Include="DI\LightweightKernel.cs" />
<Compile Include="DI\LightweightKernelModule.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Protobuild.Debug/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void Main(string[] args)

ExecEnvironment.RunProtobuildInProcess = true;
ExecEnvironment.DoNotWrapExecutionInTry = true;
Protobuild.MainClass.Main(args);
Protobuild.MainClass.Main(Environment.CurrentDirectory, args);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public void GenerationIsCorrect()

this.Generate(args: "--redirect http://protobuild.org/hach-que/TestEmptyPackage local-git://" + src);

_assert.False(File.Exists(this.GetPath("Package\\PackageLibrary\\PackageLibrary.Windows.csproj")));
_assert.False(File.Exists(this.GetPath("Package\\PackageLibrary\\PackageLibrary.Windows.csproj")), "Package\\PackageLibrary\\PackageLibrary.Windows.csproj does not exist");
_assert.True(
File.Exists(this.GetPath("SubmoduleA\\Package\\PackageLibrary\\PackageLibrary.Windows.csproj")));
File.Exists(this.GetPath("SubmoduleA\\Package\\PackageLibrary\\PackageLibrary.Windows.csproj")), "SubmoduleA\\Package\\PackageLibrary\\PackageLibrary.Windows.csproj exists");
_assert.False(
File.Exists(this.GetPath("SubmoduleB\\Package\\PackageLibrary\\PackageLibrary.Windows.csproj")));
_assert.False(File.Exists(this.GetPath("SubmoduleA\\Package\\.redirect")));
_assert.True(File.Exists(this.GetPath("SubmoduleB\\Package\\.redirect")));
_assert.True(File.Exists(this.GetPath("SubmoduleA\\LibraryA\\LibraryA.Windows.csproj")));
_assert.True(File.Exists(this.GetPath("SubmoduleB\\LibraryB\\LibraryB.Windows.csproj")));
File.Exists(this.GetPath("SubmoduleB\\Package\\PackageLibrary\\PackageLibrary.Windows.csproj")), "SubmoduleB\\Package\\PackageLibrary\\PackageLibrary.Windows.csproj exists");
_assert.False(File.Exists(this.GetPath("SubmoduleA\\Package\\.redirect")), "SubmoduleA\\Package\\.redirect exists");
_assert.True(File.Exists(this.GetPath("SubmoduleB\\Package\\.redirect")), "SubmoduleB\\Package\\.redirect does not exist");
_assert.True(File.Exists(this.GetPath("SubmoduleA\\LibraryA\\LibraryA.Windows.csproj")), "SubmoduleA\\LibraryA\\LibraryA.Windows.csproj does not exist");
_assert.True(File.Exists(this.GetPath("SubmoduleB\\LibraryB\\LibraryB.Windows.csproj")), "SubmoduleB\\LibraryB\\LibraryB.Windows.csproj does not exist");

var libraryAContents = this.ReadFile("SubmoduleA\\LibraryA\\LibraryA.Windows.csproj");
var libraryBContents = this.ReadFile("SubmoduleB\\LibraryB\\LibraryB.Windows.csproj");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public AutomatedBuildController(IAutomatedBuildRuntimeV1 automatedBuildRuntimeV1
_automatedBuildRuntimeV1 = automatedBuildRuntimeV1;
}

public int Execute(string path)
public int Execute(string workingDirectory, string path)
{
IAutomatedBuildRuntime runtime = null;
string script = null;
Expand Down Expand Up @@ -45,7 +45,7 @@ public int Execute(string path)
}
try
{
return runtime.Execute(handle);
return runtime.Execute(workingDirectory, handle);
}
catch (Exception ex)
{
Expand Down
10 changes: 3 additions & 7 deletions Protobuild.Internal/AutomatedBuild/AutomatedBuildRuntimeV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ namespace Protobuild
internal class AutomatedBuildRuntimeV1 : IAutomatedBuildRuntimeV1
{
private readonly IHostPlatformDetector _hostPlatformDetector;
private readonly IWorkingDirectoryProvider _workingDirectoryProvider;

public AutomatedBuildRuntimeV1(IHostPlatformDetector hostPlatformDetector,
IWorkingDirectoryProvider workingDirectoryProvider)
public AutomatedBuildRuntimeV1(IHostPlatformDetector hostPlatformDetector)
{
_hostPlatformDetector = hostPlatformDetector;
_workingDirectoryProvider = workingDirectoryProvider;
}

public object Parse(string text)
Expand Down Expand Up @@ -213,12 +210,11 @@ public object Parse(string text)
return instructions;
}

public int Execute(object handle)
public int Execute(string workingDirectory, object handle)
{
var instructions = (List<ParsedInstruction>) handle;

var protobuild = Assembly.GetEntryAssembly().Location;
var workingDirectory = _workingDirectoryProvider.GetPath();

var targets = string.Empty;
var buildTarget = string.Empty;
Expand Down Expand Up @@ -423,7 +419,7 @@ public int Execute(object handle)
try
{
RedirectableConsole.WriteLine("+ git rev-parse HEAD");
commit = GitUtils.RunGitAndCapture(workingDirectory, "rev-parse HEAD").Trim();
commit = GitUtils.RunGitAndCapture(workingDirectory, null, "rev-parse HEAD").Trim();
}
catch (InvalidOperationException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
internal interface IAutomatedBuildController
{
int Execute(string path);
int Execute(string workingDirectory, string path);
}
}
10 changes: 2 additions & 8 deletions Protobuild.Internal/AutomatedBuild/IAutomatedBuildRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Protobuild
namespace Protobuild
{
internal interface IAutomatedBuildRuntime
{
object Parse(string text);
int Execute(object handle);
int Execute(string workingDirectory, object handle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
<xsl:attribute name="file">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$source_project/@Path,
'\',
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions Protobuild.Internal/BuildResources/GenerateProject.CSharp.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$project_path,
'\',
Expand All @@ -511,6 +512,7 @@
<xsl:text>exists('</xsl:text>
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$project_path,
'\',
Expand Down Expand Up @@ -699,6 +701,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$source_project/@Path,
'\',
Expand Down Expand Up @@ -738,6 +741,7 @@
<HintPath>
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$source_project/@Path,
'\',
Expand Down Expand Up @@ -772,6 +776,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$source_project/@Path,
'\',
Expand Down Expand Up @@ -808,6 +813,7 @@
<HintPath>
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$source_project/@Path,
'\',
Expand Down Expand Up @@ -890,6 +896,7 @@
namespace="http://schemas.microsoft.com/developer/msbuild/2003">
<xsl:attribute name="Include">
<xsl:value-of select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$root/Input/Generation/RootPath,
$target_project/@Path,
Expand Down Expand Up @@ -1646,6 +1653,7 @@
<HintPath>
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$project/@Path,
'\',
Expand Down Expand Up @@ -1976,6 +1984,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$root/Input/Generation/RootPath,
$project/@Path,
Expand Down Expand Up @@ -2005,6 +2014,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$root/Input/Generation/RootPath,
$project/@Path,
Expand All @@ -2028,6 +2038,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$root/Input/Generation/RootPath,
$project/@Path,
Expand All @@ -2050,6 +2061,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$root/Input/Generation/RootPath,
$project/@Path,
Expand All @@ -2073,6 +2085,7 @@
<xsl:attribute name="Include">
<xsl:value-of
select="user:GetRelativePath(
$root/Input/Generation/WorkingDirectory,
concat(
$root/Input/Generation/RootPath,
$project/@Path,
Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions Protobuild.Internal/BuildResources/GenerationFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public string NormalizeXAPName(string origName)
/// <param name="from">The absolute directory to calculate from.</param>
/// <param name="to">The absolute directory that is the target.</param>
/// <returns>A relative path from one directory to another.</returns>
public string GetRelativePath(string from, string to)
public string GetRelativePath(string workingDirectory, string from, string to)
{
try
{
var current = Environment.CurrentDirectory;
var current = workingDirectory;
from = System.IO.Path.Combine(current, from.Replace('\\', '/'));
to = System.IO.Path.Combine(current, to.Replace('\\', '/'));
return (new Uri(from).MakeRelativeUri(new Uri(to)))
Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions Protobuild.Internal/BuildResources/IResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Protobuild
{
internal interface IResourceProvider
{
XslCompiledTransform LoadXSLT(ResourceType resourceType, Language language, string platform);
XslCompiledTransform LoadXSLT(string workingDirectory, ResourceType resourceType, Language language, string platform);

XmlDocument LoadXML(ResourceType resourceType, Language language, string platform);
XmlDocument LoadXML(string workingDirectory, ResourceType resourceType, Language language, string platform);
}
}

33 changes: 17 additions & 16 deletions Protobuild.Internal/BuildResources/ResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ internal class ResourceProvider : IResourceProvider
{
private readonly ILanguageStringProvider m_LanguageStringProvider;

private readonly IWorkingDirectoryProvider m_WorkingDirectoryProvider;

private readonly IGenerationFunctionsProvider _generationFunctionsProvider;

private static Dictionary<string, Dictionary<int, XslCompiledTransform>> m_CachedTransforms = new Dictionary<string, Dictionary<int, XslCompiledTransform>>();

private static object m_CachedTransformsLock = new object();

public ResourceProvider(
ILanguageStringProvider languageStringProvider,
IWorkingDirectoryProvider workingDirectoryProvider,
IGenerationFunctionsProvider generationFunctionsProvider)
{
this.m_LanguageStringProvider = languageStringProvider;
this.m_WorkingDirectoryProvider = workingDirectoryProvider;
_generationFunctionsProvider = generationFunctionsProvider;
}

Expand Down Expand Up @@ -54,7 +52,7 @@ private string GetResourceExtension(ResourceType resourceType)
throw new InvalidOperationException();
}

private Stream LoadOverriddableResource(ResourceType resourceType, Language language, string platform, out string loadHash)
private Stream LoadOverriddableResource(string workingDirectory, ResourceType resourceType, Language language, string platform, out string loadHash)
{
loadHash = string.Empty;
string name = null;
Expand Down Expand Up @@ -136,6 +134,7 @@ private Stream LoadOverriddableResource(ResourceType resourceType, Language lang
if (File.Exists(path))
{
loadHash = "path:" + path;
RedirectableConsole.WriteLine("Loaded XSLT from global path: " + path);
source = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
break;
}
Expand All @@ -146,11 +145,12 @@ private Stream LoadOverriddableResource(ResourceType resourceType, Language lang
{
foreach (var filename in onDiskNames)
{
var path = Path.Combine(this.m_WorkingDirectoryProvider.GetPath(), "Build", filename);
var path = Path.Combine(workingDirectory, "Build", filename);

if (File.Exists(path))
{
loadHash = "path:" + path;
RedirectableConsole.WriteLine("Loaded XSLT from Build folder: " + path);
source = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
break;
}
Expand All @@ -176,6 +176,7 @@ private Stream LoadOverriddableResource(ResourceType resourceType, Language lang
}
}
loadHash = "embedded:" + embeddedName;
RedirectableConsole.WriteLine("Loaded XSLT from Protobuild");
source = this.GetTransparentDecompressionStream(embeddedStream);
}

Expand All @@ -188,6 +189,7 @@ private Stream LoadOverriddableResource(ResourceType resourceType, Language lang
{
string outHashTemp;
var replacementDataStream = this.LoadOverriddableResource(
workingDirectory,
replacement.Value.ResourceName,
language,
platform,
Expand Down Expand Up @@ -224,19 +226,18 @@ private Stream LoadOverriddableResource(ResourceType resourceType, Language lang
return source;
}

public XslCompiledTransform LoadXSLT(ResourceType resourceType, Language language, string platform)
public XslCompiledTransform LoadXSLT(string workingDirectory, ResourceType resourceType, Language language, string platform)
{
Dictionary<int, XslCompiledTransform> cache;
lock (m_WorkingDirectoryProvider)
lock (m_CachedTransformsLock)
{
var currentDir = m_WorkingDirectoryProvider.GetPath();
if (!m_CachedTransforms.ContainsKey(currentDir))
if (!m_CachedTransforms.ContainsKey(workingDirectory))
{
m_CachedTransforms[currentDir] = new Dictionary<int, XslCompiledTransform>();
m_CachedTransforms[workingDirectory] = new Dictionary<int, XslCompiledTransform>();
}
cache = m_CachedTransforms[currentDir];
cache = m_CachedTransforms[workingDirectory];
}

int hash;
unchecked
{
Expand All @@ -255,7 +256,7 @@ public XslCompiledTransform LoadXSLT(ResourceType resourceType, Language languag
}

string loadHash;
var source = this.LoadOverriddableResource(resourceType, language, platform, out loadHash);
var source = this.LoadOverriddableResource(workingDirectory, resourceType, language, platform, out loadHash);
int loadHashCode = loadHash.GetHashCode();

lock (cache)
Expand Down Expand Up @@ -316,10 +317,10 @@ public XslCompiledTransform LoadXSLT(ResourceType resourceType, Language languag
return result;
}

public XmlDocument LoadXML(ResourceType resourceType, Language language, string platform)
public XmlDocument LoadXML(string workingDirectory, ResourceType resourceType, Language language, string platform)
{
string loadHash;
using (var source = this.LoadOverriddableResource(resourceType, language, platform, out loadHash))
using (var source = this.LoadOverriddableResource(workingDirectory, resourceType, language, platform, out loadHash))
{
var document = new XmlDocument();
document.Load(source);
Expand Down
Loading

0 comments on commit 0798cce

Please sign in to comment.