WebFund 2024F: Tutorial 1: Difference between revisions

From Soma-notes
Created page with "'''This tutorial is not yet finalized.''' In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern. First, with the sync version, try the following: * Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression]. * Give it an additional argument that causes ma..."
 
No edit summary
Line 5: Line 5:
First, with the sync version, try the following:
First, with the sync version, try the following:
* Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression].
* Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression].
* Give it an additional argument that causes matched lines to be output to a new file.  You'll need to use writeFileSync() from [https://denojs.org/api/fs.html the Deno fs module].  When finished, output "All done!" to the console.
* Give it an additional argument that causes matched lines to be output to a new file.  You'll need to use writeTextFileSync() from [https://docs.deno.com/api/deno/file-system the Deno file system API].  When finished, output "All done!" to the console.
* Make the output lines [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sorted] in alphabetical order.
* Make the output lines [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sorted] in alphabetical order.


Then, do all of the above with the async version.  Make sure to use writeFile(), not writeFileSync()!
Then, do all of the above with the async version.  Make sure to use writeTextFile(), not writeTextFileSync()!


You may find the following methods useful when working with arrays:[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join()].
You may find the following methods useful when working with arrays:[https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join()].
Line 73: Line 73:
</syntaxhighlight>
</syntaxhighlight>


==Getting Started with Deno==
==Getting Started with Deno and Openstack==


