Class: Tane::Commands::Create

Inherits:
Base
  • Object
show all
Defined in:
lib/tane/commands/create.rb

Class Method Summary collapse

Methods inherited from Base

fire

Methods included from Helpers

included

Class Method Details

.help_textObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tane/commands/create.rb', line 101

def help_text
  <<-EOL
Usage:

tane create 'app_name'

Creates a new Cloudfuji-enabled rails app from scratch and launches it. Use this to get started with Cloudfuji quickly and easily

tane create my_example_app
EOL
end

.process(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tane/commands/create.rb', line 5

def process(args)
  verbose_say("Authenticating you...")
  authenticate_user
  verbose_say("done!")
  
  app_name = args[0] ||= term.ask("Please enter a name for your new app:     ") { |app_name| app_name }
  target_path = term.ask("Create app in path (defaults to [#{Dir.pwd}/#{app_name}]: ")
  target_path = "#{Dir.pwd}/#{app_name}" if target_path.to_s == ""

  template_url = ENV['KIMONO_URL'] || "https://raw.github.com/Cloudfuji/kimono/master/kimono.rb"

  print "Creating a new Cloudfuji rails app in #{ target_path } (please wait, it'll take a few minutes) ...    "

  FileUtils.mkdir_p target_path

  start_throbber!

  File.open("tane.log", "w") do |file|
    IO.popen("rails _#{Tane::RAILS_VERSION}_ new #{ target_path } --quiet --template=#{ template_url } 2>&1 /dev/null", :error => [:child, :out]) do |io|
      file.puts(io.read)
    end
  end
  verbose_say("Finished creating rails app, initializing Cloudfuji resources...")

  Dir.chdir("#{ target_path }")do
    File.open("tane.log", "w") do |file|
      verbose = "--verbose" if opts.verbose
      verbose ||= ""
      IO.popen("bundle exec tane init #{verbose}", :error => [:child, :out]) do |io|
        file.puts(io.read)
      end
    end
  end

  verbose_say("done!")

  stop_throbber!

  success_messages = ["Let the hacking commence!",
                      "Hacks and glory await!",
                      "Hack and be merry!",
                      "Your hacking starts... NOW!",
                      "May the source be with you!",
                      "Take this demi-REPL, dear friend, and may it serve you well.",
                      "Lemonodor-fame is but a hack away!",
                      "Go forth to #{ app_name }, and hack for all you're worth - for all humankind!"]

  bushirens = ["Sean", "Kevin", "Akash"]

  success_message = success_messages[ rand(success_messages.length) ]
  bushiren        = bushirens       [ rand(bushirens.length)         ]

  FileUtils.mv("./tane.log", "#{ target_path }/log/tane.log")
  term.say "  Finished successfully!"
  term.say "Your app is now in #{ target_path }"
  
  
  Dir.chdir("#{ target_path }") do
    begin
      term.say "Migrating your new app to setup the basic models..."
      suppress_env_vars("BUNDLE_BIN_PATH", "BUNDLE_GEMFILE", "RUBYOPT") do
        verbose_say("migrating database!")
        system("bundle exec tane exec rake db:migrate")
      end

      File.open(".gitignore", "a") { |gitignore| gitignore.puts(".cloudfuji/tane.yml" ) }

      term.say "Initializing git repo..."
      suppress_env_vars("BUNDLE_BIN_PATH", "BUNDLE_GEMFILE", "RUBYOPT") do
        verbose_say("initializing git repo")
        system("git init")
        system("git add .")
      end

      term.say "Launching your new app!"
      term.say "Check out once rails has finished booting http://localhost:3000"
      term.say "#{bushiren} says, \"#{ success_message }\""

      # Do this in the background, it'll wait up to 120 seconds
      # for the rails server to start up and launch a browser as
      # soon as it's ready
      verbose_say("Launching `tane open' in the background..")
      system("tane open&")
      verbose_say("done!")

      suppress_env_vars("BUNDLE_BIN_PATH", "BUNDLE_GEMFILE", "RUBYOPT") do
        verbose_say("launching rails app!")
        exec("bundle exec tane exec rails s")
      end
    rescue Exception => ex
      term.say "Oh no we tried to launch the app but failed. Please try launching it yourself with \n bundle exec tane exec rails s \n if you have problems let us know at [email protected]"
    end
  end

end