Skip to content

Commit 12b133f

Browse files
committed
add Directory.Build props and targets
1 parent a78b83a commit 12b133f

21 files changed

+192
-90
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ FakesAssemblies/
208208
.vscode/
209209
/src/out
210210

211+
.build/
211212
/src/test/Module.Tests/Modules/*
212213
/src/Api/Modules/*
213214
/src/Api/Plugins/*

Directory.Build.props

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<Authors>Marvin Perez</Authors>
5+
<Product>AccountGo</Product>
6+
<Copyright>Copyright © AccountGo</Copyright>
7+
<NeutralLanguage>en-US</NeutralLanguage>
8+
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
9+
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
10+
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
11+
<PackageProjectUrl>https://github.com/AccountGo/accountgo</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/AccountGo/accountgo.git</RepositoryUrl>
13+
<RepositoryType>git</RepositoryType>
14+
<DebugType>embedded</DebugType>
15+
<IsPackable>false</IsPackable>
16+
<NoWarn>$(NoWarn);NU5105</NoWarn>
17+
18+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
19+
<GenerateFullPaths Condition="'$(VSCODE_PID)' != ''">true</GenerateFullPaths>
20+
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory).build\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
21+
<BaseOutputPath>$(MSBuildThisFileDirectory).build\bin\$(MSBuildProjectName)\</BaseOutputPath>
22+
</PropertyGroup>
23+
24+
</Project>

Directory.Build.targets

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
</PropertyGroup>
6+
7+
</Project>

src/Core/Core.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<ApplicationIcon />
65
<OutputType>Library</OutputType>
7-
<StartupObject />
86
</PropertyGroup>
97

108
<ItemGroup>

src/Dto/Dto.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<ApplicationIcon />
65
<OutputType>Library</OutputType>
7-
<StartupObject />
86
</PropertyGroup>
97

108
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace Infrastructure.AssemblyLoader
5+
{
6+
public abstract class AssemblyInfo : IAssemblyInfo
7+
{
8+
#region Private member variables
9+
private readonly string _name;
10+
private readonly Version _version;
11+
private readonly Assembly _assembly;
12+
#endregion
13+
14+
#region Public properties
15+
public string Name => this._name;
16+
public Version Version => this._version;
17+
public Assembly Assembly => this._assembly;
18+
#endregion
19+
20+
#region Ctor
21+
public AssemblyInfo(string name, Version version, Assembly assembly = null)
22+
{
23+
this._name = name;
24+
this._version = version;
25+
this._assembly = assembly;
26+
}
27+
#endregion
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using Infrastructure.Module;
6+
using Infrastructure.Plugin;
7+
using Newtonsoft.Json;
8+
9+
namespace Infrastructure.AssemblyLoader
10+
{
11+
public class AssemblyLoaderManager
12+
{
13+
public (IEnumerable<ModuleDescriptor>, IEnumerable<PluginDescriptor>) GetAssembliesToLoad()
14+
{
15+
List<ModuleDescriptor> modulesAssemblies = new List<ModuleDescriptor>();
16+
List<PluginDescriptor> pluginAssemblies = new List<PluginDescriptor>();
17+
18+
try
19+
{
20+
var modulesPath = Path.Combine(Constants.CodeBaseRootPath, ModulesJsonFile);
21+
using (var reader = new StreamReader(modulesPath))
22+
{
23+
dynamic modules = JsonConvert.DeserializeObject(reader.ReadToEnd());
24+
foreach (dynamic module in modules)
25+
{
26+
Console.WriteLine("name: {0}, version: {1}", module.name, Version.Parse(module.version.ToString()));
27+
modulesAssemblies.Add(new ModuleDescriptor(module.name.ToString(), Version.Parse(module.version.ToString())));
28+
}
29+
}
30+
}
31+
catch (Exception ex)
32+
{
33+
// TODO: Handle the log errors
34+
Console.WriteLine(ex.Message);
35+
}
36+
37+
return (modulesAssemblies, pluginAssemblies);
38+
}
39+
private static readonly string ModulesJsonFile= "modules.json";
40+
private static readonly string PluginsJsonFile = "plugins.json";
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Infrastructure.AssemblyLoader
2+
{
3+
public static class Constants
4+
{
5+
public static string CodeBaseRootPath;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace Infrastructure.AssemblyLoader
5+
{
6+
public interface IAssemblyInfo
7+
{
8+
string Name { get; }
9+
Version Version { get; }
10+
Assembly Assembly { get; }
11+
}
12+
}

src/Infrastructure/Infrastructure.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
10+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
911
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
1012
</ItemGroup>
1113

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
using System;
2-
using System.Reflection;
2+
using Infrastructure.AssemblyLoader;
33

44
namespace Infrastructure.Module
55
{
6-
public sealed class ModuleDescriptor
6+
public sealed class ModuleDescriptor : AssemblyInfo
77
{
8-
public ModuleDescriptor(string id, string name, Version version, Assembly assembly)
8+
#region Ctor
9+
public ModuleDescriptor(string name, Version version) : base (name, version)
910
{
10-
this.Id = id;
11-
this.Name = name;
12-
this.Version = version;
13-
this.Assembly = assembly;
14-
1511
}
16-
public readonly string Id;
17-
public readonly string Name;
18-
public readonly Version Version;
19-
public readonly Assembly Assembly;
12+
#endregion
2013
}
2114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Infrastructure.AssemblyLoader;
3+
4+
namespace Infrastructure.Plugin
5+
{
6+
public sealed class PluginDescriptor : AssemblyInfo
7+
{
8+
#region Ctor
9+
public PluginDescriptor(string name, Version version) : base(name, version)
10+
{
11+
}
12+
#endregion
13+
}
14+
}

src/Services/Services.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<ApplicationIcon />
65
<OutputType>Library</OutputType>
7-
<StartupObject />
86
</PropertyGroup>
97

108
<ItemGroup>

test/Module.Tests/BasicModuleLoaderTests.cs

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
using Xunit;
22
using Infrastructure.AssemblyLoader;
3+
using Infrastructure.Module;
34
using System;
45
using System.Reflection;
56
using System.IO;
7+
using System.Linq;
68

79
namespace Module.Tests
810
{
911
public class BasicModuleLoaderTests
1012
{
11-
/// TODO: add SampleModulesRefs.targets so that when Module.Tests projects builds, sample modules copied to test project Modules folder
12-
private const string assemblyPath = "/Users/Marvs/source/accountgo/test/Module.Tests/Modules/";
1313
[Fact]
1414
public void LoadNetCoreApp21ModuleProject()
1515
{
1616
//Given
17-
string path = assemblyPath + "SampleNetStandard20/netstandard2.0/SampleNetStandard20.dll";
17+
Constants.CodeBaseRootPath = GetExecutingDirectorybyAppDomain();
18+
var path = Path.Combine(Constants.CodeBaseRootPath, "SampleNetStandard20.dll");
1819
var assembly = new CustomAssemblyLoadContext().LoadFromAssemblyPath(path);
1920
var type = assembly.GetType("SampleNetStandard20.Class1");
2021
//When
@@ -24,6 +25,26 @@ public void LoadNetCoreApp21ModuleProject()
2425
Assert.Equal("Hello, World!", returnValue);
2526
}
2627

28+
[Fact]
29+
public void GetListOfModulesInJsonFile()
30+
{
31+
//Given
32+
Constants.CodeBaseRootPath = GetExecutingDirectorybyAppDomain();
33+
AssemblyLoaderManager loader = new AssemblyLoaderManager();
34+
//When
35+
var (modules, plugins) = loader.GetAssembliesToLoad();
36+
//Then
37+
Assert.NotEmpty(modules);
38+
}
39+
40+
[Fact]
41+
public void PrintSolutionVariables()
42+
{
43+
Console.WriteLine(GetAssemblyPathByCodeBase());
44+
Console.WriteLine(GetExecutingDirectorybyAppDomain());
45+
Console.WriteLine(GetExecutingDirectoryByAssemblyLocation());
46+
}
47+
2748
private string GetAssemblyPathByCodeBase()
2849
{
2950
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
@@ -39,7 +60,7 @@ private string GetExecutingDirectorybyAppDomain()
3960

4061
private string GetExecutingDirectoryByAssemblyLocation()
4162
{
42-
string path= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
63+
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
4364
return path; // /Users/Marvs/source/accountgo/test/Module.Tests/bin/Debug/netcoreapp2.1
4465
}
4566
}

test/Module.Tests/Module.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
<ProjectReference Include="..\..\src\Infrastructure\Infrastructure.csproj" />
1616
</ItemGroup>
1717

18+
<Import Project="SampleModulesRefs.targets" />
1819
</Project>

test/Module.Tests/Modules/SampleNetStandard20/netstandard2.0/SampleNetStandard20.deps.json

-47
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project>
2+
<!-- <PropertyGroup>
3+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
4+
</PropertyGroup> -->
5+
6+
<ItemGroup>
7+
<Content Include="modules.json">
8+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9+
</Content>
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="../SampleModules/SampleNetStandard20/SampleNetStandard20.csproj" />
14+
</ItemGroup>
15+
16+
</Project>

test/Module.Tests/modules.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"name": "SampleNetStandard20",
4+
"version": "0.0.1"
5+
}
6+
]

test/SampleModules/SampleNetStandard20/SampleNetStandard20.csproj

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
</PropertyGroup>
6-
7-
<PropertyGroup>
8-
<OutputPath>..\..\Module.Tests\Modules\SampleNetStandard20\</OutputPath>
5+
<OutputType>Library</OutputType>
96
</PropertyGroup>
107

118
</Project>

test/SampleModules/SampleNetStandard20/SampleNetStandard20.sln

-17
This file was deleted.

0 commit comments

Comments
 (0)