Skip to content

Commit

Permalink
chore: update-sources action
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdancondorachi committed Dec 20, 2024
1 parent 4fe5a6c commit 3d7e8e6
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 1 deletion.
77 changes: 77 additions & 0 deletions .github/workflows/update-sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Update Grammars and Themes

on:
schedule:
- cron: '0 23 * * 0' # Runs at 11:00 PM (UTC) every Sunday
workflow_dispatch: # Allows manual trigger

jobs:
update-sources:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout repository
- name: Checkout repository
uses: actions/checkout@v4

# Step 2: Set up Node.js for npm updates
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: '18'

# Step 3: Set up PHP and Composer
- name: Set up PHP and Composer
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
tools: composer

# Step 4: Check for updates to Shiki repository
- name: Check for grammars and themes updates
run: |
git clone --depth=1 https://github.com/shikijs/textmate-grammars-themes temp-shiki
latest_commit=$(cd temp-shiki && git rev-parse HEAD)
rm -rf temp-shiki
echo "LATEST_COMMIT=$latest_commit" >> $GITHUB_ENV
# Step 5: Compare with current commit hash stored in a file
- name: Compare latest commit with stored commit
id: check-update
run: |
COMMIT_FILE="latest-shiki-commit.txt"
if [ -f "$COMMIT_FILE" ]; then
STORED_COMMIT=$(cat $COMMIT_FILE)
else
STORED_COMMIT=""
fi
if [ "$STORED_COMMIT" != "$LATEST_COMMIT" ]; then
echo "New updates found. Proceeding with update."
echo "update_required=true" >> $GITHUB_ENV
else
echo "No new updates. Skipping further steps."
echo "update_required=false" >> $GITHUB_ENV
fi
# Step 6: Run npm update and composer update-sources if changes are detected
- name: Run npm update and composer update-sources
if: env.update_required == 'true'
run: |
cd vendor/phiki/phiki
npm install
npm update
composer install
composer run update-sources
cd ../../../
echo "$LATEST_COMMIT" > latest-shiki-commit.txt
# Step 7: Commit and push changes
- name: Commit and push changes
if: env.update_required == 'true'
run: |
git config user.name "GitHub Actions"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "chore: update grammars and themes"
git push https://github-actions:${{ secrets.GH_TOKEN }}@github.com/bogdancondorachi/kirby-code-highlighter.git HEAD:main
1 change: 0 additions & 1 deletion vendor/phiki/phiki/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.github
art
meta
node_modules
resources/samples
tests
Expand Down
15 changes: 15 additions & 0 deletions vendor/phiki/phiki/meta/patches/desktop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [

'(?<=^\\\\S+)=' => '=',

'(?<=^Version.*)\\\\d+(\\\\.{0,1}\\\\d*)' => '\\\\d+(\\\\.{0,1}\\\\d*)',

'(?<=^Exec.*\\\\s)-+\\\\S+' => '-+\\\\S+',

'(?<=^Exec.*)\\\\s\\\\%[fFuUick]\\\\s' => '\\\\s\\\\%[fFuUick]\\\\s',

'(?<=^Categories.*)AudioVideo|(?<=^Categories.*)Audio|(?<=^Categories.*)Video|(?<=^Categories.*)Development|(?<=^Categories.*)Education|(?<=^Categories.*)Game|(?<=^Categories.*)Graphics|(?<=^Categories.*)Network|(?<=^Categories.*)Office|(?<=^Categories.*)Science|(?<=^Categories.*)Settings|(?<=^Categories.*)System|(?<=^Categories.*)Utility' => 'AudioVideo|Audio|Video|Development|Education|Game|Graphics|Network|Office|Science|Settings|System|Utility',

];
7 changes: 7 additions & 0 deletions vendor/phiki/phiki/meta/patches/gdscript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'(?<!/\\\\s*)(\\\\$\\\\s*|%|\\\\$%\\\\s*)(/\\\\s*)?([a-zA-Z_]\\\\w*)\\\\b(?!\\\\s*/)' => '(?<!/\\\\s)(\\\\$\\\\s*|%|\\\\$%\\\\s*)(/\\\\s*)?([a-zA-Z_]\\\\w*)\\\\b(?!\\\\s*/)',

];
9 changes: 9 additions & 0 deletions vendor/phiki/phiki/meta/patches/genie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [

'\\\\s*((//).*$\\\\n?)' => '\\\\s*((\\\\/\\\\/).*$\\\\n?)',

'/((\\\\\\\\/)|([^/]))*/(?=\\\\s*[,;)\\\\.\\\\n])' => '\\\\/((\\\\\\\\\\\\/)|([^\\\\/]))*\\\\/(?=\\\\s*[,;)\\\\.\\\\n])',

];
7 changes: 7 additions & 0 deletions vendor/phiki/phiki/meta/patches/json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'\\\\\\\\(?:[\"\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4})' => '\\\\\\\\(?:[\"\\\\\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4})',

];
7 changes: 7 additions & 0 deletions vendor/phiki/phiki/meta/patches/json5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'(?:\\\\\\\\(?:[\"\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4}))' => '(?:\\\\\\\\(?:[\"\\\\\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4}))',

];
7 changes: 7 additions & 0 deletions vendor/phiki/phiki/meta/patches/jsonc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'\\\\\\\\(?:[\"\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4})' => '\\\\\\\\(?:[\"\\\\\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4})',

];
7 changes: 7 additions & 0 deletions vendor/phiki/phiki/meta/patches/jsonl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'\\\\\\\\(?:[\"\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4})' => '\\\\\\\\(?:[\"\\\\\\\\\\\\/bfnrt]|u[0-9a-fA-F]{4})',

];
13 changes: 13 additions & 0 deletions vendor/phiki/phiki/meta/patches/jsonnet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [

'\\\\\\\\([\\"\\\\\\\\/bfnrt]|(u[0-9a-fA-F]{4}))' => '\\\\\\\\([\\"\\\\\\\\\\\\/bfnrt]|(u[0-9a-fA-F]{4}))',

'\\\\\\\\[^\\"\\\\\\\\/bfnrtu]' => '\\\\\\\\[^\\"\\\\\\\\\\\\/bfnrtu]',

"\\\\\\\\(['\\\\\\\\/bfnrt]|(u[0-9a-fA-F]{4}))" => "\\\\\\\\(['\\\\\\\\\\\\/bfnrt]|(u[0-9a-fA-F]{4}))",

"\\\\\\\\[^'\\\\\\\\/bfnrtu]" => "\\\\\\\\[^'\\\\\\\\\\\\/bfnrtu]",

];
7 changes: 7 additions & 0 deletions vendor/phiki/phiki/meta/patches/typst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'\\\\\\\\([\\\\\\\\/\\\\[\\\\]{}#*_=~`$-.]|u\\\\{[0-9a-zA-Z]*\\\\}?)' => '\\\\\\\\([\\\\\\\\\\\\/\\\\[\\\\]{}#*_=~`$-.]|u\\\\{[0-9a-zA-Z]*\\\\}?)',

];
9 changes: 9 additions & 0 deletions vendor/phiki/phiki/meta/patches/vala.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [

'\\\\s*((//).*$\\\\n?)' => '\\\\s*((\\\\/\\\\/).*$\\\\n?)',

'/((\\\\\\\\/)|([^/]))*/(?=\\\\s*[,;)\\\\.\\\\n])' => '\\\\/((\\\\\\\\\\\\/)|([^\\\\/]))*/(?=\\\\s*[,;)\\\\.\\\\n])',

];
16 changes: 16 additions & 0 deletions vendor/phiki/phiki/meta/stubs/DefaultGrammars.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Phiki\Grammar;

abstract class DefaultGrammars
{
final const NAMES_TO_PATHS = [
'txt' => __DIR__ . '/../../resources/languages/txt.json',
%s
];

final const SCOPES_TO_NAMES = [
'text.txt' => 'txt',
%s
];
}
8 changes: 8 additions & 0 deletions vendor/phiki/phiki/meta/stubs/DefaultThemes.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Phiki\Theme;

abstract class DefaultThemes
{
final const NAMES_TO_PATHS = [%s];
}
16 changes: 16 additions & 0 deletions vendor/phiki/phiki/meta/stubs/Grammar.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Phiki\Grammar;

use Phiki\Contracts\GrammarRepositoryInterface;

enum Grammar: string
{
case Txt = 'txt';
%s

public function toParsedGrammar(GrammarRepositoryInterface $repository): ParsedGrammar
{
return $repository->get($this->value);
}
}
15 changes: 15 additions & 0 deletions vendor/phiki/phiki/meta/stubs/Theme.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Phiki\Theme;

use Phiki\Contracts\ThemeRepositoryInterface;

enum Theme: string
{
%s

public function toParsedTheme(ThemeRepositoryInterface $repository): ParsedTheme
{
return $repository->get($this->value);
}
}
147 changes: 147 additions & 0 deletions vendor/phiki/phiki/meta/update-sources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

use Phiki\Support\Str;

require_once __DIR__.'/../vendor/autoload.php';

