Skip to content

Commit

Permalink
Use a template instead of redirecting when visiting the web root
Browse files Browse the repository at this point in the history
With Github builds not being local, simply redirecting to /builds isn't very useful, so instead create a landing page with customizable templates.
  • Loading branch information
toolstack authored and julianxhokaxhiu committed Apr 19, 2022
1 parent dd4ecea commit d485219
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 4 deletions.
14 changes: 13 additions & 1 deletion lineageota.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"EnableLocalCache": false,
"EnableGithubCache": true,
"LocalCacheTimeout": 86400,
"GithubCacheTimeout": 86400
"GithubCacheTimeout": 86400,
"OTAListTemplate": "ota-list-table",
"BrandName": "",
"LocalHomeURL": "",
"GithubHomeURL": "",
"DeviceNames": {
"kebab": "8T",
"lemonade": "9"
},
"DeviceVendors": {
"kebab": "Oneplus",
"lemonade": "Oneplus"
}
}
]
81 changes: 78 additions & 3 deletions src/CmOta.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

use \DotNotation;
use \Flight;

use \JX\CmOta\Helpers\CurlRequest;

class CmOta {
Expand Down Expand Up @@ -103,16 +103,87 @@ public function loadConfigJSON( $file ) {
*/
public function run() {
Flight::start();

return $this;
}

/* Utility / Internal */

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

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

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

// Time to setup some variables for use later.
$builds = Flight::builds()->get();
$buildsToSort = array();
$output = '';
$model = 'Unknown';
$deviceNames = Flight::cfg()->get('DeviceNames');
$vendorNames = Flight::cfg()->get('DeviceVendors');
$parsedFilenames = array();
$githubURL = '';

if( ! is_array( $deviceNames ) ) { $deviceNames = array(); }

// Loop through the builds to do some setup work
foreach( $builds as $build ) {
// Split the filename using the parser in the build class to get some details
$filenameParts = Flight::build()->parseFilenameFull( $build['filename'] );

// Same the parsed filesnames for later use in the template
$parsedFilenames[$build['filename']] = $filenameParts;

// In case no Github URL was configured, see if we can get it from an existing Github repo
if( $githubURL == '' && strstr( $build['url'], 'github.com' ) ) {
$path = parse_url( $build['url'], PHP_URL_PATH );
$pathParts = explode( '/', $path );
$githubURL = 'https://github.com/' . $pathParts[1];
}

// Add the build to a list based on model names
$buildsToSort[$filenameParts['model']][] = $build;
}

// Sort the array based on model name
ksort( $buildsToSort );

// Sort the entries in each model based on time/date
foreach( $buildsToSort as $model => $sort ) {
usort( $sort, array( $this, 'compareByTimeStamp' ) );
}

// Setup branding information for the template
$branding = array( 'name' => Flight::cfg()->get('BrandName'),
'GithubURL' => Flight::cfg()->get('GithubHomeURL'),
'LocalURL' => Flight::cfg()->get('LocalHomeURL')
);

// Sanity check the branding, use some reasonable deductions if anything is missing
if( $branding['name'] == '' && is_array( $parsedFilenames ) ) { $branding['name'] = reset( $parsedFilenames )['type']; }
if( $branding['GithubURL'] == '' ) { $branding['GithubURL'] = $githubURL; }
if( $branding['LocalURL'] == '' ) { $branding['LocalURL'] = Flight::cfg()->get( 'basePath' ) . '/builds'; }

// Render the template
Flight::render( $templateName,
array( 'builds' => $builds,
'sortedBuilds' => $buildsToSort,
'parsedFilenames' => $parsedFilenames,
'deviceNames' => $deviceNames,
'vendorNames' => $vendorNames,
'branding' => $branding,
)
);
});

