Class: Xnlogic::CLI::Application

Inherits:
Core
  • Object
show all
Defined in:
lib/xnlogic/cli/application.rb

Instance Attribute Summary collapse

Attributes inherited from Core

#app, #ignore_options, #options, #thor

Instance Method Summary collapse

Methods inherited from Core

#write_options

Constructor Details

#initialize(options, thor) ⇒ Application

Returns a new instance of Application.



7
8
9
10
# File 'lib/xnlogic/cli/application.rb', line 7

def initialize(options, thor)
  super
  @ignore_options = ['up', 'provision']
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



5
6
7
# File 'lib/xnlogic/cli/application.rb', line 5

def app_name
  @app_name
end

#base_nameObject (readonly)

Returns the value of attribute base_name.



5
6
7
# File 'lib/xnlogic/cli/application.rb', line 5

def base_name
  @base_name
end

#existingObject (readonly)

Returns the value of attribute existing.



5
6
7
# File 'lib/xnlogic/cli/application.rb', line 5

def existing
  @existing
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/xnlogic/cli/application.rb', line 5

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/xnlogic/cli/application.rb', line 5

def root
  @root
end

Instance Method Details

#_generate_templates(template_src_to_dst, template_folder, opts, ui_message = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/xnlogic/cli/application.rb', line 191

def _generate_templates(template_src_to_dst, template_folder, opts, ui_message=nil)
  if(not ui_message.nil?)
    Xnlogic.ui.info ""
    Xnlogic.ui.info ui_message
    Xnlogic.ui.info ""
  end

  template_src_to_dst.each do |src, dst|
    thor.template("#{template_folder}/#{src}", app.join(dst), opts)
  end
end

#applicationObject



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
# File 'lib/xnlogic/cli/application.rb', line 59

def application
  require_key
  generate_vm_config if options['vm_config']
  generate_application
  options['application_version'] = Xnlogic::VERSION
  write_options
  Xnlogic.ui.info "Initializing git repo in #{app}"
  Dir.chdir(app) { system 'git init' and system 'git add .' } unless existing

  install_vagrant_note
  if options['up']
    Dir.chdir(app) { system 'vagrant up' }
  else
    Xnlogic.ui.info ""
    Xnlogic.ui.info "To start your VM, run the following:"
    Xnlogic.ui.info ""
    Xnlogic.ui.info "cd #{root}"
    Xnlogic.ui.info "xnlogic up"
  end

  if options['provision']
    Dir.chdir(app) { system 'vagrant provision' }
  end

  Xnlogic.ui.info "To begin working with your VM, ssh into it with the following command:"
  Xnlogic.ui.info ""
  Xnlogic.ui.info "cd #{root}"
  Xnlogic.ui.info "xnlogic ssh"
  Xnlogic.ui.info ""
  Xnlogic.ui.info "Once logged in to the server, try the xn-console and xn-server commands"
end

#check_argumentsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/xnlogic/cli/application.rb', line 144

def check_arguments
  if datomic_pro?
    if options['datomic_license']
      unless datomic_license
        Xnlogic.ui.warn "[WARN] Datomic license file '#{ options['datomic_license'] }' not found."
      end
    else
      Xnlogic.ui.warn "[WARN] No datomic-license specified. Pro transacter will not start."
    end
    unless options['datomic_username'] and options['datomic_key']
      Xnlogic.ui.warn "[WARN] No datomic-username or datomic-key. Will not be able to fetch datomic pro jar dependencies."
    end
  end
end

#datomic_licenseObject



159
160
161
162
163
# File 'lib/xnlogic/cli/application.rb', line 159

def datomic_license
  if options['datomic_license'] and File.exist? options['datomic_license']
    File.read(options['datomic_license']).sub(/^license-key=/, '')
  end
end

#datomic_pro?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/xnlogic/cli/application.rb', line 140

def datomic_pro?
  options.fetch('datomic_pro', options['datomic_mysql'])
end

#generate_applicationObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/xnlogic/cli/application.rb', line 244