function main()
{
$nodeModules = realpath(__DIR__.'/../node_modules');
$grammarsDirectory = realpath($nodeModules.'/tm-grammars/grammars');
$themesDirectory = realpath($nodeModules.'/tm-themes/themes');

$grammars = [];
$themes = [];

echo "Copying grammar files...\n";

eachFile($grammarsDirectory, function (SplFileInfo $grammar) use (&$grammars) {
$json = file_get_contents($grammar->getRealPath());
$basename = rtrim(basename($grammar->getFilename(), $grammar->getExtension()), '.');
$decoded = json_decode($json, true);

// NOTE: We don't support these grammar types yet.
if (isset($decoded['injectionSelector'])) {
return;
}

if (hasPatch($basename)) {
$json = applyPatch($basename, $json);
}

file_put_contents(__DIR__.'/../resources/languages/'.$grammar->getFilename(), $json);

$grammars[] = [
'path' => $grammar->getFilename(),
'name' => $decoded['name'] ?? $basename,
'scopeName' => $decoded['scopeName'],
];
});

echo "Copying theme files...\n";

eachFile($themesDirectory, function (SplFileInfo $theme) use (&$themes) {
copy($theme->getRealPath(), __DIR__.'/../resources/themes/'.$theme->getFilename());

$json = json_decode(file_get_contents($theme->getRealPath()), true);

$themes[] = [
'path' => $theme->getFilename(),
'name' => $json['name'] ?? basename($theme->getFilename(), $theme->getExtension()),
];
});

echo "Generating DefaultGrammars class...\n";

$defaultGrammarsStub = file_get_contents(__DIR__.'/stubs/DefaultGrammars.php.stub');
$namesToPaths = [];
$scopesToNames = [];

foreach ($grammars as $grammar) {
$namesToPaths[] = sprintf('"%s" => __DIR__ . "/../../resources/languages/%s"', $grammar['name'], $grammar['path']);
$scopesToNames[$grammar['scopeName']] = sprintf('"%s" => "%s"', $grammar['scopeName'], $grammar['name']);
}

$namesToPathsString = implode(",\n", $namesToPaths);
$scopesToNamesString = implode(",\n", $scopesToNames);

$defaultGrammarsStub = sprintf($defaultGrammarsStub, $namesToPathsString, $scopesToNamesString);

file_put_contents(__DIR__.'/../src/Grammar/DefaultGrammars.php', $defaultGrammarsStub);

echo "Generating Grammar enum...\n";

$grammarEnumStub = file_get_contents(__DIR__.'/stubs/Grammar.php.stub');
$grammarCases = [];

foreach ($grammars as $grammar) {
$grammarCases[] = sprintf('case %s = "%s";', Str::studly($grammar['name']), $grammar['name']);
}

$grammarCases = implode("\n", $grammarCases);
$grammarEnumStub = sprintf($grammarEnumStub, $grammarCases);

file_put_contents(__DIR__.'/../src/Grammar/Grammar.php', $grammarEnumStub);

echo "Generating DefaultThemes class...\n";

$defaultThemesStub = file_get_contents(__DIR__.'/stubs/DefaultThemes.php.stub');
$namesToPaths = [];

foreach ($themes as $theme) {
$namesToPaths[] = sprintf('"%s" => __DIR__ . "/../../resources/themes/%s"', $theme['name'], $theme['path']);
}

$namesToPathsString = implode(",\n", $namesToPaths);

$defaultThemesStub = sprintf($defaultThemesStub, $namesToPathsString);

file_put_contents(__DIR__.'/../src/Theme/DefaultThemes.php', $defaultThemesStub);

echo "Generating Theme enum...\n";

$themeEnumStub = file_get_contents(__DIR__.'/stubs/Theme.php.stub');
$themeCases = [];

foreach ($themes as $theme) {
$themeCases[] = sprintf('case %s = "%s";', Str::studly($theme['name']), $theme['name']);
}

$themeCases = implode("\n", $themeCases);
$themeEnumStub = sprintf($themeEnumStub, $themeCases);

file_put_contents(__DIR__.'/../src/Theme/Theme.php', $themeEnumStub);

echo "Done!\n";
}

function eachFile(string $path, Closure $callback): void
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));

foreach ($iterator as $file) {
if ($file->isFile()) {
$callback($file);
}
}
}

function hasPatch(string $name): bool
{
return file_exists(__DIR__.'/patches/'.$name.'.php');
}

function applyPatch(string $name, string $json): string
{
echo " Applying patches for {$name}...\n";

$patches = require __DIR__.'/patches/'.$name.'.php';

foreach ($patches as $search => $replace) {
$json = str_replace($search, $replace, $json);
}

return $json;
}

main();

0 comments on commit 3d7e8e6

Please sign in to comment.