// Main call
Expand Down Expand Up @@ -183,5 +254,9 @@ private function initBuilds() {
Flight::register( 'builds', '\JX\CmOta\Helpers\Builds', array(), function( $builds ) {
// Do nothing for now
});

Flight::register( 'build', '\JX\CmOta\Helpers\Build', array(), function( $build ) {
// Do nothing for now
});
}
}
13 changes: 13 additions & 0 deletions views/ota-list-simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>LineageOTA Builds for <?php echo $branding['name']; ?></title>
</head>
<body>
<h1>Currently available builds for <?php echo $branding['name']; ?></h1>
<?php
foreach( $builds as $build ) {
echo "<a href=" . $build['url'] . "'>" . $build['filename'] . '</a><br>' . PHP_EOL;
}
?>
</body>
</html>
87 changes: 87 additions & 0 deletions views/ota-list-tables.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
h1 {
text-align: center;
}

h2 {
text-align: center;
margin-bottom: 0px;
}

h3 {
text-align: center;
margin-top: 0px;
}

table {
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
}

th {
vertical-align: bottom;
background: blue;
color: white;
font-weight: bold;
padding: 5px;
border: 0px;
}

tr:nth-child(even) {
background-color: lightgrey;
}

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

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

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

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

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

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

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

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

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

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

ul {
text-align: center;
}

li {
display: inline-block;
padding-right: 10px;
}

ul li:not(:first-child)::before {
content: "\00b7 ";
}

69 changes: 69 additions & 0 deletions views/ota-list-tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<html>
<head>
<title>LineageOTA Builds for <?php echo $branding['name']; ?></title>
<link rel="stylesheet" href="views/ota-list-tables.css">
</head>
<body>
<h1>Currently available builds for <?php echo $branding['name']; ?></h1>
<ul>
<?php
foreach( $sortedBuilds as $model => $build ) {
if( array_key_exists( $model, $deviceNames ) ) {
echo '<li><a href="#' . $model . '">' . $model . '</a> (' . $vendorNames[$model] . ' ' . $deviceNames[$model] . ')</li>' . PHP_EOL;
} else {
echo '<li><a href="#' . $model . '">' . $model . '</a></li>' . PHP_EOL;
}
}
?>
</ul>

<p><hr /></p>

<?php
foreach( $sortedBuilds as $model => $builds ) {
if( array_key_exists( $model, $deviceNames ) ) {
echo '<h2 id="' . $model . '">' . $model . '</h2>' . PHP_EOL;
echo '<h3>(' . $vendorNames[$model] . ' ' . $deviceNames[$model] . ')</h3>' . PHP_EOL;
} else {
echo '<h2 id="' . $model . '">' . $model . '</h2>' . PHP_EOL;
}
?>
<table>
<thead>
<tr>
<th>Filename</th>
<th>Source</th>
<th>Date<br>(Y//M/D)</th>
<th>Channel</th>
<th>Version</th>
<th>MD5 Checksum</th>
</tr>
</thead>

<tbody>
<?php
foreach( $builds as $build ) {
$source = '<a href="' . $branding['LocalURL'] . '">local</a>';
if( strstr( $build['url'], 'github.com' ) ) { $source = '<a href="' . $branding['GithubURL'] . '">Github</a>'; }

echo "\t\t<tr>" . PHP_EOL;
echo "\t\t\t<td><a href=" . $build['url'] . "'>" . $build['filename'] . '</a></td>' . PHP_EOL;
echo "\t\t\t<td>" . $source . '</td>' . PHP_EOL;
echo "\t\t\t<td>" . date( 'Y/m/d', $build['timestamp'] ) . '</td>' . PHP_EOL;
echo "\t\t\t<td>" . $build['channel'] . '</td>' . PHP_EOL;
echo "\t\t\t<td>" . $build['version'] . '</td>' . PHP_EOL;
echo "\t\t\t<td>" . $build['md5sum'] . '</td>' . PHP_EOL;
echo "\t\t</tr>" . PHP_EOL;
}
?>
</tbody>
</table>

<p><hr /></p>

<?php
}
?>

</body>
</html>

0 comments on commit d485219

Please sign in to comment.