ToData now supports multiple formats.
Introduce `ProgrartC` to build a single image to work with other build tools. Added a `CombinedStorageProvider` to allow aggregate storage providers.
This commit is contained in:
@@ -6,26 +6,24 @@
|
||||
<ItemGroup>
|
||||
<!-- Avalonia packages -->
|
||||
<!-- Important: keep version in sync! -->
|
||||
<PackageVersion Include="Avalonia" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.AvaloniaEdit" Version="11.3.0" />
|
||||
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia.Fonts.Inter" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia.Diagnostics" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia.Desktop" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia.iOS" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia.Browser" Version="11.3.10" />
|
||||
<PackageVersion Include="Avalonia.Android" Version="11.3.10" />
|
||||
<!--<PackageVersion Include="Devolutions.AvaloniaTheme.DevExpress" Version="2025.12.4" />
|
||||
<PackageVersion Include="Devolutions.AvaloniaTheme.MacOS" Version="2025.12.4" />-->
|
||||
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.Fonts.Inter" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.Diagnostics" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.Desktop" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.iOS" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.Browser" Version="11.3.11" />
|
||||
<PackageVersion Include="Avalonia.Android" Version="11.3.11" />
|
||||
<PackageVersion Include="AvaloniaEdit.TextMate" Version="11.3.0" />
|
||||
<PackageVersion Include="Deadpikle.AvaloniaProgressRing" Version="0.10.11-preview20251127001" />
|
||||
<PackageVersion Include="DialogHost.Avalonia" Version="0.10.4" />
|
||||
<PackageVersion Include="Jint" Version="4.4.2" />
|
||||
<PackageVersion Include="Jint" Version="4.5.0" />
|
||||
<PackageVersion Include="Microsoft.Maui.Graphics" Version="10.0.11" />
|
||||
<PackageVersion Include="Microsoft.Maui.Graphics.Skia" Version="10.0.11" />
|
||||
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
|
||||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageVersion Include="PanAndZoom" Version="11.3.9" />
|
||||
<PackageVersion Include="PanAndZoom" Version="11.3.9.1" />
|
||||
<PackageVersion Include="SkiaSharp" Version="3.116.1" />
|
||||
<PackageVersion Include="SkiaSharp.NativeAssets.WebAssembly" Version="3.116.1" />
|
||||
<PackageVersion Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.0.1.15" />
|
||||
|
||||
@@ -47,6 +47,44 @@ namespace Progrart.Core
|
||||
{
|
||||
return surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100);
|
||||
}
|
||||
public SKData ToData(SKEncodedImageFormat format)
|
||||
{
|
||||
return surface.Snapshot().Encode(format, 100);
|
||||
}
|
||||
public SKData ToData(string extension)
|
||||
{
|
||||
SKEncodedImageFormat format = SKEncodedImageFormat.Png;
|
||||
if (extension.StartsWith('.'))
|
||||
extension = extension[1..];
|
||||
switch (extension)
|
||||
{
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
format = SKEncodedImageFormat.Jpeg;
|
||||
break;
|
||||
case "bmp":
|
||||
format = SKEncodedImageFormat.Bmp;
|
||||
break;
|
||||
case "webp":
|
||||
format = SKEncodedImageFormat.Webp;
|
||||
break;
|
||||
case "ico":
|
||||
format = SKEncodedImageFormat.Ico;
|
||||
break;
|
||||
case "heif":
|
||||
format = SKEncodedImageFormat.Heif;
|
||||
break;
|
||||
case "avif":
|
||||
format = SKEncodedImageFormat.Avif;
|
||||
break;
|
||||
case "jxl":
|
||||
format = SKEncodedImageFormat.Jpegxl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return surface.Snapshot().Encode(format, 100);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (isDisposed) return;
|
||||
|
||||
@@ -34,10 +34,11 @@ namespace Progrart.Core.ProjectSystem
|
||||
return;
|
||||
using var reader = new StreamReader(stream);
|
||||
var img = executor.RenderImage(item.Size, reader.ReadToEnd(), args);
|
||||
using var img_stream = await provider.TryOpenWrite(Path.Combine(project.OutputDir, item.Target ?? item.Source + ".png"));
|
||||
var outputFile= Path.Combine(project.OutputDir, item.Target ?? item.Source + ".png");
|
||||
using var img_stream = await provider.TryOpenWrite(outputFile);
|
||||
if (img_stream is null)
|
||||
return;
|
||||
img.DrawingCore.ToData().SaveTo(img_stream);
|
||||
img.DrawingCore.ToData(Path.GetExtension(outputFile)).SaveTo(img_stream);
|
||||
img_stream.Flush();
|
||||
}
|
||||
public async Task Build(string targetConfig, int maxJobCount = 1)
|
||||
|
||||
26
Progrart.Core/Storage/CombinedStorageProvider.cs
Normal file
26
Progrart.Core/Storage/CombinedStorageProvider.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Progrart.Core.Storage
|
||||
{
|
||||
public class CombinedStorageProvider : IStorageProvider
|
||||
{
|
||||
public List<IStorageProvider> providers = new List<IStorageProvider>();
|
||||
public async Task<Stream?> TryOpenRead(string path)
|
||||
{
|
||||
foreach (var item in providers)
|
||||
{
|
||||
var v = await item.TryOpenRead(path);
|
||||
if (v != null) return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<Stream?> TryOpenWrite(string path)
|
||||
{
|
||||
foreach (var item in providers)
|
||||
{
|
||||
var v = await item.TryOpenWrite(path);
|
||||
if (v != null) return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,5 @@
|
||||
<Deploy Solution="Debug|*" />
|
||||
</Project>
|
||||
<Project Path="Progrart/Progrart.csproj" />
|
||||
<Project Path="ProgrartC/ProgrartC.csproj" Id="f19950d7-8370-4c11-bb90-0cfd4ab94f57" />
|
||||
</Solution>
|
||||
|
||||
47
ProgrartC/Dockerfile
Normal file
47
ProgrartC/Dockerfile
Normal file
@@ -0,0 +1,47 @@
|
||||
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
# These ARGs allow for swapping out the base used to make the final image when debugging from VS
|
||||
ARG LAUNCHING_FROM_VS
|
||||
# This sets the base image for final, but only if LAUNCHING_FROM_VS has been defined
|
||||
ARG FINAL_BASE_IMAGE=${LAUNCHING_FROM_VS:+aotdebug}
|
||||
|
||||
# This stage is used when running from VS in fast mode (Default for Debug configuration)
|
||||
FROM mcr.microsoft.com/dotnet/runtime:10.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
|
||||
|
||||
# This stage is used to build the service project
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
# Install clang/zlib1g-dev dependencies for publishing to native
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
clang zlib1g-dev
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["Directory.Packages.props", "."]
|
||||
COPY ["ProgrartC/ProgrartC.csproj", "ProgrartC/"]
|
||||
RUN dotnet restore "./ProgrartC/ProgrartC.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/ProgrartC"
|
||||
RUN dotnet build "./ProgrartC.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
# This stage is used to publish the service project to be copied to the final stage
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./ProgrartC.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true
|
||||
|
||||
# This stage is used as the base for the final stage when launching from VS to support debugging in regular mode (Default when not using the Debug configuration)
|
||||
FROM base AS aotdebug
|
||||
USER root
|
||||
# Install GDB to support native debugging
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
gdb
|
||||
USER app
|
||||
|
||||
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
|
||||
FROM ${FINAL_BASE_IMAGE:-mcr.microsoft.com/dotnet/runtime-deps:10.0} AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["./ProgrartC"]
|
||||
102
ProgrartC/Program.cs
Normal file
102
ProgrartC/Program.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using Progrart.Core;
|
||||
using Progrart.Core.JSExecution;
|
||||
using Progrart.Core.ProjectSystem;
|
||||
using Progrart.Core.Storage;
|
||||
using SkiaSharp;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ProgrartC
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
|
||||
List<string> includePaths = new List<string>();
|
||||
string? sourceFile = null;
|
||||
string? outputFile = null;
|
||||
ExecuteArguments executeArguments = new ExecuteArguments();
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
string? item = args[i];
|
||||
switch (item)
|
||||
{
|
||||
case "-I":
|
||||
i++;
|
||||
includePaths.Add(args[i]);
|
||||
break;
|
||||
case "-o":
|
||||
i++;
|
||||
outputFile = args[i];
|
||||
break;
|
||||
default:
|
||||
if (File.Exists(item))
|
||||
{
|
||||
sourceFile = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.StartsWith("-D"))
|
||||
{
|
||||
string define = item.Substring(2);
|
||||
string[] parts = define.Split('=', 2);
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
executeArguments.data[parts[0]] = parts[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
executeArguments.data[parts[0]] = "true";
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sourceFile == null)
|
||||
{
|
||||
Trace.WriteLine("no input files");
|
||||
return;
|
||||
}
|
||||
if (outputFile == null)
|
||||
{
|
||||
outputFile = Path.ChangeExtension(sourceFile, "png");
|
||||
}
|
||||
CombinedStorageProvider storageProvider = new();
|
||||
{
|
||||
FileInfo sourceFileInfo = new FileInfo(sourceFile);
|
||||
if (sourceFileInfo.Directory is not null)
|
||||
includePaths.Add(sourceFileInfo.Directory.FullName);
|
||||
}
|
||||
if (!includePaths.Contains("."))
|
||||
includePaths.Add(".");
|
||||
foreach (var item in includePaths)
|
||||
{
|
||||
storageProvider.providers.Add(new ClassicStorageProvider(new DirectoryInfo(item)));
|
||||
}
|
||||
int Scale = 1024;
|
||||
|
||||
if (executeArguments.data.TryGetValue("Scale", out var scale))
|
||||
{
|
||||
if (!int.TryParse(scale, out Scale)) Scale = 1024;
|
||||
}
|
||||
ProgrartExecutor progrartExecutor = new(storageProvider);
|
||||
var result = progrartExecutor.RenderImage(Scale, File.ReadAllText(sourceFile), executeArguments);
|
||||
|
||||
SKData data;
|
||||
FileInfo fi = new FileInfo(outputFile);
|
||||
var ext = fi.Extension.ToLower();
|
||||
data = result.DrawingCore.ToData(ext);
|
||||
if(data is null)
|
||||
{
|
||||
Trace.WriteLine("failed to encode image");
|
||||
return;
|
||||
}
|
||||
using var img_stream = File.OpenWrite(outputFile);
|
||||
if (img_stream is null)
|
||||
return;
|
||||
data.SaveTo(img_stream);
|
||||
img_stream.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
ProgrartC/ProgrartC.csproj
Normal file
21
ProgrartC/ProgrartC.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishAot>true</PublishAot>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Progrart.Core\Progrart.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
10
ProgrartC/Properties/launchSettings.json
Normal file
10
ProgrartC/Properties/launchSettings.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"profiles": {
|
||||
"ProgrartC": {
|
||||
"commandName": "Project"
|
||||
},
|
||||
"Container (Dockerfile)": {
|
||||
"commandName": "Docker"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user