From 879df0ab79cb907ad384cdfbead44591c863074c Mon Sep 17 00:00:00 2001 From: gauthiier Date: Thu, 3 Nov 2016 17:23:46 +0100 Subject: [PATCH] assignments-conf update --- assignments-conf.html | 149 +++++++++++++++---------------- assignments-conf.md | 162 +++++++++++++++++----------------- img/github-assignment-x.png | Bin 116717 -> 186241 bytes img/github-new-repo-clone.png | Bin 261434 -> 205286 bytes img/github-new-repo-name.png | Bin 194753 -> 250205 bytes index.html | 10 +++ ppp | 1 + 7 files changed, 165 insertions(+), 157 deletions(-) create mode 100644 ppp diff --git a/assignments-conf.html b/assignments-conf.html index 5ea3302..48bf52b 100644 --- a/assignments-conf.html +++ b/assignments-conf.html @@ -55,87 +55,72 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf -

Initialising your assignments directory

+

Initialising your assignments repository

What follows is a recipe to initialise a git repository for your class assignments.

-

git init

-

Let's assume you have a directory on your machine ~/cth2016 where you intend to place all your assignments. To create a dedicated assignments directory:

-
$ cd ~/cth2016
-$ mkdir cth2016-assignments
-$ cd cth2016-assignments
-

This will create a new directory ~/cth2016/cth2016-assignments/ that will be turned into a git repository with the following commands:

-
$ git init
-

git add

-

Your directory is now under version control, though there is nothing in there yet. Let's first create a README file describing what the repo is about:

-
$ echo 'Repository of CTH2016 assignments' > README
-

This command will create a new README file on your system and add to it the echo string. You can, of course, create the file manually using Sublime. To make sure git is keeping track of this new file, emit the following command:

-
$ git status
-

The above should display the new file in the list of changes to the repo since it has been created. Yet the README file is not currently added to the repo (marked in red). To add the file to the repo:

-
$ git add README
-

Now git status should list the file in green and mark it as new. Any files that will be added to the directory will need to be added to the repo using the git add command.

-

git commit

-

When adding new files or changing them, you need to commit these to the repo:

-
$ git commit -a -m 'initial commit'
-

From the git command above -a means to you are committing all changes listed by git status and -m is the message for the commit. If you do a git status you should now see that the README file dissapeared from the list of changes since it has just been committed. Changing the README file will re-introduce it to list of changes. A commit is like a recorded snapshot of the repo, any changes that occur after the commit will be flagged in git status.

-

github

-

Now that you have added a README file and committed this change to your local repo, it is time to link it to Github so that your instructor and your peers can "clone" it. To do so, login to Github and on the top/right corner of the page select the '+' symbol and 'New repository'

+

Step 1: Create repository on Github

+

First create a repository under your github account. To do so, login to Github and on the top/right corner of the page select the '+' symbol and 'New repository'

-

and give it a name, make it public and do not initialise it with a README since you already have one:

+

Then fill in the information for the assignments repo.

+

-

When your (now empty) github repo is created, you need to copy its url:

+

Step 2: Clone the repository to your own machine

+

Now that you have a github repo, you need to clone it to your machine. Open a terminal and navigate (cd and ls) to a prefered directory on your machine where you want your assignments to reside (for instance the Desktop).

+

When this is done, select the "Clone or download" menu on your githib repo's page and copy the listed url:

-

and add a "remote origin" to your local git repo with this command

-
git remote add origin your-github-https-url
-

where you replace "your-github-https-url" with your actual github url (for instance, on my repo it is: https://github.com/gauthiier/cth2016-assignments.git)

-

git push

-

When this is done, you can now push your local changes to the online github repo

-
git push origin master
-

If you refresh the github webpage, you should now see it updated with your README file

-

-

Your local repo and your github repo are now linked and ready to go!

+

Coming back to your terminal, simply type the following to clone the repo on your machine:

+
$ git clone your-github-repo-url
+

(where your-github-repo-url is your copied/pasted url from github)

+

This will create a directory under the current folder where your terminal is. To view the changes, simply type:

+
$ ls -al

Creating a new assignment (Node + Git)

What follows is a recipe to create a new assignment in your newly initialised git assignments repository.

-

assignment-x

-

If you have followed the previous section, you should now have a directory named ~/cth2016/cth2016-assignments/ under which you will add a new assignment-x. To create the new directory:

-
$ cd ~/cth2016/cth2016-assignments/
-$ mkdir assignment-x
-$ cd assignment-x
-

npm init

-

Node has a package manager named npm (n-ode, p-ackage, m-anager) that automates the task of creating a project from scratch and configures it in a way that is compatible with other node projects. We will use npm during the course of the module.

-

To initialise a new project/assignment, simply emit the follow command:

+

Step 1: Create a new directory under your assignments repository

+

If you have followed the previous section, you should now have a repositroy on your machine which is linked to your github account. Let's assume that your repo is named cth2016-assignments/ and resides on your Desktop.

+

To create a new assigment, you need to point your terminal to the repo:

+
$ cd ~/Desktop/cth2016-assignments/
+

and then create a new directory, which I will call assignment-x. To do so, on the terminal, simply type:

+
$ mkdir assignment-x
+$ cd assignment-x   
+

This will create a new direcory assignment-x under ~/Desktop/cth2016-assignments/. The cd command puts your terminal inside this newly created directory so that you can initialise your actual project in step 2.

+

Step 2: Initialise you project with npm init

+

Node has a package manager named npm (n-ode, p-ackage, m-anager) that automates the task of creating a project from scratch and configures it in a way that is compatible with other node projects. We will use npm during the module.

+

To initialise your new assignment under assignment-x, simply emit the follow command:

$ npm init
-

and you will be asked various questions regarding your project's name, version, description, entry point, author, etc. which you can configure at will (press <'enter'> to select and move to the next configuration step).

+

With this commad you will be asked various questions regarding your project's name, version, description, entry point, author, etc. which you can configure at will (press <'enter'> to select and move to the next configuration step).

A package.json file is created upon the completion of npm init. This file contains all the configurations you selected and can be edited manually.

-

npm install --save commander

+

Step 3 (optional): npm install --save commander

This step is optional, yet recommended for your assignment. When making a node application, modules/libraries can be made part of your project using npm. Thousands of javascript libraries are available on www.npmjs.com which you can install with a simple npm command.

As in the first session of the class on the Command Line Interface (CLI), we will be using "commander" for our applications. To install it for you assignment, use the following command:

$ npm install --save commander

This will create a node_modules directory next to package.json where the commander code is placed. All modules installed using npm will reside in this node_modules directory.

-

index.js

-

Now that you have an npm project created and installed commander it is time to create an index.js file containing the code for the assignment. Though since I am not following a proper assignment per se, I will simply create a simple tri-lingual script that will output "Hello!", "Dag!" or "Allô!" depending on a command line arguments passed to the script which can be of the type en (english), nl (nederlands), fr (français) or nothing.

-

To create index.js simply type (or, alternatively, create it with Sublime):

+

Step 4: Create your actual assigment script(s)

+

Now that you have an npm project created and installed commander it is time to create your script(s). For instance, in this example, I will create index.js file containing the code for the fake assignment. This file can be called anything (for instance, person.js as in the example in class during week1).

+

Though since I am not following a proper assignment per se, I will simply create a simple tri-lingual script that will output "Hello!", "Dag!" or "Allô!" depending on a command line arguments passed to the script which can be of the type en (english), nl (nederlands), fr (français) or nothing.

+

To create index.js simply type (or, alternatively, create it with Sublime Text):

$ touch index.js
-

Now open the file and input the desired code:

+

Now open the file usign Sublime Text, copy/paste or input the desired code and save:


 // simplest tri-lingual program 
 
@@ -161,13 +146,16 @@ program
         console.log('...');
         break;
 }
