Class: Ronin::Repository
- Inherits:
-
Object
- Object
- Ronin::Repository
- Includes:
- DataPaths, Model, Model::HasAuthors, Model::HasLicense
- Defined in:
- lib/ronin/repository.rb
Overview
Represents a container for user scripts and miscallaneous code, that can be distributed over common SCMs (Git, Mercurial (Hg), SubVersion (SVN), Rsync).
Constant Summary collapse
- LOCAL_DOMAIN =
The default domain that repositories are added from
'localhost'
- METADATA_FILE =
Repository metadata file name
'ronin.yml'
- BIN_DIR =
Repository
bin/
directory 'bin'
- LIB_DIR =
Repository
lib/
directory 'lib'
- INIT_FILE =
The
init.rb
file to load from the LIB_DIR 'init.rb'
- DATA_DIR =
Repository
data/
directory 'data'
- SCRIPT_DIRS =
Directories containing Scripts
%w[scripts cache]
Instance Attribute Summary collapse
-
#bin_dir ⇒ Object
readonly
The
bin/
directory. -
#data_dir ⇒ Object
readonly
The
data/
directory. -
#lib_dir ⇒ Object
readonly
The
lib/
directory. -
#script_dirs ⇒ Object
readonly
Directories containing Scripts.
Class Method Summary collapse
-
.activate! ⇒ Array<Repository>
private
Activates all installed or added repositories.
-
.add!(options = {}) ⇒ Repository
private
Adds an Repository with the given options.
-
.deactivate! ⇒ Array<Repository>
private
De-activates all installed or added repositories.
-
.find(name) ⇒ Repository
private
Searches for the Repository with a given name, and potentially installed from the given domain.
-
.install!(options = {}) ⇒ Repository
private
Installs an repository.
-
.uninstall!(name) ⇒ nil
private
Uninstalls the repository with the given name or domain.
-
.update! {|repo| ... } ⇒ Object
private
Updates all repositories.
Instance Method Summary collapse
-
#activate! ⇒ Object
private
Activates the repository by adding the #lib_dir to the
$LOAD_PATH
global variable. -
#activated? ⇒ Boolean
private
Determines if the repository has been activated.
-
#cache_scripts! ⇒ Repository
private
Clears the #script_paths and re-saves the cached files within the
cache/
directory. -
#clean_scripts! ⇒ Repository
private
Deletes any #script_paths associated with the repository.
-
#deactivate! ⇒ Object
private
De-activates the repository by removing the #lib_dir from the
$LOAD_PATH
global variable. -
#each_script {|path| ... } ⇒ Enumerator
private
All paths within the
cache/
directory of the repository. -
#executables ⇒ Array<String>
private
The executable scripts in the
bin/
directory. -
#find_script(sub_path) ⇒ Script::Path?
private
Finds a cached script.
-
#initialize(attributes = {}) {|repo| ... } ⇒ Repository
constructor
private
Creates a new Repository object.
-
#initialize_metadata ⇒ Object
protected
private
Loads the metadata from METADATA_FILE within the repository.
-
#local? ⇒ Boolean
private
Determines if the repository was added locally.
-
#remote? ⇒ Boolean
private
Determines if the repository was installed remotely.
-
#sync_scripts! ⇒ Repository
private
Syncs the #script_paths of the repository, and adds any new cached files.
-
#to_s ⇒ String
Converts the repository to a String.
-
#uninstall! {|repo| ... } ⇒ Repository
private
Deletes the contents of the repository.
-
#update! {|repo| ... } ⇒ Repository
private
Updates the repository, reloads it's metadata and syncs the cached files of the repository.
Methods included from Model::HasLicense
Methods included from Model::HasAuthors
Methods included from Model
Constructor Details
#initialize(attributes = {}) {|repo| ... } ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new Ronin::Repository object.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ronin/repository.rb', line 146 def initialize(attributes={}) super(attributes) @bin_dir = self.path.join(BIN_DIR) @lib_dir = self.path.join(LIB_DIR) @data_dir = self.path.join(DATA_DIR) @script_dirs = SCRIPT_DIRS.map { |dir| self.path.join(dir) } @activated = false yield self if block_given? end |
Instance Attribute Details
#bin_dir ⇒ Object (readonly)
The bin/
directory
107 108 109 |
# File 'lib/ronin/repository.rb', line 107 def bin_dir @bin_dir end |
#data_dir ⇒ Object (readonly)
The data/
directory
113 114 115 |
# File 'lib/ronin/repository.rb', line 113 def data_dir @data_dir end |
#lib_dir ⇒ Object (readonly)
The lib/
directory
110 111 112 |
# File 'lib/ronin/repository.rb', line 110 def lib_dir @lib_dir end |
#script_dirs ⇒ Object (readonly)
Directories containing Scripts.
116 117 118 |
# File 'lib/ronin/repository.rb', line 116 def script_dirs @script_dirs end |
Class Method Details
.activate! ⇒ Array<Repository>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Activates all installed or added repositories.
385 386 387 |
# File 'lib/ronin/repository.rb', line 385 def Repository.activate! Repository.each { |repo| repo.activate! } end |
.add!(options = {}) ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds an Repository with the given options.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/ronin/repository.rb', line 220 def Repository.add!(={}) unless .has_key?(:path) raise(ArgumentError,"the :path option was not given") end path = Pathname.new([:path]). unless path.directory? raise(RepositoryNotFound,"Repository #{path} cannot be found") end if Repository.count(:path => path) > 0 raise(DuplicateRepository,"a Repository at the path #{path} was already added") end # create the repository repo = Repository.new(.merge( :path => path, :installed => false, :domain => LOCAL_DOMAIN )) if Repository.count(:name => repo.name, :domain => repo.domain) > 0 raise(DuplicateRepository,"the Repository #{repo} already exists in the database") end # save the repository if repo.save # cache any files from within the `cache/` directory of the # repository repo.cache_scripts! end return repo end |
.deactivate! ⇒ Array<Repository>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
De-activates all installed or added repositories.
401 402 403 |
# File 'lib/ronin/repository.rb', line 401 def Repository.deactivate! Repository.reverse_each { |repo| repo.deactivate! } end |
.find(name) ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Searches for the Repository with a given name, and potentially installed from the given domain.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ronin/repository.rb', line 184 def Repository.find(name) name, domain = name.to_s.split('/',2) query = {:name => name} query[:domain] = domain if domain unless (repo = Repository.first(query)) if domain raise(RepositoryNotFound,"Repository #{name.dump} from domain #{domain.dump} cannot be found") else raise(RepositoryNotFound,"Repository #{name.dump} cannot be found") end end return repo end |
.install!(options = {}) ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Installs an repository.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/ronin/repository.rb', line 287 def Repository.install!(={}) unless [:uri] raise(ArgumentError,":uri must be passed to Repository.install") end remote_repo = Pullr::RemoteRepository.new() name = remote_repo.name domain = if remote_repo.uri.scheme remote_repo.uri.host else # Use a regexp to pull out the host-name, if the URI # lacks a scheme. remote_repo.uri.to_s.match(/\@([^@:\/]+)/)[1] end if Repository.count(:name => name, :domain => domain) > 0 raise(DuplicateRepository,"a Repository already exists with the name #{name.dump} from domain #{domain.dump}") end path = File.join(Config::REPOS_DIR,name,domain) # pull down the remote repository local_repo = remote_repo.pull(path) # add the new remote repository repo = Repository.new( :path => path, :scm => local_repo.scm, :uri => remote_repo.uri, :installed => true, :name => name, :domain => domain ) # save the repository if repo.save # cache any files from within the `cache/` directory of the # repository repo.cache_scripts! end return repo end |
.uninstall!(name) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Uninstalls the repository with the given name or domain.
369 370 371 |
# File 'lib/ronin/repository.rb', line 369 def Repository.uninstall!(name) Repository.find(name).uninstall! end |
.update! {|repo| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Updates all repositories.
344 345 346 347 348 349 350 351 |
# File 'lib/ronin/repository.rb', line 344 def Repository.update! Repository.each do |repo| # update the repositories contents repo.update! yield repo if block_given? end end |
Instance Method Details
#activate! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Activates the repository by adding the #lib_dir to the $LOAD_PATH
global variable.
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/ronin/repository.rb', line 497 def activate! # add the data/ directory register_data_path(@data_dir) if @data_dir.directory? if @lib_dir.directory? # ensure all paths added to $LOAD_PATH are Strings path = @lib_dir.to_s $LOAD_PATH << path unless $LOAD_PATH.include?(path) end # load the lib/init.rb file init_path = self.path.join(LIB_DIR,INIT_FILE) require init_path if init_path.file? @activated = true return true end |
#activated? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if the repository has been activated.
487 488 489 |
# File 'lib/ronin/repository.rb', line 487 def activated? @activated == true end |
#cache_scripts! ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears the #script_paths and re-saves the cached files within the
cache/
directory.
561 562 563 564 565 566 567 568 569 |
# File 'lib/ronin/repository.rb', line 561 def cache_scripts! clean_scripts! each_script do |path| self.script_paths.new(:path => path).cache end return self end |
#clean_scripts! ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes any #script_paths associated with the repository.
617 618 619 620 |
# File 'lib/ronin/repository.rb', line 617 def clean_scripts! self.script_paths.clear return self end |
#deactivate! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
De-activates the repository by removing the #lib_dir from the
$LOAD_PATH
global variable.
522 523 524 525 526 527 528 529 |
# File 'lib/ronin/repository.rb', line 522 def deactivate! unregister_data_paths $LOAD_PATH.delete(@lib_dir.to_s) @activated = false return true end |
#each_script {|path| ... } ⇒ Enumerator
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All paths within the cache/
directory of the repository.
471 472 473 474 475 476 477 |
# File 'lib/ronin/repository.rb', line 471 def each_script(&block) return enum_for(:each_script) unless block @script_dirs.each do |dir| Pathname.glob(dir.join('**','*.rb'),&block) end end |
#executables ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The executable scripts in the bin/
directory.
443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/ronin/repository.rb', line 443 def executables scripts = [] if @bin_dir.directory? @bin_dir.entries.each do |path| scripts << path.basename.to_s if path.file? end end return scripts end |
#find_script(sub_path) ⇒ Script::Path?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Finds a cached script.
544 545 546 547 548 |
# File 'lib/ronin/repository.rb', line 544 def find_script(sub_path) paths = @script_dirs.map { |dir| File.join(dir,sub_path) } return script_paths.first(:path => paths) end |
#initialize_metadata ⇒ Object (protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Loads the metadata from METADATA_FILE within the repository.
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
# File 'lib/ronin/repository.rb', line 711 def = self.path.join(METADATA_FILE) self.title = self.name self.description = nil self.license = nil self.source = self.uri self.website = self.source self..clear if File.file?() = YAML.load_file() if (title = ['title']) self.title = title end if (description = ['description']) self.description = description end if (license = ['license']) self.license = License.first_or_predefined(:name => license) end if (uri = ['uri']) self.uri ||= uri end if (source = ['source']) self.source = source end if (website = ['website']) self.website = website end case ['authors'] when Hash ['authors'].each do |name,email| self. << Author.first_or_new(:name => name, :email => email) end when Array ['authors'].each do |name| self. << Author.first_or_new(:name => name) end end end return self end |
#local? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if the repository was added locally.
415 416 417 |
# File 'lib/ronin/repository.rb', line 415 def local? self.domain == LOCAL_DOMAIN end |
#remote? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if the repository was installed remotely.
429 430 431 |
# File 'lib/ronin/repository.rb', line 429 def remote? self.domain != LOCAL_DOMAIN end |
#sync_scripts! ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Syncs the #script_paths of the repository, and adds any new cached files.
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/ronin/repository.rb', line 582 def sync_scripts! # activates the repository before caching it's objects activate! new_paths = each_script.to_a self.script_paths.each do |script_path| # filter out pre-existing paths within the `cached/` directory new_paths.delete(script_path.path) # sync the cached file and catch any exceptions script_path.sync end # cache the new paths within the `cache/` directory new_paths.each do |path| self.script_paths.new(:path => path).cache end # deactivates the repository deactivate! return self end |
#to_s ⇒ String
Converts the repository to a String.
700 701 702 |
# File 'lib/ronin/repository.rb', line 700 def to_s "#{self.name}/#{self.domain}" end |
#uninstall! {|repo| ... } ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes the contents of the repository.
679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/ronin/repository.rb', line 679 def uninstall! deactivate! FileUtils.rm_rf(self.path) if self.installed? # destroy any cached files first clean_scripts! # remove the repository from the database destroy if saved? yield self if block_given? return self end |
#update! {|repo| ... } ⇒ Repository
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Updates the repository, reloads it's metadata and syncs the cached files of the repository.
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/ronin/repository.rb', line 640 def update! local_repo = Pullr::LocalRepository.new( :path => self.path, :scm => self.scm ) # only update if we have a repository local_repo.update(self.uri) # re-initialize the metadata # save the repository if save # syncs the cached files of the repository sync_scripts! end yield self if block_given? return self end |