Create template repositories from just a single JSON file! What is Dgen?
ProCode
Posted on July 5, 2021
Hey all! haven't written something in a while, so I thought I would put my thoughts together about a recent project I built called Dgen. This post might as well become a blog on how to create Dgen in rust. Let's keep writing!
What is it?
Dgen
is a utility that converts repositories to a JSON file. So basically useful for your starter code repositories. Which you might be storing in Github right now as template repositories.
so let's say we have a directory that looks like this:
dgen-blog
├── assets
├── index.html
├── main.js
└── style.css
1 directory, 3 files
The JSON file that Dgen
will generate for this directory will look like this:
dgen-blog.json
{
"name":"dgen-blog",
"files":[
{
"name":"index.html",
"content":"<html lang=\"en\"..."
},
{
"name":"main.js",
"content":"console.log(..."
},
{
"name":"style.css",
"content":".body {\n..."
}
],
"folders":[
{
"name":"assets",
"files":[],
"folders":[]
}
]
}
The shape of the JSON file will make sense in a moment I promise!
Is it useful?
Umm not sure, I am just experimenting with the idea. It works fine for most huge starter repositories. If you keep a collection of these JSON files locally, you would be able to generate you repositories offline without any hassle and also super fast!
How to install it?
For now, if you have rust installed you can clone the repository and run the following commands to generate the executable binary.
# clone the repo
$ git clone https://github.com/ProCode2/dgen.git
# get in the project diretory
$ cd dgen
# build the binary
$ cargo build --release
# check if its working
$ ./target/release/dgen-rs -V
# for !windows
$ ln -s ./target/release/dgen-rs /usr/local/bin
How to use it?
You'd use the binary to either create a blueprint from a directory or create a directory from a blueprint.
- Here's how to create a blueprint from a directory: Go inside the directory you want to create a blueprint of and run this command:
# creates a JSON blueprint
$ dgen-rs -b
You'd see a <directory_name>.json
file will be created in your directory if the command runs successfully. Now you can store this JSON file wherever you are collecting all the JSON blueprints and forget about the directory, the JSON file can create it again anyways! Let's see how to do that now.
- Here's how to create a directory from a blueprint: Go to the path where you will create the directory and run this command:
# creates a directory
$ dgen-rs -g /path/to/the/json/blueprint
The Shape of the JSON file
Here's the core idea - Every single directory has a name, few files with their contents, and a few folders with the same things mentioned above.(RECURSION!)
So the base json looks like this:
{
"name":"",
"files":[],
"folders":[]
}
And it grows to a weird looking JSON file, which works.
Some issues with this concept
Currently the structure for storing files is this:
{
"name": "main.js",
"content": "console.log(......."
}
Which means, contents can store only valid utf-8
characters. So how do we store images, videos, audios or even binaries. I am not sure if any starter repository have any of those. But I still want to solve this, which is a problem I created on the first place lol. For the images I might store a base64 string in content
? I am still thinking on this idea. Also I am open to suggestions as well! Let me know if you have any idea to solve this issue! or any comments on the project as well!
How to create Dgen
in next article
Well If I keep adding how to create Dgen
yourself in this article it will get pretty big! So I will be uploading a fresh new article on that topic! Stay tuned!
Thanks for reading!
Posted on July 5, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.