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

#elm_version_of(ref) ⇒ Array

Returns the supported Elm version the given ref.

Parameters:

  • ref (String)

    The ref

Returns:

  • (Array)

    The version



77
78
79
80
81
# File 'lib/elm_install/git_source.rb', line 77

def elm_version_of(ref)
  @@elm_versions ||= {}
  @@elm_versions[url] ||= {}
  @@elm_versions[url][ref] ||= identifier.elm_version(fetch(ref))
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



149
150
151
152
153
154
155
156
# File 'lib/elm_install/git_source.rb', line 149

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



118
119
120
121
122
123
124
125
126
127
# File 'lib/elm_install/git_source.rb', line 118

def matching_versions(constraints, elm_version)
  repository
    .versions
    .select do |version|
      elm_version_of(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



162
163
164
165
166
167
168
169
# File 'lib/elm_install/git_source.rb', line 162

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



141
142
143
# File 'lib/elm_install/git_source.rb', line 141

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

#repositoryRepository

Returns the local repository

Returns:



175
176
177
# File 'lib/elm_install/git_source.rb', line 175

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

#to_logString

Returns the log format

Returns:

  • (String)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/elm_install/git_source.rb', line 183

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



133
134
135
# File 'lib/elm_install/git_source.rb', line 133

def url
  @uri.to_s
end

#versions(constraints, elm_version, should_update, only_update) ⇒ 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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/elm_install/git_source.rb', line 93

def versions(constraints, elm_version, should_update, only_update)
  if repository.cloned? &&
     !repository.fetched &&
     should_update &&
     (!only_update || only_update == package_name)
    # 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