Class: ElmInstall::Repository

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/elm_install/repository.rb

Overview

Handles git repositories.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, path) ⇒ Repository

Initializes a repository.

Parameters:

  • url (String)

    The url

  • path (String)

    The path



37
38
39
40
41
42
# File 'lib/elm_install/repository.rb', line 37

def initialize(url, path)
  @fetched = false
  @path = path
  @url = url
  self
end

Instance Attribute Details

#fetchedBool (readonly)

Whether or not the repository has been fetched (updated)

Returns:

  • (Bool)


16
17
18
# File 'lib/elm_install/repository.rb', line 16

def fetched
  @fetched
end

#pathString (readonly)

The path to the directory where the repository lives

Returns:

  • (String)


12
13
14
# File 'lib/elm_install/repository.rb', line 12

def path
  @path
end

#urlString (readonly)

The url of the git repository

Returns:

  • (String)


8
9
10
# File 'lib/elm_install/repository.rb', line 8

def url
  @url
end

Class Method Details

.of(url, path) ⇒ Repository

Returns the repository for the given url and path

Parameters:

  • url (String)

    The url

  • path (String)

    The path

Returns:



25
26
27
28
# File 'lib/elm_install/repository.rb', line 25

def self.of(url, path)
  @repositories ||= {}
  @repositories[url] ||= new url, path
end

Instance Method Details

#checkout(ref) ⇒ Dir

Checks out the version and returns it’s directory

Parameters:

  • ref (String)

    The reference to checkout

Returns:

  • (Dir)

    The directory



50
51
52
53
# File 'lib/elm_install/repository.rb', line 50

def checkout(ref)
  repo.checkout ref
  directory
end

#cloneGit::Base

Clones the repository

Returns:

  • (Git::Base)


109
110
111
112
113
114
# File 'lib/elm_install/repository.rb', line 109

def clone
  Logger.arrow "Package: #{url.bold} not found in cache, cloning..."
  FileUtils.mkdir_p path
  @fetched = true
  @repo = Git.clone(url, path)
end

#cloned?Bool

Returns if the repository has been cloned yet or not

Returns:

  • (Bool)


92
93
94
# File 'lib/elm_install/repository.rb', line 92

def cloned?
  Dir.exist?(path)
end

#directoryDir

Returns the directory of the repository

Returns:

  • (Dir)

    The directory



59
60
61
62
# File 'lib/elm_install/repository.rb', line 59

def directory
  # This removes the .git from filename
  Dir.new(File.dirname(repo.repo.path))
end

#fetchVoid

Fetches changes from a repository

Returns:

  • (Void)


99
100
101
102
103
# File 'lib/elm_install/repository.rb', line 99

def fetch
  return if fetched
  repo.fetch
  @fetched = true
end

#repoGit::Base

Returns the existing repository or clones it if it does not exists.

Returns:

  • (Git::Base)


82
83
84
85
86
87
# File 'lib/elm_install/repository.rb', line 82

def repo
  clone unless cloned?
  @repo ||= Git.open path
  @repo.reset_hard
  @repo
end

#versionsArray<Semverse::Version>

Returns the versions of the repository

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/elm_install/repository.rb', line 68

def versions
  @versions ||=
    repo
    .tags
    .map(&:name)
    .select { |tag| tag =~ /(.*\..*\..*)/ }
    .map { |tag| Semverse::Version.try_new tag }
    .compact
end