#!/usr/bin/env ruby # use {{object}} and {{objects}} and {{object.classify}} as template require 'optparse' require 'colorize' require 'fileutils' require 'sequel' require 'pp' Sequel.extension :inflector unless ARGV[0] puts "./generate [object singular]".red exit end template_dir = 'config/templates' exit puts "Lux::Template dir #{template_dir} is not accessible" unless Dir.exists?(template_dir) tpl_desc = { p:'api', m:'model', a:'admin', c:'cell', v:'view' } @object = ARGV[0] @objects = @object.pluralize puts "Singular : #{@object.yellow}" puts "Plural : #{@objects.yellow}" def parse_vars(data) object = @object objects = @objects klass = @object.classify data.gsub(/\{\{([^\}]+)\}\}/) { eval $1 } end # get all files templates = {} for el in Dir["./#{template_dir}/*.*"].map{ |file| file.split('/').last } begin data = parse_vars(File.read("#{template_dir}/#{el}")) rescue puts '-' puts "File error: #{el.red}: #{$!.message}" exit end type = el[0,1] path = el.split('|', 2)[1] path = parse_vars(path).gsub('#','/') templates[type] ||= [] templates[type].push [path, data] end # # puts "Lux::Templates : #{templates.keys.sort.map{ |el| tpl_desc[el.to_sym] ? tpl_desc[el.to_sym].sub(el, el.upcase.yellow) : el.yellow }.join(', ')}" puts "Lux::Templates : #{templates.keys.map{ |el| "#{tpl_desc[el.to_sym]}(#{el.yellow})" }.join(', ')}" print "Execute : " parse_templates = STDIN.gets.chomp for type in templates.keys next unless parse_templates.index(type) for el in templates[type] file, data = *el if File.exists?(file) print 'exists'.yellow.rjust(20) else FileUtils.mkdir_p(file.sub(/\/[^\/]+$/,'')) rescue false File.open(file, 'w') { |f| f.write(data) } print 'created'.green.rjust(20) end puts ": #{file}" end end # if parse_templates.include?('m') # sql = %[CREATE TABLE IF NOT EXISTS "public"."#{@objects}" ("id" serial, PRIMARY KEY ("id"));] # LuxCli.run "psql -d %s -c '%s'" % [ENV.fetch('DB_NAME'), sql] # end