Class: Xing::CLI::Generators::NewProject

Inherits:
Object
  • Object
show all
Includes:
Caliph::CommandLineDSL
Defined in:
lib/xing/cli/generators/new_project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ruby_versionObject

Returns the value of attribute ruby_version.



8
9
10
# File 'lib/xing/cli/generators/new_project.rb', line 8

def ruby_version
  @ruby_version
end

#shellObject



10
11
12
# File 'lib/xing/cli/generators/new_project.rb', line 10

def shell
  @shell ||= Caliph.new
end

#target_nameObject

Returns the value of attribute target_name.



7
8
9
# File 'lib/xing/cli/generators/new_project.rb', line 7

def target_name
  @target_name
end

Instance Method Details

#generateObject

For the moment, this is the simplest thing that can work. Zero templating is done so the project will still have the default module names etc.



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
# File 'lib/xing/cli/generators/new_project.rb', line 17

def generate
  command = cmd('cp', '-a', File.expand_path('../../../../../default_configuration/base_app/', __FILE__), target_name)
  result = shell.run(command)

  unless result.succeeded?
    raise "Attempt to copy base application to #{target_name} failed!"
  end

  write_ruby_version
  write_ruby_version "frontend"
  write_ruby_version "backend"

  with_temporary_database_yml do
    Bundler.with_clean_env do
      shell.run(cmd("cd", target_name) &
                cmd("bundle", "install")).must_succeed!

      shell.run(cmd("cd", File.join(target_name, "frontend")) &
                cmd("bundle", "install") &
                cmd("npm", "install")).must_succeed!

      shell.run(cmd("cd", File.join(target_name, "backend")) &
                cmd("bundle", "install") &
                cmd("rake", "xing:install:migrations")).must_succeed!
    end
  end
end

#with_temporary_database_ymlObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xing/cli/generators/new_project.rb', line 45

def with_temporary_database_yml
  dbyml_path = File.join(target_name, "backend", "config", "database.yml")
  if File.exist?(dbyml_path)
    yield
  else
    begin
      File.open(dbyml_path, "w"){}
      yield
    ensure
      File.unlink(dbyml_path)
    end
  end
end

#write_ruby_version(*subdir) ⇒ Object



59
60
61
62
63
# File 'lib/xing/cli/generators/new_project.rb', line 59

def write_ruby_version(*subdir)
  File.open(File.join(*([target_name] + subdir + [".ruby-version"])), "w") do |rv|
    rv.write(ruby_version)
  end
end