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

Class Method Summary collapse

Class Method Details

.compile(source, version, target, verbose: false) ⇒ Object

Compile downloaded Org package

Parameters:

  • source (String)

    path to the org-mode tarball to install

  • version (String)

    version of the org package to install

  • target (String)

    path to the final install directory

  • verbose (Boolean) (defaults to: false)

    whether the process should be verbose



94
95
96
97
98
99
100
101
102
# File 'lib/fronde/org.rb', line 94

def compile(source, version, target, verbose: false)
  untar_cmd = ['tar', '-xzf', source]
  system(*untar_cmd)
  FileUtils.mv "org-mode-release_#{version}", target
  # Fix a weird unknown package version
  ::File.write("#{target}/mk/version.mk", "ORGVERSION ?= #{version}")
  system(*make_org_cmd(target, 'compile', verbose:))
  system(*make_org_cmd(target, 'autoloads', verbose:))
end

.current_versionObject



11
12
13
14
# File 'lib/fronde/org.rb', line 11

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/fronde/org.rb', line 57

def download(destination = 'var/tmp')
  org_last_version = last_version(force: false, cookie_dir: destination)
  tarball = "org-mode-release_#{org_last_version}.tar.gz"
  uri = URI("https://git.savannah.gnu.org/cgit/emacs/org-mode.git/snapshot/#{tarball}")
  # Will crash on purpose if anything goes wrong
  Net::HTTP.start(uri.host) do |http|
    fetch_org_tarball http, Net::HTTP::Get.new(uri), destination
  end
  org_last_version
end

.fetch_org_tarball(http, request, destination) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/fronde/org.rb', line 68

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

.fetch_version_numberObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fronde/org.rb', line 39

def fetch_version_number
  # Retrieve last org version from git repository tags page.
  tag_rx = Regexp.new(
    '<a href=\'/cgit/emacs/org-mode.git/tag/\?h=' \
    '(?<tag>release_(?<number>[^\']+))\'>\k<tag></a>'
  )
  versions = URI(
    'https://git.savannah.gnu.org/cgit/emacs/org-mode.git/refs/'
  ).open.readlines.map do |line|
    line.match(tag_rx) { |matchdata| matchdata[:number] }
  end
  versions.compact.first
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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fronde/org.rb', line 27

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

.make_org_cmd(org_dir, target, verbose: false) ⇒ Object



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

def make_org_cmd(org_dir, target, verbose: false)
  make = ['make', '-C', org_dir, target]
  return make.join(' ') if verbose

  make.insert(3, '-s')
  make << 'EMACSQ="emacs -Q --eval \'(setq inhibit-message t)\'"'
  make.join(' ')
end