def generate_application
  opts = template_options
  namespaced_path = opts[:namespaced_path]
  templates = {
    "gitignore.tt" => ".gitignore",
    ".rspec.tt" => ".rspec",
    "gemspec.tt" => "#{namespaced_path}.gemspec",
    "Rakefile.tt" => "Rakefile",
    "Readme.md.tt" => "Readme.md",
    "logback.xml.tt" => "logback.xml",
    "config.ru.tt" => "config.ru",
    "dev/console.rb.tt" => "dev/console.rb",
    "config/mcfly_config.yml.tt" => "config/mcfly_config.yml",
    "tasks/deploy.rb.tt" => "tasks/deploy.rb",
    "torquebox.yml.tt" => "torquebox.yml",
    "torquebox_init.rb.tt" => "torquebox_init.rb",
    "lib/gemname.rb.tt" => "lib/#{namespaced_path}.rb",
    "lib/gemname/version.rb.tt" => "lib/#{namespaced_path}/version.rb",
    "lib/gemname/initializers/inflections.rb.tt" => "lib/#{namespaced_path}/initializers/inflections.rb",
    "lib/gemname/parts/has_notes.rb.tt" => "lib/#{namespaced_path}/parts/has_notes.rb",
    "lib/gemname/parts/note.rb.tt" => "lib/#{namespaced_path}/parts/note.rb",
    "lib/gemname/type/url.rb.tt" => "lib/#{namespaced_path}/type/url.rb",
    "lib/gemname/fixtures.rb.tt" => "lib/#{namespaced_path}/fixtures.rb",
    "lib/gemname/models.rb.tt" => "lib/#{namespaced_path}/models.rb",
    "lib/gemname/permissions.rb.tt" => "lib/#{namespaced_path}/permissions.rb",
    "lib/gemname/type.rb.tt" => "lib/#{namespaced_path}/type.rb",
    "lib/fixtures/sample_fixtures.rb.tt" => "lib/fixtures/sample_fixtures.rb",
    "assets/index.html.tt" => "assets/index.html",
    "assets/images/xnlogic-logo-site.png" => "assets/images/xnlogic-logo-site.png",
    "assets/images/xnlogic-logo-site-x2.png" => "assets/images/xnlogic-logo-site-x2.png",

    "spec/spec_helper.rb.tt" => "spec/spec_helper.rb",
    "spec/gemname/gemname_spec.rb.tt" => "spec/#{namespaced_path}/#{name}_spec.rb",
  }

  _generate_templates(templates, 'application', opts, 'Creating application templates')
end

#generate_vm_configObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/xnlogic/cli/application.rb', line 204

def generate_vm_config
  transactor_properties = if options['datomic_mysql']
                            "datomic/mysql.properties.tt"
                          elsif options['datomic_pro']
                            "datomic/pro.properties.tt"
                          else
                            "datomic/free.properties.tt"
                          end
  datomic_installer = if datomic_pro?
                        'datomic/install_pro_transactor.sh.tt'
                      else
                        'datomic/install_free_transactor.sh.tt'
                      end
  base_templates = {
    "Vagrantfile.tt" => "Vagrantfile",
    "Gemfile.tt" => "Gemfile",
    "config/vagrant.provision.tt" => "config/vagrant.provision",
    "config/vagrant.up.tt" => "config/vagrant.up",
    "config/datomic.conf" => "config/datomic.conf",
    "config/start.sh" => "config/start.sh",
    transactor_properties => "config/transactor.properties",
    "config/xnlogic.conf.tt" => "config/xnlogic.conf",
    datomic_installer => 'script/install_transactor.sh',
  }

  if datomic_pro?
    base_templates['datomic/m2_settings.xml.tt'] = 'config/m2_settings.xml'
    base_templates['datomic/pom.xml.tt'] = 'config/pom.xml'
    base_templates['datomic/get_datomic_jar.sh.tt'] = 'script/get_datomic_jar.sh'
    if options['datomic_mysql']
      base_templates['datomic/mysql/db.sql'] = 'config/mysql/db.sql'
      base_templates['datomic/mysql/table.sql'] = 'config/mysql/table.sql'
      base_templates['datomic/mysql/user.sql'] = 'config/mysql/user.sql'
    end
  end

  _generate_templates(base_templates, 'vagrant', template_options, 'Creating Vagrant configuration')
end

#in_existing_projectObject



32
33
34
35
36
37
38
39
# File 'lib/xnlogic/cli/application.rb', line 32

def in_existing_project
  @existing = true
  @root = options.fetch('root', '.')
  @app = Pathname.pwd.join(root)
  super()
  @name ||= options['name']
  self
