I enjoyed the srcset project so much that I want to create a set of scripts for creating and managing my projects. In recent Linux news it's been said that the home directory will have a Projects folder by default. I have always created this folder anyway and the scripts I will be creating assume this path exists or will create it if it doesn't. This post will spotlight the new-website command.
To recap what I said in the first part of this series, I aim for my scripts to follow some basic guidelines when possible and ideal:
- a command does one thing and does it well
- a command should be able to accept data from pipe
The new-website script, like the previous srcset project, is simple. I bounce back and forth between different project directory structures for websites and I am aiming to try and stick to one convention and have some uniformity (writing this I am thinking of changing it already in the script). Everyone seems to have their own preferred way of laying things out. I try to keep things as simple as possible structurally.
The new-website script can create a simple, single page website that would be classified as a landing page, or a "full" site with the common additional pages (about, contact, gallery). I added the additional option for creating a vanilla html-css-js or a bootstrap project with the bootstrap CDN links in the html files. Personally I add the bootstrap files to the project when it's finished because it slightly improves page loading since the webpage doesn't have to call out for resources.
I have initially chosen to place any additional files, images and scripts in an assets subdirectory sorted by type. The index.html and 404.html are placed in the root of the site and all other pages are placed in assets/html. This project was a bit more complex than the srcset script and the final script runs a little over 500 lines (close to 300 lines written by myself and the rest added by Bashly).
As with the srcset project I started with the following commands:
$ mkdir new-website
$ cd new-website
$ bashly init
Also, just as I did in the last project, I tried working with the seperate files in a lib directory but with as many lines as it was getting up to it was just easier for me to keep things straight by putting everything in a single file. A majority of the lines are the boilerplate. Instead of having the starter code in template files and just copying them over, I decided to use functions that cat HEREDOCs of the code:
This way I can just redirect the output of these functions to whatever files I needed. This keeps the project light and simple and with far less files to manage because the idea is to end up with a single executable bash script I can move into my path. The bashly.yaml file was fairly shorter with this project because I set all of the globals and checking in the root_command.sh script.Once the meat of the script is written in the root_command.sh file I run 'bashly generate' to get my finished project. I tested it every ideal way I could think of and I'm fairly sure I handled any bad usage in the script.As I stated before the script can accept input from stdin and can be piped:It can also create multiple projects with one command by passing a comma separated list as the argument:
I'll note that to provide this ability I made use of the following code:
The Internal Field Separator is set to ',' to split the argument string into array elements using the comma as the separator. All the code for this project is viewable on Github.








Comments
Post a Comment