lib/vim-flavor/flavor.rb in vim-flavor-0.0.4 vs lib/vim-flavor/flavor.rb in vim-flavor-1.0.0
- old
+ new
@@ -1,119 +1,126 @@
module Vim
module Flavor
class Flavor
- @@properties = [
- :groups,
- :locked_version,
- :repo_name,
- :repo_uri,
- :version_contraint,
- ]
+ # A short name of a repository.
+ # Possible formats are "$user/$repo", "$repo" and "$repo_uri".
+ attr_accessor :repo_name
- @@properties.each do |p|
- attr_accessor p
- end
+ # A constraint to choose a proper version.
+ attr_accessor :version_constraint
- def initialize()
- @groups = []
- end
+ # A version of a plugin to be installed.
+ attr_accessor :locked_version
- def ==(other)
- return false if self.class != other.class
- @@properties.all? do |p|
- self.send(p) == other.send(p)
- end
+ # Return true if this flavor's repository is already cloned.
+ def cached?
+ Dir.exists?(cached_repo_path)
end
- def zapped_repo_dir_name
- @repo_name.gsub(/[^A-Za-z0-9._-]/, '_')
- end
-
def cached_repo_path
@cached_repo_path ||=
- "#{Vim::Flavor.dot_path}/repos/#{zapped_repo_dir_name}"
+ "#{ENV['HOME'].to_stash_path}/repos/#{@repo_name.zap}"
end
- def make_deploy_path(vimfiles_path)
- "#{vimfiles_path.to_flavors_path()}/#{zapped_repo_dir_name}"
+ def self.github_repo_uri(user, repo)
+ @github_repo_uri ||= lambda {|user, repo|
+ "git://github.com/#{user}/#{repo}.git"
+ }
+ @github_repo_uri.call(user, repo)
end
+ def repo_uri
+ @repo_uri ||=
+ if /^([^\/]+)$/.match(repo_name)
+ m = Regexp.last_match
+ self.class.github_repo_uri('vim-scripts', m[1])
+ elsif /^([A-Za-z0-9_-]+)\/(.*)$/.match(repo_name)
+ m = Regexp.last_match
+ self.class.github_repo_uri(m[1], m[2])
+ elsif /^[a-z]+:\/\/.*$/.match(repo_name)
+ repo_name
+ else
+ raise "Invalid repo_name: #{repo_name.inspect}"
+ end
+ end
+
def clone()
- message = %x[
+ sh %Q[
{
- git clone '#{@repo_uri}' '#{cached_repo_path}'
+ git clone '#{repo_uri}' '#{cached_repo_path}'
} 2>&1
]
- if $? != 0 then
- raise RuntimeError, message
- end
true
end
def fetch()
- message = %x[
+ sh %Q{
{
- cd #{cached_repo_path.inspect} &&
- git fetch origin
+ cd '#{cached_repo_path}' &&
+ git fetch --tags
} 2>&1
- ]
- if $? != 0 then
- raise RuntimeError, message
- end
+ }
end
def deploy(vimfiles_path)
- deploy_path = make_deploy_path(vimfiles_path)
- message = %x[
+ deployment_path = "#{vimfiles_path.to_flavors_path}/#{repo_name.zap}"
+ sh %Q[
{
cd '#{cached_repo_path}' &&
git checkout -f '#{locked_version}' &&
- git checkout-index -a -f --prefix='#{deploy_path}/' &&
+ git checkout-index -a -f --prefix='#{deployment_path}/' &&
{
vim -u NONE -i NONE -n -N -e -s -c '
- silent! helptags #{deploy_path}/doc
+ silent! helptags #{deployment_path}/doc
qall!
' || true
}
} 2>&1
]
- if $? != 0 then
- raise RuntimeError, message
- end
+ true
end
- def undeploy(vimfiles_path)
- deploy_path = make_deploy_path(vimfiles_path)
- message = %x[
- {
- rm -fr '#{deploy_path}'
- } 2>&1
- ]
- if $? != 0 then
- raise RuntimeError, message
- end
+ def use_appropriate_version()
+ @locked_version =
+ version_constraint.find_the_best_version(list_versions)
end
- def list_versions()
- tags = %x[
+ def use_specific_version(locked_version)
+ @locked_version = locked_version
+ end
+
+ def list_tags()
+ output = sh %Q[
{
cd '#{cached_repo_path}' &&
git tag
} 2>&1
]
- if $? != 0 then
- raise RuntimeError, message
- end
+ output.split(/[\r\n]/)
+ end
+ def versions_from_tags(tags)
tags.
- split(/[\r\n]/).
select {|t| t != '' && Gem::Version.correct?(t)}.
map {|t| Gem::Version.create(t)}
end
- def update_locked_version()
- @locked_version =
- version_contraint.find_the_best_version(list_versions())
+ def list_versions()
+ versions_from_tags(list_tags())
+ end
+
+ def sh script
+ output = send(:`, script)
+ if $? == 0
+ output
+ else
+ raise RuntimeError, output
+ end
+ end
+
+ def satisfied_with?(locked_flavor)
+ repo_name == locked_flavor.repo_name &&
+ version_constraint.compatible?(locked_flavor.locked_version)
end
end
end
end