Module: Fronde::Org

Defined in:
lib/fronde/org.rb,
lib/fronde/org/file.rb,
lib/fronde/org/file_extracter.rb

Overview

Everything related to Org mode

The module itself wraps code necessary to download the last version of the Emacs package. It also serves as a namespace for the class responsible for handling Org files: File.

Defined Under Namespace

Modules: FileExtracter Classes: File

Constant Summary collapse

GNU_ELPA_URL =
'https://elpa.gnu.org/packages/org'

Class Method Summary collapse

Class Method Details

.current_versionObject



16
17
18
19
# File 'lib/fronde/org.rb', line 16

def current_version
  # Do not crash if Org is not yet installed (and thus return nil)
  Dir.glob('lib/org-*').first&.delete_prefix('lib/org-')
end

.download(destination = 'var/tmp') ⇒ String

Download latest org-mode tarball.

Parameters:

  • destination (String) (defaults to: 'var/tmp')

    where to save the org-mode tarball

Returns:

  • (String)

    the downloaded org-mode version



71
72
73
74
75
76
77
78
79
# File 'lib/fronde/org.rb', line 71

def download(destination = 'var/tmp')
  org_last_version = last_version(force: false, cookie_dir: destination)
  uri = URI("#{GNU_ELPA_URL}-#{org_last_version}.tar")
  # Will crash on purpose if anything goes wrong
  http_get_client(uri) do |response|
    fetch_org_tarball response, destination
  end
  org_last_version
end

.extract(source, target) ⇒ Object

Extract downloaded Org tarball

Parameters:

  • source (String)

    path to the org-mode tarball to install

  • target (String)

    path to the final install directory



94
95
96
# File 'lib/fronde/org.rb', line 94

def extract(source, target)
  system 'tar', '-C', target, '-xf', source
end

.fetch_org_tarball(response, destination) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/fronde/org.rb', line 81

def fetch_org_tarball(response, destination)
  # Remove version number in dest file to allow easy rake file
  # task naming
  dest_file = ::File.expand_path('org.tar', destination)
  ::File.open(dest_file, 'w') do |io|
    response.read_body { |chunk| io.write chunk }
  end
end

.fetch_version_numberObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fronde/org.rb', line 52

def fetch_version_number
  # Retrieve last org version from GNU ELPA page.
  uri = URI("#{GNU_ELPA_URL}.html")
  response = http_get_client(uri).body
  version_line = response.each_line(chomp: true).find do |line|
    line.start_with? '<dt>Latest</dt> <dd><a href='
  end
  return unless version_line

  version_match = version_line.match(/org-(?<version>[0-9.]+)\.tar/)
  return version_match[:version] if version_match

  nil
end

.http_get_client(uri) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/fronde/org.rb', line 44

def http_get_client(uri, &)
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    request = Net::HTTP::Get.new(uri)
    request['User-Agent'] = Fronde::USER_AGENT
    http.request request, &
  end
end

.last_version(force: false, cookie_dir: 'var/tmp') ⇒ String

Fetch and return the last published version of Org.

To be nice with Org servers, this method will keep the fetched version number in a cache file. You can bypass it by using the force parameter.

Parameters:

  • force (Boolean) (defaults to: false)

    Whether we should first remove the guard file if it exists

  • destination (String)

    Where to store the cookie file to remember the last version number

Returns:

  • (String)

    the new x.x.x version string of Org



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fronde/org.rb', line 32

def last_version(force: false, cookie_dir: 'var/tmp')
  cookie = "#{cookie_dir}/last_org_version"
  return ::File.read cookie if !force && ::File.exist?(cookie)

  org_version = fetch_version_number
  raise 'No remote Org version found' unless org_version

  FileUtils.mkdir_p cookie_dir
  ::File.write cookie, org_version
  org_version
end