@ -1,5 +1,4 @@
using Microsoft.CodeAnalysis.CSharp ;
using System ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
@ -29,7 +28,7 @@ namespace Volo.Abp.Cli.Bundling
var projectFiles = Directory . GetFiles ( directory , "*.csproj" ) ;
if ( ! projectFiles . Any ( ) )
{
throw new BundlingException ( "No project file found in the directory" ) ;
throw new BundlingException ( "No project file found in the directory. The working directory must have a Blazor project file. " ) ;
}
var projectFilePath = projectFiles [ 0 ] ;
@ -40,7 +39,7 @@ namespace Volo.Abp.Cli.Bundling
{
new DotNetProjectInfo ( string . Empty , projectFilePath , true )
} ;
DotNetProjectBuilder . BuildProjects ( projects , string . Empty ) ;
}
@ -63,11 +62,13 @@ namespace Volo.Abp.Cli.Bundling
private BundleContext GetScriptContext ( List < BundleTypeDefinition > bundleDefinitions )
{
var scriptContext = new BundleContext ( ) ;
foreach ( var bundleDefinition in bundleDefinitions )
{
var contributer = CreateContributerInstance ( bundleDefinition . BundleContributerType ) ;
contributer . AddScripts ( scriptContext ) ;
}
scriptContext . Add ( "_framework/blazor.webassembly.js" ) ;
return scriptContext ;
}
@ -75,11 +76,13 @@ namespace Volo.Abp.Cli.Bundling
private BundleContext GetStyleContext ( List < BundleTypeDefinition > bundleDefinitions )
{
var styleContext = new BundleContext ( ) ;
foreach ( var bundleDefinition in bundleDefinitions )
{
var contributer = CreateContributerInstance ( bundleDefinition . BundleContributerType ) ;
contributer . AddStyles ( styleContext ) ;
} ;
}
return styleContext ;
}
@ -102,9 +105,11 @@ namespace Volo.Abp.Cli.Bundling
content = UpdatePlaceholders ( content , StylePlaceholderStart , StylePlaceholderEnd , styleDefinitions ) ;
content = UpdatePlaceholders ( content , ScriptPlaceholderStart , ScriptPlaceholderEnd , scriptDefinitions ) ;
using var writer = new StreamWriter ( htmlFilePath , false , fileEncoding ) ;
await writer . WriteAsync ( content ) ;
await writer . FlushAsync ( ) ;
using ( var writer = new StreamWriter ( htmlFilePath , false , fileEncoding ) )
{
await writer . WriteAsync ( content ) ;
await writer . FlushAsync ( ) ;
}
}
private string UpdatePlaceholders ( string content , string placeholderStart , string placeholderEnd , string definitions )
@ -118,24 +123,22 @@ namespace Volo.Abp.Cli.Bundling
private string GenerateStyleDefinitions ( BundleContext context )
{
var builder = new StringBuilder ( ) ;
builder . AppendLine ( $"{StylePlaceholderStart}" ) ;
foreach ( var style in context . BundleDefinitions )
{
if ( style . AdditionalProperties ! = null & & style . AdditionalProperties . Any ( ) )
{
builder . Append ( $"\t<link href=\" { style . Source } \ " rel=\"stylesheet\" " ) ;
foreach ( var additionalProperty in style . AdditionalProperties )
{
builder . Append ( $"{additionalProperty.Key}={additionalProperty.Value} " ) ;
}
builder . AppendLine ( "/>" ) ;
}
else
builder . Append ( $" <link href=\" { style . Source } \ " rel=\"stylesheet\"" ) ;
foreach ( var additionalProperty in style . AdditionalProperties )
{
builder . AppendLine ( $"\t<link href=\" { style . Source } \ " rel=\"stylesheet\" /> " ) ;
builder . Append ( $"{additionalProperty.Key}={additionalProperty.Value} " ) ;
}
builder . AppendLine ( "/>" ) ;
}
builder . Append ( $"\t{StylePlaceholderEnd}" ) ;
builder . Append ( $" {StylePlaceholderEnd}" ) ;
return builder . ToString ( ) ;
}
@ -146,50 +149,41 @@ namespace Volo.Abp.Cli.Bundling
builder . AppendLine ( $"{ScriptPlaceholderStart}" ) ;
foreach ( var script in context . BundleDefinitions )
{
if ( script . AdditionalProperties ! = null & & script . AdditionalProperties . Any ( ) )
{
builder . Append ( $"\t<script src=\" { script . Source } \ " " ) ;
foreach ( var additionalProperty in script . AdditionalProperties )
{
builder . Append ( $"{additionalProperty.Key}={additionalProperty.Value} " ) ;
}
builder . AppendLine ( "></script>" ) ;
}
else
builder . Append ( $" <script src=\" { script . Source } \ "" ) ;
foreach ( var additionalProperty in script . AdditionalProperties )
{
builder . AppendLine ( $"\t<script src=\" { script . Source } \ "></script> " ) ;
builder . Append ( $"{additionalProperty.Key}={additionalProperty.Value} " ) ;
}
builder . AppendLine ( "></script>" ) ;
}
builder . Append ( $"\t {ScriptPlaceholderEnd}" ) ;
builder . Append ( $" {ScriptPlaceholderEnd}" ) ;
return builder . ToString ( ) ;
}
private IBundleContribute r CreateContributerInstance ( Type bundleContributerType )
private IBundleContributo r CreateContributerInstance ( Type bundleContributerType )
{
var instance = Activator . CreateInstance ( bundleContributerType ) ;
return instance . As < IBundleContributer > ( ) ;
return ( IBundleContributor ) Activator . CreateInstance ( bundleContributerType ) ;
}
private void ExecuteBundleContributers ( List < BundleTypeDefinition > bundleDefinitions , BundleContext styleContext , BundleContext scriptContext )
private void FindBundleContributersRecursively (
Type module ,
int level ,
List < BundleTypeDefinition > bundleDefinitions )
{
foreach ( var bundleDefinition in bundleDefinitions )
var bundleContributers = module . Assembly
. GetTypes ( )
. Where ( t = > t . IsAssignableTo < IBundleContributor > ( ) )
. ToList ( ) ;
if ( bundleContributers . Count > 1 )
{
var contributer = CreateContributerInstance ( bundleDefinition . BundleContributerType ) ;
contributer . AddStyles ( styleContext ) ;
contributer . AddScripts ( scriptContext ) ;
throw new BundlingException ( $"Each project must contain only one class implementing {nameof(IBundleContributor)}" ) ;
}
}
private void FindBundleContributersRecursively ( Type module , int level , List < BundleTypeDefinition > bundleDefinitions )
{
var dependencyDescriptors = module
. GetCustomAttributes ( )
. OfType < IDependedTypesProvider > ( ) ;
var bundleContributer = module . Assembly . GetTypes ( ) . SingleOrDefault ( t = > t . IsAssignableTo < IBundleContributer > ( ) ) ;
if ( bundleContributer ! = null )
if ( bundleContributers . Any ( ) )
{
var bundleContributer = bundleContributers [ 0 ] ;
var definition = bundleDefinitions . SingleOrDefault ( t = > t . BundleContributerType = = bundleContributer ) ;
if ( definition ! = null )
{
@ -208,6 +202,10 @@ namespace Volo.Abp.Cli.Bundling
}
}
var dependencyDescriptors = module
. GetCustomAttributes ( )
. OfType < IDependedTypesProvider > ( ) ;
foreach ( var descriptor in dependencyDescriptors )
{
foreach ( var dependedModuleType in descriptor . GetDependedTypes ( ) )
@ -219,19 +217,10 @@ namespace Volo.Abp.Cli.Bundling
private Type GetStartupModule ( string assemblyPath )
{
var assembly = Assembly . LoadFrom ( assemblyPath ) ;
return assembly . GetTypes ( ) . SingleOrDefault ( IsAbpModule ) ;
static bool IsAbpModule ( Type type )
{
var typeInfo = type . GetTypeInfo ( ) ;
return
typeInfo . IsClass & &
! typeInfo . IsAbstract & &
! typeInfo . IsGenericType & &
typeof ( IAbpModule ) . GetTypeInfo ( ) . IsAssignableFrom ( type ) ;
}
return Assembly
. LoadFrom ( assemblyPath )
. GetTypes ( )
. SingleOrDefault ( AbpModule . IsAbpModule ) ;
}
private string GetFrameworkFolderPath ( string projectDirectory , string frameworkVersion )
@ -244,15 +233,12 @@ namespace Volo.Abp.Cli.Bundling
var document = new XmlDocument ( ) ;
document . Load ( projectFilePath ) ;
var sdk = document . DocumentElement . GetAttribute ( "Sdk" ) ;
if ( sdk = = SupportedWebAssemblyProjectType )
{
var frameworkVersion = document . SelectSingleNode ( "//TargetFramework" ) . InnerText ;
return frameworkVersion ;
}
else
if ( sdk ! = SupportedWebAssemblyProjectType )
{
throw new BundlingException ( $"Unsupported project type. Project type must be {SupportedWebAssemblyProjectType}." ) ;
}
return document . SelectSingleNode ( "//TargetFramework" ) . InnerText ;
}
private string GetAssemblyFilePath ( string directory , string frameworkVersion , string projectFileName )