Module: Boot::Lib::Commands

Defined in:
lib/Boot/Lib/autoload.rb,
lib/Boot/Lib/Commands/New.rb,
lib/Boot/Lib/Commands/Help.rb,
lib/Boot/Lib/Commands/Config.rb,
lib/Boot/Lib/Commands/Version.rb,
lib/Boot/Lib/Commands/Template.rb

Constant Summary collapse

New =
SubCommand.new(
  'new', # Name of the sub command
  'Creates a new project from a template',
  options_obj
) { |options, args|
  parsed_options = options.parse(args)

  # The first argument, as long as not a option
  # is the template
  if (!Boot::Lib::Core::SubCommand.is_flag(args[0]))
    template_name = args[0]
  else
    puts options_obj.banner
    next
  end


  output_path = !parsed_options[:out].nil? ? Dir.pwd + '/' + parsed_options[:out] : Dir.pwd

  if (Dir.exists?(output_path) && !((Dir[output_path + "/*"]).empty?))
    puts "Error: #{output_path} exists and is not empty"
    next # terminate
  end

  if (File.exists?(output_path))
    puts "Error: #{output_path} exists and is a file"
    next # terminate
  end

  # Strip all args before the -- arg, signaling args to the template
  c = 0
  while c < args.length do
    break if (args[c] == '--')
    c+=1
  end
  template_args = args[c+1..-1]
  if (template_args.nil?) # no -- found
    template_args = []
  end

  # Get template by name
  template = Core::Template.get_template_by_name(template_name)
  if template.nil?
    puts "Fatal: Could not find template #{template_name}"
    exit(1)
  end

  # Create a project base on the template
  puts "Creating #{output_path} based on '#{template.name}' template"

  loading_thread = nil

  loading_thread = Thread.new {
    print "Doing stuff"
    while true do
      print ".."
      sleep 0.3
    end
  }

  creation_thread = Thread.new {
    Thread.current.thread_variable_set("success", true); 
    begin
      template.create(template_args, output_path)
    rescue ArgumentError => e
      Thread.current.thread_variable_set("success", false);
      puts "\r" + e.message
    end
  }

  creation_thread.join
  loading_thread.exit

  # Unless an exception occured
  if (creation_thread.thread_variable_get("success"))
    puts "\nDone!"
  end
}
Help =
SubCommand.new(
  'help', # Name of the command
  'Print the help message', # Description
  options_obj, # Has no options
  false
) do |_options, _args|
  Boot.sub_commands.each do |_key, cmd|
    cmd.print_help_message
  end
end
Config =
SubCommand.new(
  'config', # Name of the sub command
  'Read configuration options',
  options_obj
) { |options, args|
  parsed_options = options.parse(args);
  if (!parsed_options[:all])
    option_name = args[0]
    option_val = Boot.config.config[option_name]
    if (option_val == nil)
      puts "No option '#{option_name}'"
    else
      puts "#{option_name} = #{option_val}"
    end
  else
    puts JSON.pretty_generate(Boot.config.config)
  end
}
Version =
SubCommand.new(
  'version', # Name of the sub command
  'Print the version',
  options_obj,
  false
) { |options, args|
  puts Boot::VERSION
}
Template =
SubCommand.new(
  'template', # Name of the sub command
  'Print info for a template',
  options_obj
) { |options, args|
  parsed_options = options.parse(args)
  if (parsed_options[:list])
    templates = {}

    Boot.config.templates_path.each do |dir|
      Dir[dir + "/*"].each do |template_path|
        name = template_path.split('/')[-1]
        if (templates[name].nil?)
          templates[name] = Core::Template.get_template_by_name(name)
        end
      end
    end

    templates.each do |key, value|
      if (parsed_options[:verbose])
        puts "key:      " + key
        puts "name:     " + value.name
        puts "location: " + value.path
        puts "description:\n"
        puts value.description
        puts
      else
        puts key
      end
    end

    next
  end

  if (args.length != 1)
    puts "usage 'boot template [template name]'"
  end

  template_name = args[0]
  template = Core::Template.get_template_by_name(template_name)

  if (template.nil?)
    puts "Could not find template '#{template_name}'"
  else
    msg = ''
    msg << 'Template:    ' + template.name + "\n"
    msg << 'Description: ' + template.description + "\n"
    msg << 'Location:    ' + template.path + "\n"
    msg << "\nOptions\n"
    
    template.template_options.each do |key, value|
      msg << key
      msg << ' '

      if (!value['values'].nil?)
        msg << '['
        value['values'].each do |key,value|
          msg << "#{key}/"
        end
        msg = msg[0..-2] # Remove the last /
        msg << "]\n"
      elsif(!value['symbol'].nil?)
        msg << value['symbol']
        msg << "\n"
      else
        fail if (value['files'].nil?) # Assertion
        msg << "\n"
      end
      msg << "\t" + value['description']
      msg << "\n\n"
    end

    puts msg
  end
}