Guest User

Untitled

a guest
Feb 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.53 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/scm/scm'
  2. require File.dirname(__FILE__) + '/scm/git'
  3. require File.dirname(__FILE__) + '/scm/svn'
  4.  
  5. require 'open-uri'
  6. require 'fileutils'
  7.  
  8. module Rails
  9. class TemplateRunner
  10. attr_reader :behavior, :description, :root
  11.  
  12. def initialize(root, template) # :nodoc:
  13. @root = Dir.pwd + "/" + root
  14.  
  15. puts "applying template: #{template}"
  16.  
  17. load_template(template)
  18.  
  19. puts "#{template} applied."
  20. end
  21.  
  22. def load_template(template)
  23. begin
  24. code = open(template).read
  25. in_root { self.instance_eval(code) }
  26. rescue LoadError
  27. raise "The template [#{template}] could not be loaded."
  28. end
  29. end
  30.  
  31. # Create a new file in the Rails project folder. Specify the
  32. # relative path from RAILS_ROOT. Data is the return value of a block
  33. # or a data string.
  34. #
  35. # ==== Examples
  36. #
  37. # file("lib/fun_party.rb") do
  38. # hostname = ask("What is the virtual hostname I should use?")
  39. # "vhost.name = #{hostname}"
  40. # end
  41. #
  42. # file("config/apach.conf", "your apache config")
  43. #
  44. def file(filename, data = nil, &block)
  45. puts "creating file #{filename}"
  46. dir, file = [File.dirname(filename), File.basename(filename)]
  47.  
  48. inside(dir) do
  49. File.open(file, "w") do |f|
  50. if block_given?
  51. f.write(block.call)
  52. else
  53. f.write(data)
  54. end
  55. end
  56. end
  57. end
  58.  
  59. # Install a plugin. You must provide either a Subversion url or Git url.
  60. #
  61. # ==== Examples
  62. #
  63. # plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git'
  64. # plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
  65. #
  66. def plugin(name, options)
  67. puts "installing plugin #{name}"
  68.  
  69. if options[:git] || options[:svn]
  70. in_root do
  71. `script/plugin install #{options[:svn] || options[:git]}`
  72. end
  73. else
  74. puts "! no git or svn provided for #{name}. skipping..."
  75. end
  76. end
  77.  
  78. # Install a gem into vendor/gems. You can install a gem in one of three ways:
  79. #
  80. # 1. Provide a git repository URL...
  81. #
  82. # gem 'will-paginate', :git => 'git://github.com/mislav/will_paginate.git'
  83. #
  84. # 2. Provide a subversion repository URL...
  85. #
  86. # gem 'will-paginate', :svn => 'svn://svnhub.com/mislav/will_paginate/trunk'
  87. #
  88. # 3. Provide a gem name and use your system sources to install and unpack it.
  89. #
  90. # gem 'ruby-openid'
  91. #
  92. def gem(name, options = {})
  93. puts "vendoring gem #{name}"
  94. if options[:git]
  95. inside("vendor/gems") { Git.clone(options[:git], options[:branch]) }
  96. elsif options[:svn]
  97. inside("vendor/gems") { Svn.checkout(options[:svn], options[:branch]) }
  98. else
  99. # TODO: Gem dependencies. Split output on \n, iterate lines looking for Downloaded...
  100. inside("vendor/gems") do
  101. # Filename may be different than gem name
  102. filename = (`gem fetch #{name}`).chomp.gsub(/Downloaded /, '')
  103. `gem unpack #{filename}.gem`
  104. File.delete("#{filename}.gem")
  105. end
  106. end
  107. end
  108.  
  109. # Run a command in git.
  110. #
  111. # ==== Examples
  112. #
  113. # git :init
  114. # git :add => "this.file that.rb"
  115. # git :add => "onefile.rb", :rm => "badfile.cxx"
  116. #
  117. def git(command = {})
  118. puts "running git #{command}"
  119.  
  120. in_root do
  121. if command.is_a?(Symbol)
  122. Git.run(command.to_s)
  123. else
  124. command.each do |command, options|
  125. Git.run("#{command} #{options}")
  126. end
  127. end
  128. end
  129. end
  130.  
  131. # Create a new file in the vendor/ directory. Code can be specified
  132. # in a block or a data string can be given.
  133. #
  134. # ==== Examples
  135. #
  136. # vendor("sekrit.rb") do
  137. # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--"
  138. # "salt = '#{sekrit_salt}'"
  139. # end
  140. #
  141. # vendor("foreign.rb", "# Foreign code is fun")
  142. #
  143. def vendor(filename, data = nil, &block)
  144. puts "vendoring file #{filename}"
  145. inside("vendor") do |folder|
  146. File.open("#{folder}/#{filename}", "w") do |f|
  147. if block_given?
  148. f.write(block.call)
  149. else
  150. f.write(data)
  151. end
  152. end
  153. end
  154. end
  155.  
  156. # Create a new file in the lib/ directory. Code can be specified
  157. # in a block or a data string can be given.
  158. #
  159. # ==== Examples
  160. #
  161. # lib("crypto.rb") do
  162. # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'"
  163. # end
  164. #
  165. # lib("foreign.rb", "# Foreign code is fun")
  166. #
  167. def lib(filename, data = nil)
  168. puts "add lib file #{filename}"
  169. inside("lib") do |folder|
  170. File.open("#{folder}/#{filename}", "w") do |f|
  171. if block_given?
  172. f.write(block.call)
  173. else
  174. f.write(data)
  175. end
  176. end
  177. end
  178. end
  179.  
  180. # Create a new Rake task in the lib/tasks/application.rake file.
  181. # Code can be specified in a block or a data string can be given.
  182. #
  183. # ==== Examples
  184. #
  185. # task(:whatever) do
  186. # name = ask("What should the task be named?")
  187. #
  188. # "
  189. # task :#{name} do
  190. # puts 'hi from rake'
  191. # end
  192. # "
  193. # end
  194. #
  195. # task(:go_away, "puts 'be gone!'")
  196. #
  197. def task(name, description = nil, &block)
  198. puts "adding task :#{name}"
  199. inside("lib/tasks") do |folder|
  200. File.open("#{folder}/application.rake", "a+") do |f|
  201. if block_given?
  202. f.write(block.call)
  203. else
  204. f.write(data)
  205. end
  206. end
  207. end
  208. end
  209.  
  210. # Create a new Rakefile with the provided code (either in a block or a string).
  211. #
  212. # ==== Examples
  213. #
  214. # rakefile("bootstrap.rake") do
  215. # project = ask("What is the UNIX name of your project?")
  216. #
  217. # "namespace :#{project} do
  218. # task :bootstrap do
  219. # puts "i like boots!"
  220. # end
  221. # end"
  222. # end
  223. #
  224. # rakefile("seed.rake", "puts 'im plantin ur seedz'")
  225. #
  226. def rakefile(filename, data = nil, &block)
  227. puts "adding rakefile #{filename}"
  228. inside("lib/tasks") do |folder|
  229. File.open("#{folder}/#{filename}", "w") do |f|
  230. if block_given?
  231. f.write(block.call)
  232. else
  233. f.write(data)
  234. end
  235. end
  236. end
  237. end
  238.  
  239. # Create a new initializer with the provided code (either in a block or a string).
  240. #
  241. # ==== Examples
  242. #
  243. # initializer("globals.rb") do
  244. # data = ""
  245. #
  246. # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do
  247. # data << "#{const} = :entp"
  248. # end
  249. #
  250. # data
  251. # end
  252. #
  253. # initializer("api.rb", "API_KEY = '123456'")
  254. #
  255. def initializer(filename, data = nil, &block)
  256. puts "adding initializer #{filename}"
  257. inside("config/initializers") do |folder|
  258. File.open("#{folder}/#{filename}", "w") do |f|
  259. if block_given?
  260. f.write(block.call)
  261. else
  262. f.write(data)
  263. end
  264. end
  265. end
  266. end
  267.  
  268. # Generate something using a generator from Rails or a plugin.
  269. # The second parameter is the argument string that is passed to
  270. # the generator or an Array that is joined.
  271. #
  272. # ==== Example
  273. #
  274. # generate(:authenticated, "user session")
  275. #
  276. def generate(what, args = nil)
  277. puts "generating #{what}"
  278. args = args.join(" ") if args.class == Array
  279.  
  280. in_root { `#{root}/script/generate #{what} #{args}` }
  281. end
  282.  
  283. # Executes a command
  284. #
  285. # ==== Example
  286. # inside('vendor') do
  287. # run('ln -s ~/edge rails)
  288. # end
  289. def run(command)
  290. puts "executing #{command} from #{Dir.pwd}"
  291. `#{command}`
  292. end
  293.  
  294. # Just run the capify command in root
  295. #
  296. # ==== Example
  297. #
  298. # capify!
  299. #
  300. def capify!
  301. in_root { `capify .` }
  302. end
  303.  
  304. # Add Rails to /vendor/rails
  305. #
  306. # ==== Example
  307. #
  308. # freeze!
  309. #
  310. def freeze!(args = {})
  311. puts "vendoring rails edge"
  312. in_root { `rake rails:freeze:edge` }
  313. end
  314.  
  315. # Get a user's input
  316. #
  317. # ==== Example
  318. #
  319. # answer = ask("Should I freeze the latest Rails?")
  320. # freeze! if ask("Should I freeze the latest Rails?") == "yes"
  321. #
  322. def ask(string)
  323. puts string
  324. gets.strip
  325. end
  326.  
  327. # Make an entry in Rails routing file conifg/routes.rb
  328. #
  329. # === Example
  330. #
  331. # route "map.root :controller => :welcome"
  332. #
  333. def route(routing_code)
  334. sentinel = 'ActionController::Routing::Routes.draw do |map|'
  335.  
  336. in_root do
  337. gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
  338. "#{match}\n #{routing_code}\n"
  339. end
  340. end
  341. end
  342.  
  343. protected
  344.  
  345. # Do something in the root of the Rails application or
  346. # a provided subfolder; the full path is yielded to the block you provide.
  347. # The path is set back to the previous path when the method exits.
  348. def inside(dir = '', &block)
  349. folder = File.join(root, dir)
  350. FileUtils.mkdir_p(folder) unless File.exist?(folder)
  351. FileUtils.cd(folder) { block.arity == 1 ? yield(folder) : yield }
  352. end
  353.  
  354. def in_root
  355. FileUtils.cd(root) { yield }
  356. end
  357.  
  358. # Helper to test if the user says yes(y)?
  359. #
  360. # ==== Example
  361. #
  362. # freeze! if yes?("Should I freeze the latest Rails?")
  363. #
  364. def yes?(question)
  365. answer = ask(question).downcase
  366. answer == "y" || answer == "yes"
  367. end
  368.  
  369. # Helper to test if the user does NOT say yes(y)?
  370. #
  371. # ==== Example
  372. #
  373. # capify! if no?("Will you be using vlad to deploy your application?")
  374. #
  375. def no?(question)
  376. !yes?(question)
  377. end
  378.  
  379. def gsub_file(relative_destination, regexp, *args, &block)
  380. path = destination_path(relative_destination)
  381. content = File.read(path).gsub(regexp, *args, &block)
  382. File.open(path, 'wb') { |file| file.write(content) }
  383. end
  384.  
  385. def destination_path(relative_destination)
  386. File.join(root, relative_destination)
  387. end
  388. end
  389. end
Add Comment
Please, Sign In to add comment