PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/gnome_terminals_4_rails.yml

https://github.com/felixgao/chitsheet
YAML | 150 lines | 100 code | 33 blank | 17 comment | 0 complexity | 4a3535f4a74683f940479819c61e58c4 MD5 | raw file
  1. ---
  2. gnome_terminals_4_rails: |-
  3. #!/usr/bin/env ruby
  4. # A Ruby script that generates a bash script which automates the opening
  5. # of terminals in Gnome.
  6. def assemble_script
  7. script = $script1 + $listing + $script2 +
  8. "echo #{$commands} \n\n#{$commands}" +
  9. $error_message
  10. File.open(ARGV[0], 'w') do |f|
  11. f.puts script
  12. end
  13. end
  14. def display_help_and_exit
  15. puts <<-EOF
  16. Gnome Terminals 4 Rails.
  17. USAGE: terminals4rails file_name command1 command2 ... "title":"exec"
  18. file_name: File name of the generated bash script.
  19. tilte: Title of the terminal tab
  20. exec: bash command. GIGO
  21. Built-in commands:
  22. bash The Bash
  23. vim Vim editor
  24. vimNERD Vim with NERDTree, if pre-installed
  25. server Start script/server
  26. autospec Start autospect
  27. EOF
  28. exit
  29. end
  30. # Display helf if there are less than 2 arguments.
  31. display_help_and_exit if ARGV.length < 2
  32. ARGV.each { |arg| display_help_and_exit if arg == "--help" or arg == "-h" }
  33. # Start the core script
  34. $script1 = <<-EOF
  35. #!/bin/bash
  36. # Rails Terminals for Rails and Gnome
  37. # A simple script that opens a Gnome terminal with titled tabs.
  38. # Tabs with following bash commands:
  39. EOF
  40. $script2 = <<-EOF
  41. # USAGE: terminal4rails.sh working_dir
  42. dir=$1 # The first argument is the path of the working directory.
  43. err=0 # If err becomes negative, an error message is generated in the end.
  44. if [ -z $dir ] || [ ! -d $dir ]; then
  45. echo "Please specify a working directory as the argument"
  46. exit
  47. fi
  48. # If the argument is a relative path, then augment it with
  49. # the current dir.
  50. if [ ${dir:0:1} != '~' ] && [ ${dir:0:1} != '/' ]; then
  51. dir=`pwd`'/'$dir
  52. fi
  53. echo "Working directory: $dir"
  54. EOF
  55. $error_message =<<-EOF
  56. if [ $err -lt 0 ]; then
  57. echo "WARNING: Some tabs could not be opened."
  58. fi
  59. EOF
  60. $commands="gnome-terminal --working-directory=$dir --geometry=112x30 "
  61. execs = Array.new
  62. ARGV[1..-1].each do |arg|
  63. case arg
  64. when "bash" then exec="bash"; title="Bash"
  65. when "vim" then exec="vim"; title="Vim"
  66. when "vimNERD" then exec='vim +NERDTree'; title="Vim"
  67. when "server" then exec="script/server && bash"
  68. title="script/server"
  69. when "autospec" then exec='autospec'; title="autospec"
  70. else
  71. if arg =~ /.+:.+/
  72. title, exec = arg.split(":")
  73. else raise "ERROR: Input is not recognised."
  74. end
  75. end
  76. first_exec = exec.split(" ")[0]
  77. if first_exec[0] == '"' or first_exec[0] == "'"
  78. first_exec = first_exec[1..-1]
  79. end
  80. script_title = title.downcase.gsub(/\W/, "_" )
  81. $script2 += <<-EOF
  82. #{script_title}_t="#{title}"
  83. check_exec=(`whereis -b #{first_exec}`) # Check whether binary exist
  84. if [ ${#check_exec[@]} -gt "1" ]; then
  85. echo "#{title} found in ${check_exec[1]}."
  86. #{script_title}_e="bash -c '#{exec}'"
  87. elif [ -x $dir/#{first_exec} ]; then
  88. echo "#{title} found in "$dir"#{first_exec}."
  89. #{script_title}_e="bash -c '#{exec}'"
  90. else
  91. #{script_title}_e="echo" # Closes the tab w/o action.
  92. err=$(( $err - 1 ))
  93. echo "#{first_exec} not found."
  94. fi
  95. EOF
  96. execs << [exec, script_title]
  97. end
  98. $listing = ""
  99. execs.each do |exec, title|
  100. $commands += "--tab -t $#{title}_t -e \"$#{title}_e\" "
  101. $listing += "# * #{exec}\n"
  102. end
  103. # Ask for permission to overwrite an existing file.
  104. if File.exists? ARGV[0] then
  105. print "Do you want to overwrite the file #{ARGV[0]} (y/n): "
  106. answer = $stdin.gets
  107. if answer.downcase[0] == "y" then
  108. puts "Script is assembled."
  109. assemble_script
  110. else
  111. puts "Script terminates without writing."
  112. exit
  113. end
  114. else
  115. puts "Script is assembled."
  116. assemble_script
  117. end