Class: Externals::Ext
- Inherits:
-
Object
- Object
- Externals::Ext
- Extended by:
- FileUtils
- Includes:
- FileUtils
- Defined in:
- lib/externals/ext.rb
Class Method Summary collapse
- .new_opts(main_options, sub_options) ⇒ Object
- .project_class(scm) ⇒ Object
- .project_classes ⇒ Object
- .registered_scms ⇒ Object
- .run(*arguments) ⇒ Object
Instance Method Summary collapse
- #checkout(args, options) ⇒ Object
- #configuration ⇒ Object
- #export(args, options) ⇒ Object
- #freeze(args, _options) ⇒ Object
- #help(_args, _options) ⇒ Object
- #init(args, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Ext
constructor
A new instance of Ext.
- #install(args, options) ⇒ Object
- #main_project ⇒ Object
- #print_commands(commands) ⇒ Object
- #projects ⇒ Object
- #reload_configuration ⇒ Object
- #status(_args, options) ⇒ Object
- #subproject_by_name_or_path(name) ⇒ Object (also: #subproject)
- #subprojects ⇒ Object
- #switch(args, options) ⇒ Object
- #touch_emptydirs(_args, _options) ⇒ Object
- #unfreeze(args, _options) ⇒ Object
- #uninstall(args, options) ⇒ Object
- #update(_args, options) ⇒ Object
- #update_ignore(_args, options) ⇒ Object
- #version(_args, _options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Ext
Returns a new instance of Ext.
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/externals/ext.rb', line 286 def initialize = {} super() @configuration = nil @projects = nil scm = configuration['.'] scm = scm['scm'] if scm scm || [:scm] end |
Class Method Details
.new_opts(main_options, sub_options) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/externals/ext.rb', line 95 def self.new_opts , opts = OptionParser.new( "ext [OPTIONS] <command> [repository] [-b <branch>] [path]" ) opts.summary_indent = ' ' opts.summary_width = 24 summary_width = 53 project_classes.each do |project_class| project_class.fill_in_opts(opts, , , :summary_width => summary_width) end opts.on("--scm SCM", "-s SCM", String, *"The SCM used to manage the main project. For example, '--scm svn'.".lines_by_width(summary_width) ) {|scm| [:scm] = [:scm] = scm} opts.on("--branch BRANCH", "-b BRANCH", String, *"The branch you want the subproject to checkout when doing 'ext install'".lines_by_width(summary_width) ) {|branch| [:branch] = [:branch] = branch} opts.on("--revision REVISION", "-r REVISION", String, *"The revision you want the subproject to checkout when doing 'ext install'".lines_by_width(summary_width) ) {|revision| [:revision] = [:revision] = revision} opts.on("--force_removal", "-f", String, *"When doing an uninstall of a subproject, remove it's files and subfolders, too.".lines_by_width(summary_width) ) { [:force_removal] = true } opts.on( "--workdir DIR", "-w DIR", String, *"The working directory to execute commands from. Use this if for some reason you cannot execute ext from the main project's directory (or if it's just inconvenient, such as in a script or in a Capistrano task)".lines_by_width(summary_width)) {|dir| raise "No such directory: #{dir}" unless File.exist?(dir) && File.directory?(dir) [:workdir] = dir } opts.on( "--help", *"does the same as 'ext help' If you use this with a command it will ignore the command and run help instead.".lines_by_width(summary_width) ) {[:help] = true} opts.on("--version", *"Displays the version number of externals and then exits. Same as 'ext version'".lines_by_width(summary_width)) { [:version] = true } opts end |
.project_class(scm) ⇒ Object
297 298 299 |
# File 'lib/externals/ext.rb', line 297 def self.project_class(scm) Externals.const_get("#{scm.to_s.cap_first}Project") end |
.project_classes ⇒ Object
301 302 303 304 305 |
# File 'lib/externals/ext.rb', line 301 def self.project_classes registered_scms.map do |scm| project_class(scm) end end |
.registered_scms ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/externals/ext.rb', line 211 def self.registered_scms return @registered_scms if @registered_scms @registered_scms ||= [] scmdir = File.join(File.dirname(__FILE__), 'scms') Dir.entries(scmdir).each do |file| if file =~ /^(.*)_project\.rb$/ @registered_scms << $1 end end @registered_scms end |
.run(*arguments) ⇒ Object
147 148 149 150 151 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 178 179 180 181 182 183 184 |
# File 'lib/externals/ext.rb', line 147 def self.run *arguments = {} = {} opts = new_opts , args = opts.parse(arguments) unless args.nil? || args.empty? command = args[0] args = args[1..] || [] end command &&= command.to_sym command = :help if [:help] command = :version if [:version] if !command || command.to_s == '' # :nocov: puts "hey... you didn't tell me what you want to do." puts "Try 'ext help' for a list of commands" exit 1 # :nocov: end unless COMMANDS.index command # :nocov: puts "unknown command: #{command}" puts "for a list of commands try 'ext help'" exit 1 # :nocov: end Dir.chdir([:workdir] || ".") do new().send(command, args, ) end end |
Instance Method Details
#checkout(args, options) ⇒ Object
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/externals/ext.rb', line 638 def checkout args, ||= {} repository = args[0] path = args[1] || "." main_project = do_checkout_or_export repository, path, , :checkout if path == "." path = main_project.name end Dir.chdir path do self.class.new({}).co [], {} end end |
#configuration ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/externals/ext.rb', line 268 def configuration return @configuration if @configuration file_string = '' if File.exist?('.externals') open('.externals', 'r') do |f| file_string = f.read end end @configuration = Configuration::Configuration.new(file_string) end |
#export(args, options) ⇒ Object
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/externals/ext.rb', line 655 def export args, ||= {} repository = args[0] path = args[1] || "." main_project = do_checkout_or_export repository, path, , :export if path == "." # TODO: test (or delete) this code path! # :nocov: path = main_project.name # :nocov: end Dir.chdir path do self.class.new({}).ex [], {} #args, options end end |
#freeze(args, _options) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/externals/ext.rb', line 331 def freeze args, project = subproject_by_name_or_path(args[0]) raise "No such project named #{args[0]}" unless project revision = args[1] || project.current_revision section = configuration[project.path] if section[:branch] branch = project.current_branch if branch section[:branch] = branch else # TODO: test (or delete) this code path! # :nocov: section.rm_setting :branch # :nocov: end end section[:revision] = revision configuration.write '.externals' reload_configuration subproject_by_name_or_path(args[0]).up end |
#help(_args, _options) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/externals/ext.rb', line 193 def help(_args, ) puts "There's a tutorial available at http://nopugs.com/ext-tutorial\n\n" puts "#{self.class.new_opts({}, {})}\n\n" puts "\nCommands that apply to the main project or the .externals file:" puts "#{MAIN_COMMANDS.join(', ')}\n\n" print_commands(MAIN_COMMANDS_HASH) puts "\nCommands that apply to the main project and all subprojects:" puts "#{FULL_COMMANDS.join(', ')}\n\n" print_commands(FULL_COMMANDS_HASH) puts "\nCommands that only apply to the subprojects:" puts "#{SHORT_COMMANDS.join(', ')}\n\n" print_commands(SHORT_COMMANDS_HASH) end |
#init(args, options = {}) ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/externals/ext.rb', line 675 def init args, = {} # :nocov: raise ".externals already exists" if File.exist?('.externals') # :nocov: scm = [:scm] if !scm possible_project_classes = self.class.project_classes.select(&:detected?) raise "Could not determine this project's scm" if possible_project_classes.empty? if possible_project_classes.size > 1 # :nocov: raise "This project appears to be managed by multiple SCMs: #{ possible_project_classes.join(',')} Please explicitly declare the SCM (using --git or --svn, or, by creating .externals manually" # :nocov: end scm = possible_project_classes.first.scm end config = Configuration::Configuration.new_empty raise ".externals already exists" if File.exist?('.externals') config.add_empty_section '.' # TODO: If we are using subversion, we should warn about not setting a branch if scm == "svn" if [:branch] config['.'][:repository] = SvnProject.extract_repository( SvnProject.info_url, [:branch] ) elsif args[0] # TODO: test (or delete) this code path! # :nocov: config['.'][:repository] = args[0].strip # :nocov: end end config['.'][:scm] = scm config.write '.externals' reload_configuration end |
#install(args, options) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/externals/ext.rb', line 379 def install args, if !File.exist?('.externals') # :nocov: warn "This project does not appear to be managed by externals. Try 'ext init' first" exit NO_EXTERNALS_FILE # :nocov: end repository = args[0] path = args[1] = .dup scm = [:scm] scm ||= infer_scm(repository) unless scm # :nocov: warn "Unable to determine SCM from the repository name. You need to either specify the scm used to manage the subproject that you are installing. Use an option to specify it (such as --git or --svn)" exit COULD_NOT_DETERMINE_SCM # :nocov: end project = self.class.project_class(scm).new(:repository => repository, :path => path, :scm => scm) path = project.path raise "no path" unless path raise "already exists" if configuration[path] project.branch = [:branch] if [:branch] project.revision = [:revision] if [:revision] attributes = project.attributes.dup attributes.delete(:path) configuration[path] = project.attributes configuration.write '.externals' reload_configuration project.co update_ignore args, end |
#main_project ⇒ Object
264 265 266 |
# File 'lib/externals/ext.rb', line 264 def main_project projects.detect(&:main_project?) end |
#print_commands(commands) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/externals/ext.rb', line 186 def print_commands(commands) commands.each do |command| puts Command.new(*command) end puts end |
#projects ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/externals/ext.rb', line 227 def projects return @projects if @projects @projects = [] configuration.sections.each do |section| @projects << Ext.project_class(section[:scm] || infer_scm(section[:repository])).new( section.attributes.merge(:path => section.title)) end #let's set the parents of these projects main = main_project subprojects.each {|subproject| subproject.parent = main} @projects end |
#reload_configuration ⇒ Object
280 281 282 283 284 |
# File 'lib/externals/ext.rb', line 280 def reload_configuration @configuration = nil @projects = nil configuration end |
#status(_args, options) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/externals/ext.rb', line 508 def status _args, ||= {} scm = [:scm] if !scm scm ||= configuration['.'] scm &&= scm['scm'] end if !scm # TODO: test (or delete) this code path! # :nocov: possible_project_classes = self.class.project_classes.select(&:detected?) raise "Could not determine this projects scm" if possible_project_classes.empty? if possible_project_classes.size > 1 raise "This project appears to be managed by multiple SCMs: #{ possible_project_classes.join(',')} Please explicitly declare the SCM (by using --git or --svn, or, by creating the .externals file manually" end scm = possible_project_classes.first.scm # :nocov: end unless scm # :nocov: raise "You need to either specify the scm as the first line in .externals, or use an option to specify it (such as --git or --svn)" # :nocov: end project = main_project project.scm ||= scm project.st self.class.new({}).st [], {} #args, options end |
#subproject_by_name_or_path(name) ⇒ Object Also known as: subproject
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/externals/ext.rb', line 241 def subproject_by_name_or_path name name = name.strip project = subprojects.detect {|p| p.path.strip == name} project ||= subprojects.detect do |p| File.split(p.path).last.strip == name end project || subprojects.detect do |p| # TODO: test (or delete) this code path! # :nocov: p.name == name # :nocov: end end |
#subprojects ⇒ Object
256 257 258 259 260 261 262 |
# File 'lib/externals/ext.rb', line 256 def subprojects s = [] projects.each do |project| s << project unless project.main_project? end s end |
#switch(args, options) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/externals/ext.rb', line 548 def switch args, branch = args[0] ||= {} scm = [:scm] if !scm scm ||= configuration['.'] scm &&= scm['scm'] end if !scm # TODO: test (or delete) this code path! # :nocov: possible_project_classes = self.class.project_classes.select(&:detected?) raise "Could not determine this projects scm" if possible_project_classes.empty? if possible_project_classes.size > 1 raise "This project appears to be managed by multiple SCMs: #{ possible_project_classes.join(',')} Please explicitly declare the SCM (by using --git or --svn, or, by creating the .externals file manually" end scm = possible_project_classes.first.scm # :nocov: end unless scm # :nocov: raise "You need to either specify the scm as the first line in .externals, or use an option to specify it (such as --git or --svn)" # :nocov: end old_config = configuration project = main_project project.scm ||= scm if project.current_branch == branch puts "Already on branch #{branch}" else project.switch branch, project.up reload_configuration #update subprojects self.class.new({}).up [], {} #args, options removed_project_paths = old_config.removed_project_paths( configuration ).select{|path| File.exist?(path)} if !removed_project_paths.empty? puts "WARNING: The following subprojects are no longer being maintained in the .externals file. You might want to remove them. You can copy and paste the commands below if you actually wish to delete them." removed_project_paths.each do |path| if File.exist?(path) puts " rm -r #{path}" end end end end end |
#touch_emptydirs(_args, _options) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/externals/ext.rb', line 474 def touch_emptydirs _args, require 'find' excludes = ['.', '..', '.svn', '.git'] excludes.dup.each do |exclude| excludes << "./#{exclude}" end paths = [] Find.find('.') do |f| if File.directory?(f) excluded = false File.split(f).each do |part| excluded ||= excludes.index(part) end if !excluded && (Dir.entries(f) - excludes).empty? paths << f end end end paths.each do |p| # TODO: is there not an easier way to touch a file?? # rubocop:disable Style/FileOpen f = File.open(File.join(p, ".emptydir"), "w") # rubocop:enable Style/FileOpen ensure f.close end end |
#unfreeze(args, _options) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/externals/ext.rb', line 358 def unfreeze args, project = subproject_by_name_or_path(args[0]) raise "No such project named #{args[0]}" unless project section = configuration[project.path] unless section[:revision] # :nocov: puts "Uhh... #{project.name} wasn't frozen, so I can't unfreeze it." exit 1 # :nocov: end section.rm_setting :revision configuration.write '.externals' reload_configuration subproject_by_name_or_path(args[0]).up end |
#uninstall(args, options) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/externals/ext.rb', line 427 def uninstall args, unless File.exist?('.externals') # :nocov: raise "Hmm... there's no .externals file in this directory." # :nocov: end project = subproject_by_name_or_path(args[0]) raise "No such project named #{args[0]}" unless project main_project.drop_from_ignore project.path configuration.remove_section(project.path) configuration.write '.externals' reload_configuration if [:force_removal] Dir.chdir File.dirname(project.path) do rm_rf File.basename(project.path) end end end |
#update(_args, options) ⇒ Object
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'lib/externals/ext.rb', line 615 def update _args, ||= {} scm = [:scm] if !scm scm ||= configuration['.'] scm &&= scm['scm'] end unless scm # :nocov: raise "You need to either specify the scm as the first line in .externals, or use an option to specify it (such as --git or --svn)" # :nocov: end project = main_project project.scm ||= scm project.up self.class.new({}).up [], {} #args, options end |
#update_ignore(_args, options) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/externals/ext.rb', line 451 def update_ignore _args, scm = configuration['.'] scm = scm['scm'] if scm scm ||= [:scm] unless scm # :nocov: raise "You need to either specify the scm as the first line in .externals (for example, scm = git), " \ "or use an option to specify it (such as --git or --svn)" # :nocov: end project = self.class.project_class(scm).new(:path => ".") raise "only makes sense for main project" unless project.main_project? subprojects.each do |subproject| project.update_ignore subproject.path end end |
#version(_args, _options) ⇒ Object
723 724 725 |
# File 'lib/externals/ext.rb', line 723 def version(_args, ) puts Externals::VERSION end |