Class: Mnenv::VersionsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mnenv/versions_manager.rb

Overview

Manages the local clone of the metanorma/versions repository Similar to how Homebrew manages its core tap

Constant Summary collapse

VERSIONS_REPO =
'https://github.com/metanorma/versions.git'
DEFAULT_MNENV_DIR =
File.expand_path('~/.mnenv')
VERSIONS_DIR =
'versions'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mnenv_dir: nil) ⇒ VersionsManager

Returns a new instance of VersionsManager.



16
17
18
19
# File 'lib/mnenv/versions_manager.rb', line 16

def initialize(mnenv_dir: nil)
  @mnenv_dir = mnenv_dir || DEFAULT_MNENV_DIR
  @versions_path = File.join(@mnenv_dir, VERSIONS_DIR)
end

Instance Attribute Details

#mnenv_dirObject (readonly)

Returns the value of attribute mnenv_dir.



14
15
16
# File 'lib/mnenv/versions_manager.rb', line 14

def mnenv_dir
  @mnenv_dir
end

#versions_pathObject (readonly)

Returns the value of attribute versions_path.



14
15
16
# File 'lib/mnenv/versions_manager.rb', line 14

def versions_path
  @versions_path
end

Instance Method Details

#clone_repoObject

Clone the versions repository using the git gem



56
57
58
59
60
61
62
# File 'lib/mnenv/versions_manager.rb', line 56

def clone_repo
  FileUtils.mkdir_p(mnenv_dir)
  Git.clone(VERSIONS_REPO, VERSIONS_DIR, path: mnenv_dir, depth: 1)
  touch_update_marker
rescue Git::GitExecuteError => e
  raise "Failed to clone versions repository: #{e.message}"
end

#cloned?Boolean

Check if versions repository is cloned

Returns:

  • (Boolean)


35
36
37
# File 'lib/mnenv/versions_manager.rb', line 35

def cloned?
  File.directory?(File.join(versions_path, '.git'))
end

#data_pathObject

Path to the data directory within the versions repository



51
52
53
# File 'lib/mnenv/versions_manager.rb', line 51

def data_path
  File.join(versions_path, 'data')
end

#ensure_versions_data(update: false) ⇒ String

Ensure versions repository is cloned and up to date

Parameters:

  • update (Boolean) (defaults to: false)

    Force update (git fetch) even if already cloned

Returns:

  • (String)

    Path to the versions data directory



24
25
26
27
28
29
30
31
32
# File 'lib/mnenv/versions_manager.rb', line 24

def ensure_versions_data(update: false)
  if cloned?
    update_clone if update || stale?
  else
    clone_repo
  end

  data_path
end

#stale?Boolean

Check if the clone is stale (older than 24 hours)

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
# File 'lib/mnenv/versions_manager.rb', line 40

def stale?
  return true unless cloned?

  last_update_file = File.join(versions_path, '.mnenv_last_update')
  return true unless File.exist?(last_update_file)

  last_update = File.mtime(last_update_file)
  Time.now - last_update > 86_400 # 24 hours
end

#updateObject

Force update the versions repository



77
78
79
80
81
82
83
84
# File 'lib/mnenv/versions_manager.rb', line 77

def update
  if cloned?
    update_clone
  else
    clone_repo
  end
  data_path
end

#update_cloneObject

Update the versions repository using the git gem



65
66
67
68
69
70
71
72
73
74
# File 'lib/mnenv/versions_manager.rb', line 65

def update_clone
  return unless cloned?

  g = Git.open(versions_path)
  g.fetch
  g.reset_hard('origin/main')
  touch_update_marker
rescue Git::GitExecuteError => e
  warn "Warning: Failed to update versions repository: #{e.message}"
end