+

Step 5: Run you script

To run the script

-
$ node index.js -l fr
+
$ node index.js -l fr
+Allô!

where you can replace fr by en or nl or nothing. Try it out!

-

README

+

If you called you script another name (for instance, person.js), please change the above index.js with the name of your own script.

+

Step 6: Create a README

One of the interesting feature of commander is that it can auto generate a "help" output from your script based on the command line options you have listed in your program.

To see this help output, simply type:

-
node index.js --help
+
$ node index.js --help
  Usage: index [options]
 
   Options:
@@ -179,26 +167,39 @@ program
 

It is very common for command line scripts to feature such --help argument that when executed, displays what the program expects as input. Thus, for this class, each README you will create (for both assignments and final project) is expected to have (at least) this type of "helper documentation".

To initialise a README and add the help output from your script:

$ node index.js --help > README
-

You can, of course, edit this README manually. It is nonetheless a very good practice to start the redaction with the help as it signals how to use your script to people that may be interested in it.

-

git add

+

Alternatively, you can create the README with Sublime Text and copy/paste the result of the --help manually.

+

It is a very good practice to start the redaction with the --help as it signals how to use your script to people that may be interested in it.

+

Step 7: Add your created files to git

Now that you have a proper project initialised, the next step is to add all its files to git.

-

Yet before we do so, there one important file you need to fetch from github which will instruct git not to include to you repository unwanted files that may be installed by npm or by your operating system. Git has a special file called .gitignore that lists files and file patterns that have to be ignored by git. Luckily, there is a github repository maintained by the git community that lists common files to ignore depending on the programming language and environment your are using.

-

To download the Node .gitignore and install it under the current assignment-x, type the following command:

-
$ wget https://raw.githubusercontent.com/github/gitignore/master/Node.gitignore -O .gitignore
-

It is now time to look which file needs to be added to git. In order to do so type:

+

To view which file needs to be added to git type:

$ git status
-

This should indicate which directories and/or files that are untracked. If you have followed the previous steps, git status should simply signal that the current directory ./ is untracked. To add it to git:

+

This should indicate which directories and/or files are untracked by git.

+

If you have followed the previous steps, git status should simply signal that the current directory ./ is untracked. To add it to git:

$ git add ./
-

To make sure git properly added the new assignment, simply check with git status that your package.json, index.js, README, and various file in node_modules are marked as new files (green).

-

git commit

+

To make sure git properly added the new assignment, simply type (again):

+
$ git status
+

and check that your package.json, index.js (or whatever you have called your file(s)), and README are marked as new files (green).

+

Step 8: Commit your changes and push to Github

The next step is to commit these new files to your local git repo:

$ git commit -a -m "initial commit for assignment-x"
-

git push

And push the commit to your assignments github repo:

$ git push origin master

You should now see these changes online in your new cth2016-assignments/assignment-x/ directory:

+

NOTE: node_modules is not be pushed to github, why? Because when someone will clone your project (your instructor for example), she/he will npm install it her/himself as it is listed as a dependency in package.json.


+

Updating your assignment as you make progress

+

It is assumed that you will make many changes to your assigment while your a working on it.

+

The ususall cycle of working on some piece of code is to first (1) add you files to the repo (re: Step 7), (2) modify your files while you are working, and (3) when your code is working as inteted, commit your changes and push to github (re: Step 8).

+

The usual updating loop looks a bit like this:

+
    +
  1. git status
  2. +
  3. (optional) git add xyz depending on what was added to the project while working on it.
  4. +
  5. git commit -a -m "message for the commit"
  6. +
  7. git push origin master
  8. +
  9. work a bit more
  10. +
  11. loop and goto 1.
  12. +

External Resources

Github