Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
Last active March 4, 2020 18:58
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 ForeverZer0/9e3cc0fe9cb3011646a4beaf4d8d8453 to your computer and use it in GitHub Desktop.
Save ForeverZer0/9e3cc0fe9cb3011646a4beaf4d8d8453 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'tty-prompt'
require 'fileutils'
@prompt = TTY::Prompt.new
def find_product(name)
paths = []
Dir.entries(Dir.home).each do |entry|
path = File.join(Dir.home, entry)
next unless File.directory?(path)
next unless /\.#{name}[0-9.]+$/i.match?(entry)
paths << path
end
paths
end
def delete_java_prefs(name)
prefs = File.expand_path('~/.java/.userPrefs/jetbrains')
unless File.directory?(prefs)
@prompt.say('Unable to locate Java user preferences directory')
return
end
Dir.entries(prefs).each do |entry|
path = File.join(prefs, entry)
next unless File.directory?(path)
next unless /#{name}/i.match?(entry)
FileUtils.rm_rf(path)
@prompt.say('Deleted Java user preferences for application')
end
end
def delete_user_key(base_dir)
keys = Dir.glob(File.join(base_dir, "config/eval/*evaluation.key"))
if keys.empty?
@prompt.say 'Unable to locate evaluation key'
return
end
File.delete(keys.first)
@prompt.say 'Deleted evaluation key'
end
def edit_xml(base_dir)
path = File.join(base_dir, 'config/options/other.xml')
unless File.exist?(path)
@prompt.say 'Unable to locate "other.xml" for application'
return
end
lines = File.readlines(path)
File.delete(path)
File.open(path, 'wb') do |io|
lines.each do |line|
next if /evlsprt/.match?(line)
io.puts line
end
end
@prompt.say 'Removed "evlsprt" elements from other.xml'
end
name = @prompt.ask('Which JetBrains product would you like to reset evaluation period for?') do |q|
q.validate(Proc.new { |a| find_product(a).size > 0 })
q.messages[:valid?] = 'Unable to locate JetBrains product by that name'
end
paths = find_product(name)
# Get the base directory
if paths.size > 1
# More than one matching product, give choice
dir = @prompt.select("Found #{path.size} matching products, please select one:") do |q|
paths.each do |path|
match = /\.([A-Za-z-]+)+([0-9.-]+)/.match(File.basename(path))
q.choice(name: "#{$1} #{$2}", value: path)
end
end
elsif paths.size == 1
# Only one match
dir = paths.first
/\.([A-Za-z-]+)+([0-9.-]+)/.match(File.basename(dir))
@prompt.say("Found #{$1} #{$2}")
else
# Soemthing went wrong...
@prompt.say('Done')
exit
end
delete_java_prefs(name)
delete_user_key(dir)
edit_xml(dir)
@prompt.say 'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment