require 'erb' require 'fileutils' require_relative 'shift-lang/generator' require_relative 'shift-lang/version' module Shift class CLI def initialize(args) if(!args[0]) puts "Enter the project name : " project_name = gets.chomp if(!File.exists?(project_name)) Dir.mkdir(project_name) Dir.chdir(project_name) puts "Creating " + project_name + ".shift" puts "Creating views directory" Dir.mkdir("views") file = File.new(project_name + ".shift", "w") file.puts "" file.close else puts "A file already exists with the same name" end else case args[0] when "generate" case args[1] when "python" generate_python when "ruby" generate_ruby when "javascript" generate_javascript else puts "Unknown target platform. Options are ruby, python and javascript" end else puts "Unknown option" end end end def get_files_from_directory(directory, extension = ".shift", log = true) files = [] Dir.foreach(directory) do | file | if(File.directory?(file) == false) file = File.join(directory, file) if(File.extname(file) == extension) if log puts "Found file : " + File.absolute_path(file) end files.push File.absolute_path(file) end else if ((file.to_s != '.') && (file.to_s != '..')) file = File.join(directory, file) files.concat get_files_from_directory(file) end end end files end def get_files() get_files_from_directory Dir.pwd end def get_project_name() project_name = Dir.pwd.split("/") project_name[project_name.length - 1] end def generate_python() files = get_files generated_code = Shift::Generator::generatePython(files) if generated_code == "" return end project_name = get_project_name view_sources = File.absolute_path(File.join(Dir.pwd, "views")) gae_sources = File.absolute_path(File.join(File.dirname(__FILE__), "app_engine_templates")) pystache = File.join(gae_sources, "pystache") config = File.join(gae_sources, "app.yaml") config_contents = File.open(config, "r") { |io| io.read } config_template = ERB.new(config_contents) if(!File.exists?("release")) Dir.mkdir("release") end Dir.chdir("release") if(File.exists?("python")) FileUtils.remove_dir("python", true) end Dir.mkdir("python") Dir.chdir("python") puts "Creating target folder 'release/" + project_name + "'" Dir.mkdir(project_name) Dir.chdir(project_name) puts "Writing application file" File.open(project_name + ".py", "w") do |file| file.puts generated_code end puts "Copying templates" FileUtils.cp_r(pystache, Dir.pwd) FileUtils.cp_r(view_sources, Dir.pwd) template_files = get_files_from_directory(File.join(Dir.pwd, "views"), ".html", false) template_files.each do | template_file | text = File.read(template_file) File.open(template_file, "w") { | file | file.puts text.gsub("{{id}}", "{{#key}}{{id}}{{/key}}") } end puts "Writing Google App Engine configuration file" File.open("app.yaml", "w") do |file| file.puts config_template.result(binding) end puts "Done." end def generate_ruby() files = get_files generated_code = Shift::Generator::generateRuby(files) if generated_code == "" return end project_name = get_project_name view_sources = File.absolute_path(File.join(Dir.pwd, "views")) template_sources = File.absolute_path(File.join(File.dirname(__FILE__), "heroku_templates")) gemfile = File.join(template_sources, "Gemfile") procfile = File.join(template_sources, "Procfile") if(!File.exists?("release")) Dir.mkdir("release") end Dir.chdir("release") if(File.exists?("ruby")) FileUtils.remove_dir("ruby", true) end Dir.mkdir("ruby") Dir.chdir("ruby") puts "Creating target folder 'release/" + project_name + "'" Dir.mkdir(project_name) Dir.chdir(project_name) puts "Writing application file" File.open("app.rb", "w") do |file| file.puts generated_code end puts "Copying templates" FileUtils.cp_r(view_sources, Dir.pwd) puts "Generating Heroku configuration files" FileUtils.cp(gemfile, Dir.pwd) FileUtils.cp(procfile, Dir.pwd) puts "Done." end def generate_javascript() files = get_files generated_code = Shift::Generator::generateJavaScript(files) if generated_code == "" return end project_name = get_project_name view_sources = File.absolute_path(File.join(Dir.pwd, "views")) if(!File.exists?("release")) Dir.mkdir("release") end Dir.chdir("release") if(File.exists?("javascript")) FileUtils.remove_dir("javascript", true) end Dir.mkdir("javascript") Dir.chdir("javascript") puts "Creating target folder 'release/" + project_name + "'" Dir.mkdir(project_name) Dir.chdir(project_name) puts "Writing application file" File.open("app.js", "w") do |file| file.puts generated_code end puts "Copying templates" FileUtils.cp_r(view_sources, Dir.pwd) puts "Done." end end end