1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
require 'find'
require 'net/http'
require 'uri'
require 'digest/sha1'
require 'json'
require 'fileutils'
@command = ARGV[0]
@pkgName = ARGV[1]
@device = JSON.parse(File.read('./device.json'), symbolize_names: true)
@device.each do |key, elem| #symbolize also values
@device[key] = @device[key].to_sym rescue @device[key]
end
def setPkg
require './packages/' + @pkgName
@pkg = Object.const_get(@pkgName.capitalize)
puts "Found #{@pkgName}, version #{@pkg.version}"
end
def search (pkgName)
Find.find ('packages') do |filename|
return setPkg if filename == 'packages/' + pkgName + '.rb'
end
puts "package #{pkgName} not found :("
end
def download
if @pkg.binary_url && @pkg.binary_url.has_key?(@device[:architecture])
url = @pkg.binary_url[@device[:architecture]]
source = false
puts "Precompiled binary available, downloading..."
else
url = @pkg.source_url
source = true
puts "No precompiled binary available for your platform, downloading source..."
end
uri = URI.parse url
filename = File.basename(uri.path)
system('wget', '--content-disposition', url)
abort 'Checksum mismatch, try again' unless Digest::SHA1.hexdigest( File.read("./#{filename}") ) == @pkg.binary_sha1[@device[:architecture]]
puts "Archive downloaded"
return {source: source, filename: filename}
end
def install
meta = download
if meta[:source] == true
puts "Building from source, this may take a while..."
@pkg.build
puts "Installing..."
@pkg.install
else
puts "Unpacking archive, this may take a while..."
system "tar", "zxf", meta[:filename]
puts "Installing..."
FileUtils.mv './usr', './xd'
FileUtils.mv './dlist', "./meta/#{@pkgName}.directorylist"
FileUtils.mv './filelist', "./meta/#{@pkgName}.filelist"
end
#add to installed packages
@device[:installed_packages].push(name: @pkgName, version: @pkg.version)
File.open('./device.json', 'w') do |file|
output = JSON.parse @device.to_json
file.write JSON.pretty_generate(output)
end
puts "#{@pkgName.capitalize} installed!"
end
def remove
File.open("./meta/#{@pkgName}.filelist").each_line do |line|
File.unlink '.' + line.chomp
end
File.readlines("./meta/#{@pkgName}.directorylist").reverse.each do |line|
begin
Dir.rmdir '.' + line.chomp
rescue => exception #swallow exception
end
end
File.unlink "./meta/#{@pkgName}.filelist"
File.unlink "./meta/#{@pkgName}.directorylist"
#remove from installed packages
@device[:installed_packages].each do |elem|
@device[:installed_packages].delete elem if elem[:name] == @pkgName
end
File.open('./device.json', 'w') do |file|
out = JSON.parse @device.to_json
file.write JSON.pretty_generate(out)
end
puts "#{@pkgName.capitalize} removed!"
end
case @command
when "search"
search @pkgName
when "download"
search @pkgName
download
when "install"
search @pkgName
install
when "remove"
search @pkgName
remove
when nil
puts "Chromebrew, version 0.1"
puts "Usage: cbrew [command] [package]"
puts "Available commands: search, download, install, remove"
else
puts "I have no idea how to do #{@command} :("
puts "Available commands: search, download, install, remove"
end