end

#install_vagrant_noteObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/xnlogic/cli/application.rb', line 109

def install_vagrant_note
  ver = `vagrant --version` rescue ''
  if ver == ''
    Xnlogic.ui.info ""
    Xnlogic.ui.info "Please ensure that the following dependencies are installed on your computer"
    Xnlogic.ui.info "to continue."
    Xnlogic.ui.info " - Vagrant:    https://www.vagrantup.com/"
    Xnlogic.ui.info " - VirtualBox: https://www.virtualbox.org/wiki/Downloads"
  end
end

#options_fileObject



28
29
30
# File 'lib/xnlogic/cli/application.rb', line 28

def options_file
  app + options_filename
end

#options_filenameObject



24
25
26
# File 'lib/xnlogic/cli/application.rb', line 24

def options_filename
  'config/app_options.yaml'
end

#require_keyObject



52
53
54
55
56
57
# File 'lib/xnlogic/cli/application.rb', line 52

def require_key
  unless options['key']
    Xnlogic.ui.info 'You must specify a --key option to generate an application.'
    exit 1
  end
end

#set_name(app_name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/xnlogic/cli/application.rb', line 12

def set_name(app_name)
  if File.exists?(options_filename)
    Xnlogic.ui.info "Can not create an application within an application."
    exit 1
  end
  @name = app_name.chomp("/").tr('-', '_') # remove trailing slash if present
  @root = options.fetch('root', @name)
  @app = Pathname.pwd.join(root)
  options['name'] = @name
  self
end

#show_source(source) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/xnlogic/cli/application.rb', line 41

def show_source(source)
  case options['format']
  when 'bundler'
    Xnlogic.ui.info "source \"#{source}\""
  when 'rubygems'
    Xnlogic.ui.info "gem sources -a #{source}"
  else
    Xnlogic.ui.info source
  end
end

#template_optionsObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/xnlogic/cli/application.rb', line 165

def template_options
  namespaced_path = name
  constant_name = namespaced_path.split('_').map{|p| p[0..0].upcase + p[1..-1] }.join
  git_user_name = `git config user.name`.chomp
  git_user_email = `git config user.email`.chomp
  check_arguments
  {
    :name                     => name,
    :namespaced_path          => namespaced_path,
    :constant_name            => constant_name,
    :author                   => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
    :email                    => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
    :vm_cpus                  => options['cpus'],
    :vm_memory                => options['memory'],
    :xn_key                   => options['key'],
    :datomic_pro              => datomic_pro?,
    :datomic_license          => datomic_license,
    :datomic_mysql            => options['datomic_mysql'],
    :datomic_optional_version => options['datomic_version'],
    :datomic_version          => (options['datomic_version'] || "0.9.5130"),
    :datomic_username         => options.fetch('datomic_username', '[username from my.datomic.com]'),
    :datomic_key              => options.fetch('datomic_key', '[key from my.datomic.com]'),
  }
end

#update_key(xn_key) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xnlogic/cli/application.rb', line 121

def update_key(xn_key)
  # Set the option
  options['key'] = xn_key

  # Check that we are in an existing project folder
  in_existing_project

  # Gemerate the necessary templates
  templates = {
    "Gemfile.tt" => "Gemfile",
    "config/vagrant.provision.tt" => "config/vagrant.provision",
    "config/xnlogic.conf.tt" => "config/xnlogic.conf",
  }
  _generate_templates(templates, 'vagrant', template_options, 'Updating configuration')

  # Write the options YAML file
  write_options
end

#vm_configObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xnlogic/cli/application.rb', line 91

def vm_config
  generate_vm_config
  options['vm_config_version'] = Xnlogic::VERSION
  write_options
  install_vagrant_note

  if options['provision'] or options['up']
    Dir.chdir(app) { system 'vagrant up' } if options['up']
    Dir.chdir(app) { system 'vagrant provision' } if options['provision']
  else
    Xnlogic.ui.info ""
    Xnlogic.ui.info "If you have an existing VM that you are updating, run 'xnlogic provision',"
    Xnlogic.ui.info "otherwise, run 'xnlogic up'"
    Xnlogic.ui.info ""
  end
  Xnlogic.ui.info "Once that is done, run 'xnlogic ssh' to log in to the VM."
end