Skip to content

Commit dfab2d6

Browse files
committed
extract wwwroot files if they don't exist
1 parent f262c46 commit dfab2d6

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

Framework/Intersect.Framework/Reflection/AssemblyExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Globalization;
33
using System.Reflection;
44
using System.Resources;
5+
using System.Text.RegularExpressions;
6+
57
// ReSharper disable MemberCanBePrivate.Global
68

79
namespace Intersect.Framework.Reflection;
@@ -151,6 +153,20 @@ public static IEnumerable<Type> FindValueSubtypesOf(this Assembly assembly, Type
151153
public static IEnumerable<Type> FindValueSubtypesOf<TParentType>(this Assembly assembly) =>
152154
assembly.FindValueSubtypesOf(typeof(TParentType));
153155

156+
public static string[] FindMatchingResources(this Assembly assembly, string search)
157+
{
158+
return assembly.IsDynamic
159+
? []
160+
: assembly.GetManifestResourceNames().Where(name => name.Contains(search)).ToArray();
161+
}
162+
163+
public static string[] FindMatchingResources(this Assembly assembly, Regex regex)
164+
{
165+
return assembly.IsDynamic
166+
? []
167+
: assembly.GetManifestResourceNames().Where(name => regex.IsMatch(name)).ToArray();
168+
}
169+
154170
public static bool TryFindResource(
155171
this Assembly assembly,
156172
string resourceName,

Intersect.Server.Core/Core/Bootstrapper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static Bootstrapper()
4444

4545
public static ILockingActionQueue MainThread { get; private set; }
4646

47-
public static void Start(Assembly entryAssembly, params string[] args)
47+
public static void Start(Assembly entryAssembly, string[] args, params Action<ServerCommandLineOptions>[] configuredActions)
4848
{
4949
(string[] Args, Parser Parser, ServerCommandLineOptions CommandLineOptions) parsedArguments =
5050
ParseCommandLineArgs(args);
@@ -57,6 +57,11 @@ public static void Start(Assembly entryAssembly, params string[] args)
5757
}
5858
}
5959

60+
foreach (var configureAction in configuredActions)
61+
{
62+
configureAction(parsedArguments.CommandLineOptions);
63+
}
64+
6065
if (!PreContextSetup(args))
6166
{
6267
Console.Error.WriteLine("[FATAL] Pre-context setup failed.");

Intersect.Server/Intersect.Server.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
<Content Update="wwwroot\favicon.ico" CopyToOutputDirectory="PreserveNewest" />
4545
<Content Update="wwwroot\css\theme.css" CopyToOutputDirectory="PreserveNewest" />
4646
<Content Update="wwwroot\css\site.css" CopyToOutputDirectory="PreserveNewest" />
47+
48+
<EmbeddedResource Include="wwwroot\**" CopyToOutputDirectory="PreserveNewest">
49+
<LogicalName>$([System.String]::new('%(RelativeDir)').Replace('\','/'))%(FileName)%(Extension)</LogicalName>
50+
</EmbeddedResource>
4751
</ItemGroup>
4852

4953
<ItemGroup Label="Resources (ResX)">

Intersect.Server/Program.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,52 @@ public static void Main(string[] args)
4040

4141
Client.EnqueueNetworkTask = action => ServerNetwork.Pool.QueueWorkItem(action);
4242

43-
Bootstrapper.Start(executingAssembly, args);
43+
Bootstrapper.Start(executingAssembly, args, ExtractWwwroot);
4444
}
4545
catch (Exception exception)
4646
{
4747
ServerContext.DispatchUnhandledException(exception.InnerException ?? exception);
4848
}
4949
}
5050

51+
private static void ExtractWwwroot(ServerCommandLineOptions options)
52+
{
53+
var assembly = typeof(Program).Assembly;
54+
var wwwrootResourceNames = assembly.FindMatchingResources("wwwroot");
55+
var workingDirectory = options.WorkingDirectory;
56+
if (string.IsNullOrWhiteSpace(workingDirectory))
57+
{
58+
workingDirectory = Environment.CurrentDirectory;
59+
}
60+
61+
foreach (var wwwrootResourceName in wwwrootResourceNames)
62+
{
63+
var resolvedOutputPath = Path.Combine(workingDirectory, wwwrootResourceName);
64+
FileInfo outputFileInfo = new(resolvedOutputPath);
65+
if (outputFileInfo.Exists)
66+
{
67+
// Don't overwrite existing files
68+
continue;
69+
}
70+
71+
using var manifestResourceStream = assembly.GetManifestResourceStream(wwwrootResourceName);
72+
if (manifestResourceStream == null)
73+
{
74+
// TODO: Pre-context logging
75+
continue;
76+
}
77+
78+
DirectoryInfo outputFileDirectoryInfo = outputFileInfo.Directory;
79+
if (outputFileDirectoryInfo is { Exists: false })
80+
{
81+
outputFileDirectoryInfo.Create();
82+
}
83+
84+
using var outputFileStream = outputFileInfo.OpenWrite();
85+
manifestResourceStream.CopyTo(outputFileStream);
86+
}
87+
}
88+
5189
/// <summary>
5290
/// Host builder method for Entity Framework Design Tools to use when generating migrations.
5391
/// </summary>

0 commit comments

Comments
 (0)