From b81b5f377cedfb17376c9a5e33f7a6e746902851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 19 Nov 2020 11:39:18 +0300 Subject: [PATCH] Refactored abp bundle service --- .../Abp/Cli/Bundling/BundleTypeDefinition.cs | 1 + .../Abp/Cli/Bundling/BundlingException.cs | 9 +- .../Volo/Abp/Cli/Bundling/BundlingService.cs | 96 ++++++++----------- .../Volo/Abp/Bundling/BundleDefinition.cs | 6 ++ 4 files changed, 53 insertions(+), 59 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleTypeDefinition.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleTypeDefinition.cs index 5fc9ecadeb..57500a1db5 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleTypeDefinition.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleTypeDefinition.cs @@ -5,6 +5,7 @@ namespace Volo.Abp.Cli.Bundling internal class BundleTypeDefinition { public int Level { get; set; } + public Type BundleContributerType { get; set; } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingException.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingException.cs index 31f40a2cd7..7bcdf55a73 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingException.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingException.cs @@ -1,10 +1,9 @@ -using System; - -namespace Volo.Abp.Cli.Bundling +namespace Volo.Abp.Cli.Bundling { - public class BundlingException : Exception + public class BundlingException : AbpException { - public BundlingException(string message) : base(message) + public BundlingException(string message) + : base(message) { } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs index 001e33c5df..252454b082 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs @@ -1,5 +1,4 @@ -using Microsoft.CodeAnalysis.CSharp; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -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 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 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,23 +123,21 @@ 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"); - } - else + builder.Append($"\t"); + builder.Append($"{additionalProperty.Key}={additionalProperty.Value} "); } + + builder.AppendLine("/>"); } + builder.Append($"\t{StylePlaceholderEnd}"); return builder.ToString(); @@ -142,6 +145,8 @@ namespace Volo.Abp.Cli.Bundling private string GenerateScriptDefinitions(BundleContext context) { + //TODO: refactor as like GenerateStyleDefinitions + var builder = new StringBuilder(); builder.AppendLine($"{ScriptPlaceholderStart}"); foreach (var script in context.BundleDefinitions) @@ -167,27 +172,18 @@ namespace Volo.Abp.Cli.Bundling private IBundleContributer CreateContributerInstance(Type bundleContributerType) { - var instance = Activator.CreateInstance(bundleContributerType); - return instance.As(); + return (IBundleContributer) Activator.CreateInstance(bundleContributerType); } - private void ExecuteBundleContributers(List bundleDefinitions, BundleContext styleContext, BundleContext scriptContext) + private void FindBundleContributersRecursively( + Type module, + int level, + List bundleDefinitions) { - foreach (var bundleDefinition in bundleDefinitions) - { - var contributer = CreateContributerInstance(bundleDefinition.BundleContributerType); - contributer.AddStyles(styleContext); - contributer.AddScripts(scriptContext); - } - } + var bundleContributer = module.Assembly + .GetTypes() + .SingleOrDefault(t => t.IsAssignableTo()); - private void FindBundleContributersRecursively(Type module, int level, List bundleDefinitions) - { - var dependencyDescriptors = module - .GetCustomAttributes() - .OfType(); - - var bundleContributer = module.Assembly.GetTypes().SingleOrDefault(t => t.IsAssignableTo()); if (bundleContributer != null) { var definition = bundleDefinitions.SingleOrDefault(t => t.BundleContributerType == bundleContributer); @@ -208,6 +204,10 @@ namespace Volo.Abp.Cli.Bundling } } + var dependencyDescriptors = module + .GetCustomAttributes() + .OfType(); + foreach (var descriptor in dependencyDescriptors) { foreach (var dependedModuleType in descriptor.GetDependedTypes()) @@ -219,19 +219,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 +235,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) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleDefinition.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleDefinition.cs index 2a410bce76..7a3bf315be 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleDefinition.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleDefinition.cs @@ -5,6 +5,12 @@ namespace Volo.Abp.Bundling public class BundleDefinition { public string Source { get; set; } + public Dictionary AdditionalProperties { get; set; } + + public BundleDefinition() + { + AdditionalProperties = new Dictionary(); + } } }