lib/tools.rb in capucine-0.1.7 vs lib/tools.rb in capucine-0.2.0

- old
+ new

@@ -1,52 +1,190 @@ # -*- encoding: utf-8 -*- +require 'fileutils' +require 'open-uri' +require 'erb' +require 'npm.rb' +require 'cdnjs.rb' + module Capucine class Tools - require 'fileutils' - require 'erb' - require "compass-sass.rb" - require "coffeescript.rb" - require "incloudr.rb" - def self.new_project name = nil + def initialize(capucine) + @cap = capucine + end - if name - new_project_name = name - else - new_project_name = Capucine.settings.project_name + 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 - Capucine.settings.working_dir = File.join Capucine.settings.current_dir, new_project_name - self.archive_file Capucine.settings.working_dir - FileUtils.mkdir Capucine.settings.working_dir if not File.directory? Capucine.settings.working_dir + # With Conf + if config_or_name - self.init files = true + 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 self.init all = nil - self.archive_file File.join Capucine.settings.working_dir, 'capucine.yaml' + def compile(scope = nil) - if all - files = Dir.glob File.join(Capucine.settings.gem_content_dir, 'shared', '**') - self.archive_file Capucine.settings.working_dir - FileUtils.cp_r files, Capucine.settings.working_dir + 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_incloudr = @cap.incloudr.run_watch if do_incloudr + + + 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 - Capucine::Watchr.compile if all - Capucine.settings.reset_working_dir + 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 self.render_template template_file, content = nil + def update + system('gem uninstall -a -x capucine') + system('gem install capucine') + end + + def clean + # TODO + end + + #====================== + def extract_commands_from_scope(scope) + all = ['sass', 'coffee', '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 + if scope.include?(command) + todo[i] = true + 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 - template = ERB.new File.new(template_file).read - output = template.result(binding) + output = ERB.new(File.new(template_file).read).result(binding) + return output end - def self.archive_file path, force = true + 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 @@ -71,13 +209,11 @@ FileUtils.mkdir_p new_dir_name if is_dir FileUtils.mv path, new_dir_name end - def self.clean - FileUtils.rm File.join(Capucine.settings.working_dir, '.compass.rb') - FileUtils.rm_r File.join(Capucine.settings.working_dir, '.incloudr') - end - end + + + end