Skip to content

Commit

Permalink
Replace Flight templating engine with Twig
Browse files Browse the repository at this point in the history
Also cleanup the default tables template.
  • Loading branch information
toolstack authored and julianxhokaxhiu committed Apr 19, 2022
1 parent 6cff4af commit 8181e0b
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 96 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ There are several configuration settings for temples as follows:
Included Templates:

* ota-list-simple: a simple header and list of files names, no additional details or links provided.
* ota-list-table: a page containing a seires of tables, one per device, that list in date order all builds for that device. Includes jump lists to find devices, links to local/github pages, dates, versions, md4sums, etc.
* ota-list-table: a page containing a seires of tables, one per device, that list in date order all builds for that device. Includes jump lists to find devices, links to local/github pages, dates, versions, md5sums, etc.

Twig is used as the templating language, see their [documentation](https://twig.symfony.com/doc/3.x/) for more details.

## REST Server Unit Testing

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"require": {
"mikecao/flight": "2.*",
"julianxhokaxhiu/dotnotation": "dev-master",
"ext-zip": "*"
"ext-zip": "*",
"twig/twig": "3.*"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion lineageota.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"EnableGithubCache": true,
"LocalCacheTimeout": 86400,
"GithubCacheTimeout": 86400,
"OTAListTemplate": "ota-list-table",
"OTAListTemplate": "ota-list-tables",
"BrandName": "",
"LocalHomeURL": "",
"GithubHomeURL": "",
Expand Down
32 changes: 26 additions & 6 deletions src/CmOta.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public function loadConfigJSON( $file ) {
* @return class Return always itself, so it can be chained within calls
*/
public function run() {
$loader = new \Twig\Loader\FilesystemLoader( Flight::cfg()->get('realBasePath') . '/views' );

$twigConfig = array();

Flight::register( 'twig', '\Twig\Environment', array( $loader, array() ), function ($twig) {
// Nothing to do here
});

Flight::start();

return $this;
Expand All @@ -110,19 +118,27 @@ public function run() {
/* Utility / Internal */

// Used to compare timestamps in the build ksort call inside of initRouting for the "/" route
function compareByTimeStamp( $a, $b ) {
private function compareByTimeStamp( $a, $b ) {
return $a['timestamp'] - $b['timestamp'];
}

// Format a file size string nicely
private function formatFileSize( $bytes, $dec = 2 ) {
$size = array( ' B', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB' );
$factor = floor( ( strlen( $bytes ) - 1 ) / 3 );

return sprintf( "%.{$dec}f", $bytes / pow( 1024, $factor ) ) . @$size[$factor];
}

// Setup Flight's routing information
private function initRouting() {
// Just list the builds folder for now
Flight::route('/', function() {
// Get the template name we're going to use and tack on .php
$templateName = Flight::cfg()->get('OTAListTemplate') . '.php';
// Get the template name we're going to use and tack on .twig
$templateName = Flight::cfg()->get('OTAListTemplate') . '.twig';

// Make sure the template exists, otherwise fall back to our default
if( ! file_exists( 'views/' . $templateName ) ) { $templateName = 'ota-list-tables.php'; }
if( ! file_exists( 'views/' . $templateName ) ) { $templateName = 'ota-list-tables.twig'; }

// Time to setup some variables for use later.
$builds = Flight::builds()->get();
Expand All @@ -132,6 +148,7 @@ private function initRouting() {
$deviceNames = Flight::cfg()->get('DeviceNames');
$vendorNames = Flight::cfg()->get('DeviceVendors');
$parsedFilenames = array();
$formatedFileSizes = array();
$githubURL = '';

if( ! is_array( $deviceNames ) ) { $deviceNames = array(); }
Expand All @@ -151,6 +168,8 @@ private function initRouting() {
$githubURL = 'https://github.com/' . $pathParts[1];
}

$formatedFileSizes[$build['filename']] = $this->formatFileSize( $build['size'], 0 );

// Add the build to a list based on model names
$buildsToSort[$filenameParts['model']][] = $build;
}
Expand All @@ -174,14 +193,15 @@ private function initRouting() {
if( $branding['GithubURL'] == '' ) { $branding['GithubURL'] = $githubURL; }
if( $branding['LocalURL'] == '' ) { $branding['LocalURL'] = Flight::cfg()->get( 'basePath' ) . '/builds'; }

// Render the template
Flight::render( $templateName,
// Render the template with Twig
Flight::twig()->display( $templateName,
array( 'builds' => $builds,
'sortedBuilds' => $buildsToSort,
'parsedFilenames' => $parsedFilenames,
'deviceNames' => $deviceNames,
'vendorNames' => $vendorNames,
'branding' => $branding,
'formatedFileSizes' => $formatedFileSizes,
)
);
});
Expand Down
13 changes: 0 additions & 13 deletions views/ota-list-simple.php

This file was deleted.

11 changes: 11 additions & 0 deletions views/ota-list-simple.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>LineageOTA Builds for {{ branding['name'] }}</title>
</head>
<body>
<h1>Currently available builds for {{ branding['name'] }}</h1>
{% for build in builds %}
<a href="{{ build['url'] }}">{{ build['filename'] }}</a><br>
{% endfor %}
</body>
</html>
10 changes: 5 additions & 5 deletions views/ota-list-tables.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ tr:nth-child(even) {
}

td {
padding: 5px;
padding: 20px;
border: 0px;
}

table th:nth-child(1) {
text-align: left;
text-align: center;
}

table th:nth-child(2) {
text-align: center;
}

table th:nth-child(3) {
text-align: center;
text-align: left;
}

table th:nth-child(4) {
Expand All @@ -56,11 +56,11 @@ table th:nth-child(5) {
text-align: center;
}

table td:nth-child(2) {
table td:nth-child(1) {
text-align: center;
}

table td:nth-child(3) {
table td:nth-child(2) {
text-align: center;
}

Expand Down
69 changes: 0 additions & 69 deletions views/ota-list-tables.php

This file was deleted.

62 changes: 62 additions & 0 deletions views/ota-list-tables.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<html>
<head>
<title>LineageOTA Builds for {{ branding['name'] }}</title>
<link rel="stylesheet" href="views/ota-list-tables.css">
</head>
<body>
<h1>Currently available builds for {{ branding['name'] }}</h1>
<ul>
{% for model,build in sortedBuilds %}
{% if deviceNames[model] is defined %}
<li><a href="#{{ model }}">{{ model }}</a> ({{ vendorNames[model] }} {{ deviceNames[model] }})</li>
{% else %}
<li><a href="#{{ model }}">{{ model }}</a></li>
{% endif %}
{% endfor %}
</ul>

<p><hr /></p>

{% for model,builds in sortedBuilds %}
<h2 id="{{ model }}">{{ model }}</h2>
{% if deviceNames[model] is defined %}
<h3>( {{ vendorNames[model] }} {{ deviceNames[model] }})</h3>
{% endif %}
<table>
<thead>
<tr>
<th>Channel</th>
<th>Version</th>
<th>Filename</th>
<th>Size</th>
<th>Source</th>
<th>Date<br>(Y//M/D)</th>
</tr>
</thead>

<tbody>
{% for build in builds %}
<tr>
<td>{{ build['channel'] }}</td>
<td>{{ build['version'] }}</td>
<td><a href="{{ build['url'] }}">{{ build['filename'] }}</a><br>[md5sum: {{ build['md5sum'] }}]</td>
<td>{% set filename = build['filename'] %}{{ formatedFileSizes[filename] }}</td>

{% if build['url'] matches '/github\.com/' %}
<td><a href="' . {{ branding['LocalURL'] }}">local</a></td>
{% else %}
<td><a href="' . {{ branding['GithubURL'] }}">Github</a></td>
{% endif %}

<td>{{ build['timestamp'] | date('Y/m/d') }}</td>
</tr>
{% endfor %}
</tbody>
</table>

<p><hr /></p>

{% endfor %}

</body>
</html>

0 comments on commit 8181e0b

Please sign in to comment.