Posted By


draq on 04/19/10

Tagged


Statistics


Viewed 414 times
Favorited by 0 user(s)

terminals4rails


/ Published in: Ruby
Save to your folder(s)

This Ruby script generates the [Gnome Terminals For Rails script](http://snipplr.com/view/32227/gnome-terminals-for-rails/ "Gnome Terminals For Rails").


Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby
  2.  
  3. # A Ruby script that generates a bash script which automates the opening
  4. # of terminals in Gnome.
  5.  
  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.  
  15. def display_help_and_exit
  16. puts <<-EOF
  17. Gnome Terminals 4 Rails.
  18.  
  19. USAGE: terminals4rails file_name command1 command2 ... "title":"exec"
  20.  
  21. file_name: File name of the generated bash script.
  22. tilte: Title of the terminal tab.
  23. exec: bash command. GIGO
  24.  
  25. Built-in commands:
  26. bash The Bash
  27. vim Vim editor
  28. vimNERD Vim with NERDTree, if pre-installed
  29. server Start script/server
  30. autospec Start autospect
  31. EOF
  32.  
  33. exit
  34. end
  35.  
  36. # Display helf if there are less than 2 arguments.
  37. display_help_and_exit if ARGV.length < 2
  38. ARGV.each { |arg| display_help_and_exit if arg == "--help" or arg == "-h" }
  39.  
  40.  
  41. # Start the core script
  42.  
  43. $script1 = <<-EOF
  44. #!/bin/bash
  45.  
  46. # Rails Terminals for Rails and Gnome
  47. # A simple script that opens a Gnome terminal with titled tabs.
  48.  
  49. # Tabs with following bash commands:
  50. EOF
  51.  
  52. $script2 = <<-EOF
  53.  
  54. # USAGE: terminal4rails.sh working_dir
  55.  
  56. dir=$1 # The first argument is the path of the working directory.
  57. err=0 # If err becomes negative, an error message is generated in the end.
  58.  
  59. if [ -z $dir ] || [ ! -d $dir ]; then
  60. echo "Please specify a working directory as the argument"
  61. exit
  62. fi
  63.  
  64. # If the argument is a relative path, then augment it with
  65. # the current dir.
  66. if [ ${dir:0:1} != '~' ] && [ ${dir:0:1} != '/' ]; then
  67. dir=`pwd`'/'$dir
  68. fi
  69.  
  70. echo "Working directory: $dir"
  71.  
  72. EOF
  73.  
  74. $error_message =<<-EOF
  75.  
  76. if [ $err -lt 0 ]; then
  77. echo "WARNING: Some tabs could not be opened."
  78. fi
  79. EOF
  80.  
  81. $commands="gnome-terminal --working-directory=$dir --geometry=112x30 "
  82.  
  83. execs = Array.new
  84.  
  85. ARGV[1..-1].each do |arg|
  86. case arg
  87. when "bash" then exec="bash"; title="Bash"
  88. when "vim" then exec="vim"; title="Vim"
  89. when "vimNERD" then exec='vim +NERDTree'; title="Vim"
  90. when "server" then exec="script/server && bash"
  91. title="script/server"
  92. when "autospec" then exec='autospec'; title="autospec"
  93. else
  94. if arg =~ /.+:.+/
  95. title, exec = arg.split(":")
  96. else raise "ERROR: Input is not recognised."
  97. end
  98. end
  99.  
  100. first_exec = exec.split(" ")[0]
  101. if first_exec[0] == '"' or first_exec[0] == "'"
  102. first_exec = first_exec[1..-1]
  103. end
  104. script_title = title.downcase.gsub(/\W/, "_" )
  105.  
  106. $script2 += <<-EOF
  107. #{script_title}_t="#{title}"
  108. command_path=$( type -P #{first_exec} | sed -e 's/^ *//g;s/ *$//g')
  109. executable_path=$( type -P ${dir}#{first_exec} | \\
  110. sed -e 's/^ *//g;s/ *$//g')
  111.  
  112. if [ $command_path > /dev/null ]; then
  113. echo "#{title} found in $command_path."
  114. #{script_title}_e="bash -c '#{exec}'"
  115. elif [ $executable_path > /dev/null ]; then
  116. echo "#{title} found in $executable_path."
  117. #{script_title}_e="bash -c '#{exec}'"
  118. else
  119. #{script_title}_e="echo" # Closes the tab w/o action.
  120. err=$(( $err - 1 ))
  121. echo "#{first_exec} not found."
  122. fi
  123.  
  124. EOF
  125.  
  126. execs << [exec, script_title]
  127. end
  128.  
  129. $listing = ""
  130.  
  131. execs.each do |exec, title|
  132. $commands += "--tab -t $#{title}_t -e \"$#{title}_e\" "
  133. $listing += "# * #{exec}\n"
  134. end
  135.  
  136. # Ask for permission to overwrite an existing file.
  137. if File.exists? ARGV[0] then
  138. print "Do you want to overwrite the file #{ARGV[0]} (y/n): "
  139. answer = $stdin.gets
  140. if answer.downcase[0] == "y" then
  141. puts "Script is assembled."
  142. assemble_script
  143. else
  144. puts "Script terminates without writing."
  145. exit
  146. end
  147. else
  148. puts "Script is assembled."
  149. assemble_script
  150. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.