Top Level Namespace

Defined Under Namespace

Modules: Cxx

Instance Method Summary collapse

Instance Method Details

#choose_building_blockObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cxx/wizard/project_wizard.rb', line 41

def choose_building_block
  building_block = nil
  whole_archive = nil
  choose do |menu|
    say 'What building-block do you whant to create?'
    menu.choice(:exe) { building_block = 'exe' }
    menu.choice(:lib) do
      building_block = 'static_lib'
      whole_archive = confirm('Is this a test-library', false)
    end
    menu.prompt = 'Select a building-block: '
  end
  [building_block, whole_archive]
end

#choose_toolchainObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cxx/wizard/project_wizard.rb', line 56

def choose_toolchain
  res = nil
  toolchains = []
  toolchain_pattern = /cxxproject_(.*)toolchain/
  Gem::Specification.latest_specs.each do |gem|
    if gem.name =~ toolchain_pattern then
      toolchains << $1
    end
  end
  if toolchains.length > 0 then
    choose do |menu|
      say "What toolchain do you whant to use?"
      toolchains.each do |toolchain|
        menu.choice(toolchain.to_sym) { res = toolchain }
      end
      menu.prompt = "Select a toolchain: "
    end
  else
    say "No toolchains installed!"
    candidates = `gem list --remote "cxxproject_.*toolchain"`
    say "You need at least one toolchain-plugin,- candidates are:\n#{candidates}"
  end
  res
end

#confirm(question, default = true) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cxx/wizard/project_wizard.rb', line 112

def confirm(question, default = true)
  res = nil
  while res == nil
    confirm = ask("#{question}? ") { |q| q.default = default ? "Y/n" : "y/N" }
    if confirm.eql?("Y/n") || confirm.downcase.eql?("yes") || confirm.downcase.eql?("y") then
      res = true
    elsif confirm.eql?("y/N") || confirm.downcase.eql?("no") || confirm.downcase.eql?("n") then
      res = false
    else
      say "Please enter \"yes\" or \"no\"."
    end
  end
  return res
end

#create_binding(name, building_block, whole_archive, toolchain) ⇒ Object



101
102
103
# File 'lib/cxx/wizard/project_wizard.rb', line 101

def create_binding(name, building_block, whole_archive, toolchain)
  return binding()
end

#create_project(dir_name, building_block, whole_archive, toolchain, generate_rakefile) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cxx/wizard/project_wizard.rb', line 81

def create_project(dir_name, building_block, whole_archive, toolchain, generate_rakefile)
  rakefile_template = IO.read(File.join(File.dirname(__FILE__),"Rakefile.rb.template"))
  project_template = IO.read(File.join(File.dirname(__FILE__),"project.rb.template"))
  binding = create_binding("new-item", building_block, whole_archive, toolchain)

  if !File.directory?(dir_name) then
    mkdir_p(dir_name, :verbose => false)
  end

  rakefile_file = "#{dir_name}/Rakefile.rb"
  if generate_rakefile && (!File.exists?(rakefile_file) || confirm("Override existing '#{rakefile_file}'")) then
    write_template(rakefile_file, rakefile_template, binding)
  end

  project_file = "#{dir_name}/project.rb"
  if !File.exists?(project_file) || confirm("Override existing '#{project_file}'") then
    write_template(project_file, project_template, binding)
  end
end

#cxx(projects, output_dir, toolchain_name, base_dir, &block) ⇒ Object



226
227
228
# File 'lib/cxx.rb', line 226

def cxx(projects, output_dir, toolchain_name, base_dir, &block)
  Cxx::RubyDsl.new(projects, output_dir, toolchain_name, base_dir, &block)
end

#prepare_project(dir_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cxx/wizard/project_wizard.rb', line 6

def prepare_project(dir_name)
  begin
    require 'highline/import'
    if ["--help","-h"].include?(dir_name)
      say "cxx [directory] -- will create a new project in [directory]"
      return
    end
    if ["--version","-v"].include?(dir_name)
      say "cxx version #{Cxx::VERSION}"
      return
    end
    say "This will create a new cxx-project in directory: '#{dir_name}'"
    if confirm("Are you sure you want to continue") then
      building_block, whole_archive = choose_building_block
      generate_makefile = confirm("Do you also whant to generate a rakefile", building_block.eql?("exe"))

      toolchain = nil
      if generate_makefile then
        toolchain = choose_toolchain
        return unless toolchain
      end

      create_project(dir_name, building_block, whole_archive, toolchain, generate_makefile)
      say "Completed project-setup ;-)"
    else
      say "Stopped project-setup!"
    end

  rescue Interrupt
    say "\nStopped project-setup!"
  rescue LoadError
    say "Please run 'gem install highline'"
  end
end

#write_template(file_name, template, binding) ⇒ Object



105
106
107
108
109
110
# File 'lib/cxx/wizard/project_wizard.rb', line 105

def write_template(file_name, template, binding)
  say "...write: '#{file_name}'"
  File.open(file_name, 'w') do |f|
    f.write ERB.new(template).result(binding)
  end
end