Class: Vim::Flavor::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/vim-flavor/facade.rb

Instance Method Summary collapse

Constructor Details

#initializeFacade

Returns a new instance of Facade.



7
8
9
# File 'lib/vim-flavor/facade.rb', line 7

def initialize
  @target_repo_names = []
end

Instance Method Details

#choose_a_flavor(nfg) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vim-flavor/facade.rb', line 99

def choose_a_flavor(nfg)
  vs = nfg.map {|nf| nf.locked_version}.uniq
  if vs.length == 1
    nfg.first
  else
    lv = find_latest_version(vs)
    if lv and nfg.all? {|nf| nf.satisfied_with?(lv)}
      nf = nfg.first
      nf.use_specific_version(lv)
      nf
    else
      stop_by_incompatible_declarations(nfg)
    end
  end
end

#complete(current_flavor_table, locked_flavor_table, mode, groups) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/vim-flavor/facade.rb', line 89

def complete(current_flavor_table, locked_flavor_table, mode, groups)
  nfs = complete_flavors(current_flavor_table, locked_flavor_table, mode, groups, 1, 'you')

  Hash[
    nfs.group_by {|nf| nf.repo_name}.map {|repo_name, nfg|
      [repo_name, choose_a_flavor(nfg)]
    }
  ]
end

#complete_a_flavor(nf, locked_flavor_table, mode, groups, level, requirer) ⇒ Object



138
139
140
141
142
# File 'lib/vim-flavor/facade.rb', line 138

def complete_a_flavor(nf, locked_flavor_table, mode, groups, level, requirer)
  lf = locked_flavor_table[nf.repo_name]
  [complete_a_flavor_itself(nf, lf, mode, level, requirer)] +
    complete_a_flavor_dependencies(nf, locked_flavor_table, mode, groups, level)
end

#complete_a_flavor_dependencies(nf, locked_flavor_table, mode, groups, level) ⇒ Object



179
180
181
182
183
184
# File 'lib/vim-flavor/facade.rb', line 179

def complete_a_flavor_dependencies(nf, locked_flavor_table, mode, groups, level)
  nf.checkout()
  flavorfile_path = FlavorFile.path_from(nf.cached_repo_path, false)
  ff = FlavorFile.load_or_new(flavorfile_path)
  complete_flavors(ff.flavor_table, locked_flavor_table, mode, groups, level + 1, nf.repo_name)
end

#complete_a_flavor_itself(nf, lf, mode, level, requirer) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vim-flavor/facade.rb', line 152

def complete_a_flavor_itself(nf, lf, mode, level, requirer)
  trace "#{'  ' * level}Use #{nf.repo_name} ..."

  already_cached = nf.cached?
  nf.clone() unless already_cached

  e_mode = effective_mode(mode, nf.repo_name)
  if e_mode == :install and lf and nf.satisfied_with?(lf.locked_version)
    if not nf.cached_version?(lf.locked_version)
      nf.fetch()
      if not nf.cached_version?(lf.locked_version)
        raise RuntimeError, "#{nf.repo_name} is locked to #{lf.locked_version}, but no such version exists"
      end
    end
    nf.use_specific_version(lf.locked_version)
  else
    nf.fetch() if already_cached
    nf.use_appropriate_version()
  end

  nf.requirer = requirer

  trace " #{nf.locked_version}\n"

  nf
end

#complete_flavors(current_flavor_table, locked_flavor_table, mode, groups, level, requirer) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/vim-flavor/facade.rb', line 129

def complete_flavors(current_flavor_table, locked_flavor_table, mode, groups, level, requirer)
  current_flavor_table.values.map(&:dup).sort_by(&:repo_name).
  select {|nf| groups.include?(nf.group)}.
  on_failure {trace " failed\n"}.
  flat_map {|nf|
    complete_a_flavor(nf, locked_flavor_table, mode, groups, level, requirer)
  }
end

#deploy_flavors(flavors, flavors_path) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/vim-flavor/facade.rb', line 186

def deploy_flavors(flavors, flavors_path)
  trace "Deploying plugins...\n"

  a_flavors_path = File.absolute_path(flavors_path)
  lockfile_path = LockFile.path_from(a_flavors_path, false)
  deployment_memo = LockFile.load_or_new(lockfile_path)

  # To uninstall flavors which were deployed by vim-flavor 1.0.2 or
  # older, the whole deployed flavors have to be removed.  Because
  # deployment_memo is recorded since 1.0.3.
  FileUtils.rm_rf(
    [a_flavors_path],
    :secure => true
  ) if deployment_memo.flavors.empty?

  flavors.
  before_each {|f| trace "  #{f.repo_name} #{f.locked_version} ..."}.
  on_failure {trace " failed\n"}.
  each do |f|
    df = deployment_memo.flavor_table[f.repo_name]
    deployed_version = (df and df.locked_version)
    if f.locked_version == deployed_version
      trace " skipped (already deployed)\n"
    else
      FileUtils.rm_rf(
        [f.make_deployment_path(a_flavors_path)],
        :secure => true
      )
      f.deploy(a_flavors_path)
      trace " done\n"
    end
  end

  deployment_memo.flavors.each do |df|
    if flavors.all? {|f| f.repo_name != df.repo_name}
      trace "  #{df.repo_name} ..."
      FileUtils.rm_rf(
        [df.make_deployment_path(a_flavors_path)],
        :secure => true
      )
      trace " uninstalled\n"
    end
  end

  deployment_memo.flavors = flavors
  deployment_memo.save()
