Class: Erhu::App

Inherits:
Object show all
Defined in:
lib/erhu/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(erhufile_path = nil) ⇒ App

Returns a new instance of App.



4
5
6
7
8
9
10
11
12
13
# File 'lib/erhu/app.rb', line 4

def initialize(erhufile_path=nil)
  @erhufile = erhufile_path || File.join(Dir.pwd, "ErhuFile")

  @erhu_path = File.join(Dir.pwd, ".erhu")
  unless Dir.exist?(@erhu_path)
    FileUtils.mkdir_p(@erhu_path)
    puts "Created #{@erhu_path}"
  end
  @database_path = File.join(@erhu_path, "database.yml")
end

Instance Attribute Details

#erhufileObject

Returns the value of attribute erhufile.



3
4
5
# File 'lib/erhu/app.rb', line 3

def erhufile
  @erhufile
end

Instance Method Details

#databaseObject



15
16
17
18
19
20
21
# File 'lib/erhu/app.rb', line 15

def database
  if File.exist?(@database_path)
    @database ||= YAML.load_file(@database_path) || {}
  else
    @database ||= {}
  end
end

#git(repository_url, branch: nil, name: nil, tag: nil, &block) ⇒ Object



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
# File 'lib/erhu/app.rb', line 31

def git(repository_url, branch: nil, name: nil, tag: nil, &block)
  name = name || File.basename(URI.parse(repository_url).path, '.git')
  
  info = self.database.fetch(name, {})
  info.delete(:commit)
  if info == {repository_url: repository_url, branch: branch, name: name, tag: tag}
    puts "ignore #{repository_url}"
    return
  else
    FileUtils.rm_rf("#{@target}/#{name}")
  end

  warn!("Do not use Git for version control without careful consideration.")

  spinner = TTY::Spinner.new("[:spinner] git clone #{repository_url} ...")
  spinner.auto_spin
  repo = Git.clone(repository_url, "#{@target}/#{name}", branch: branch)
  unless tag.blank?
    
    tags = repo.tags
    if tags.map { |tag| tag.name }.include?(tag)
      commit_id = tags.select { |node| node.name == tag }.first.objectish
      repo.checkout(tag, start_point: commit_id, new_branch: true)
    end
  end
  spinner.stop("Done!")

  block.call(repo, self) unless block.blank?

  self.database[name] = {repository_url: repository_url,
    branch: branch, name: name, tag: tag, commit: repo.revparse('HEAD')
  }
rescue => e
  error! e
end

#package(package_url, name: nil, extension: nil, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/erhu/app.rb', line 67

def package(package_url, name: nil, extension: nil, &block)
  raise "package url is required" if package_url.blank?
  raise "name: 'package name' is required" if name.blank?

  _extension = extract_extension(package_url)
  if _extension == ".zip" or _extension == ".tar.gz"
    extension ||= _extension
  else
    extension ||= ".zip"
  end

  package_name = name
  package_hex = name.unpack('H*').first
  package_file_path = "#{@erhu_path}/#{package_name}-#{package_hex}#{extension}"
  
  if File.exist?(package_file_path)
    puts "ignored #{package_url}"
    return
  end

  bar = TTY::ProgressBar.new("downloading #{package_name} :percent [:bar]", total: 50)
  total_size = 0

  http.download package_url, destination: package_file_path,
    content_length_proc: -> (content_length) {
      total_size = content_length
    },
    progress_proc: -> (progress) {
      if total_size > 0
        bar.ratio = progress / total_size.to_f
      end
    }
  bar.finish

  unless block.blank?
    block.call(package_file_path, self)
    return
  end

  self.zip(package_file_path, package_name) if extension == ".zip"
  ungzip(package_file_path, File.join(@target, package_name)) if extension == ".tar.gz"
end

#runObject



114
115
116
117
118
119
# File 'lib/erhu/app.rb', line 114

def run
  instance_eval File.read(@erhufile), @erhufile, 1
  File.open(@database_path, 'w') do |file|
    file.write self.database.to_yaml
  end
end

#target(path) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/erhu/app.rb', line 23

def target(path)
  @target = path
  unless Dir.exist?(@target)
    FileUtils.mkdir_p(@target)
    puts "Created #{@target}"
  end
end

#zip(package_file_path, package_name) ⇒ Object



110
111
112
# File 'lib/erhu/app.rb', line 110

def zip(package_file_path, package_name)
  unzip(package_file_path, File.join(@target, package_name))
end