# encoding: utf-8 module LocalPac module Actions class AddExamplesToLocalStorage private attr_reader :repo, :files, :options, :vcs_engine, :storage_path public def initialize(storage_path, options = {}, vcs_engine = GitRepository) @options = options @vcs_engine = vcs_engine @storage_path = storage_path end def run @files = Dir.glob(::File.expand_path('../../../../files/examples/*', __FILE__)).collect do |f| OpenStruct.new(repo_path: "examples/#{::File.basename(f)}", name: "examples::#{::File.basename(f).sub(/\.[^.]+$/, '')}".to_sym, content: ::File.read(f)) end @files << OpenStruct.new(repo_path: 'proxy.pac', name: :proxy, content: ::File.read(::File.expand_path('../../../../files/proxy.pac', __FILE__))) begin @repo = vcs_engine.new(storage_path) rescue Rugged::OSError raise Exceptions::RepositoryDoesNotExist, "Sorry, but the repository at #{storage_path} does not exist" unless ::Dir.exists? storage_path end if need_to_run? || options[:force] == true files.each { |f| repo.add_content(f.repo_path, f.content) } repo.add_content('proxy.pac', ::File.read(::File.expand_path('../../../../files/proxy.pac', __FILE__))) else LocalPac.ui_logger.warn "Example files already exists. Do not create them again!." end end private def need_to_run? files.any? { |f| repo.find_file(f.name).nil? } end end end end