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
# File 'lib/cxx/wizard/project_wizard.rb', line 41

def choose_building_block
  res = nil
  choose do |menu|
    say "What building-block do you whant to create?"
    menu.choice(:exe) { res = "exe" }
    menu.choice(:lib) { res = "source_lib" }
    menu.prompt = "Select a building-block: "
  end
  res
end

#choose_toolchainObject



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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cxx/wizard/project_wizard.rb', line 108

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, toolchain) ⇒ Object



97
98
99
# File 'lib/cxx/wizard/project_wizard.rb', line 97

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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cxx/wizard/project_wizard.rb', line 77

def create_project(dir_name, building_block, 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, 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



217
218
219
# File 'lib/cxx.rb', line 217

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 = 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, 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



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

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