Commit d4d5a7d7 authored by Damian Montero's avatar Damian Montero Committed by GitHub

Merge pull request #720 from uberhacker/enhance-search-command

Enhance crew search command
parents e66947f7 5ab0970c
......@@ -36,8 +36,79 @@ $LOAD_PATH.unshift "#{CREW_LIB_PATH}lib"
USER = `whoami`.chomp
# colorization
class String
def colorize(color_code, shade)
"\e[#{shade};#{color_code}m#{self}\e[0m"
end
def black
colorize(30, 0)
end
def red
colorize(31, 0)
end
def green
colorize(32, 0)
end
def orange
colorize(33, 0)
end
def blue
colorize(34, 0)
end
def purple
colorize(35, 0)
end
def cyan
colorize(36, 0)
end
def lightgray
colorize(37, 0)
end
def gray
colorize(30, 1)
end
def lightred
colorize(31, 1)
end
def lightgreen
colorize(32, 1)
end
def yellow
colorize(33, 1)
end
def lightblue
colorize(34, 1)
end
def lightpurple
colorize(35, 1)
end
def lightcyan
colorize(36, 1)
end
def white
colorize(37, 1)
end
end
#disallow sudo
abort "Chromebrew should not be run as root." if Process.uid == 0 && @command != "remove"
abort "Chromebrew should not be run as root.".lightred if Process.uid == 0
@device = JSON.parse(File.read(CREW_CONFIG_PATH + 'device.json'), symbolize_names: true)
#symbolize also values
......@@ -45,20 +116,33 @@ abort "Chromebrew should not be run as root." if Process.uid == 0 && @command !=
@device[key] = @device[key].to_sym rescue @device[key]
end
def print_package(pkgName, extra = false)
search pkgName, true
Find.find(CREW_CONFIG_PATH + 'meta/') do |packageList|
print '(i) '.lightgreen if packageList == CREW_CONFIG_PATH + 'meta/' + pkgName + '.filelist'
end
print @pkg.name
print ": #{@pkg.description}" if @pkg.description
if extra
puts ""
puts @pkg.homepage if @pkg.homepage
print "version #{@pkg.version}"
end
puts ""
end
def set_package (pkgName, silent = false)
require CREW_LIB_PATH + 'packages/' + pkgName
@pkg = Object.const_get(pkgName.capitalize)
@pkg.name = pkgName
puts "Found #{pkgName}, version #{@pkg.version}" unless silent
print_package(pkgName, true) unless silent
end
def list_packages
Find.find (CREW_LIB_PATH + 'packages') do |filename|
Find.find(CREW_CONFIG_PATH + 'meta/') do |packageList|
packageName = File.basename filename, '.rb'
print '(i) ' if packageList == CREW_CONFIG_PATH + 'meta/' + packageName + '.filelist'
if File.extname(filename) == '.rb'
print_package File.basename filename, '.rb'
end
puts File.basename filename, '.rb' if File.extname(filename) == '.rb'
end
end
......@@ -66,7 +150,7 @@ def search (pkgName, silent = false)
Find.find (CREW_LIB_PATH + 'packages') do |filename|
return set_package(pkgName, silent) if filename == CREW_LIB_PATH + 'packages/' + pkgName + '.rb'
end
abort "package #{pkgName} not found :("
abort "Package #{pkgName} not found. :(".lightred
end
def help (pkgName)
......@@ -94,6 +178,7 @@ def help (pkgName)
puts "Tips:"
puts " crew search | grep '(i)' will return all installed packages."
puts " crew search | grep -v '(i)' will return all available packages not already installed."
puts " crew search | grep -i 'pattern' will return all packages with 'pattern' in the description."
when "update"
puts "Update crew."
puts "Usage: crew update"
......@@ -134,12 +219,12 @@ def whatprovides (pkgName)
fileArray.sort.each do |item|
puts item
end
puts "\nTotal found: #{fileArray.length}"
puts "\nTotal found: #{fileArray.length}".lightgreen
end
end
def update
abort "'crew update' is used to update crew itself. Use 'crew upgrade <packageName> to upgrade a specific package." if @pkgName
abort "'crew update' is used to update crew itself. Use 'crew upgrade <packageName> to upgrade a specific package.".orange if @pkgName
#update package lists
Dir.chdir CREW_LIB_PATH do
......@@ -165,7 +250,7 @@ def update
puts ""
puts "Run 'crew upgrade' to upgrade everything or 'crew upgrade <packageName>' to upgrade a specific package."
else
puts "Your software is up to date."
puts "Your software is up to date.".lightgreen
end
end
......@@ -186,7 +271,7 @@ def upgrade
resolve_dependencies_and_install
@pkg.in_upgrade = false
else
puts "#{@pkg.name} is already up to date."
puts "#{@pkg.name} is already up to date.".lightgreen
end
else
toBeUpdated = []
......@@ -205,9 +290,9 @@ def upgrade
resolve_dependencies_and_install
@pkg.in_upgrade = false
end
puts "Packages have been updated."
puts "Packages have been updated.".lightgreen
else
puts "Your software is already up to date."
puts "Your software is already up to date.".lightgreen
end
end
end
......@@ -217,7 +302,7 @@ def download
source = @pkg.is_source?(@device[:architecture])
if !url
abort "No precompiled binary for #{@device[:architecture]} nor source is available."
abort "No precompiled binary for #{@device[:architecture]} nor source is available.".lightred
elsif !source
puts "Precompiled binary available, downloading..."
elsif @pkg.build_from_source
......@@ -235,9 +320,9 @@ def download
end
Dir.chdir CREW_BREW_DIR do
system('wget', '--continue', '--no-check-certificate', url, '-O', filename)
abort 'Checksum mismatch :/ try again' unless Digest::SHA1.hexdigest( File.read("./#{filename}") ) == sha1sum
abort 'Checksum mismatch. :/ Try again.'.lightred unless Digest::SHA1.hexdigest( File.read("./#{filename}") ) == sha1sum
end
puts "Archive downloaded"
puts "Archive downloaded".lightgreen
return {source: source, filename: filename}
end
......@@ -256,7 +341,7 @@ def unpack (meta)
# Check the number of directories in the archive
entries=Dir["#{extract_dir}/*"]
if entries.length == 0
abort "empty archive: #{meta[:filename]}"
abort "Empty archive: #{meta[:filename]}".lightred
elsif entries.length == 1 && File.directory?(entries.first)
# Use `extract_dir/dir_in_archive` if there is only one directory.
target_dir = entries.first
......@@ -317,7 +402,7 @@ def resolve_dependencies_and_install
search origin, true
install
rescue InstallError => e
abort "#{@pkg.name} failed to install: #{e.to_s}"
abort "#{@pkg.name} failed to install: #{e.to_s}".lightred
ensure
#cleanup
unless ARGV[2] == 'keep'
......@@ -382,14 +467,14 @@ def resolve_dependencies
return if dependencies.empty?
puts "Following packages also need to be installed: "
puts "The following packages also need to be installed: "
dependencies.each do |dep|
print dep + " "
end
puts ""
puts "Do you agree? [Y/n]"
print "Do you agree? [Y/n] "
response = STDIN.getc
case response
when "n"
......@@ -398,7 +483,7 @@ def resolve_dependencies
puts "Proceeding..."
proceed = true
else
puts "I don't understand '#{response}' :("
puts "I don't understand '#{response}'. :(".lightred
abort "No changes made."
end
......@@ -412,7 +497,7 @@ end
def install
if !@pkg.in_upgrade && @device[:installed_packages].any? { |pkg| pkg[:name] == @pkg.name }
puts "Package #{@pkg.name} already installed, skipping..."
puts "Package #{@pkg.name} already installed, skipping...".lightgreen
return
end
......@@ -420,7 +505,7 @@ def install
meta = download
target_dir = unpack meta
if meta[:source] == true
abort "You don't have a working C compiler. Run 'crew install buildessential' to get one and try again." unless system("gcc", "--version")
abort "You don't have a working C compiler. Run 'crew install buildessential' to get one and try again.".lightred unless system("gcc", "--version")
# build from source and place binaries at CREW_DEST_DIR
# CREW_DEST_DIR contains usr/local/... hierarchy
......@@ -453,7 +538,7 @@ def install
output = JSON.parse @device.to_json
file.write JSON.pretty_generate(output)
end
puts "#{@pkg.name.capitalize} installed!"
puts "#{@pkg.name.capitalize} installed!".lightgreen
end
def resolve_dependencies_and_build
......@@ -467,7 +552,7 @@ def resolve_dependencies_and_build
search origin, true
build_package Dir.pwd
rescue InstallError => e
abort "#{@pkg.name} failed to build: #{e.to_s}"
abort "#{@pkg.name} failed to build: #{e.to_s}".lightred
ensure
#cleanup
unless ARGV[2] == 'keep'
......@@ -480,8 +565,8 @@ def resolve_dependencies_and_build
end
def build_package (pwd)
abort "It is not possible to build fake package" if @pkg.is_fake?
abort "It is not possible to build without source" if !@pkg.is_source?(@device[:architecture])
abort "It is not possible to build fake package".lightred if @pkg.is_fake?
abort "It is not possible to build without source".lightred if !@pkg.is_source?(@device[:architecture])
# download source codes and unpack it
meta = download
......@@ -515,14 +600,14 @@ def archive_package (pwd)
Dir.chdir pwd do
system "sha1sum #{pkg_name} > #{pkg_name}.sha1"
end
puts "#{pkg_name} is built!"
puts "#{pkg_name} is built!".lightgreen
end
def remove (pkgName)
#make sure the package is actually installed
unless @device[:installed_packages].any? { |pkg| pkg[:name] == pkgName }
puts "Package #{pkgName} isn't installed."
puts "Package #{pkgName} isn't installed.".lightred
return
end
......@@ -564,7 +649,7 @@ def remove (pkgName)
file.write JSON.pretty_generate(out)
end
puts "#{pkgName.capitalize} removed!"
puts "#{pkgName.capitalize} removed!".lightgreen
end
......@@ -578,7 +663,7 @@ when "help"
end
when "search"
if @pkgName
search @pkgName
print_package @pkgName, true
else
list_packages
end
......@@ -620,10 +705,10 @@ when "remove"
help "remove"
end
when nil
puts "Chromebrew, version 0.4.2"
puts "Chromebrew, version 0.4.3"
puts "Usage: crew [command] [package]"
help nil
else
puts "I have no idea how to do #{@command} :("
puts "I have no idea how to do #{@command} :(".lightred
help nil
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment