Skip to content

Commit

Permalink
recursive lookup from Project Directory to the Solution Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jul 6, 2021
1 parent 9fea081 commit e3f6177
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 54 deletions.
60 changes: 40 additions & 20 deletions src/Mobile.BuildTools.Reference/Utils/EnvironmentAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -33,27 +32,48 @@ public static IDictionary<string, string> GatherEnvironmentVariables(IBuildConfi
var projectDirectory = buildConfiguration.ProjectDirectory;
var solutionDirectory = buildConfiguration.SolutionDirectory;
var configuration = buildConfiguration.BuildConfiguration;
new[]
{
// Legacy Support
Path.Combine(projectDirectory, Constants.SecretsJsonFileName),
Path.Combine(projectDirectory, string.Format(Constants.SecretsJsonConfigurationFileFormat, configuration)),
Path.Combine(solutionDirectory, Constants.SecretsJsonFileName),
Path.Combine(solutionDirectory, string.Format(Constants.SecretsJsonConfigurationFileFormat, configuration)),
// End Legacy Support

Path.Combine(projectDirectory, Constants.AppSettingsJsonFileName),
Path.Combine(projectDirectory, string.Format(Constants.AppSettingsJsonConfigurationFileFormat, configuration)),
Path.Combine(solutionDirectory, Constants.AppSettingsJsonFileName),
Path.Combine(solutionDirectory, string.Format(Constants.AppSettingsJsonConfigurationFileFormat, configuration)),
}.Distinct()
.ForEach(x =>
var directories = new List<string>(new[] {
projectDirectory,
});

var stoppingDir = new DirectoryInfo(solutionDirectory).Parent.FullName;
var lookupDir = projectDirectory;
do
{
if (Path.GetFileName(x).StartsWith("secrets") && File.Exists(x))
buildConfiguration.Logger.LogWarning("The secrets.json has been deprecated and will no longer be supported in a future version. Please migrate to appsettings.json");
lookupDir = new DirectoryInfo(lookupDir).Parent?.FullName;
if (lookupDir is null || stoppingDir == lookupDir)
{
break;
}
else if(!directories.Contains(lookupDir))
{
directories.Add(lookupDir);
}
} while (lookupDir != solutionDirectory);

LoadSecrets(x, ref env);
});
directories
.SelectMany(x => new[]
{
// Legacy Support
Path.Combine(x, Constants.SecretsJsonFileName),
Path.Combine(x, string.Format(Constants.SecretsJsonConfigurationFileFormat, configuration)),
})
.ForEach(x =>
{
if (File.Exists(x))
{
buildConfiguration.Logger.LogWarning($"The secrets.json has been deprecated and will no longer be supported in a future version. Please migrate '{x}' to appsettings.json");
LoadSecrets(x, ref env);
}
});

directories
.SelectMany(x => new[]
{
Path.Combine(x, Constants.AppSettingsJsonFileName),
Path.Combine(x, string.Format(Constants.AppSettingsJsonConfigurationFileFormat, configuration)),
})
.ForEach(x => LoadSecrets(x, ref env));

