Browse Source

Bundle command supoorts maui-blazor project

pull/14637/head
liangshiwei 4 years ago
parent
commit
54843277db
  1. 3
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleOptions.cs
  2. 29
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlerBase.cs
  3. 3
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingConsts.cs
  4. 54
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs
  5. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/IBundlingService.cs
  6. 12
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/PathHelper.cs
  7. 25
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/BundleCommand.cs

3
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleOptions.cs

@ -15,6 +15,9 @@ public class BundleOptions
[NotNull]
public string ProjectFileName { get; set; }
[NotNull]
public string ProjectType { get; set; }
public bool Minify { get; set; }
}

29
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlerBase.cs

@ -35,7 +35,7 @@ public abstract class BundlerBase : IBundler, ITransientDependency
var bundleFileDefinitions = context.BundleDefinitions.Where(t => t.ExcludeFromBundle == false).ToList();
var fileDefinitionsExcludingFromBundle = context.BundleDefinitions.Where(t => t.ExcludeFromBundle).ToList();
var bundledContent = BundleFiles(options, bundleFileDefinitions);
var bundledContent = options.ProjectType == BundlingConsts.WebAssembly? BundleWebAssemblyFiles(options, bundleFileDefinitions) : BundleMauiBlazorFiles(options, bundleFileDefinitions);
File.WriteAllText(bundleFilePath, bundledContent);
return GenerateDefinition(bundleFilePath,fileDefinitionsExcludingFromBundle);
@ -59,7 +59,7 @@ public abstract class BundlerBase : IBundler, ITransientDependency
return false;
}
private string BundleFiles(BundleOptions options, List<BundleDefinition> bundleDefinitions)
private string BundleWebAssemblyFiles(BundleOptions options, List<BundleDefinition> bundleDefinitions)
{
var staticAssetsFilePath = Path.Combine(options.Directory, "bin", "Debug", options.FrameworkVersion,
$"{options.ProjectFileName}.staticwebassets.runtime.json");
@ -99,7 +99,7 @@ public abstract class BundlerBase : IBundler, ITransientDependency
var fileName =
definition.Source.Substring(slashIndex + 1, definition.Source.Length - slashIndex - 1);
var filePath =
Path.Combine(PathHelper.GetFrameworkFolderPath(options.Directory, options.FrameworkVersion),
Path.Combine(PathHelper.GetWebAssemblyFrameworkFolderPath(options.Directory, options.FrameworkVersion),
fileName);
content = GetFileContent(filePath, false);
}
@ -119,6 +119,29 @@ public abstract class BundlerBase : IBundler, ITransientDependency
}
}
private string BundleMauiBlazorFiles(BundleOptions options, List<BundleDefinition> bundleDefinitions)
{
var builder = new StringBuilder();
foreach (var definition in bundleDefinitions)
{
var filePath = Path.Combine(definition.Source.Split('/'));
var fileFullPath = Directory.GetFiles(options.Directory, string.Empty, SearchOption.AllDirectories).FirstOrDefault(x => x.EndsWith(filePath));
if(fileFullPath == null)
{
throw new AbpException("Not found: " + definition.Source);
}
var content = GetFileContent(fileFullPath, options.Minify);
content = ProcessBeforeAddingToTheBundle(definition.Source, Path.Combine(options.Directory, "wwwroot"),
content);
builder.AppendLine(content);
}
return builder.ToString();
}
private string GetFileContent(string filePath, bool minify)
{
var content = File.ReadAllText(filePath);

3
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingConsts.cs

@ -7,4 +7,7 @@ internal static class BundlingConsts
internal const string ScriptPlaceholderStart = "<!--ABP:Scripts-->";
internal const string ScriptPlaceholderEnd = "<!--/ABP:Scripts-->";
internal const string SupportedWebAssemblyProjectType = "Microsoft.NET.Sdk.BlazorWebAssembly";
internal const string SupportedMauiBlazorProjectType = "Microsoft.NET.Sdk.Razor";
internal const string WebAssembly = "webassembly";
internal const string MauiBlazor = "maui-blazor";
}

54
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs

