Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/mime-sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: MIME database sync

on:
schedule:
- cron: '0 3 * * 1'
workflow_dispatch:

jobs:
sync:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Run MIME sync tool
run: dotnet run --project ManagedCode.MimeTypes.Sync --configuration Release -- --prefer-remote

- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "chore: sync MIME database"
title: "chore: sync MIME database"
body: |
Automated update of MIME database from configured sources.
branch: chore/sync-mime-database
delete-branch: true
labels: |
automation
dependencies
22 changes: 16 additions & 6 deletions ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace ManagedCode.MimeTypes.Generator;

[Generator]

Check warning on line 13 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This compiler extension should not be implemented in an assembly with target framework '.NET 8.0'. References to other target frameworks will cause the compiler to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1041.md)

Check warning on line 13 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This compiler extension should not be implemented in an assembly with target framework '.NET 9.0'. References to other target frameworks will cause the compiler to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1041.md)

Check warning on line 13 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This compiler extension should not be implemented in an assembly with target framework '.NET 8.0'. References to other target frameworks will cause the compiler to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1041.md)

Check warning on line 13 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

This compiler extension should not be implemented in an assembly with target framework '.NET 9.0'. References to other target frameworks will cause the compiler to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1041.md)
public class MimeTypeSourceGenerator : ISourceGenerator

Check warning on line 14 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'ManagedCode.MimeTypes.Generator.MimeTypeSourceGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 14 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'ManagedCode.MimeTypes.Generator.MimeTypeSourceGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 14 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'ManagedCode.MimeTypes.Generator.MimeTypeSourceGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 14 in ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'ManagedCode.MimeTypes.Generator.MimeTypeSourceGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'
{
public void Initialize(GeneratorInitializationContext context)
{
Expand Down Expand Up @@ -65,13 +65,16 @@

foreach (var item in properties)
{
defineDictionaryBuilder.AppendLine($"MimeTypes.Add(string.Intern(\"{item.Name}\"),string.Intern(\"{item.Value}\"));");
types[ParseKey(item.Name)] = item.Value.ToString();
var extension = item.Name.Trim();
var mimeValue = item.Value.ToString()?.Trim() ?? string.Empty;

defineDictionaryBuilder.AppendLine($"RegisterMimeTypeInternal(\"{Escape(extension)}\", \"{Escape(mimeValue)}\");");
types[ParseKey(extension)] = mimeValue;
}

foreach (var item in types)
{
propertyBuilder.AppendLine($"public static string {item.Key} => \"{item.Value}\";");
propertyBuilder.AppendLine($"public static string {item.Key} => \"{Escape(item.Value)}\";");
}

context.AddSource("MimeHelper.Properties.cs", SourceText.From(@$"
Expand Down Expand Up @@ -124,17 +127,24 @@
return possiblePaths.FirstOrDefault(File.Exists) ?? possiblePaths[0];
}

private string ParseKey(string key)
private static string ParseKey(string key)
{
if (char.IsDigit(key[0]))
{
key = "_" + key;
}
key = key.Replace("-", "_");

key = key.Replace("-", "_").Replace('.', '_');

return key.ToUpperInvariant();
}

private static string Escape(string value)
{
return value
.Replace("\\", "\\\\")
.Replace("\"", "\\\"");
}
}


12 changes: 12 additions & 0 deletions ManagedCode.MimeTypes.Sync/ManagedCode.MimeTypes.Sync.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>
</Project>
Loading
Loading