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

- old
+ new

@@ -1,52 +1,66 @@ module Capucine class Incloudr + + require 'cdnjs.rb' + require 'npm.rb' + require 'open-uri' require 'fileutils' - require 'json' require 'packr' - @@noderoot = 'http://registry.npmjs.org/' - @@cdnjsroot = 'https://raw.github.com/cdnjs/cdnjs/master/ajax/libs/' - @@cdnjs = 'http://www.cdnjs.com/packages.json' + def initialize(capucine) + @cap = capucine + end - def self.run_once - files = Capucine.settings.config['incloudr_libs'] + def run_once + files = @cap.settings.conf['incloudr_libs'] return false if files.length == 0 - dir = File.join(Capucine.settings.working_dir, Capucine.settings.config['incloudr_output_dir']) + + dir = File.join(@cap.settings.working_dir, @cap.settings.conf['incloudr_output_dir']) FileUtils.rm_r dir if File.exist?(dir) FileUtils.mkdir_p dir @file = nil + files.each {|file| self.pack file} puts "[incloudr] - Packaged" end - def self.pack file + def pack file + @file = file - if @file.kind_of? String # CDNJS - @file = {} - @file['name'] = file - @file['type'] = 'cdnjs' - else - @file['type'] = 'npm' unless @file['source'] # NPM - end + @file['version'] = @file['version'].to_s if @file['version'] + @file['type'] = @file['type'] || 'cdnjs' + @file['type'] = 'url' if @file['source'] - @file['type'] = @file['type'] || 'url' # URL + name = @file['file_name'] || @file['name'] - @output = File.join(Capucine.settings.working_dir, Capucine.settings.config['incloudr_output_dir'], @file['name'].gsub(/$/, '.js')) - @output_min = File.join(Capucine.settings.working_dir, Capucine.settings.config['incloudr_output_dir'], @file['name'].gsub(/$/, '.min.js')) + @dir = @cap.settings.working_dir + @conf = @cap.settings.conf + @output = File.join(@dir, @conf['incloudr_output_dir'], name.gsub(/$/, '.js')) + @output_min = File.join(@dir, @conf['incloudr_output_dir'], name.gsub(/$/, '.min.js')) + self.cdnjs if @file['type'] == 'cdnjs' self.url if @file['type'] == 'url' - # self.npm(file) if type == 'npm' + # self.npm if @file['type'] == 'npm' end + def url raw_content = nil + if not raw_content + begin + content = open(@file['source']).read + rescue => e + puts "Error" + return e + end + else + content = raw_content + end - def self.url raw_content = nil - content = raw_content || open(@file['source']).read content_min = "" content_min << Packr.pack(content) f1 = File.open(@output, 'w+') f1.write(content) @@ -55,36 +69,23 @@ f2 = File.open(@output_min, 'w+') f2.write(content_min) f2.close end + def cdnjs + api = Capucine::CDNJS.new(@file['name'], @file['version'], content = false) - def self.cdnjs - cdnjs_url_pkg = "#{@@cdnjsroot}#{@file['name']}/package.json" - content = JSON.parse open(cdnjs_url_pkg).read - version = @file['version'] || content['version'] - filename = content['filename'] - raw_file_url = "#{@@cdnjsroot}#{@file['name']}/#{version}/#{filename}" - raw_content = open(raw_file_url).read - self.url raw_content + if not api.result[:error] + self.url api.result[:content] + else + puts "Error on #{@file['name']}" + end end - def self.npm file - # infos = JSON.parse open("#{@@noderoot}#{file['name']}").read - # version = file['version'] || infos['dist-tags']['lastest'] - # lib = infos['versions'][version] - # tarball_url = lib['dist']['tarball'] + def npm + # TODO ! - # tarball = open(tarball_url).read - - # p version - end - def self.list - content = JSON.parse open(@@cdnjs).read - packages = content['packages'] - packages.each {|p| puts "#{p['name']}\n"} - end end end