Class: Nyara::Command

Inherits:
Thor show all
Includes:
FileNames, ORM, Thor::Actions
Defined in:
lib/nyara/command.rb

Defined Under Namespace

Modules: FileNames, ORM

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ORM

#orm

Methods included from FileNames

#gitignore, #template_ext

Class Method Details

.source_rootObject


29
30
31
# File 'lib/nyara/command.rb', line 29

def self.source_root
  __dir__
end

Instance Method Details

#consoleObject


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/nyara/command.rb', line 130

def console
  env = options[:environment].shellescape
  cmd = options[:shell]
  unless cmd
    if File.read('Gemfile') =~ /\bpry\b/
      cmd = 'pry'
    end
  end

  cmd ||= 'irb'
  if cmd != 'irb'
    cmd = "bundle exec pry"
  end
  exec "NYARA_SHELL=1 NYARA_ENV=#{env} #{cmd} -r./config/application.rb"
end

#generate(thing, migration = nil) ⇒ Object


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
100
101
102
103
104
105
106
# File 'lib/nyara/command.rb', line 69

def generate thing, migration=nil
  case thing
  when 'session.key'
    file = "config/session.key"
    file = File.join @rel_dir, file if @rel_dir
    create_file file do
      Session.generate_key
    end
  when 'session_cipher.key'
    file = 'config/session_cipher.key'
    file = File.join @rel_dir, file if @rel_dir
    create_file file do
      Session.generate_cipher_key
    end
  when 'database.yml'
    @app_name ||= File.dirname Dir.pwd
    file = 'config/database.yml'
    file = File.join @rel_dir, file if @rel_dir
    case orm
    when 'mongoid', 'activerecord'
      src = "#{__dir__}/optional_templates/#{orm}.yml.erb"
      create_file file do
        ERB.new(File.read src).result binding
      end
    end
  when 'migration'
    if migration.nil?
      abort 'Use: nyara g migration YOUR_MIGRATION_NAME'
    end
    version = options[:version] || Time.now.utc.strftime("%Y%m%d%H%M%S")
    create_file "db/migrate/#{version}_#{migration}.rb" do
      @name = migration.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }
      src = "#{__dir__}/optional_templates/migration.rb.erb"
      ERB.new(File.read src).result binding
    end
    @name = nil
  end
end

#new(name) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nyara/command.rb', line 43

def new name
  require 'fileutils'

  app_dir = File.expand_path File.join(Dir.pwd, name)
  @rel_dir = name
  @app_name = File.basename app_dir
  templte_dir = File.join(File.dirname(__FILE__), "templates")

  directory 'templates', name
  generate 'session.key'
  generate 'database.yml'
  puts '          \\ 👻  /'
ensure
  @app_name = nil
  @rel_dir = nil
end

#serverObject


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nyara/command.rb', line 113

def server
  env = options[:environment].shellescape
  cmd = "NYARA_ENV=#{env} ruby config/boot.rb"

  if options[:port]
    cmd << " -p " << options[:port].to_s.shellescape
  end
  if options[:daemon]
    cmd << " -d"
  end
  exec cmd
end

#versionObject


34
35
36
# File 'lib/nyara/command.rb', line 34

def version
  puts "Nyara #{Nyara::VERSION}"
end