mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Fix #4056: Ungraceful UX when add-grammar fails
This commit is contained in:
@@ -50,17 +50,18 @@ def command(*args)
|
|||||||
output.each_line do |line|
|
output.each_line do |line|
|
||||||
log " > #{line}"
|
log " > #{line}"
|
||||||
end
|
end
|
||||||
warn "Command failed. Aborting."
|
raise "Command failed. Aborting."
|
||||||
exit 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
usage = """Usage:
|
usage = <<~EOH
|
||||||
|
Usage:
|
||||||
#{$0} [-v|--verbose] [--replace grammar] url
|
#{$0} [-v|--verbose] [--replace grammar] url
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
#{$0} https://github.com/Alhadis/language-roff
|
#{$0} https://github.com/Alhadis/language-roff
|
||||||
#{$0} --replace sublime-apl https://github.com/Alhadis/language-apl
|
#{$0} --replace sublime-apl https://github.com/Alhadis/language-apl
|
||||||
"""
|
EOH
|
||||||
|
|
||||||
$replace = nil
|
$replace = nil
|
||||||
$verbose = true
|
$verbose = true
|
||||||
@@ -85,11 +86,23 @@ $url = ARGV[0]
|
|||||||
# No URL? Print a usage message and bail.
|
# No URL? Print a usage message and bail.
|
||||||
unless $url
|
unless $url
|
||||||
warn usage
|
warn usage
|
||||||
exit 1;
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
# Exit early if docker isn't installed or running.
|
# Flags to track which changes should be reverted on an error
|
||||||
log "Checking docker is installed and running"
|
did_remove? = false
|
||||||
|
did_add? = false
|
||||||
|
gitmodules = File.read("#{ROOT}/.gitmodules")
|
||||||
|
git_config = File.read("#{ROOT}/.git/config")
|
||||||
|
|
||||||
|
def restore_configs
|
||||||
|
File.write("#{ROOT}/.gitmodules", gitmodules)
|
||||||
|
File.write("#{ROOT}/.git/config", git_config)
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
# Exit early if Docker isn't installed or running.
|
||||||
|
log "Checking Docker is installed and running"
|
||||||
command('docker', 'ps')
|
command('docker', 'ps')
|
||||||
|
|
||||||
# Ensure the given URL is an HTTPS link
|
# Ensure the given URL is an HTTPS link
|
||||||
@@ -102,14 +115,16 @@ Dir.chdir(ROOT)
|
|||||||
|
|
||||||
if repo_old
|
if repo_old
|
||||||
log "Deregistering: #{repo_old}"
|
log "Deregistering: #{repo_old}"
|
||||||
|
removed = repo_old
|
||||||
command('git', 'submodule', 'deinit', repo_old)
|
command('git', 'submodule', 'deinit', repo_old)
|
||||||
command('git', 'rm', '-rf', repo_old)
|
command('git', 'rm', '-rf', repo_old)
|
||||||
command('script/grammar-compiler', 'update', '-f') if $compile
|
command('script/grammar-compiler', 'update', '-f')
|
||||||
end
|
end
|
||||||
|
|
||||||
log "Registering new submodule: #{repo_new}"
|
log "Registering new submodule: #{repo_new}"
|
||||||
|
added = repo_new
|
||||||
command('git', 'submodule', 'add', '-f', https, repo_new)
|
command('git', 'submodule', 'add', '-f', https, repo_new)
|
||||||
command('script/grammar-compiler', 'add', repo_new) if $compile
|
command('script/grammar-compiler', 'add', repo_new)
|
||||||
|
|
||||||
log "Confirming license"
|
log "Confirming license"
|
||||||
if repo_old
|
if repo_old
|
||||||
@@ -122,3 +137,20 @@ log "Updating grammar documentation in vendor/README.md"
|
|||||||
command('bundle', 'exec', 'rake', 'samples')
|
command('bundle', 'exec', 'rake', 'samples')
|
||||||
command('script/sort-submodules')
|
command('script/sort-submodules')
|
||||||
command('script/list-grammars')
|
command('script/list-grammars')
|
||||||
|
rescue => ex
|
||||||
|
if did_add?
|
||||||
|
`git submodule deinit #{repo_new}`
|
||||||
|
`rm -rf #{repo_new}`
|
||||||
|
`rm -rf .git/modules/#{repo_new}/`
|
||||||
|
end
|
||||||
|
restore_configs()
|
||||||
|
if did_remove?
|
||||||
|
`rm -rf #{repo_old}`
|
||||||
|
`git submodule add -f "#{https}", "#{repo_old}"`
|
||||||
|
|
||||||
|
# Revert twice. Make no assumpsions about
|
||||||
|
# badly-mangled the working tree is.
|
||||||
|
restore_configs()
|
||||||
|
end
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user