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

#netsh_refreshObject

refresh list of networks available



45
46
47
48
49
50
51
# File 'lib/whee.rb', line 45

def netsh_refresh
  # disconnect from current to force refresh
  %x(netsh wlan disconnect)
  
  # sleep until refresh
  sleep 3
end

#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



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/whee.rb', line 53

def run
  # ensure wpilib project
  if !wpilib_proj?
    puts "File '.\\.wpilib\\wpilib_preferences.json' not found."
    exit(1)
  else
    # set options from wpilib proj
    wpilib_pref 
  end

  # run command line options parser
  parse_options

  # refresh list of networks
  netsh_refresh

  # 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(1)
  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] || Time.now.year.to_s
  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

#wpilib_prefObject

sets default options based on ./.wpilib/wpilib_preferences.json



34
35
36
37
38
39
40
41
42
# File 'lib/whee.rb', line 34

def wpilib_pref
  require 'json'
  
  # no error checking because this method is only called after `wpilib_proj?`
  preferences = JSON.parse(File.read(".\\.wpilib\\wpilib_preferences.json"))

  @@options[:year] = preferences["projectYear"]
  @@options[:team] = preferences["teamNumber"]
end

#wpilib_proj?Boolean

checks whether current directory is a wpilib project



29
30
31
# File 'lib/whee.rb', line 29

def wpilib_proj? 
  File.exist?(".\\.wpilib\\wpilib_preferences.json")
end