#!/usr/bin/env ruby # encoding: utf-8 # TODO: include private part from CLI in all Thor classes. # Create constants for repetitive strings. require 'thor' require 'rvmpow' require 'rvmpow/shared_private_methods' class Add < Thor include Shared def initialize(*args) super @pow = RvmPow::Pow.new end desc "powenv", RvmPow::ADD_POWENV def powenv exec_cmd { @pow.createPowenvFile } end desc "powlink", RvmPow::ADD_LINK def powlink exec_cmd { @pow.createPowLink } end desc "restart", RvmPow::TOUCH_RESTART def restart exec_cmd { @pow.touchRestartFile } end desc "gitignore", RvmPow::ADD_GITIGNORE def gitignore exec_cmd { @pow.gitignorePowenv } end desc "addRvmInfoToGemfile", RvmPow::ADD_GEMFILE def gemfile exec_cmd { @pow.addRvmInfoToGemfile } end end class Remove < Thor include Shared def initialize(*args) super @pow = RvmPow::Pow.new end desc "powenv", RvmPow::REMOVE_POWENV def powenv exec_cmd { @pow.deletePowenvFile } end desc "restart", RvmPow::REMOVE_RESTART def restart exec_cmd { @pow.deleteRestartFile } end desc "powlink", RvmPow::REMOVE_LINK def powlink exec_cmd { @pow.deletePowLink } end desc "gitignore", RvmPow::REMOVE_GITIGNORE def gitignore exec_cmd { @pow.restoreGitignore } end desc "gemfile", RvmPow::REMOVE_GEMFILE def gemfile exec_cmd {@pow.restoreGemfile } end end class CLI < Thor include Shared def initialize(*args) super @pow = RvmPow::Pow.new end desc "init", "enable pow" long_desc <<-LONGDESC `rvmpow init` will: - create a '.powenv' file in the current directory. \x5- add .powenv to the .gitignore file. \x5- add app link in ~/.pow \x5- add ruby and gemset info to Gemfile. \x5- touch restart.txt in ./tmp/ LONGDESC option :show, type: :boolean, desc: "open in default browser" def init exec_cmd {nil} shitHappens = false # fail safe to avoid multiple initializations if File.exists?('.powenv') say "Initialization aborted.\nLooks like you already ran it on this project." exit 0 end if @pow.createPowenvFile say_status "Done", RvmPow::ADD_POWENV, :green else say_status "Failed", RvmPow::ADD_POWENV, :red shitHappens = true end if @pow.createPowLink say_status "Done", RvmPow::ADD_LINK, :green else say_status "Failed", RvmPow::ADD_LINK, :red shitHappens = true end if @pow.touchRestartFile say_status "Done", RvmPow::TOUCH_RESTART, :green else say_status "Failed", RvmPow::TOUCH_RESTART, :red shitHappens = true end if @pow.gitignorePowenv say_status "Done", RvmPow::ADD_GITIGNORE, :green else say_status "Failed", RvmPow::ADD_GITIGNORE, :red shitHappens = true end if @pow.addRvmInfoToGemfile say_status "Done", RvmPow::ADD_GEMFILE, :green else say_status "Failed", RvmPow::ADD_GEMFILE, :red shitHappens = true end if options[:show] if shitHappens say_status "Failed", "Open app in default browser canceled due to previous error(s)", :red say "Initialization failed.", :red else say_status "Done", "Open app in default browser", :green open end end end desc "open", "open the current Rack app in the default browser" def open %x(open http://#{RvmPow::APP_NAME}.dev) end desc "clear", "disable pow" long_desc <<-LONGDESC `rvmpow clear` will remove only the files necessary for pow. It's essentially reverting to the state before you ran `rvmpow init` LONGDESC def clear exec_cmd {nil} shitHappens = false if @pow.restoreGitignore say_status "Done", RvmPow::REMOVE_GITIGNORE, :green else say_status "Failed", RvmPow::REMOVE_GITIGNORE, :red shitHappens = true end if @pow.restoreGemfile say_status "Done", RvmPow::REMOVE_GEMFILE, :green else say_status "Failed", RvmPow::REMOVE_GEMFILE, :red shitHappens = true end if @pow.deletePowenvFile say_status "Done", RvmPow::REMOVE_POWENV, :green else say_status "Failed", RvmPow::REMOVE_POWENV, :red shitHappens = true end if @pow.deleteRestartFile say_status "Done", RvmPow::REMOVE_RESTART, :green else say_status "Failed", RvmPow::REMOVE_RESTART, :red shitHappens = true end if @pow.deletePowLink say_status "Done", RvmPow::REMOVE_LINK, :green else say_status "Failed", RvmPow::REMOVE_LINK, :red shitHappens = true end if shitHappens say "Reverting all changes failed.", :red end end desc "add SUBCOMMAND", "add specific options" subcommand "add", Add desc "remove SUBCOMMAND", "remove specific options" subcommand "remove", Remove end CLI.start(ARGV)