For this class you will be using [http://denojs.org/ deno.js] and a web broswer.  While these are both cross-platform, we can provide the most support for running these applications inside the class [http://www.lubuntu.net/ Lubuntu] virtual machine appliance.  We will be using [https://www.virtualbox.org/ VirtualBox] as our preferred virtualization platform; however, VMware Workstation/Fusion and other virtualization platforms should be able to run the appliance as well.
For this class you will be using [http://deno.com/ Deno] and a web broswer.  While these are both cross-platform, we can provide the most support for running these applications inside the class [http://www.ubuntu.com/ Ubuntu] Openstack cloud instances.


In the labs deno.js is also installed on the lab machines under Programs->Programming->deno.js.  From the deno.js command prompt you can run deno programs in a fashion similar to a Linux command prompt; however, expect there to occasionally be bugs that are not present in the Lubuntu environment. Consider yourself warned!
To get going with OpenStack, follow the [https://carleton.ca/scs/tech-support/scs-open-stack/ SCS guide to OpenStack]. Inside the COMP2406 project, create your own instance, with the name of your instance being your Carleton username plus "-comp2406-1" (so, "janesmith-comp2406-1"). If you need no make another instance, increment the number. You should normally have only one instance running; please delete non-functioning instances.


==Running the VM==
(I don't expect you to mess up your instance but mistakes happen!)


In the SCS labs you should be able to run the VM by starting Virtualbox (listed in the Applications menu) and selecting the COMP 2406 virtual machine. After the VM has fully booted up you should be logged in automatically as the user "student". If the screen locks or you need administrative access (via sudo) you'll need the password for the student account, however.  This password is "tneduts!" (students backwards followed by an !).  There is also an admin account in case your student account gets corrupted for any reason.  The password for it is "nimda!".
Should should select the latest "comp2406-2024f-" image as the starting image. This one will have deno already installed in the student account. If you use another image, you'll have to manually install deno and add its binary to the current path.


We highly recommend running your VM in full-screen mode.  (Don't maximize the window; instead select full screen from the view menu.)  Do all of your work inside of the VM; it should be fast enough and you won't have any issues with sharing files or with host firewalls.


If you want to run the VM on your own machine, see the section on configuring your own VM [http://homeostasis.scs.carleton.ca/wiki/index.php/Fundamentals_of_Web_Applications_%28Winter_2016%29#Running_the_VM_on_your_own_machineson the main course webpage].
==Hello, World!==


Note as we will explain, you will have the ability to easily save the work you do from any VM to your SCS account and restore it to any other copy of the class VM.  Thus feel free to play around with VMs; if you break anything, you can always revert.  Remember though that in the labs you '''must''' save and restore your work, as all of your changes to the VM will be lost when you logout!
'''This text should be updated.'''
 
While you may update the software in the VM, those updates will be lost when you next login to the lab machines; thus, you probably only want to update a VM installed on your own system.
 
 
==Hello, World!==


To create your first deno application, start [http://www.geany.org/ geany], [http://brackets.io/ brackets], [http://www.vim.org/ vim], or [http://www.gnu.org/software/emacs/ emacs] code editors by clicking on their quick launch icons at the bottom left of the screen (beside the LXDE start menu button).
To create your first deno application, start [http://www.geany.org/ geany], [http://brackets.io/ brackets], [http://www.vim.org/ vim], or [http://www.gnu.org/software/emacs/ emacs] code editors by clicking on their quick launch icons at the bottom left of the screen (beside the LXDE start menu button).
Line 105: Line 99:


   cd Documents
   cd Documents
   deno hello.js
   deno run hello.js


And you should see <tt>Hello, World!</tt> output to your terminal.
And you should see <tt>Hello, World!</tt> output to your terminal.

Revision as of 15:00, 12 September 2024

This tutorial is not yet finalized.

In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern.

First, with the sync version, try the following:

  • Make the search pattern into a regular expression.
  • Give it an additional argument that causes matched lines to be output to a new file. You'll need to use writeTextFileSync() from the Deno file system API. When finished, output "All done!" to the console.
  • Make the output lines sorted in alphabetical order.

Then, do all of the above with the async version. Make sure to use writeTextFile(), not writeTextFileSync()!

You may find the following methods useful when working with arrays:push(), pop(), shift(), unshift(), and join().

Code

simplegrep_sync.js

Downloadable version.

if (Deno.args.length < 2) {
    console.error('Not enough parameters given. Try this: ' +
                  '"deno run --allow-read simplegrep_sync term filename.txt"'); 
    Deno.exit(1);
}

var searchterm = Deno.args[0];
var filename = Deno.args[1];

var rawContents = Deno.readTextFileSync(filename);
var lines = rawContents.split('\n');

for (var i = 0; i < lines.length; i++) {
    if (lines[i].indexOf(searchterm) > -1) {
        console.log(lines[i]);
    }
}

simplegrep_async.js

Downloadable version.

if (Deno.args.length < 2) {
    console.error('Not enough parameters given. Try this: ' +
                  '"deno run --allow-read  simplegrep_async term filename.txt"'); 
    Deno.exit(1);
}

const searchterm = Deno.args[0];
const filename = Deno.args[1];

const returnMatches = function(rawContents) {

    const lines = rawContents.split('\n');

    lines.forEach(function(theLine) {
        if (theLine.indexOf(searchterm) > -1) {
            console.log(theLine);
        }
    });
}

const readPromise = Deno.readTextFile(filename);
try {
    const rawContents = await readPromise;
    returnMatches(rawContents);
} catch (error) {
    console.error(`Error reading file: ${error.message}`);
    Deno.exit(1);
}

Getting Started with Deno and Openstack

For this class you will be using Deno and a web broswer. While these are both cross-platform, we can provide the most support for running these applications inside the class Ubuntu Openstack cloud instances.

To get going with OpenStack, follow the SCS guide to OpenStack. Inside the COMP2406 project, create your own instance, with the name of your instance being your Carleton username plus "-comp2406-1" (so, "janesmith-comp2406-1"). If you need no make another instance, increment the number. You should normally have only one instance running; please delete non-functioning instances.

(I don't expect you to mess up your instance but mistakes happen!)

Should should select the latest "comp2406-2024f-" image as the starting image. This one will have deno already installed in the student account. If you use another image, you'll have to manually install deno and add its binary to the current path.


Hello, World!

This text should be updated.

To create your first deno application, start geany, brackets, vim, or emacs code editors by clicking on their quick launch icons at the bottom left of the screen (beside the LXDE start menu button).

(If you are a fan of vi but want to try emacs, you should type Alt-X viper-mode. You're welcome.)

In your editor of choice, create a file hello.js in your Documents folder with the following contents:

  console.log("Hello World!");

You can now run this file by opening an LXTerminal (under Accessories) and typing:

  cd Documents
  deno run hello.js

And you should see Hello, World! output to your terminal.

You can also run deno interactively by simply running deno with no arguments. You'll then get a prompt where you can enter any code that you like and see what it does. To exit this environment, type Control-D.

Note that when run interactively, we say that deno is running a read-eval-print loop (REPL). It reads input, evaluates it, and then prints the results. This structure is very old in computer science, going back to the first LISP interpreters from the early 1960's.