# -*- encoding: utf-8 -*- require 'fileutils' require 'open-uri' require 'erb' require 'npm.rb' require 'cdnjs.rb' module Capucine class Tools def initialize(capucine) @cap = capucine end def new_project(scope = nil, name = nil) name = name || @cap.settings.project_name # Custom name or 'capucine' @cap.settings.working_dir = File.join @cap.settings.working_dir, name self.archive_file(@cap.settings.working_dir) config_file = get_config_file_from_scope(scope) files = Dir.glob File.join(@cap.settings.content_dir, 'shared', '**') FileUtils.mkdir @cap.settings.working_dir FileUtils.cp_r files, @cap.settings.working_dir FileUtils.cp config_file, File.join(@cap.settings.working_dir, 'capucine.yaml') @cap.settings.set_user_config_file(File.join(@cap.settings.working_dir, 'capucine.yaml')) return self end def init(scope = nil, config_or_name = nil) self.archive_file(@cap.settings.user_config_file) if @cap.settings.user_config_file # Without Conf if not scope and not config_or_name file = File.join(@cap.settings.content_dir,'shared', 'capucine.yaml') FileUtils.cp file, File.join(@cap.settings.working_dir, 'capucine.yaml') return file end # With Conf if config_or_name begin d = open(config_or_name).read rescue puts "Error downloading : #{config_or_name}" return self end file = File.new File.join(@cap.settings.working_dir, 'capucine.yaml'), 'w+' file.write(d) file.close # file.unlink return self end if scope file = File.join(@cap.settings.root_dir,'templates', "#{scope}.yaml") if not File.exists?(file) puts "Sorry, this template does not exists : #{scope}" return self end FileUtils.cp file, File.join(@cap.settings.working_dir, 'capucine.yaml') end return self end def compile(scope = nil) scope = (scope) ? scope : 'all' do_things = self.extract_commands_from_scope(scope) do_sass = do_things[0] do_coffee = do_things[1] do_incloudr = do_things[2] @cap.sass.run_once if do_sass @cap.coffee.run_once if do_coffee @cap.incloudr.run_once if do_incloudr end def watch(scope = nil) self.compile(scope) scope = (scope) ? scope : 'all' do_things = self.extract_commands_from_scope(scope) do_sass = do_things[0] do_coffee = do_things[1] do_incloudr = do_things[2] thread_sass = @cap.sass.run_watch if do_sass thread_coffee = @cap.coffee.run_watch if do_coffee thread_sass.join if thread_sass thread_coffee.join if thread_coffee end def js(scope, query = nil) if scope[0] == 'search' if scope[1] == 'npm' else libs = Capucine::CDNJS.search(query) libs.each do |lib| puts "#{lib['name']} --- #{lib['version']}" end end end if scope[0] == 'list' if scope[1] == 'npm' else libs = Capucine::CDNJS.get_all libs.each do |lib| puts "#{lib['name']} --- #{lib['version']}" end end end end def update system('gem uninstall -a -x capucine') system('gem install capucine') end def clean # TODO end #====================== def extract_commands_from_scope(scope) all = ['use_compass', 'use_coffeescript', 'use_incloudr'] todo = [false,false,false] scope = (scope != 'all') ? scope.split(',') : all # [] or ['sass', 'coffee'] all.each_with_index do |command, i| if @cap.settings.conf[command] == true todo[i] = true # if scope.include?(command) # end end end return todo end def get_config_file_from_scope(scope = nil) if not scope file = File.join(@cap.settings.content_dir,'shared', 'capucine.yaml') elsif scope.match(/^http:\/\//) # files = from the web else # files = from github end return file end def render_template template_file, content = nil config = content output = ERB.new(File.new(template_file).read).result(binding) return output end def archive_file path, force = true return if not File.exist? path is_empty = false is_dir = File.directory? path dir_length = Dir.glob("#{path}/**").length if is_dir is_empty = true if dir_length == 0 return if is_empty date = Time.now.strftime("%Y-%m-%d_%Hh%Mm%Ss") new_dir_name = path if is_dir new_dir_name = "#{path}_#{date}" end if not is_dir extension = File.extname path base_path = File.dirname path file_name = File.basename(path, extension) new_dir_name = File.join base_path, "#{file_name}_#{date}#{extension}" end FileUtils.mkdir_p new_dir_name if is_dir FileUtils.mv path, new_dir_name end end end