#!/usr/bin/env ruby require 'optparse' require 'yaml' SOLARGRAPH_YML = ".solargraph.yml" DEFINITIONS_RB = "config/definitions.rb" def run parse_options unless is_rails? puts "Must run from a Rails app folder. Aborting." exit 1 end exec_cmd "gem install solargraph", title: "Installing Solargraph gem..." exec_cmd "solargraph download-core", title: "Installing documentation. This might take a while..." exec_cmd "yard gems" exec_cmd "solargraph bundle" unless File.exist?(SOLARGRAPH_YML) exec_cmd "curl -s https://gist.githubusercontent.com/cmer/024991c85c4de01dab17632b2dc7f064/raw > #{SOLARGRAPH_YML}", title: "Copying .solargraph.yml..." else puts "Skipped copying .solargraph.yml. Already exists." end unless File.exist?(DEFINITIONS_RB) exec_cmd "curl -s https://gist.githubusercontent.com/castwide/28b349566a223dfb439a337aea29713e/raw > #{DEFINITIONS_RB}", title: "Copying definitions.rb..." else puts "Skipped copying .definitions.rb. Already exists." end install_sgr if install_sgr? puts "Done!" end def is_rails? File.exist?(File.join(Dir.pwd, './bin/rails')) end def exec_cmd(cmd, title: nil) puts title if title out = `#{cmd}` if $? != 0 puts out puts "\nAn error as occured. Aborting.\n" exit 1 end end def install_sgr? @options[:sgr] end def install_sgr exec_cmd "gem install solargraph-rails --pre", title: "Installing `solargraph-rails` gem..." exec_cmd "bundle add annotate", title: "Adding `annotate` gem to Gemfile..." exec_cmd "rails g annotate:install", title: "Initializing `annotate` gem..." exec_cmd "rake annotate_models", title: "Annotating models..." puts "Adding `solargraph-rails` plugin to #{SOLARGRAPH_YML}..." h = YAML.load_file(SOLARGRAPH_YML) h['plugins'] ||= [] h['plugins'] << 'solargraph-rails' unless h['plugins'].include?('solargraph-rails') File.open(SOLARGRAPH_YML, "w") do |file| file.write h .to_yaml end end def parse_options @options = {} optparse = OptionParser.new do |opts| # Set a banner, displayed at the top of the help screen. opts.banner = "Usage: solargraph-rails-init [options]" @options[:sgr] = false opts.on('-g', '--solargraph-rails-gem', 'Install and configure the solargraph-rails gem') do @options[:sgr] = true end # This displays the help screen, all programs are assumed to have this option. opts.on('-h', '--help', 'Display this screen') do puts opts exit end end optparse.parse! end run