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

Inherits:
Object
  • Object
show all
Includes:
Architecture, 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.



12
13
14
# File 'lib/xing/cli/generators/new_project.rb', line 12

def ruby_version
  @ruby_version
end

#shellObject



15
16
17
# File 'lib/xing/cli/generators/new_project.rb', line 15

def shell
  @shell ||= Caliph.new
end

#target_nameObject

Returns the value of attribute target_name.



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

def target_name
  @target_name
end

#with_gemsetObject

Returns the value of attribute with_gemset.



13
14
15
# File 'lib/xing/cli/generators/new_project.rb', line 13

def with_gemset
  @with_gemset
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.



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

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"

  if with_gemset
    write_ruby_gemset
    write_ruby_gemset "frontend"
    write_ruby_gemset "backend"
  end

  write_database_yml
  write_secrets_yml

  write_git_control_files

  Bundler.with_clean_env do
    if with_gemset
      bundler = shell.run(setup_env_command &
        cmd("cd", target_name) &
        cmd("gem", "install", "bundler"))
    end

    shell.run(
      setup_env_command &
              cmd("cd", target_name) &
              cmd("bundle", "install")).must_succeed!

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

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

end

#setup_env_commandObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/xing/cli/generators/new_project.rb', line 134

def setup_env_command
  if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
    args = [".", File.expand_path('../../../../../bin/xing-rvm-setup-env', __FILE__), ruby_version]
    args[3] = target_name if with_gemset
    cmd(*args)
  else
    # put other rb environemnt scripts here
    cmd(":")
  end
end

#with_templatesObject



110
111
112
113
114
# File 'lib/xing/cli/generators/new_project.rb', line 110

def with_templates
  architecture source: File.expand_path('../../../../../default_configuration/templates/', __FILE__) , destination: target_name  do |arc|
    yield(arc)
  end
end

#write_database_ymlObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/xing/cli/generators/new_project.rb', line 72

def write_database_yml
  dbyml_path = File.join(target_name, "backend", "config", "database.yml")
  if !File.exist?(dbyml_path)
    with_templates do |arc|
      arc.copy file: "backend/config/database.yml", context: { app_name: target_name }
      arc.copy file: "backend/config/database.yml.example", context: { app_name: target_name }
      arc.copy file: "backend/config/database.yml.ci", context: { app_name: target_name }
    end
  end
end

#write_file_to(name, subdir) ⇒ Object



116
117
118
119
120
# File 'lib/xing/cli/generators/new_project.rb', line 116

def write_file_to(name, subdir)
  File.open(File.join(*([target_name] + subdir + [name])), "w") do |rv|
    yield(rv)
  end
end

#write_git_control_filesObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/xing/cli/generators/new_project.rb', line 83

def write_git_control_files
  with_templates do |arc|
    arc.copy file: "gitignore", as: ".gitignore"
    arc.copy file: "backend/gitignore", as:"backend/.gitignore"
    arc.copy file: "frontend/gitignore", as: "frontend/.gitignore"
    arc.copy file: "gitattributes", as: ".gitattributes"
    arc.copy file: "backend/gitattributes", as: "backend/.gitattributes"
    arc.copy file: "frontend/gitattributes", as: "frontend/.gitattributes"
  end
end

#write_ruby_gemset(*subdir) ⇒ Object



128
129
130
131
132
# File 'lib/xing/cli/generators/new_project.rb', line 128

def write_ruby_gemset(*subdir)
  write_file_to(".ruby-gemset", subdir) do |rv|
    rv.write(target_name)
  end
end

#write_ruby_version(*subdir) ⇒ Object



122
123
124
125
126
# File 'lib/xing/cli/generators/new_project.rb', line 122

def write_ruby_version(*subdir)
  write_file_to(".ruby-version", subdir) do |rv|
    rv.write(ruby_version)
  end
end

#write_secrets_ymlObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/xing/cli/generators/new_project.rb', line 94

def write_secrets_yml
  secyml_path = File.join(target_name, "backend", "config", "secrets.yml")
  if !File.exist?(secyml_path)
    context = {
      dev_secret_key_base: SecureRandom.hex(64),
      test_secret_key_base: SecureRandom.hex(64),
      app_name: target_name
    }
    with_templates do |arc|
      arc.copy file: "backend/config/secrets.yml", context: context
      arc.copy file: "backend/config/secrets.yml.example", context: context
      arc.copy file: "backend/config/secrets.yml.ci", context: context
    end
  end
end