Skip to content

Commit

Permalink
Refactor build-lite.sh
Browse files Browse the repository at this point in the history
Instead of hardcoding substitutions, make build-lite.sh accept a syntax
resembling that of the C preprocessor so that lite-specific changes can
be made more easily.
This commit also brings the lite-specific changes up to date with
recent changes to the frontend codebase.
  • Loading branch information
psvenk committed Apr 24, 2020
1 parent 3393b11 commit d61f403
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
52 changes: 20 additions & 32 deletions build-lite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Note: This script has only been tested with Bash in a GNU environment
# (on Linux); your mileage may vary on BSD, macOS, Cygwin/MSYS (Windows), etc.

rm -r dist-lite/
rm -rf dist-lite/
mkdir -p dist-lite/

cp -r public/* dist-lite/
Expand Down Expand Up @@ -34,46 +34,34 @@ do
done
# https://stackoverflow.com/a/21256704

cp -r ./node_modules/@fortawesome/fontawesome-free/webfonts/ dist-lite/fonts/fontawesome/webfonts
cp -r ./node_modules/@fortawesome/fontawesome-free/webfonts/ \
dist-lite/fonts/fontawesome/webfonts
# Copy Font Awesome files

rm dist-lite/login.html
# Remove login page

sed '/Logout/d' -i dist-lite/home.html
# Remove "Logout" button
perl -0777 -pi -e 's[<!--#ifdef lite>\n((.|\n)*?)\n<#endif-->][\1]g' \
dist-lite/home.html

sed -e '1h;2,$H;$!d;g' -e 's/\
\$\.ajax({\
\ \ \ \ url:\ "\/data.*then(responseCallback);/\
responseCallback({\
classes: [],\
recent: {\
recentActivityArray: [],\
recentAttendanceArray: []\
},\
overview: [],\
username: "",\
quarter: "0"\
});/' \
-i dist-lite/js/home.js
perl -0777 -pi -e 's[<!--#ifndef lite-->\n((.|\n)*?)\n<!--#endif-->][]g' \
dist-lite/home.html

sed -e '1h;2,$H;$!d;g' -e 's/\
\$\.ajax({\
\ \ \ \ url:\ "schedule.json.*then(schedulesCallback);/\
schedulesCallback(\
);/' \
-i dist-lite/js/clock.js
perl -0777 -pi -e 's[//#ifdef lite\n/\*\n((.|\n)*?)\n\*/\n//#endif][\1]g' \
dist-lite/js/*.js

sed -e '/schedulesCallback(/r dist-lite/schedule.json' \
-i dist-lite/js/clock.js
perl -0777 -pi -e 's[//#ifndef lite\n((.|\n)*?)\n//#endif][]g' \
dist-lite/js/*.js

# Replace AJAX calls with initialization using blank data
# https://unix.stackexchange.com/a/235016
# https://unix.stackexchange.com/a/32912
# Preprocess HTML and JS files to make lite-specific changes as needed
# https://stackoverflow.com/a/1103177
# https://stackoverflow.com/a/5869735

version="$(grep version package.json | sed 's/.*: "//' | sed 's/",//')"
# Get version number from package.json
sed -i -e '/#include dist-lite\/schedule.json/r dist-lite/schedule.json' \
dist-lite/js/clock.js
# Include contents of schedule.json
# https://unix.stackexchange.com/a/32912

sed -i 's/await \$\.ajax("\/version")/"'$version'"/g' dist-lite/js/*
version="$(git describe)"
sed -i 's/\/\/#include $version/"'$version'"/g' dist-lite/js/*
# Hard-code version number
2 changes: 2 additions & 0 deletions public/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ <h4 id="title">Aspine</h4>
<button class="tablinks" onclick="openTab(event, 'clock')">Clock</button>
<!--<button class="tablinks" onclick="openTab(event, 'calendar-tab')">Calendar</button>-->
<button class="tablinks" onclick="openTab(event, 'reports')" id="reports_open">Reports</button>
<!--#ifndef lite-->
<button class="tablinks tablinks-right" onclick="location.href='/logout'">Logout</button>
<!--#endif-->
<div class="GPA gpa_custom-select custom-select" style="width:270px;">
<select id="gpa_select">
<option id="init_gpa" value="0">Current Quarter GPA: </option>
Expand Down
24 changes: 24 additions & 0 deletions public/js/buttonFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,19 @@ let exportTableData = async function(prefs) {
resetTableData();

let obj = {};

//#ifndef lite
obj.version = await $.ajax("/version");
//#endif

//#ifdef lite
/*
obj.version = (
//#include $version
);
*/
//#endif

obj.username = currentTableData.username;
obj.overview = currentTableData.overview;

Expand Down Expand Up @@ -355,7 +367,19 @@ let exportTableData = async function(prefs) {
};

let importTableData = async function(obj) {

//#ifndef lite
let version = await $.ajax("/version");
//#endif

//#ifdef lite
/*
let version = (
//#include $version
);
*/
//#endif

if (
obj.version.match(/^v?(\d+)/)[1] !== version.match(/^v?(\d+)/)[1]
) {
Expand Down
9 changes: 9 additions & 0 deletions public/js/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ let schedulesCallback = function(response) {
}, 1000);
};

//#ifndef lite
$.ajax({
url: "schedule.json",
method: "GET"
}).then(schedulesCallback);
//#endif
//#ifdef lite
/*
schedulesCallback(
//#include dist-lite/schedule.json
);
*/
//#endif

function drawHand(ctx, radius, pos, length, width) {
ctx.strokeStyle = 'white';
Expand Down
19 changes: 19 additions & 0 deletions public/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,16 +1026,35 @@ $("#import_button").click(async () => {
});
});

//#ifndef lite
$.ajax({
url: "/data",
method: "POST",
data: { quarter: 0 },
dataType: "json json",
}).then(responseCallback);
//#endif

//#ifdef lite
/*
responseCallback({ nologin: true });
*/
//#endif

document.getElementById("default_open").click();

// Populate the version number at the bottom of the page.
// Pointfree style does not work here because jQuery's .text behaves both as
// an attribute and as a function.

//#ifndef lite
$.ajax("/version").then(ver => $("#version").text(ver));
//#endif

//#ifdef lite
/*
$("#version").text(
//#include $version
);
*/
//#endif

0 comments on commit d61f403

Please sign in to comment.