Class: Externals::Ext
- Inherits:
-
Object
- Object
- Externals::Ext
- Extended by:
- FileUtils
- Includes:
- FileUtils
- Defined in:
- lib/externals/ext.rb
Instance Attribute Summary collapse
-
#path_calculator ⇒ Object
Returns the value of attribute path_calculator.
Class Method Summary collapse
- .new_opts(main_options, sub_options) ⇒ Object
- .project_class(scm) ⇒ Object
- .project_classes ⇒ Object
- .project_type_detector(name) ⇒ Object
- .project_type_files ⇒ Object
- .project_types ⇒ 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
- #install_project_type(name) ⇒ 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.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/externals/ext.rb', line 310 def initialize = {} super() scm = configuration['.'] scm = scm['scm'] if scm scm ||= [:scm] type = configuration['.'] type = type['type'] if type type ||= [:type] if type install_project_type type else possible_project_types = self.class.project_types.select do |project_type| self.class.project_type_detector(project_type).detected? end if possible_project_types.size > 1 raise "We found multiple project types that this could be: #{possible_project_types.join(',')} Please use the --type option to tell ext which to use." else possible_project_types.each do |project_type| install_project_type project_type end end end end |
Instance Attribute Details
#path_calculator ⇒ Object
Returns the value of attribute path_calculator.
104 105 106 |
# File 'lib/externals/ext.rb', line 104 def path_calculator @path_calculator end |
Class Method Details
.new_opts(main_options, sub_options) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 |
# File 'lib/externals/ext.rb', line 126 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("--type TYPE", "-t TYPE", String, *"The type of project the main project is. For example, 'rails'.".lines_by_width(summary_width) ) {|type| [:scm] = [:type] = type} 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) ) {|branch| [: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.exists?(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
341 342 343 |
# File 'lib/externals/ext.rb', line 341 def self.project_class(scm) Externals.module_eval("#{scm.to_s.cap_first}Project", __FILE__, __LINE__) end |
.project_classes ⇒ Object
345 346 347 348 349 350 351 352 |
# File 'lib/externals/ext.rb', line 345 def self.project_classes retval = [] registered_scms.each do |scm| retval << project_class(scm) end retval end |
.project_type_detector(name) ⇒ Object
756 757 758 |
# File 'lib/externals/ext.rb', line 756 def self.project_type_detector name Externals.module_eval("#{name.classify}Detector", __FILE__, __LINE__) end |
.project_type_files ⇒ Object
116 117 118 119 120 |
# File 'lib/externals/ext.rb', line 116 def self.project_type_files project_types.map do |project_type| "#{File.join(PROJECT_TYPES_DIRECTORY, project_type)}.rb" end end |
.project_types ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/externals/ext.rb', line 106 def self.project_types types = Dir.entries(PROJECT_TYPES_DIRECTORY).select do |file| file =~ /\.rb$/ end types.map do |type| /^(.*)\.rb$/.match(type)[1] end end |
.registered_scms ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/externals/ext.rb', line 240 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
180 181 182 183 184 185 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 |
# File 'lib/externals/ext.rb', line 180 def self.run *arguments = {} = {} opts = new_opts , args = opts.parse(arguments) unless args.nil? || args.empty? command = args[0] args = args[1..(args.size - 1)] || [] end command &&= command.to_sym command = :help if [:help] command = :version if [:version] if !command || command.to_s == '' puts "hey... you didn't tell me what you want to do." puts "Try 'ext help' for a list of commands" exit end unless COMMANDS.index command puts "unknown command: #{command}" puts "for a list of commands try 'ext help'" exit end Dir.chdir([:workdir] || ".") do self.new().send(command, args, ) end end |
Instance Method Details
#checkout(args, options) ⇒ Object
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/externals/ext.rb', line 657 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 [], {} #args, options end end |
#configuration ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/externals/ext.rb', line 292 def configuration return @configuration if @configuration file_string = '' if File.exists? '.externals' open('.externals', 'r') do |f| file_string = f.read end end @configuration = Configuration::Configuration.new(file_string) end |
#export(args, options) ⇒ Object
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/externals/ext.rb', line 674 def export args, ||= {} repository = args[0] path = args[1] || "." main_project = do_checkout_or_export repository, path, , :export if path == "." path = main_project.name end Dir.chdir path do self.class.new({}).ex [], {} #args, options end end |
#freeze(args, options) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/externals/ext.rb', line 376 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 section.rm_setting :branch end end section[:revision] = revision configuration.write '.externals' reload_configuration subproject_by_name_or_path(args[0]).up end |
#help(args, options) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/externals/ext.rb', line 223 def help(args, ) puts "There's a tutorial available at http://nopugs.com/ext-tutorial\n\n" puts "#{self.class.new_opts({},{}).to_s}\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
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 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 |
# File 'lib/externals/ext.rb', line 691 def init args, = {} raise ".externals already exists" if File.exists? '.externals' scm = [:scm] type = [:type] if !scm possible_project_classes = self.class.project_classes.select do |project_class| project_class.detected? end raise "Could not determine this project's 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.map(&:to_s).join(',')} Please explicitly declare the SCM (using --git or --svn, or, by creating .externals manually" end scm = possible_project_classes.first.scm end if !type possible_project_types = self.class.project_types.select do |project_type| self.class.project_type_detector(project_type).detected? end if possible_project_types.size > 1 raise "We found multiple project types that this could be: #{possible_project_types.join(',')} Please use the --type option to tell ext which to use." elsif possible_project_types.size == 0 puts "WARNING: We could not automatically determine the project type. Be sure to specify paths when adding subprojects to your .externals file" else type = possible_project_types.first end end config = Configuration::Configuration.new_empty raise ".externals already exists" if File.exists?('.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] config['.'][:repository] = args[0].strip end end config['.'][:scm] = scm config['.'][:type] = type if type config.write '.externals' reload_configuration end |
#install(args, options) ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/externals/ext.rb', line 419 def install args, if !File.exists? '.externals' STDERR.puts "This project does not appear to be managed by externals. Try 'ext init' first" exit NO_EXTERNALS_FILE end repository = args[0] path = args[1] = .dup scm = [:scm] scm ||= infer_scm(repository) unless scm STDERR.puts "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 end project = self.class.project_class(scm).new(:repository => repository, :path => path || path_calculator.new, :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 |
#install_project_type(name) ⇒ Object
760 761 762 |
# File 'lib/externals/ext.rb', line 760 def install_project_type name self.path_calculator = Externals.module_eval("#{name.classify}ProjectType::DefaultPathCalculator", __FILE__, __LINE__) end |
#main_project ⇒ Object
288 289 290 |
# File 'lib/externals/ext.rb', line 288 def main_project projects.detect {|p| p.main_project?} end |
#print_commands(commands) ⇒ Object
216 217 218 219 220 221 |
# File 'lib/externals/ext.rb', line 216 def print_commands(commands) commands.each do |command| puts Command.new(*command) end puts end |
#projects ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/externals/ext.rb', line 255 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
304 305 306 307 308 |
# File 'lib/externals/ext.rb', line 304 def reload_configuration @configuration = nil @projects = nil configuration end |
#status(args, options) ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/externals/ext.rb', line 535 def status args, ||= {} scm = [:scm] if !scm scm ||= configuration['.'] scm &&= scm['scm'] end if !scm possible_project_classes = self.class.project_classes.select do |project_class| project_class.detected? end 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.map(&:to_s).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 end unless scm 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)" 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
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/externals/ext.rb', line 268 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| p.name == name end end |
#subprojects ⇒ Object
280 281 282 283 284 285 286 |
# File 'lib/externals/ext.rb', line 280 def subprojects s = [] projects.each do |project| s << project unless project.main_project? end s end |
#switch(args, options) ⇒ Object
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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
# File 'lib/externals/ext.rb', line 572 def switch args, branch = args[0] ||= {} scm = [:scm] if !scm scm ||= configuration['.'] scm &&= scm['scm'] end if !scm possible_project_classes = self.class.project_classes.select do |project_class| project_class.detected? end 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.map(&:to_s).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 end unless scm 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)" 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.exists?(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.exists? path puts " rm -r #{path}" end end end end end |
#touch_emptydirs(args, options) ⇒ Object
504 505 506 507 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 |
# File 'lib/externals/ext.rb', line 504 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| exclude ||= excludes.index(part) end if !excluded && ((Dir.entries(f) - excludes).size == 0) paths << f end end end paths.each do |p| open(File.join(p,".emptydir"), "w").close end end |
#unfreeze(args, options) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/externals/ext.rb', line 400 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] puts "Uhh... #{project.name} wasn't frozen, so I can't unfreeze it." exit end section.rm_setting :revision configuration.write '.externals' reload_configuration subproject_by_name_or_path(args[0]).up end |
#uninstall(args, options) ⇒ Object
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/externals/ext.rb', line 463 def uninstall args, raise "Hmm... there's no .externals file in this directory." if !File.exists? '.externals' 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
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/externals/ext.rb', line 636 def update args, ||= {} scm = [:scm] if !scm scm ||= configuration['.'] scm &&= scm['scm'] end unless scm 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)" end project = main_project project.scm ||= scm project.up self.class.new({}).up [], {} #args, options end |
#update_ignore(args, options) ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/externals/ext.rb', line 484 def update_ignore args, scm = configuration['.'] scm = scm['scm'] if scm scm ||= [:scm] unless scm 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)" 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 |