Class: ElmInstall::GitSource

Inherits:
Source show all
Defined in:
lib/elm_install/git_source.rb

Overview

Git Source

Instance Attribute Summary collapse

Attributes inherited from Source

#identifier, #options

Instance Method Summary collapse

Constructor Details

#initialize(uri, branch) ⇒ GitSource

Initializes a git source by URI and branch

Parameters:

  • uri (Uri)

    The uri

  • branch (Branch)

    The branch



17
18
19
20
21
# File 'lib/elm_install/git_source.rb', line 17

def initialize(uri, branch)
  @branch = branch
  @uri = uri
  self
end

Instance Attribute Details

#branchBranch (readonly)

Returns The branch.

Returns:



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

def branch
  @branch
end

#uriUri (readonly)

Returns The uri.

Returns:

  • (Uri)

    The uri



5
6
7
# File 'lib/elm_install/git_source.rb', line 5

def uri
  @uri
end

Instance Method Details

#copy_to(version, directory) ⇒ Object

Copies the version into the given directory

Parameters:

Returns:

  • nil



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/elm_install/git_source.rb', line 54

def copy_to(version, directory)
  # Delete the directory to make sure no pervious version remains if
  # we are using a branch or symlink if using Dir.
  FileUtils.rm_rf(directory) if directory.exist?

  # Create directory if not exists
  FileUtils.mkdir_p directory

  # Copy hole repository
  FileUtils.cp_r("#{fetch(version).path}/.", directory)

  # Remove .git directory
  FileUtils.rm_rf(File.join(directory, '.git'))

  nil
end

#fetch(version) ⇒ Dir

Downloads the version into a temporary directory

Parameters:

Returns:

  • (Dir)

    The directory for the source of the version



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elm_install/git_source.rb', line 29

def fetch(version)
  # Get the reference from the branch
  ref =
    case @branch
    when Branch::Just
      @branch.ref
    when Branch::Nothing
      case version
      when String
        version
      else
        version.to_simple
      end
    end

  repository.checkout ref
end

#hostString

Returns the host for the repository

Returns:

  • (String)

    The host



131
132
133
134
135
136
137
138
# File 'lib/elm_install/git_source.rb', line 131

def host
  case @uri
  when Uri::Github
    'github.com'
  else
    @uri.uri.host
  end
end

#matching_versions(constraints, elm_version) ⇒ Array

Returns the matchign versions for a repository for the given constraints

Parameters:

  • constraints (Array)

    The constraints

  • elm_version (String)

    The Elm version to match against

Returns:

  • (Array)

    The versions



100
101
102
103
104
105
106
107
108
109
# File 'lib/elm_install/git_source.rb', line 100

def matching_versions(constraints, elm_version)
  repository
    .versions
    .select do |version|
      identifier.elm_version(fetch(version.to_s)) == elm_version &&
        constraints.all? { |constraint| constraint.satisfies?(version) }
    end
    .sort
    .reverse
end

#package_nameString

Returns the package name for the repository

Returns:

  • (String)

    The name



144
145
146
147
148
149
150
151
# File 'lib/elm_install/git_source.rb', line 144

def package_name
  case @uri
  when Uri::Github
    @uri.name
  else
    @uri.uri.path.sub(%r{^/}, '')
  end
end

#pathString

Returns the temporary path for the repository

Returns:

  • (String)

    The path



123
124
125
# File 'lib/elm_install/git_source.rb', line 123

def path
  File.join(options[:cache_directory].to_s, host, package_name)
end

#repositoryRepository

Returns the local repository

Returns:



157
158
159
# File 'lib/elm_install/git_source.rb', line 157

def repository
  @repository ||= Repository.new url, path
end

#to_logString

Returns the log format

Returns:

  • (String)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/elm_install/git_source.rb', line 165

def to_log
  case @uri
  when Uri::Ssh, Uri::Http
    case @branch
    when Branch::Just
      "#{url} at #{@branch.ref}"
    else
      # NOTE: Cannot happen
      # :nocov:
      url
      # :nocov:
    end
  else
    url
  end
end

#urlString

Returns the url for the repository

Returns:

  • (String)

    The url



115
116
117
# File 'lib/elm_install/git_source.rb', line 115

def url
  @uri.to_s
end

#versions(constraints, elm_version) ⇒ Array

Returns the available versions for a repository

Parameters:

  • constraints (Array)

    The constraints

  • elm_version (String)

    The Elm version to match against

Returns:

  • (Array)

    The versions



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/elm_install/git_source.rb', line 78

def versions(constraints, elm_version)
  if repository.cloned? && !repository.fetched?
    # Get updates from upstream
    Logger.arrow "Getting updates for: #{package_name.bold}"
    repository.fetch
  end

  case @branch
  when Branch::Just
    [identifier.version(fetch(@branch.ref))]
  when Branch::Nothing
    matching_versions constraints, elm_version
  end
end