Class: Maintainers::Runner
- Inherits:
-
Object
- Object
- Maintainers::Runner
- Defined in:
- lib/maintainers/runner.rb
Overview
Runner entry point
Instance Method Summary collapse
- #add(options) ⇒ Object
- #create(options) ⇒ Object
-
#initialize(options) ⇒ Runner
constructor
A new instance of Runner.
- #remove(options) ⇒ Object
-
#run ⇒ Object
Run, Lola, Run.
- #validate(maintainers) ⇒ Object
- #write_file(filename, maintainers) ⇒ Object
Constructor Details
#initialize(options) ⇒ Runner
Returns a new instance of Runner.
10 11 12 13 14 15 |
# File 'lib/maintainers/runner.rb', line 10 def initialize() @options = # for now just assume it's always MAINTAINERS [:filename] ||= 'MAINTAINERS' end |
Instance Method Details
#add(options) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/maintainers/runner.rb', line 63 def add() filename = [:filename] if !File.exist?(filename) $stderr.puts "No #{filename} file exists yet. You can use the 'create' subcommand to create one." exit 1 end maintainers = JSON.load(File.read(filename)) new_maintainer = { "github" => [:github] } new_maintainer["email"] = [:email] if [:email] new_maintainer["name"] = [:name] if [:name] index = maintainers["people"].index { |person| person["github"] == [:github] } if index current = maintainers["people"][index] new_maintainer.merge! current maintainers["people"][index] = new_maintainer else maintainers["people"] << new_maintainer end write_file(filename, maintainers) end |
#create(options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/maintainers/runner.rb', line 47 def create() filename = [:filename] if File.exist?(filename) $stderr.puts "#{filename} already exists. Remove it and then re-run this command." exit 1 end # minimum content for a maintainers file maintainers = {} maintainers["version"] = 1 maintainers["issues"] = [:issues] maintainers["people"] = [] write_file(filename, maintainers) end |
#remove(options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/maintainers/runner.rb', line 86 def remove() filename = [:filename] if !File.exist?(filename) $stderr.puts "No #{filename} file exists yet. You can use the 'create' subcommand to create one." exit 1 end maintainers = JSON.load(File.read(filename)) index = maintainers["people"].index { |person| person["github"] == [:github] } if index maintainers["people"].slice!(index) else puts "I didn't find #{[:github]} in the file #{filename}" end write_file(filename, maintainers) end |
#run ⇒ Object
Run, Lola, Run
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/maintainers/runner.rb', line 19 def run case @options[:subcommand] when 'create' create(@options) when 'add' add(@options) when 'remove' remove(@options) end end |
#validate(maintainers) ⇒ Object
30 31 32 33 34 |
# File 'lib/maintainers/runner.rb', line 30 def validate(maintainers) maintainers_schema = JSON.parse(File.read('schema/MAINTAINERS.json')) JSON::Validator.validate(maintainers_schema, maintainers) end |
#write_file(filename, maintainers) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/maintainers/runner.rb', line 36 def write_file(filename, maintainers) maintainers_json = JSON.pretty_generate(maintainers) if !validate(maintainers_json) $stderr.puts "Invalid maintainers string!" exit 1 end File.open(filename, 'w') { |f| f.write(maintainers_json) } end |