Skip to content

Commit

Permalink
Update code with new build process
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas-Man committed Mar 22, 2024
1 parent bdb4165 commit 9fccd4a
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .clasp.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"scriptId":"1z3zhO0nbuORRJg_f96axDu7wKbiqfk0H4t3jGcNxWebiDuled3DNjNxo","rootDir":"/Users/aspann/Documents/Development/google/javascript/BerlitzSchedule/code"}
{"scriptId":"1z3zhO0nbuORRJg_f96axDu7wKbiqfk0H4t3jGcNxWebiDuled3DNjNxo","rootDir":"/Users/aspann/Documents/Development/google/javascript/BerlitzSchedule/dist"}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
code/configuration.js
code/.clasp.json
node_modules/**
dist/**
3 changes: 3 additions & 0 deletions .prettyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!dist2/*.js
!dist/*.js
**/**
34 changes: 34 additions & 0 deletions bin/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

BASEDIR=$(pwd)
SOURCE=$BASEDIR/code
DEST=$BASEDIR/dist

APPSCRIPT='appsscript.json'

# Make dir if it does not exist.
mkdir -p ${DEST}

# Clear Distribution Directly for update
echo "Clearing ${DEST}..."
rm -v ${DEST}/*
echo "Done."

# Copy AppsScript JSON file
cp ${BASEDIR}/${APPSCRIPT} ${DEST}

for FULL_PATH_NAME in $(ls ${SOURCE}/*.js); do
FILE=$(basename ${FULL_PATH_NAME})
egrep -v '(^i|^\/* eslint-disable)' ${FULL_PATH_NAME} |
while read mLine; do
echo ${mLine} >>${DEST}/${FILE}
done

# There is an issue with this script. Its adding /Applications/ and other folders. This ia temp fix
sed '/^\/Application/d' ${DEST}/${FILE} >/tmp/temp.txt && mv /tmp/temp.txt ${DEST}/${FILE}
# Report exports at the end of the file This is used as these may span multiple lines
sed '/^export {/,/};/d' ${DEST}/${FILE} >/tmp/temp.txt && mv /tmp/temp.txt ${DEST}/${FILE}
done

# Fix code formatting after striping import and export statements
npx prettier ${DEST} --write
47 changes: 27 additions & 20 deletions code/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function setDateObject(date, month, year, time) {
function lookupLocation(location) {
// This function takes the short LC name and looks up the full name which is used in
// Google Maps and hopefully Apple Maps Allowing for calendar apps to provide map directions
// eslint-disable-next-line no-param-reassign
if (!locations[location]) location = '';
// eslint-disable-next-line no-param-reassign
else location = locations[location];
return location;
}
Expand Down Expand Up @@ -47,9 +49,19 @@ function createDetails(unit) {
return details;
}

function makeEventTitle(unit) {
let title = unit.type;
if (unit.isBonus) {
title += ' - Bonus';
}
title += ` (${unit.count})`;
return title;
}

// Add a single event to the calendar
function addUnitToCalendar(calendar, date, month, year, unit) {
if (unit.type === 'Vacation' || (unit.type === 'Blocked' && !unit.comment.includes('onus'))) {
// Blank
} else {
const details = createDetails(unit);
const event = calendar.createEvent(
Expand All @@ -65,15 +77,6 @@ function addUnitToCalendar(calendar, date, month, year, unit) {
}
}

function makeEventTitle(unit) {
let title = unit.type;
if (unit.isBonus) {
title += ' - Bonus';
}
title += ` (${unit.count})`;
return title;
}

function openCalendar() {
// Gets the public calendar named "BerlitzWork" using its ID.
const calendar = CalendarApp.getCalendarById(calendarID);
Expand All @@ -85,6 +88,19 @@ function openCalendar() {
return calendar;
}

// To avoid duplicate calendar entries when the schedule has been resent.
// Delete existing entries if they exist.
function deleteExistingEvents(calendar, schedule) {
const events = calendar.getEventsForDay(setDateObject(schedule.date, schedule.month, schedule.year, '00:00'));
if (events.length > 0) {
Logger.log(`Deleting all events for ${schedule.date} the ${schedule.month}, ${schedule.year}`);
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const i in events) {
events[i].deleteEvent();
}
}
}

// Loop through all units for the given schedule
// call addUnitToCalendar for each unit
function addUnitsToCalendar(calendar, schedule) {
Expand All @@ -94,18 +110,9 @@ function addUnitsToCalendar(calendar, schedule) {
let i = 0;
while (i < schedule.units.length) {
addUnitToCalendar(calendar, schedule.date, schedule.month, schedule.year, schedule.units[i]);
// eslint-disable-next-line no-plusplus
i++;
}
}

// To avoid duplicate calendar entries when the schedule has been resent.
// Delete existing entries if they exist.
function deleteExistingEvents(calendar, schedule) {
const events = calendar.getEventsForDay(setDateObject(schedule.date, schedule.month, schedule.year, '00:00'));
if (events.length > 0) {
Logger.log(`Deleting all events for ${schedule.date} the ${schedule.month}, ${schedule.year}`);
for (const i in events) {
events[i].deleteEvent();
}
}
}
export { openCalendar, addUnitsToCalendar };
22 changes: 7 additions & 15 deletions code/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,16 @@ const months = {
December: 57,
};

const basic_lessons = ['Private', 'Method', 'OutService', 'Office'];
const pl_bonus_times = ['07:00', '07:45'];
const basicLessons = ['Private', 'Method', 'OutService', 'Office'];
const plBonusTimes = ['07:00', '07:45'];

// Regular Expression Patterns
const trvl_blk_regex =
const trvlBlkRegex =
/(?<startTime>\d{2}:\d{2})\s(?:AM|PM)\s-\s(?<endTime>\d{2}:\d{2})\s(?:AM|PM)\s[(](?<count>\d{1,2})\sUnit[s]?[)]\s?\r?\n(?=Type)Type:\s(?<type>\w+)\r?\n(?=Comment)Comments:\s(?<comment>(?:(?![-]{20}).)*)/gi;
const lessons_pv_regex_nonzoom =
const lessonsPvRegexNonzoom =
/(?<startTime>\d{2}:\d{2})\s(?:AM|PM)\s-\s(?<endTime>\d{2}:\d{2})\s(?:AM|PM)\s[(](?<count>\d{1,2})\sUnit[s]?[)]\s?\r?\n(?:(?=Location)Location:\s(?<location>\w+(?:\s?\w+))(?:\s[(]Room\s[N]?\d{1,2}[)])?|(?!Location)(?=Class)Class:\s(?<class>(?:(?!\n).)*))\r?\nType:\s(?<type>(?:(?!\n).)*)\r?\n(?:(?=Material)Material:\s(?<material>(?:(?!\n).)*)\r?\n|(?!Material))(?:(?=Comment)Comments:\s(?<comment>(?:(?!\r?\n).)*(?:\r?\n)?(?:(?!-{20}).)*)|(?!Comments))/gi;
const lessons_pv_regex =
const lessonsPvRegex =
/(?<startTime>\d{2}:\d{2})\s(?:AM|PM)\s-\s(?<endTime>\d{2}:\d{2})\s(?:AM|PM)\s[(](?<count>\d{1,2})\sUnit[s]?[)]\s?\r?\n(?:(?=Location)Location:\s(?<location>\w+(?:\s?\w+))(?:\s[(]Room\s[N]?\d{1,3}[)])?|(?!Location)(?=Class)Class:\s(?<class>(?:(?!\n).)*))\r?\n(?:(?=-- Delivered)-- Delivered via (?<zoom>Zoom) --\r?\n|(?!Delivered))Type:\s(?<type>(?:(?!\n).)*)\r?\n(?:(?=Material)Material:\s(?<material>(?:(?!\n).)*)\r?\n|(?!Material))(?:(?=Comment)Comments:\s(?<comment>(?:(?!\r?\n).)*(?:\r?\n)?(?:(?!-{20}).)*)|(?!Comment))/gi;
const date_regex = /(?<day>\w+)\s(?<month>\w+)\s(?<date>\d{1,2})(?:st|nd|rd|th),\s(?<year>\d{4})$/im;
const dateRegex = /(?<day>\w+)\s(?<month>\w+)\s(?<date>\d{1,2})(?:st|nd|rd|th),\s(?<year>\d{4})$/im;

export {
months,
basic_lessons,
pl_bonus_times,
trvl_blk_regex,
lessons_pv_regex,
lessons_pv_regex_nonzoom,
date_regex,
};
export { months, basicLessons, plBonusTimes, trvlBlkRegex, lessonsPvRegex, lessonsPvRegexNonzoom, dateRegex };
1 change: 1 addition & 0 deletions code/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function getRelevantMessages(filter) {
Logger.log('Getting Schedule Emails to be processed');
const threads = GmailApp.search(filter);
const messages = [];
// eslint-disable-next-line func-names
threads.forEach(function (thread) {
const count = thread.getMessageCount();
if (count > 1) {
Expand Down
3 changes: 0 additions & 3 deletions code/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ function doNightly() {
doProcessing(nightlyFilter);
}

global.doGet = doGet;
global.updateSchedule = updateSchedule;
global.doNightly = doNightly;
17 changes: 12 additions & 5 deletions code/processing.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import { getRelevantMessages } from './mail';
import { writeSpreadSheet } from './configuration';
import { openSpreadSheet } from './spreadsheet';
import { getRelevantMessages, labelMessageAsDone } from './mail';
import { writeSpreadSheet, nightlyFilter } from './configuration';
import { openSpreadSheet, saveDataToSheet } from './spreadsheet';
import { openCalendar, addUnitsToCalendar } from './calendar';
import { lessonsScheduledToday, newSchedule, matchTravelBlock, matchLessons } from './schedule';
import { tallyAndAssignUnits, findFirstUnit, addUnitsToSchedule } from './units';

function processMessages(messages) {
let ss;
if (writeSpreadSheet) {
ss = openSpreadSheet();
if (ss == -1) {
if (ss === -1) {
Logger.log('Unable to open Spreadsheet\nExiting Script\n');
return ss;
}
}

const cal = openCalendar();
if (cal == -1) {
if (cal === -1) {
Logger.log('Unable to open Calendar\nExiting Script\n');
return cal;
}

// process oldest message to newest
let message = messages.length;
while (message > 0) {
// eslint-disable-next-line no-plusplus
--message;
const subject = messages[message].getSubject();
const body = messages[message].getPlainBody();
if (!lessonsScheduledToday(body)) {
Logger.log(`Email: ${subject}`);
labelMessageAsDone(messages[message]);
// eslint-disable-next-line no-continue
continue; // Everything is ok. But no messages to process
} else {
// Create schedule object
Expand Down Expand Up @@ -67,6 +72,7 @@ function NightlyProcessing() {
const messages = getRelevantMessages(nightlyFilter);
// A check to see if message is empty could be used to exist this function earlier.
Logger.log('Retrieved relevant messages');
// eslint-disable-next-line no-unused-vars
status = processMessages(messages);
// status is currently unused. Could be used to send email notification in the future.
Logger.log('Processing Completed.');
Expand All @@ -79,6 +85,7 @@ function doProcessing(filter) {
const messages = getRelevantMessages(filter);
// A check to see if message is empty could be used to exist this function earlier.
Logger.log('Retrieved relevant messages');
// eslint-disable-next-line no-unused-vars
status = processMessages(messages);
// status is currently unused. Could be used to send email notification in the future.
Logger.log('Processing Completed.');
Expand Down
9 changes: 5 additions & 4 deletions code/schedule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-restricted-syntax */
import { restDay } from './configuration';
import { date_regex, trvl_blk_regex, lessons_pv_regex } from './constants';
import { dateRegex, trvlBlkRegex, lessonsPvRegex } from './constants';
import { createDefaultUnit } from './units';

function createScheduleObject() {
Expand All @@ -14,6 +14,7 @@ function createScheduleObject() {
schedule.bonuses = 0;
schedule.restDay = 0;
schedule.pv = 0;
// eslint-disable-next-line func-names
schedule.isRestDay = function () {
if (this.day === restDay) {
return true;
Expand All @@ -26,7 +27,7 @@ function createScheduleObject() {
// Get email date information and initialize object
function newSchedule(subject) {
Logger.log('Creating Schedule Object and matching Date information');
const match = subject.match(date_regex);
const match = subject.match(dateRegex);

if (match.length < 4) {
Logger.log('Unable to match Date information for Schedule');
Expand Down Expand Up @@ -61,7 +62,7 @@ function lessonsScheduledToday(text) {
function matchTravelBlock(text) {
Logger.log('Matching Travels, Methods and Blocked Units');
const units = [];
const matches = text.matchAll(trvl_blk_regex);
const matches = text.matchAll(trvlBlkRegex);

for (const match of matches) {
const unit = createDefaultUnit();
Expand All @@ -82,7 +83,7 @@ function matchTravelBlock(text) {
function matchLessons(text) {
Logger.log('Starting matchLessons');
const units = [];
const matches = text.matchAll(lessons_pv_regex);
const matches = text.matchAll(lessonsPvRegex);
for (const match of matches) {
Logger.log('Found matches\n');
const unit = createDefaultUnit();
Expand Down
Loading

0 comments on commit 9fccd4a

Please sign in to comment.