if (includeManifest)
{
Expand Down
37 changes: 28 additions & 9 deletions tests/Mobile.BuildTools.Tests/Fixtures/FixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,48 @@ public abstract class FixtureBase
protected ITestOutputHelper _testOutputHelper { get; }
protected string ProjectDirectory { get; }

protected FixtureBase(ITestOutputHelper testOutputHelper)
protected FixtureBase(ITestOutputHelper testOutputHelper)
: this(null, testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

protected FixtureBase(string projectDirectory, ITestOutputHelper testOutputHelper)
: this(testOutputHelper)
{
{
_testOutputHelper = testOutputHelper;
ProjectDirectory = projectDirectory;
}

protected TestBuildConfiguration GetConfiguration(string testName = null)
{
var stackTrace = new StackTrace();
var testOutput = Path.Combine("Tests", GetType().Name, testName ?? stackTrace.GetFrame(1).GetMethod().Name);
ResetTestOutputDirectory(testOutput);
{
if(string.IsNullOrEmpty(testName))
{
var stackTrace = new StackTrace();
testName = stackTrace.GetFrame(1).GetMethod().Name;
}

if (testName.Length > 20)
testName = System.Guid.NewGuid().ToString();

var tempDir = Path.GetTempPath();
var projectDirectory = ProjectDirectory;
var solutionDirectory = ProjectDirectory;
if (string.IsNullOrEmpty(projectDirectory))
{
projectDirectory = Path.Combine(tempDir, "Tests", GetType().Name, testName, "SolutionDir", "src", testName);
solutionDirectory = Path.Combine(tempDir, "Tests", GetType().Name, testName, "SolutionDir");
}

var testOutput = Path.Combine(tempDir, "Tests", GetType().Name, testName);
ResetTestOutputDirectory(testOutput);
ResetTestOutputDirectory(projectDirectory);

return new TestBuildConfiguration
{
Logger = new XunitLog(_testOutputHelper),
Platform = Platform.Unsupported,
IntermediateOutputPath = testOutput,
ProjectDirectory = ProjectDirectory,
ProjectDirectory = projectDirectory,
SolutionDirectory = solutionDirectory,
BuildConfiguration = "Debug",
ProjectName = "AwesomeApp",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mobile.BuildTools.Tests.Fixtures.Generators
public class AppleImageCollectionGeneratorFixture : ImageCollectionGeneratorFixture
{
public AppleImageCollectionGeneratorFixture(ITestOutputHelper testOutputHelper)
: base(Path.Combine("Templates", "Apple"), testOutputHelper)
: base(testOutputHelper)
{
PlatformOffset = 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mobile.BuildTools.Build;
using Mobile.BuildTools.Generators.Images;
using Mobile.BuildTools.Models.AppIcons;
using Mobile.BuildTools.Tests.Mocks;
using Mobile.BuildTools.Utils;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -29,12 +30,6 @@ public ImageCollectionGeneratorFixture(ITestOutputHelper testOutputHelper)
Initialize();
}

protected ImageCollectionGeneratorFixture(string projectDirectory, ITestOutputHelper testOutputHelper)
: base(projectDirectory, testOutputHelper)
{
Initialize();
}

private void Initialize()
{
var rdInfo = new FileInfo(Path.Combine("resources", "resourceDefinition.json"));
Expand Down Expand Up @@ -72,7 +67,8 @@ public void GeneratorLocatesCorrectNumberOfAssetsWithDebugDirectory()
[Fact]
public void GeneratorLocatesCorrectNumberOfAssetsWithPlatformDirectory()
{
var config = GetConfiguration();
var config = GetConfiguration();
CopyResources(config);
var generator = CreateGenerator(config, ImageDirectory, PlatformImageDirectory);
generator.Execute();

Expand All @@ -82,7 +78,8 @@ public void GeneratorLocatesCorrectNumberOfAssetsWithPlatformDirectory()
[Fact]
public void GeneratorLocatesCorrectNumberOfAssetsWithPlatformAndDebugDirectory()
{
var config = GetConfiguration();
var config = GetConfiguration();
CopyResources(config);
var generator = CreateGenerator(config, ImageDirectory, DebugImageDirectory, PlatformImageDirectory);
generator.Execute();

Expand Down Expand Up @@ -179,7 +176,9 @@ public void PlatformIconUsesWatermarkForDebug()
[InlineData("platform")]
public void StandardImageGeneratesProperOutputs(string file)
{
var config = GetConfiguration();
var config = GetConfiguration();
CopyResources(config);

var generator = CreateGenerator(config, ImageDirectory, PlatformImageDirectory);
generator.Execute();

Expand Down Expand Up @@ -218,6 +217,30 @@ public void DoesNotThrowException()
var generator = CreateGenerator(config, ImageDirectory);
var ex = Record.Exception(() => generator.Execute());
Assert.Null(ex);
}

private void CopyResources(TestBuildConfiguration config)
{
if (Platform != Platform.iOS)
return;

var templates = Path.Combine(System.Environment.CurrentDirectory, "Templates", "Apple");
CopyFilesRecursively(templates, config.ProjectDirectory);
}

private static void CopyFilesRecursively(string sourcePath, string targetPath)
{
//Now Create all of the directories
foreach (var dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}

//Copy all the files & Replaces any files with the same name
foreach (var newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
}

internal abstract ImageCollectionGeneratorBase CreateGenerator(IBuildConfiguration config, params string[] searchFolders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Mobile.BuildTools.Tests.Fixtures.Utils
public class EnvironmentAnalyzerFixture : FixtureBase, IDisposable
{
public EnvironmentAnalyzerFixture(ITestOutputHelper testOutputHelper)
: base(nameof(EnvironmentAnalyzerFixture), testOutputHelper)
: base(null, testOutputHelper)
{
}

Expand All @@ -39,13 +39,12 @@ public void GetsValuesFromJson(string filename)
}
};
config.Configuration.AppSettings[config.ProjectName] = new List<SettingsConfig>(new[] { settingsConfig });
var projectDir = new DirectoryInfo(config.IntermediateOutputPath).Parent.FullName;
config.SolutionDirectory = config.ProjectDirectory = projectDir;

var secrets = new
{
SampleProp = "Hello Tests"
};
File.WriteAllText(Path.Combine(projectDir, filename), JsonConvert.SerializeObject(secrets));
File.WriteAllText(Path.Combine(config.ProjectDirectory, filename), JsonConvert.SerializeObject(secrets));

var mergedSecrets = EnvironmentAnalyzer.GatherEnvironmentVariables(config);

Expand All @@ -70,8 +69,6 @@ public void GetsValuesFromConfigurationEnvironment()
};
config.Configuration.Environment.Defaults.Add("SampleProp", "Hello Tests");
config.Configuration.AppSettings[config.ProjectName] = new List<SettingsConfig>(new[] { settingsConfig });
var projectDir = new DirectoryInfo(config.IntermediateOutputPath).Parent.FullName;
config.SolutionDirectory = config.ProjectDirectory = projectDir;

var mergedSecrets = EnvironmentAnalyzer.GatherEnvironmentVariables(config);

Expand Down Expand Up @@ -100,8 +97,6 @@ public void GetsValuesForBuildConfigurationFromConfigurationEnvironment()
{ "SampleProp", "Hello Override" }
};
config.Configuration.AppSettings[config.ProjectName] = new List<SettingsConfig>(new[] { settingsConfig });
var projectDir = new DirectoryInfo(config.IntermediateOutputPath).Parent.FullName;
config.SolutionDirectory = config.ProjectDirectory = projectDir;

var mergedSecrets = EnvironmentAnalyzer.GatherEnvironmentVariables(config);

Expand All @@ -125,8 +120,6 @@ public void GetsValuesFromHostEnvironment()
}
};
config.Configuration.AppSettings[config.ProjectName] = new List<SettingsConfig>(new[] { settingsConfig });
var projectDir = new DirectoryInfo(config.IntermediateOutputPath).Parent.FullName;
config.SolutionDirectory = config.ProjectDirectory = projectDir;
Environment.SetEnvironmentVariable("SampleProp1", nameof(GetsValuesFromHostEnvironment), EnvironmentVariableTarget.Process);

var mergedSecrets = EnvironmentAnalyzer.GatherEnvironmentVariables(config);
Expand All @@ -153,8 +146,6 @@ public void OverridesConfigEnvironmentFromHostEnvironment()
}
};
config.Configuration.AppSettings[config.ProjectName] = new List<SettingsConfig>(new[] { settingsConfig });
var projectDir = new DirectoryInfo(config.IntermediateOutputPath).Parent.FullName;
config.SolutionDirectory = config.ProjectDirectory = projectDir;
config.Configuration.Environment.Defaults["SampleProp3"] = "Hello Config Environment";

Environment.SetEnvironmentVariable("SampleProp3", nameof(OverridesConfigEnvironmentFromHostEnvironment), EnvironmentVariableTarget.Process);
Expand Down Expand Up @@ -183,14 +174,12 @@ public void OverridesConfigEnvironmentFromJson(string filename)
}
};
config.Configuration.AppSettings[config.ProjectName] = new List<SettingsConfig>(new[] { settingsConfig });
var projectDir = new DirectoryInfo(config.IntermediateOutputPath).Parent.FullName;
config.SolutionDirectory = config.ProjectDirectory = projectDir;
config.Configuration.Environment.Defaults["SampleProp"] = "Hello Config Environment";
var secrets = new
{
SampleProp = "Hello Tests"
};
File.WriteAllText(Path.Combine(projectDir, filename), JsonConvert.SerializeObject(secrets));
File.WriteAllText(Path.Combine(config.ProjectDirectory, filename), JsonConvert.SerializeObject(secrets));

var mergedSecrets = EnvironmentAnalyzer.GatherEnvironmentVariables(config);

Expand Down

0 comments on commit e3f6177

Please sign in to comment.