Last active
August 26, 2022 14:17
-
-
Save GLodi/daf0b419df7ab644083862130c6e2c61 to your computer and use it in GitHub Desktop.
A hook to make sure that your README is updated with the current project's structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
=begin | |
A hook to make sure that the README is updated with the current project's structure. | |
The scripts appends at the end of your project's README a markdown representation of | |
your project. Now you have no excuses not to document your wonderful work! | |
Inspired by Readme Driven Development by Tom Preston-Werner (https://tom.preston-werner.com/2010/08/23/readme-driven-development.html) | |
Place it under .git/hooks and make sure that it is executable (chmod +x pre-commit). | |
=end | |
class ReadmeChecker | |
SRC_ROOT = "/src".freeze | |
README_INCIPIT = "# Project Structure".freeze | |
README_PATH = Dir.pwd + "/README.md".freeze | |
PASS_MSG = "pre-commit - SUCCESS - check passato".freeze | |
NO_README_MSG = "pre-commit - ERROR - README not found".freeze | |
NO_SRC_ROOT_MSG = "pre-commit - ERROR - src root not found".freeze | |
class << self | |
def perform | |
check_files_exist | |
unless File.readlines(README_PATH).grep(README_INCIPIT + "\n").any? | |
File.write(README_PATH, README_INCIPIT + "\n\n", mode: "a+") | |
explore(Dir.pwd + SRC_ROOT, 0) | |
end | |
end | |
private | |
def check_files_exist | |
p NO_README_MSG and exit 0 unless File.exist?(Dir.pwd + "/README.md") | |
p NO_SRC_ROOT_MSG and exit 0 unless File.exist?(Dir.pwd + SRC_ROOT) | |
end | |
def explore(path, depth) | |
paths = Dir.glob(path + "/*") | |
write_to_readme(path, depth) | |
paths.each do |p| | |
explore(p, depth + 1) if File.directory?(p) | |
end | |
end | |
def write_to_readme(path, depth) | |
to_write = "#" * (depth + 2) + " " + path[path.rindex("/") + 1..-1] | |
File.write(README_PATH, to_write + "\n\n", mode: "a+") | |
end | |
end | |
end | |
ReadmeChecker.perform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment