Version 1
[yaffs-website] / vendor / twbs / bootstrap-sass / tasks / converter / network.rb
1 require 'shellwords'
2 class Converter
3   module Network
4     protected
5
6     def get_paths_by_type(dir, file_re, recursive = true)
7       get_file_paths(dir, recursive).select { |path| path =~ file_re }
8     end
9
10     def get_file_paths(dir, recursive = true)
11       get_tree(get_tree_sha(dir), recursive)['tree'].select { |f| f['type'] == 'blob' }.map { |f| f['path'] }
12     end
13
14     def read_files(path, files)
15       full_path = "https://raw.githubusercontent.com/#@repo/#@branch_sha/#{path}"
16       contents = read_cached_files(path, files)
17       log_http_get_files contents.keys, full_path, true if contents.keys
18       files -= contents.keys
19       log_http_get_files files, full_path, false
20       files.map do |name|
21         Thread.start {
22           contents[name] = open("#{full_path}/#{name}").read
23           Thread.exclusive { write_cached_files path, name => contents[name] }
24         }
25       end.each(&:join)
26       contents
27     end
28
29     def read_cached_files(path, files)
30       full_path = "#@cache_path/#@branch_sha/#{path}"
31       contents  = {}
32       if File.directory?(full_path)
33         files.each do |name|
34           path = "#{full_path}/#{name}"
35           contents[name] = File.read(path, mode: 'rb') if File.exists?(path)
36         end
37       end
38       contents
39     end
40
41     def write_cached_files(path, files)
42       full_path = "./#@cache_path/#@branch_sha/#{path}"
43       files.each do |name, content|
44         FileUtils.mkdir_p File.dirname(File.join(full_path, name))
45         File.open("#{full_path}/#{name}", 'wb') { |f| f.write content }
46       end
47     end
48
49
50     def get_file(url)
51       uri = URI(url)
52       cache_path = "./#@cache_path#{uri.path}#{uri.query.tr('?&=', '-') if uri.query}"
53       FileUtils.mkdir_p File.dirname(cache_path)
54       if File.exists?(cache_path)
55         log_http_get_file url, true
56         File.read(cache_path, mode: 'rb')
57       else
58         log_http_get_file url, false
59         content = open(url).read
60         File.open(cache_path, 'wb') { |f| f.write content }
61         content
62       end
63     end
64
65     # get sha of the branch (= the latest commit)
66     def get_branch_sha
67       @branch_sha ||= begin
68         if @branch + "\n" == %x[git rev-parse #@branch]
69           @branch
70         else
71           cmd = "git ls-remote #{Shellwords.escape "https://github.com/#@repo"} #@branch"
72           log cmd
73           result = %x[#{cmd}]
74           raise 'Could not get branch sha!' unless $?.success? && !result.empty?
75           result.split(/\s+/).first
76         end
77       end
78     end
79
80     # Get the sha of a dir
81     def get_tree_sha(dir, tree = get_trees)
82       tree['tree'].find { |t| t['path'] == dir }['sha']
83     end
84
85     def get_trees
86       @trees ||= get_tree(@branch_sha)
87     end
88
89     def get_tree(sha, recursive = true)
90       get_json("https://api.github.com/repos/#@repo/git/trees/#{sha}#{'?recursive=1' if recursive}")
91     end
92
93     def get_json(url)
94       JSON.parse get_file(url)
95     end
96   end
97 end