Class: Gasoline::Patchor

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

Overview

The Patchor will do all the patching on the Propane support file, it finds (and creates if needed) the file, adds the Propane header, and then adds all the drops from the jerrycan.

Constant Summary collapse

PATCHOR_FILE =
"#{ENV['HOME']}/Library/Application Support/Propane/unsupported/caveatPatchor.js"

Instance Method Summary collapse

Constructor Details

#initializePatchor

Returns a new instance of Patchor.



8
9
10
11
12
# File 'lib/gasoline/patchor.rb', line 8

def initialize
  @lines = []
  bootstrap
  load_lines
end

Instance Method Details

#bootstrapObject

Will create the Propane, caveatPatchor.js file



19
20
21
22
23
24
25
26
27
# File 'lib/gasoline/patchor.rb', line 19

def bootstrap
  return if File.exist?(patchor_file)

  FileUtils.mkdir_p(File.split(patchor_file).first)
  FileUtils.touch(patchor_file)
  File.open(patchor_file, "w") do |f|
    f.write(Gasoline::Texts::propane_header)
  end
end

#cut_line_indexObject

The index of the line matching the cut snippet, if it can’t be found we’ll take the last line.

Returns an int



45
46
47
# File 'lib/gasoline/patchor.rb', line 45

def cut_line_index
  @lines.find_index{|line| line =~ /#{Gasoline::Texts::cut_here_line}/} || @lines.length
end

#dropsObject

The contents of our Jerrycan, laid bare to be patched.

Returns an array of ‘Drop`s



60
61
62
# File 'lib/gasoline/patchor.rb', line 60

def drops
  jerrycan.drops
end

#jerrycanObject

An instance of the Jerrycan, mainly added to have quick access to it, while developing.

Returns a ‘Jerrycan` instance



53
54
55
# File 'lib/gasoline/patchor.rb', line 53

def jerrycan
  @jerrycan ||= Gasoline::Jerrycan.new
end

#linesObject

Current lines in memory

Returns an array of lines



32
33
34
# File 'lib/gasoline/patchor.rb', line 32

def lines
  @lines
end

#load_linesObject

Loads the entire file into memory



37
38
39
# File 'lib/gasoline/patchor.rb', line 37

def load_lines
  @lines = File.readlines(patchor_file)
end

#patch_it_chewieObject

Transform the @lines to have:

1. All the lines above the cut
2. The cut here snippet
3. All patches from the drops. (will download them)

Returns an array of lines



71
72
73
74
75
76
# File 'lib/gasoline/patchor.rb', line 71

def patch_it_chewie
  to_keep = @lines[0...cut_line_index]
  snippet = [Gasoline::Texts::cut_here_snippet]
  to_add = drops.collect(&:patch).compact
  @lines = (to_keep + snippet + to_add)
end

#patch_it_chewie!Object

First fills up the @lines instance, then saves the file



79
80
81
82
83
# File 'lib/gasoline/patchor.rb', line 79

def patch_it_chewie!
  patch_it_chewie
  Gasoline::Applescript.refresh_propane_if_running
  save
end

#patchor_fileObject



14
15
16
# File 'lib/gasoline/patchor.rb', line 14

def patchor_file
  PATCHOR_FILE
end

#saveObject

Writes the file to disk.



86
87
88
89
# File 'lib/gasoline/patchor.rb', line 86

def save
  File.open("file.tmp", "w") {|f| f.write(@lines.join(""))}
  FileUtils.mv("file.tmp", patchor_file)
end