What is C# Yellow Book

Rob Miles‘ book, The C# Yellow Book is a beginner-friendly book to introduce students to C# and programming in general. It is short and makes a for a fun read. The book is not only free, but it also comes with code samples, that a student can use to learn and test their concepts.

Why convert code to images

I picked this book for a Coding Bootcamp that I am running in my local community. I want my students to read the code samples, run them in their Visual Studio, and explain the code line by line. It is a simple task. Unfortunately, many students take the copy-paste path to cheat through the assignment.

I have observed that learners who make it a habit of typing the examples from the books and coding samples perform better than those who just read the examples. Learning programming is like learning to swim. You cannot learn to swim by merely reading about it. Perhaps, when you are familiar with a language and have a few years of experience under your belt, you can skip over the trouble of typing the examples. But when you are new, typing and trying the example code will only help you understand it better.

So to force some of my challenging students to type, I decided to provide them with the code samples in image formats.

How to convert code to images

Carbon is easy to use, and performant website that converts your source code to beautiful photos. You can customize the programming language parser, fonts, themes, and even window type. It is a fantastic service.

There is a third-party command-line tool carbon-now-cli that lets you use the Carbon service from the comforts of your terminal.

What’s the challenge

Download the code samples and archive and views its content. Notice,

  1. It has 57 folders
  2. Each folder contains a complete Visual Studio project
  3. All projects are named YellowBookSamples
  4. Project folders have a bin directory that contains the debug build of the project
  5. Project folders also have an obj directory which holds intermediate files

I do not need to distribute the bin and obj folder. I also do not need Visual Studio projects. I expect students to create the projects themselves.

Only files that I want to loot from this sample archive is the Program.cs file present in every folder.

The objective

Write a fish shell script that will pick Program.cs file from each folder, rename it to the folder name, convert it to an image, and then place it in a new destination folder.

So if a Program.cs file is in “Code Sample 37 Simple Interface” folder. I want a Code Sample 37 Simple Interface.cs.png file.

Let’s get this task done.

Get carbon-now-cli

Install carbon-now-cli.

yarn global add carbon-now-cli

Fish Shell script — Step by step

Unzip the archive and cd into the folder.

Run the following fish shell script.

for f in *
  echo $f
end

It shows the name of each folder.

We know each Program.cs file is present inside a folder named YellowBookSamples in each of these folders. Let’s check this assumption.

for f in *
  test -f $f/YellowBookSamples/Program.cs; or echo "not found in '$f'"
end

This script tests if the file is present in the folder or not. If the file does not exist, then it prints the message “not found in <folder name>”.

On running, I got

not found in 'Code Sample 57 WPF Adding Machine'

So only Project “Code Sample 57 WPF Adding Machine” is different. All other folders have file in YellowBookSamples/Program.cs. “WPF Adding Machine” is different. We will skip this WPF sample code.

We have all the information we need. Let’s create a destination folder.

mkdir ../dest

Now we write a script to pick the Program.cs file and convert it to an image.

for f in *
  set code $f/YellowBookSamples/Program.cs
  test -f $code
  and begin
    mkdir ../dest/$f
    carbon-now -h -l ../dest/$f -t $f  $code
  end
end
  1. set code $f/YellowBookSamples/Program.cs create the path of the file, like “Code Sample 46 Storing accounts in an array/YellowBookSamples/Program.cs”, and assign it to the variable code
  2. test -f $code tests if the file exists
  3. and begin is a block of that will only run if test in the previous line returns true. You can read more about and begin in this article
  4. mkdir ../dest/$f creates a sub-folder in the destination directory
  5. Call carbon-now command to convert the file to an image

When I used carbon-now, it did not work unless I used it with the -h, i.e. the headless option. -l ../dest/$f is the location to save the image and -t $f is the image name.

The result is that each code sample is converted to an image and placed in its folder.

Fish script result

Unfortunately, carbon-now command did not work for eight projects. I had to convert them manually.

carbon-now sends the file to the Carbon via URL parameters, because as of writing this article, Carbon does not have an API. The files are probably too long to be handled via URL.

Finally, I optimized the images using ImageOptim. I ran the following script from the dest folder.

for f in **.png
  echo $f
  open -a ImageOptim $f
  sleep 60
end

I had to add a one-minute because ImageOptim sometimes crashes if too many images are passed to it at once.

Github Repository

I have uploaded the images in this Github repository.

Like this post? Share on: TwitterFacebookEmail


Keep Reading


Published

Last Updated

Category

Command-line & Shell

Tags

Contact

Email Newsletter