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

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

Defined Under Namespace

Classes: UserInput

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

#code_of_conduct_templaterObject



111
112
113
114
115
116
117
118
# File 'lib/xing/cli/generators/new_project.rb', line 111

def code_of_conduct_templater
  @code_of_conduct_templater ||= Xing::CLI::Templaters::CodeOfConductTemplater.new(
    target_name,
    context.merge({
      email: user_input.coc_contact_email
    }),
    !user_input.code_of_conduct)
end

#contextObject



120
121
122
# File 'lib/xing/cli/generators/new_project.rb', line 120

def context
  { app_name: target_name }
end

#control_files_templaterObject



83
84
85
86
# File 'lib/xing/cli/generators/new_project.rb', line 83

def control_files_templater
  @control_files_templater ||= Xing::CLI::Templaters::ControlFilesTemplater.new(
    target_name, context)
end

#database_yml_templaterObject



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

def database_yml_templater
  @database_yml_templater ||= begin
    dbyml_path = File.join(target_name, "backend", "config", "database.yml")
    Xing::CLI::Templaters::DatabaseYmlTemplater.new(target_name, context, File.exist?(dbyml_path))
  end
end

#doc_files_templaterObject



88
89
90
91
92
93
94
95
# File 'lib/xing/cli/generators/new_project.rb', line 88

def doc_files_templater
  @doc_files_templater ||= Xing::CLI::Templaters::DocFilesTemplater.new(
    target_name,
    context.merge({
      code_of_conduct_reference: (user_input.code_of_conduct ?
        "All contributors must abide by the [Code of Conduct](CODE_OF_CONDUCT.md)" : "")
    }))
end

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

def generate

  user_input.gather

  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

  database_yml_templater.template
  secrets_yml_templater.template
  control_files_templater.template
  doc_files_templater.template
  code_of_conduct_templater.template

  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

#secrets_yml_templaterObject



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

def secrets_yml_templater
  @secrets_yml_templater ||= begin
    secyml_path = File.join(target_name, "backend", "config", "secrets.yml")
    Xing::CLI::Templaters::SecretsYmlTemplater.new(
      target_name,
      context.merge({
        dev_secret_key_base: SecureRandom.hex(64),
        test_secret_key_base: SecureRandom.hex(64),
      }),
      File.exist?(secyml_path)
    )
  end
end

#setup_env_commandObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/xing/cli/generators/new_project.rb', line 142

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

#user_inputObject



153
154
155
# File 'lib/xing/cli/generators/new_project.rb', line 153

def user_input
  @user_input ||= UserInput.new
end

#write_file_to(name, subdir) ⇒ Object



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

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

#write_ruby_gemset(*subdir) ⇒ Object



136
137
138
139
140
# File 'lib/xing/cli/generators/new_project.rb', line 136

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

#write_ruby_version(*subdir) ⇒ Object



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

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