Sha256: 138e7e25b37e6b5777be5fad616975b6e96824d17b778be4795db9b6befd6295

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

#!/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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.1.35 ./bin/cli/generate
lux-fw-0.1.17 ./bin/cli/generate