end

#effective_mode(mode, repo_name) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/vim-flavor/facade.rb', line 144

def effective_mode(mode, repo_name)
  return :install if
    mode == :update and
    not @target_repo_names.empty? and
    not @target_repo_names.member?(repo_name)
  mode
end

#find_latest_version(vs) ⇒ Object



115
116
117
# File 'lib/vim-flavor/facade.rb', line 115

def find_latest_version(vs)
  vs.all? {|v| PlainVersion === v} and vs.max() or nil
end

#install(vimfiles_path) ⇒ Object



11
12
13
# File 'lib/vim-flavor/facade.rb', line 11

def install(vimfiles_path)
  install_or_update(:install, vimfiles_path)
end

#install_or_update(mode, vimfiles_path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vim-flavor/facade.rb', line 54

def install_or_update(mode, vimfiles_path)
  flavorfile_path = FlavorFile.path_from(Dir.getwd(), true)
  flavorfile = FlavorFile.load_or_new(flavorfile_path)
  lockfile_path = LockFile.path_from(Dir.getwd(), true)
  lockfile = LockFile.load_or_new(lockfile_path)
  refresh_flavors(
    mode,
    flavorfile,
    lockfile,
    [:runtime],
    vimfiles_path.to_flavors_path
  )
end

#refresh_flavors(mode, flavorfile, lockfile, groups, flavors_path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vim-flavor/facade.rb', line 68

def refresh_flavors(mode, flavorfile, lockfile, groups, flavors_path)
  trace "Checking versions...\n"

  lockfile.update(
    complete(
      flavorfile.flavor_table,
      lockfile.flavor_table,
      mode,
      groups
    )
  )
  lockfile.save()

  deploy_flavors(
    lockfile.flavors,
    File.absolute_path(flavors_path)
  )

  trace "Completed.\n"
end

#stop_by_incompatible_declarations(nfg) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/vim-flavor/facade.rb', line 119

def stop_by_incompatible_declarations(nfg)
  ss = []
  ss << 'Found incompatible declarations:'
  nfg.each do |nf|
    ss << "  #{nf.repo_name} #{nf.version_constraint} is required by #{nf.requirer}"
  end
  ss << 'Please resolve the conflict.'
  abort ss.join("\n")
end

#test(files_or_dirs, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vim-flavor/facade.rb', line 20

def test(files_or_dirs, options)
  trace "-------- Preparing dependencies\n"

  flavorfile_path = FlavorFile.path_from(Dir.getwd(), true)
  flavorfile = FlavorFile.load_or_new(flavorfile_path)
  flavorfile.flavor 'kana/vim-vspec', '~> 1.5', :group => :development unless
    flavorfile.flavor_table.has_key?('kana/vim-vspec')
  lockfile_path = LockFile.path_from(Dir.getwd(), true)
  lockfile = LockFile.load_or_new(lockfile_path)
  flavors_path = Dir.getwd().to_stash_path.to_flavors_path

  refresh_flavors(
    options[:update_dependencies] ? :update : :install,
    flavorfile,
    lockfile,
    [:runtime, :development],
    flavors_path
  )

  trace "-------- Testing a Vim plugin\n"

  runner = "#{flavors_path}/#{'kana/vim-vspec'.zap}/bin/prove-vspec"
  plugin_paths = lockfile.flavors.map {|f|
    "#{flavors_path}/#{f.repo_name.zap}"
  }
  runtime_paths = ([Dir.getwd()] + plugin_paths).flat_map {|p| ['-d', p]}
  command =
    %Q{ '#{runner}' \
        #{runtime_paths.flat_map {|p| ['-d', p]}.shelljoin} \
        #{files_or_dirs.shelljoin} }
  succeeded = system('bash', '-c', command)
  exit(1) unless succeeded
end

#trace(message) ⇒ Object



234
235
236
# File 'lib/vim-flavor/facade.rb', line 234

def trace message
  print message
end

#update(vimfiles_path, target_repo_names) ⇒ Object



15
16
17
18
# File 'lib/vim-flavor/facade.rb', line 15

def update(vimfiles_path, target_repo_names)
  @target_repo_names = target_repo_names
  install_or_update(:update, vimfiles_path)
end