Class: Main

Inherits:
Object
  • Object
show all
Defined in:
lib/whee.rb

Constant Summary collapse

@@options =

global options

{
  :team => nil,
  :name => nil,
  :year => nil,
  :connect => false
}

Instance Method Summary collapse

Instance Method Details

#parse_optionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whee.rb', line 12

def parse_options
  OptionParser.new do |opts|
    # team number
    opts.on("-t", "--team=TEAM", Integer, "Team number to connect to")
  
    # network name
    opts.on("-n", "--name=NAME", String, "Network name excluding team number")
  
    # year for wpilib jdk
    opts.on("-y", "--year=YEAR", Integer, "WPILib version to use JDK from")

    # only connect to robot
    opts.on("-c", "--connect", "Only connect and not deploy")
  end.parse!(into: @@options)
end

#runObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/whee.rb', line 28

def run
  # run command line options parser
  parse_options

  # output of netsh
  networks = %x(netsh wlan show networks).lines.filter do |line|
    # check if line is a network ssid
    line.start_with?(/SSID [\d]+/)
  end.collect do |line|
    # remove start from string
    line.strip.gsub(/SSID [\d]+ : /, "")
  end

  # netsh exit code
  error = %x(echo %ERRORLEVEL%).strip.downcase == "false"

  # exit if netsh fails
  if error
    puts "Netsh failed with a non-zero exit code."
    exit(1)
  end

  # team and name are set by options if present otherwise default regex
  team = (@@options[:team].nil?) ? '^[\d]{3,4}' : "^(#{@@options[:team].to_s[0..3]})"
  name = (@@options[:name].nil?) ? '[\w]*$' : "_(#{@@options[:name].strip})$"
  frc_regexp = Regexp.new(team + name)

  # check which networks match the frc radio name regex
  networks.filter! do |network|
    frc_regexp.match?(network) 
  end

  # exit if no robot network to connect to
  if networks.empty?
    puts "No robot network found to connect to."
    exit(2)
  end

  # index of desired network
  index = 0

  # prompt user if more than one option is present after regex
  if networks.length > 1
    puts "Multiple networks found:\n"
    
    # print out each index and option
    networks.each_index do |index|
      puts "[#{index+1}] #{networks[index]}"
    end

    # get user input
    print "\nEnter index of network to connect to: "
    index = gets.chomp.to_i - 1

    # exit if incorrect index
    if index < 0 || index > networks.length - 1
      puts "Index out of bounds."
      exit(1)
    end
  end

  # connect to the desired network
  %x(netsh wlan connect ssid=#{networks[index]} name=#{networks[index]})
  error = %x(echo %ERRORLEVEL%).strip.downcase == "false"

  if !error
    puts "Successfully connected to robot network."

    # exit if connect-only mode is set
    if @@options[:connect]
      exit(0)
    end
  else
    puts "Failed to connect to robot network."
    exit(1)
  end

  # year for jdk location
  year = (@@options[:year].nil?) ? Time.now.year.to_s : @@options[:year]
  dir = "C:\\Users\\Public\\wpilib\\#{year}\\jdk"

  # check if jdk exists
  if !Dir.exists?(dir)
    puts "Invalid year provided. JDK not found."
    exit(1)
  end

  # set gradle wrapper java home
  ENV['JAVA_HOME'] = dir
  
  # run gradle deploy
  begin
    deploy = %x(gradlew.bat deploy)
    error = %x(echo %ERRORLEVEL%).strip.downcase.to_i != 0 
  rescue Errno::ENOENT
    # rescue and exit if gradle wrapper isn't found
    puts "Gradle wrapper not found. Make sure this is a WPILib project directory."
    exit(1)
  end
 
  puts deploy
end