Created
August 22, 2014 11:30
-
-
Save hak8or/813dc1792be52d6d4322 to your computer and use it in GitHub Desktop.
Compilation script turned into a dependency management hybrid thing.
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
require "erubis" | |
require 'yaml' | |
require 'colorize' | |
require 'fileutils' | |
# Holds the project class/schema. | |
require_relative "project" | |
# Expected input: script_file_name proj_name devel/prod | |
# Handling of project name. | |
if ARGV[0].nil? | |
fail("No project name given!".red) | |
else | |
proj_name = ARGV[0] | |
end | |
# If no output mode was specified, assume development. Otherwise set mode to what | |
# was given in the command line. The mode is for now controling the output directory | |
# only. | |
if ARGV[1].nil? | |
warn("Assuming development compilation mode.".cyan) | |
mode = "development" | |
else | |
if ((ARGV[1].downcase != "development") && (ARGV[0].downcase != "production")) | |
fail("No proper mode was given") | |
end | |
mode = ARGV[1].downcase | |
end | |
# Setup directories. | |
output_dir = mode + "_output/" + proj_name | |
source_dir = "website_source/projects/" + proj_name | |
templates_dir = "website_source/templates" | |
# Checks for the existance of the specified directory and based | |
# on a hash will either fail or warn while creating the dir. A false | |
# is returned to indicate the dir did not exist and a warning was issued. | |
def check_dir(directory, opts = {}) | |
if !File.directory?(directory) | |
if opts[:fail_if_not_present] | |
fail(directory.red + " dir is missing!".red) | |
else | |
warn(directory.cyan + " dir is missing! Making a new one.".cyan) | |
FileUtils.mkdir(directory) | |
return false | |
end | |
end | |
end | |
# Same as check_dir but for files. Instead of just creating a file it | |
# copies the file from the provided hash. | |
def check_file(path, opts = {}) | |
if !File.exist?(path) | |
if opts[:fail_if_not_present] | |
fail(path.red + " file is missing!".red) | |
else | |
warn(path.cyan + " file is missing! Copying from templates.".cyan) | |
puts " Copying from: " + opts[:source] | |
puts " to: " + path | |
# Generates a file in that place so cp does not yell at me about | |
# the file not existing. | |
File.new(path, 'w') | |
# Copy the file | |
FileUtils.cp(opts[:source], path) | |
return false | |
end | |
end | |
end | |
Dependancy = Struct.new(:path, :fail_if_not_present, :source, :file_or_dir) | |
dependancies = [ | |
# Resources folder for website. | |
Dependancy.new("website_source", :fail_if_not_present => true), | |
# Output folder for website. | |
Dependancy.new(mode + "_output"), | |
# Same as above but added in the project dir | |
Dependancy.new(output_dir), | |
# Holds the templates for various pages. | |
Dependancy.new(templates_dir, :fail_if_not_present => true), | |
# Specific template file for the project page, or the writeup. | |
Dependancy.new(templates_dir + "/project.eruby", :fail_if_not_present => true), | |
# Holds the configuration for the project page. | |
Dependancy.new(source_dir + "/project.yml", :source => templates_dir + '/project.yml') | |
] | |
for dependancy in dependancies do | |
puts "Checking " + dependancy.path | |
# If files_or_dir was not set, try to guess what the dep is, and set as | |
# dir if unkown. | |
if dependancy.file_or_dir.nil? | |
end | |
if dependancy.file_or_dir == "dir" | |
# There is probably a better way to pass these hashes ... | |
check_dir( | |
dependancy.path, | |
:fail_if_not_present => dependancy[:fail_if_not_present], | |
:source => dependancy[:source]) | |
else | |
check_dir( | |
dependancy.path, | |
:fail_if_not_present => dependancy[:fail_if_not_present], | |
:source => dependancy[:source]) | |
end | |
end | |
# Load the template for the project page. | |
template = Erubis::Eruby.new( File.read(templates_dir + '/project.eruby') ) | |
# Load the project.yml file. | |
project_file = YAML.load_file(source_dir + "/project.yml") | |
# Check if the project yaml file is empty. If it is, overwrite it with a template. | |
if project_file == false | |
warn(source_dir.cyan + "/project.yml appears to be empty. Overwriting it with a template.".cyan) | |
# Overwrite it with a template. | |
FileUtils.cp(templates_dir + '/project.yml', source_dir + '/project.yml') | |
# Update the yaml file object. | |
project_file = YAML.load_file(source_dir + "/project.yml") | |
end | |
# Check if there are any project inside the yml file. | |
if project_file["description"].empty? | |
warn(" Making a project-less project page.".cyan) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment