lib/assetify/asset.rb in assetify-0.0.2 vs lib/assetify/asset.rb in assetify-0.1.1
- old
+ new
@@ -1,50 +1,51 @@
require 'net/http'
+require 'fileutils'
module Assetify
class Asset
- attr_accessor :type, :name, :url
+ include Helpers
+ attr_accessor :type, :name, :url, :ns
def initialize(type, name, url, ver = nil, params={})
raise "NoType" unless type
raise "NoName" unless name
raise "NoURL" unless url
@type, @name = type, name
@ver = ver
@url = @ver ? url.gsub(/{VERSION}/, @ver) : url
- @new_name = params[:name]
- @version = params[:version]
+ @ns = params[:ns] || ""
+ @pkg = params[:pkg]
end
def filename
- fname = Opt[:newname] ? name : url.split("/").last
- fname += ".#{type}" unless fname =~ /\.#{type}$/
+ return @filename if @filename
+ @filename = Opt[:newname] ? name : url.split("/").last
+ @filename += ".#{type}" unless @filename =~ /\.#{type}$/
+ @filename
end
+ def path
+ @path = File.join(Opt["#{type}path".to_sym], @ns ? @ns.to_s : "")
+ end
+
def fullpath
- path = Opt["#{type}path".to_sym]
- File.join(path, filename) #Dir.pwd,
+ @fullpath ||= File.join(path, filename)
end
def check?
File.exists? fullpath
end
def install!(force = false)
- print "Installing #{name}..."
- return puts "Installed" if !force && check?
- unless @data
- uri = URI.parse url
- Net::HTTP.start(uri.host) do |http|
- @data = http.get(uri.path)
- end
- end
- #puts "Writing to #{fullpath}"
- File.open(fullpath, "w") { |f| f.puts(@data) }
- puts "DONE"
+ print "-> #{name}"
+ # points = Thread.new { loop do; print "."; sleep 1; end }
+ return print_result "Installed" if !force && check?
+ @data ||= @pkg ? @pkg.get(url) : download
+ write
+ print_result :ok
end
- end
-
+ end
end