Skip to content

Instantly share code, notes, and snippets.

@mgold
Created March 24, 2016 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgold/e6ce8426766f9b9a329d to your computer and use it in GitHub Desktop.
Save mgold/e6ce8426766f9b9a329d to your computer and use it in GitHub Desktop.
A script to help installing elm packages.
require "json"
require "net/http"
require "uri"
def download(name)
puts "Installing " + name
`elm package install #{name} --yes`
end
def prompt(instructions)
resp = ""
print instructions
resp = $stdin.gets.chomp
if resp == "y"
true
elsif resp == "n"
false
else
prompt "Must type 'y' for yes or 'n' for no: "
end
end
package = ARGV[0].downcase
yesToPrompts = ARGV.include? "--yes"
if package.nil?
$stderr.puts "Error: no package specified"
exit 1
end
if package.include? "start"
if prompt "Would you like to download StartApp and related packages? "
download "evancz/elm-effects"
download "evancz/elm-html"
download "evancz/elm-http"
download "evancz/start-app"
exit
end
end
uri = URI.parse('http://package.elm-lang.org/all-packages?elm-package-version=0.16')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code == "200"
result = JSON.parse(response.body)
names = result.map{|h| h["name"]}
matches = names.select{|n| n.downcase.include? package}
if matches.empty?
puts "No matching packages found."
elsif matches.length == 1
match = matches.first
if yesToPrompts
download match, true
else
puts "Found one match: " + match
if prompt "Do you want to install it now? (y/n) "
download match
else
puts "Okay, you can download #{match} yourself."
end
end
else
puts "Multiple matching packages were found. You should install the one(s) you want."
puts matches.map{|s| "\t"+s}
end
else
$stderr.puts "Error downloading package list."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment