Class: ChosenAssets::SourceFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/chosen_assets/source_fetcher.rb

Constant Summary collapse

GithubRepo =
'harvesthq/chosen'

Instance Method Summary collapse

Constructor Details

#initialize(github_repo, tag_name = nil) ⇒ SourceFetcher

Returns a new instance of SourceFetcher.



11
12
13
14
# File 'lib/chosen_assets/source_fetcher.rb', line 11

def initialize(github_repo, tag_name = nil)
  @github_repo = github_repo || GithubRepo
  @tag_name = tag_name
end

Instance Method Details

#fetchObject

desc ‘fetch source files’, ‘fetch source files from GitHub’



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chosen_assets/source_fetcher.rb', line 17

def fetch
  releases_data = JSON.load( open("https://api.github.com/repos/#{@github_repo}/releases") )
  release = if @tag_name.nil? 
    releases_data.first
  else
    releases_data.find {|hash| hash["tag_name"] == @tag_name}
  end

  # We assume there's only one asset, and we want it
  asset       = release['assets'].first
  asset_name  = asset['name']

  local_zip_path = "tmp/#{asset_name}"
  
  open(local_zip_path, "wb") do |file|
    # Need to send Accept  application/octet-stream to github to get the binary
    # http://developer.github.com/v3/repos/releases/#get-a-single-release-asset
    file << open(asset['url'], "Accept" => "application/octet-stream").read
  end

  # We're just gonna shell out to the 'unzip' command, OSX and unix prob
  # has it, good enough. 
  local_source_path = "tmp/#{asset_name.chomp(File.extname(asset_name))}.scss"
  system("unzip", local_zip_path, "-d", local_source_path)

  # Copy all the files over

  
  Dir.glob("#{local_source_path}/*.css").each do |source|
    dest = "vendor/assets/stylesheets/#{File.basename source}.scss"
    puts "copy to #{dest}"
    FileUtils.copy source, dest

    # replace url() with asset-pipeline-aware scss asset-url()
    content = File.read(dest)
    content.gsub!(/ url\(([^)]+)\)/, ' image-url(\\1)')
    File.open(dest, 'wb') { |file| file.write(content) }
  end

  Dir.glob("#{local_source_path}/*.js").each do |source|
    puts "copy to vendor/assets/javascripts/#{File.basename source}"
    FileUtils.copy source, "vendor/assets/javascripts/#{File.basename source}"
  end

  Dir.glob("#{local_source_path}/*.{png,gif,jpg,jpeg}").each do |source|
    puts "copy to vendor/assets/images/#{File.basename source}"
    FileUtils.copy source, "vendor/assets/images/#{File.basename source}"
  end


end