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" ...@@ -36,8 +36,79 @@ $LOAD_PATH.unshift "#{CREW_LIB_PATH}lib"
USER = `whoami`.chomp 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 #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) @device = JSON.parse(File.read(CREW_CONFIG_PATH + 'device.json'), symbolize_names: true)
#symbolize also values #symbolize also values
...@@ -45,20 +116,33 @@ abort "Chromebrew should not be run as root." if Process.uid == 0 && @command != ...@@ -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] @device[key] = @device[key].to_sym rescue @device[key]
end 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) def set_package (pkgName, silent = false)
require CREW_LIB_PATH + 'packages/' + pkgName require CREW_LIB_PATH + 'packages/' + pkgName
@pkg = Object.const_get(pkgName.capitalize) @pkg = Object.const_get(pkgName.capitalize)
@pkg.name = pkgName @pkg.name = pkgName
puts "Found #{pkgName}, version #{@pkg.version}" unless silent print_package(pkgName, true) unless silent
end end
def list_packages def list_packages
Find.find (CREW_LIB_PATH + 'packages') do |filename| Find.find (CREW_LIB_PATH + 'packages') do |filename|
Find.find(CREW_CONFIG_PATH + 'meta/') do |packageList| if File.extname(filename) == '.rb'
packageName = File.basename filename, '.rb' print_package File.basename filename, '.rb'
print '(i) ' if packageList == CREW_CONFIG_PATH + 'meta/' + packageName + '.filelist'
end end
puts File.basename filename, '.rb' if File.extname(filename) == '.rb'
end end
end end
...@@ -66,7 +150,7 @@ def search (pkgName, silent = false) ...@@ -66,7 +150,7 @@ def search (pkgName, silent = false)
Find.find (CREW_LIB_PATH + 'packages') do |filename| Find.find (CREW_LIB_PATH + 'packages') do |filename|
return set_package(pkgName, silent) if filename == CREW_LIB_PATH + 'packages/' + pkgName + '.rb' return set_package(pkgName, silent) if filename == CREW_LIB_PATH + 'packages/' + pkgName + '.rb'
end end
abort "package #{pkgName} not found :(" abort "Package #{pkgName} not found. :(".lightred
end end
def help (pkgName) def help (pkgName)
...@@ -94,6 +178,7 @@ def help (pkgName) ...@@ -94,6 +178,7 @@ def help (pkgName)
puts "Tips:" puts "Tips:"
puts " crew search | grep '(i)' will return all installed packages." 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 -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" when "update"
puts "Update crew." puts "Update crew."
puts "Usage: crew update" puts "Usage: crew update"
...@@ -134,12 +219,12 @@ def whatprovides (pkgName) ...@@ -134,12 +219,12 @@ def whatprovides (pkgName)
fileArray.sort.each do |item| fileArray.sort.each do |item|
puts item puts item
end end
puts "\nTotal found: #{fileArray.length}" puts "\nTotal found: #{fileArray.length}".lightgreen
end end
end end
def update 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 #update package lists
Dir.chdir CREW_LIB_PATH do Dir.chdir CREW_LIB_PATH do
...@@ -165,7 +250,7 @@ def update ...@@ -165,7 +250,7 @@ def update
puts "" puts ""
puts "Run 'crew upgrade' to upgrade everything or 'crew upgrade <packageName>' to upgrade a specific package." puts "Run 'crew upgrade' to upgrade everything or 'crew upgrade <packageName>' to upgrade a specific package."
else else
puts "Your software is up to date." puts "Your software is up to date.".lightgreen
end end
end end
...@@ -186,7 +271,7 @@ def upgrade ...@@ -186,7 +271,7 @@ def upgrade
resolve_dependencies_and_install resolve_dependencies_and_install
@pkg.in_upgrade = false @pkg.in_upgrade = false
else else
puts "#{@pkg.name} is already up to date." puts "#{@pkg.name} is already up to date.".lightgreen
end end
else else
toBeUpdated = [] toBeUpdated = []
...@@ -205,9 +290,9 @@ def upgrade ...@@ -205,9 +290,9 @@ def upgrade
resolve_dependencies_and_install resolve_dependencies_and_install
@pkg.in_upgrade = false @pkg.in_upgrade = false
end end
puts "Packages have been updated." puts "Packages have been updated.".lightgreen
else else
puts "Your software is already up to date." puts "Your software is already up to date.".lightgreen
end end
end end
end end
...@@ -217,7 +302,7 @@ def download ...@@ -217,7 +302,7 @@ def download
source = @pkg.is_source?(@device[:architecture]) source = @pkg.is_source?(@device[:architecture])
if !url 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 elsif !source
puts "Precompiled binary available, downloading..." puts "Precompiled binary available, downloading..."
elsif @pkg.build_from_source elsif @pkg.build_from_source
...@@ -235,9 +320,9 @@ def download ...@@ -235,9 +320,9 @@ def download
end end
Dir.chdir CREW_BREW_DIR do Dir.chdir CREW_BREW_DIR do
system('wget', '--continue', '--no-check-certificate', url, '-O', filename) 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 end
puts "Archive downloaded" puts "Archive downloaded".lightgreen
return {source: source, filename: filename} return {source: source, filename: filename}
end end
...@@ -256,7 +341,7 @@ def unpack (meta) ...@@ -256,7 +341,7 @@ def unpack (meta)
# Check the number of directories in the archive # Check the number of directories in the archive
entries=Dir["#{extract_dir}/*"] entries=Dir["#{extract_dir}/*"]
if entries.length == 0 if entries.length == 0
abort "empty archive: #{meta[:filename]}" abort "Empty archive: #{meta[:filename]}".lightred
elsif entries.length == 1 && File.directory?(entries.first) elsif entries.length == 1 && File.directory?(entries.first)
# Use `extract_dir/dir_in_archive` if there is only one directory. # Use `extract_dir/dir_in_archive` if there is only one directory.
target_dir = entries.first target_dir = entries.first
...@@ -317,7 +402,7 @@ def resolve_dependencies_and_install ...@@ -317,7 +402,7 @@ def resolve_dependencies_and_install
search origin, true search origin, true
install install
rescue InstallError => e rescue InstallError => e
abort "#{@pkg.name} failed to install: #{e.to_s}" abort "#{@pkg.name} failed to install: #{e.to_s}".lightred
ensure ensure
#cleanup #cleanup
unless ARGV[2] == 'keep' unless ARGV[2] == 'keep'
...@@ -382,14 +467,14 @@ def resolve_dependencies ...@@ -382,14 +467,14 @@ def resolve_dependencies
return if dependencies.empty? 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| dependencies.each do |dep|
print dep + " " print dep + " "
end end
puts "" puts ""
puts "Do you agree? [Y/n]" print "Do you agree? [Y/n] "
response = STDIN.getc response = STDIN.getc
case response case response
when "n" when "n"
...@@ -398,7 +483,7 @@ def resolve_dependencies ...@@ -398,7 +483,7 @@ def resolve_dependencies
puts "Proceeding..." puts "Proceeding..."
proceed = true proceed = true
else else
puts "I don't understand '#{response}' :(" puts "I don't understand '#{response}'. :(".lightred
abort "No changes made." abort "No changes made."
end end
...@@ -412,7 +497,7 @@ end ...@@ -412,7 +497,7 @@ end
def install def install
if !@pkg.in_upgrade && @device[:installed_packages].any? { |pkg| pkg[:name] == @pkg.name } 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 return
end end
...@@ -420,7 +505,7 @@ def install ...@@ -420,7 +505,7 @@ def install
meta = download meta = download
target_dir = unpack meta target_dir = unpack meta
if meta[:source] == true 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 # build from source and place binaries at CREW_DEST_DIR
# CREW_DEST_DIR contains usr/local/... hierarchy # CREW_DEST_DIR contains usr/local/... hierarchy
...@@ -453,7 +538,7 @@ def install ...@@ -453,7 +538,7 @@ def install
output = JSON.parse @device.to_json output = JSON.parse @device.to_json
file.write JSON.pretty_generate(output) file.write JSON.pretty_generate(output)
end end
puts "#{@pkg.name.capitalize} installed!" puts "#{@pkg.name.capitalize} installed!".lightgreen
end end
def resolve_dependencies_and_build def resolve_dependencies_and_build
...@@ -467,7 +552,7 @@ def resolve_dependencies_and_build ...@@ -467,7 +552,7 @@ def resolve_dependencies_and_build
search origin, true search origin, true
build_package Dir.pwd build_package Dir.pwd
rescue InstallError => e rescue InstallError => e
abort "#{@pkg.name} failed to build: #{e.to_s}" abort "#{@pkg.name} failed to build: #{e.to_s}".lightred
ensure ensure
#cleanup #cleanup
unless ARGV[2] == 'keep' unless ARGV[2] == 'keep'
...@@ -480,8 +565,8 @@ def resolve_dependencies_and_build ...@@ -480,8 +565,8 @@ def resolve_dependencies_and_build
end end
def build_package (pwd) def build_package (pwd)
abort "It is not possible to build fake package" if @pkg.is_fake? abort "It is not possible to build fake package".lightred 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 without source".lightred if !@pkg.is_source?(@device[:architecture])
# download source codes and unpack it # download source codes and unpack it
meta = download meta = download
...@@ -515,14 +600,14 @@ def archive_package (pwd) ...@@ -515,14 +600,14 @@ def archive_package (pwd)
Dir.chdir pwd do Dir.chdir pwd do
system "sha1sum #{pkg_name} > #{pkg_name}.sha1" system "sha1sum #{pkg_name} > #{pkg_name}.sha1"
end end
puts "#{pkg_name} is built!" puts "#{pkg_name} is built!".lightgreen
end end
def remove (pkgName) def remove (pkgName)
#make sure the package is actually installed #make sure the package is actually installed
unless @device[:installed_packages].any? { |pkg| pkg[:name] == pkgName } unless @device[:installed_packages].any? { |pkg| pkg[:name] == pkgName }
puts "Package #{pkgName} isn't installed." puts "Package #{pkgName} isn't installed.".lightred
return return
end end
...@@ -564,7 +649,7 @@ def remove (pkgName) ...@@ -564,7 +649,7 @@ def remove (pkgName)
file.write JSON.pretty_generate(out) file.write JSON.pretty_generate(out)
end end
puts "#{pkgName.capitalize} removed!" puts "#{pkgName.capitalize} removed!".lightgreen
end end
...@@ -578,7 +663,7 @@ when "help" ...@@ -578,7 +663,7 @@ when "help"
end end
when "search" when "search"
if @pkgName if @pkgName
search @pkgName print_package @pkgName, true
else else
list_packages list_packages
end end
...@@ -620,10 +705,10 @@ when "remove" ...@@ -620,10 +705,10 @@ when "remove"
help "remove" help "remove"
end end
when nil when nil
puts "Chromebrew, version 0.4.2" puts "Chromebrew, version 0.4.3"
puts "Usage: crew [command] [package]" puts "Usage: crew [command] [package]"
help nil help nil
else else
puts "I have no idea how to do #{@command} :(" puts "I have no idea how to do #{@command} :(".lightred
help nil help nil
end 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