@ -29,7 +29,7 @@ public class BundlingService : IBundlingService, ITransientDependency
public IStyleBundler StyleBundler { get; set; }
public IConfigReader ConfigReader { get; set; }
public async Task BundleAsync(string directory, bool forceBuild)
public async Task BundleAsync(string directory, bool forceBuild, string projectType = BundlingConsts.WebAssembly)
{
var projectFiles = Directory.GetFiles(directory, "*.csproj");
if (!projectFiles.Any())
@ -40,9 +40,9 @@ public class BundlingService : IBundlingService, ITransientDependency
var projectFilePath = projectFiles[0];
CheckProjectIsSupportedType(projectFilePath);
CheckProjectIsSupportedType(projectFilePath, projectType);
var config = ConfigReader.Read(PathHelper.GetWwwRootPath(directory));
var config = projectType == BundlingConsts.WebAssembly? ConfigReader.Read(PathHelper.GetWwwRootPath(directory)): ConfigReader.Read(directory);
var bundleConfig = config.Bundle;
if (forceBuild)
@ -55,9 +55,9 @@ public class BundlingService : IBundlingService, ITransientDependency
DotNetProjectBuilder.BuildProjects(projects, string.Empty);
}
var frameworkVersion = GetTargetFrameworkVersion(projectFilePath);
var frameworkVersion = GetTargetFrameworkVersion(projectFilePath, projectType);
var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
var assemblyFilePath = PathHelper.GetAssemblyFilePath(directory, frameworkVersion, projectName);
var assemblyFilePath = projectType == BundlingConsts.WebAssembly? PathHelper.GetWebAssemblyFilePath(directory, frameworkVersion, projectName) : PathHelper.GetMauiBlazorAssemblyFilePath(directory, projectName);
var startupModule = GetStartupModule(assemblyFilePath);
var bundleDefinitions = new List<BundleTypeDefinition>();
@ -65,7 +65,7 @@ public class BundlingService : IBundlingService, ITransientDependency
bundleDefinitions = bundleDefinitions.OrderByDescending(t => t.Level).ToList();
var styleContext = GetStyleContext(bundleDefinitions, bundleConfig.Parameters);
var scriptContext = GetScriptContext(bundleDefinitions, bundleConfig.Parameters);
var scriptContext = GetScriptContext(bundleDefinitions, bundleConfig.Parameters, projectType);
string styleDefinitions;
string scriptDefinitions;
@ -101,17 +101,20 @@ public class BundlingService : IBundlingService, ITransientDependency
}
private BundleContext GetScriptContext(List<BundleTypeDefinition> bundleDefinitions,
BundleParameterDictionary parameters)
BundleParameterDictionary parameters, string projectType)
{
var scriptContext = new BundleContext
{
Parameters = parameters
};
scriptContext.BundleDefinitions.AddIfNotContains(
x => x.Source == "_framework/blazor.webassembly.js",
() => new BundleDefinition { Source = "_framework/blazor.webassembly.js" });
if (projectType == BundlingConsts.WebAssembly)
{
scriptContext.BundleDefinitions.AddIfNotContains(
x => x.Source == "_framework/blazor.webassembly.js",
() => new BundleDefinition { Source = "_framework/blazor.webassembly.js" });
}
foreach (var bundleDefinition in bundleDefinitions)
{
var contributor = CreateContributorInstance(bundleDefinition.BundleContributorType);
@ -283,24 +286,41 @@ public class BundlingService : IBundlingService, ITransientDependency
.SingleOrDefault(AbpModule.IsAbpModule);
}
private string GetTargetFrameworkVersion(string projectFilePath)
private string GetTargetFrameworkVersion(string projectFilePath, string projectType)
{
var document = new XmlDocument();
document.Load(projectFilePath);
return document.SelectSingleNode("//TargetFramework").InnerText;
return projectType switch {
BundlingConsts.WebAssembly => document.SelectSingleNode("//TargetFramework").InnerText,
BundlingConsts.MauiBlazor => document.SelectNodes("//TargetFrameworks")[0].InnerText,
_ => null
};
}
private void CheckProjectIsSupportedType(string projectFilePath)
private void CheckProjectIsSupportedType(string projectFilePath, string projectType)
{
var document = new XmlDocument();
document.Load(projectFilePath);
var sdk = document.DocumentElement.GetAttribute("Sdk");
if (sdk != BundlingConsts.SupportedWebAssemblyProjectType)
switch (projectType)
{
throw new BundlingException(
$"Unsupported project type. Project type must be {BundlingConsts.SupportedWebAssemblyProjectType}.");
case BundlingConsts.WebAssembly:
if (sdk != BundlingConsts.SupportedWebAssemblyProjectType)
{
throw new BundlingException(
$"Unsupported project type. Project type must be {BundlingConsts.SupportedWebAssemblyProjectType}.");
}
break;
case BundlingConsts.MauiBlazor:
if (sdk != BundlingConsts.SupportedMauiBlazorProjectType)
{
throw new BundlingException(
$"Unsupported project type. Project type must be {BundlingConsts.SupportedMauiBlazorProjectType}.");
}
break;
}
}
}

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/IBundlingService.cs

@ -4,5 +4,5 @@ namespace Volo.Abp.Cli.Bundling;
public interface IBundlingService
{
Task BundleAsync(string directory, bool forceBuild);
Task BundleAsync(string directory, bool forceBuild, string projectType = BundlingConsts.WebAssembly);
}

12
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/PathHelper.cs

@ -1,20 +1,26 @@
using System.IO;
using System.Linq;
namespace Volo.Abp.Cli.Bundling;
internal static class PathHelper
{
internal static string GetFrameworkFolderPath(string projectDirectory, string frameworkVersion)
internal static string GetWebAssemblyFrameworkFolderPath(string projectDirectory, string frameworkVersion)
{
return Path.Combine(projectDirectory, "bin", "Debug", frameworkVersion, "wwwroot", "_framework"); ;
}
internal static string GetAssemblyFilePath(string directory, string frameworkVersion, string projectFileName)
internal static string GetWebAssemblyFilePath(string directory, string frameworkVersion, string projectFileName)
{
var outputDirectory = GetFrameworkFolderPath(directory, frameworkVersion);
var outputDirectory = GetWebAssemblyFrameworkFolderPath(directory, frameworkVersion);
return Path.Combine(outputDirectory, projectFileName + ".dll");
}
internal static string GetMauiBlazorAssemblyFilePath(string directory, string projectFileName)
{
return Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories).First(f => !f.Contains("android") && f.EndsWith(projectFileName + ".dll"));
}
internal static string GetWwwRootPath(string directory)
{
return Path.Combine(directory, "wwwroot");

25
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/BundleCommand.cs

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Bundling;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Commands;
@ -29,7 +30,8 @@ public class BundleCommand : IConsoleCommand, ITransientDependency
var forceBuild = commandLineArgs.Options.ContainsKey(Options.ForceBuild.Short) ||
commandLineArgs.Options.ContainsKey(Options.ForceBuild.Long);
var projectType = GetProjectType(commandLineArgs);
if (!Directory.Exists(workingDirectory))
{
@ -42,7 +44,7 @@ public class BundleCommand : IConsoleCommand, ITransientDependency
try
{
await BundlingService.BundleAsync(workingDirectory, forceBuild);
await BundlingService.BundleAsync(workingDirectory, forceBuild, projectType);
}
catch (BundlingException ex)
{
@ -68,11 +70,24 @@ public class BundleCommand : IConsoleCommand, ITransientDependency
sb.AppendLine("");
sb.AppendLine("-wd|--working-directory <directory-path> (default: empty)");
sb.AppendLine("-f | --force (default: false)");
sb.AppendLine("-t | --project-type (default: webassembly)");
sb.AppendLine("");
sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI");
return sb.ToString();
}
private string GetProjectType(CommandLineArgs commandLineArgs)
{
var projectType = commandLineArgs.Options.GetOrNull(Options.ProjectType.Short, Options.ProjectType.Long);
projectType ??= BundlingConsts.WebAssembly;
return projectType.ToLower() switch {
"webassembly" => BundlingConsts.WebAssembly,
"maui-blazor" => BundlingConsts.MauiBlazor,
_ => throw new CliUsageException(ExceptionMessageHelper.GetInvalidOptionExceptionMessage("Project Type"))
};
}
public static class Options
{
@ -82,6 +97,12 @@ public class BundleCommand : IConsoleCommand, ITransientDependency
public const string Long = "working-directory";
}
public static class ProjectType
{
public const string Short = "t";
public const string Long = "project-type";
}
public static class ForceBuild
{
public const string Short = "f";

Loading…
Cancel
Save