Class: Arli::Commands::Generate

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Includes:
Arduino::Library, Helpers::SystemCommands
Defined in:
lib/arli/commands/generate.rb

Constant Summary

Constants included from Helpers::Output

Helpers::Output::CHAR_FAILURE, Helpers::Output::CHAR_SUCCESS

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #name

Instance Method Summary collapse

Methods included from Helpers::SystemCommands

#backup!, #handle_preexisting_folder, #run_system_command

Methods included from Helpers::Output

#___, #__p, #__pf, #__pt, #abort?, #action_fail, #action_ok, #backup?, #cursor, #debug, #debug?, disable!, enable!, enabled?, #error, #fuck, #header, #hr, #indent_cursor, #info, #ok, #overwrite?, #print_action_failure, #print_action_starting, #print_action_success, #print_target_dir, #quiet?, #raise_invalid_arli_command!, #report_exception, #verbose?

Methods inherited from Base

#initialize, #library_path, #params, #runtime, #temp_path

Constructor Details

This class inherits a constructor from Arli::Commands::Base

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



19
20
21
# File 'lib/arli/commands/generate.rb', line 19

def dir
  @dir
end

#librariesObject

Returns the value of attribute libraries.



19
20
21
# File 'lib/arli/commands/generate.rb', line 19

def libraries
  @libraries
end

#settingsObject

Returns the value of attribute settings.



19
20
21
# File 'lib/arli/commands/generate.rb', line 19

def settings
  @settings
end

Instance Method Details

#additional_infoObject



89
90
91
92
# File 'lib/arli/commands/generate.rb', line 89

def additional_info
  "\nGenerating project #{project_name.bold.green} into #{workspace.bold.yellow}\n" +
      "Template: #{template_repo.bold.red}\n"
end

#runObject



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
75
# File 'lib/arli/commands/generate.rb', line 44

def run
  Dir.chdir(workspace) do
    run_with_info(
        "Grabbing the template from\n • #{template_repo.bold.green}...",
        "git clone -v #{template_repo} #{project_name} 2>&1"
    )
    Dir.chdir(project_name) do
      FileUtils.rm_rf('.git')
      FileUtils.rm_rf('example')
      run_with_info(
          "Configuring the new project #{project_name.bold.yellow}",
          'git init .'
      )
      run_with_info('Customizing your README and other files...')

      rename_files!

      configure_template!
      configure_arlifile!

      configure_main!

      run_with_info(
          'Running setup of the dependencies...',
          'bin/setup'
      )
      run_with_info("The project #{project_name.bold.yellow} is ready.\n" +
                        'Follow README.md for build instructions.')
    end
  end
  __pt hr
end

#run_with_info(message, command = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/arli/commands/generate.rb', line 77

def run_with_info(message, command = nil)
  indent     = '    '
  ok_indent  = indent + ' ✔  '.green
  err_indent = indent + ' X   '.red
  info("\n" + message.magenta)
  return unless command
  info(indent + command.bold.yellow)
  o, e, s = run_system_command(command)
  info(ok_indent + o.chomp.gsub(/\n/, "\n#{ok_indent}").blue) if o && o.chomp != ''
  warn(err_indent + +e.chomp.gsub(/\n/, "\n#{err_indent}").red) if e && e.chomp != ''
end

#setupObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arli/commands/generate.rb', line 21

def setup
  config.generate.project_name = config.runtime.argv.first

  self.settings = config.generate
  self.libraries = []

  (settings.libs || []).each do |lib|
    library = find_library({ name: lib }, version: :latest)
    if library
      self.libraries << library
    else
      raise ::Arli::Errors::LibraryNotFound, "Can not find library by name #{lib}"
    end
  end

  raise ::Arli::Errors::RequiredArgumentsMissing, 'Project name is required' unless project_name
  raise ::Arli::Errors::RequiredArgumentsMissing, 'Template Repo is missing' unless template_repo

  self.dir = settings.workspace + '/' + project_name
  handle_preexisting_folder(dir) if Dir.exist?(dir)
  FileUtils.mkdir_p(workspace) unless Dir.exist?(workspace)
end