Class: Externals::Ext

Inherits:
Object
  • Object
show all
Extended by:
FileUtils
Includes:
FileUtils
Defined in:
lib/externals/ext.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Ext

Returns a new instance of Ext.



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
340
341
342
343
# File 'lib/externals/ext.rb', line 311

def initialize options = {}
  super()

  @configuration = nil
  @projects = nil

  scm = configuration['.']
  scm = scm['scm'] if scm
  scm ||= options[:scm]

  type = configuration['.']
  type = type['type'] if type

  type ||= options[: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_calculatorObject

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 main_options, sub_options
  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, main_options, sub_options,
      :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| sub_options[:type] = main_options[: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| sub_options[:scm] = main_options[: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| sub_options[:branch] = main_options[: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| sub_options[:revision] = main_options[: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| sub_options[: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)
    main_options[: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)
  ) {main_options[:help] = true}
  opts.on("--version", *"Displays the version number of externals and then exits.
    Same as 'ext version'".lines_by_width(summary_width)) {
    main_options[:version] = true
  }
  opts
end

.project_class(scm) ⇒ Object



345
346
347
# File 'lib/externals/ext.rb', line 345

def self.project_class(scm)
  Externals.module_eval("#{scm.to_s.cap_first}Project", __FILE__, __LINE__)
end

.project_classesObject



349
350
351
352
353
354
355
356
# File 'lib/externals/ext.rb', line 349

def self.project_classes
  retval = []
  registered_scms.each do |scm|
    retval << project_class(scm)
  end

  retval
end

.project_type_detector(name) ⇒ Object



762
763
764
# File 'lib/externals/ext.rb', line 762

def self.project_type_detector name
  Externals.module_eval("#{name.classify}Detector", __FILE__, __LINE__)
end

.project_type_filesObject



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_typesObject



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_scmsObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/externals/ext.rb', line 241

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
  main_options = {}
  sub_options = {}

  opts = new_opts main_options, sub_options

  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 main_options[:help]
  command = :version if main_options[: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(main_options[:workdir] || ".") do
    self.new(main_options).send(command, args, sub_options)
  end
end

Instance Method Details

#checkout(args, options) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/externals/ext.rb', line 663

def checkout args, options
  options ||= {}

  repository = args[0]
  path = args[1] || "."

  main_project = do_checkout_or_export repository, path, options, :checkout

  if path == "."
    path = main_project.name
  end

  Dir.chdir path do
    self.class.new({}).co [], {} #args, options
  end
end

#configurationObject



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/externals/ext.rb', line 293

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



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/externals/ext.rb', line 680

def export args, options
  options ||= {}

  repository = args[0]
  path = args[1] || "."

  main_project = do_checkout_or_export repository, path, options, :export

  if path == "."
    path = main_project.name
  end

  Dir.chdir path do
    self.class.new({}).ex [], {} #args, options
  end
end

#freeze(args, options) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/externals/ext.rb', line 380

def freeze args, options
  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, options)
  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



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
751
752
753
754
755
756
# File 'lib/externals/ext.rb', line 697

def init args, options = {}
  raise ".externals already exists" if File.exist?('.externals')

  scm = options[:scm]
  type = options[: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.exist?('.externals')

  config.add_empty_section '.'

  # TODO: If we are using subversion, we should warn about not setting a branch
  if scm == "svn"
    if options[:branch]
      config['.'][:repository] = SvnProject.extract_repository(
        SvnProject.info_url,
        options[: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



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
462
463
464
465
# File 'lib/externals/ext.rb', line 423

def install args, options
  if !File.exist?('.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]

  orig_options = options.dup

  scm = options[: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 = options[:branch] if options[:branch]
  project.revision = options[:revision] if options[:revision]

  attributes = project.attributes.dup
  attributes.delete(:path)
  configuration[path] = project.attributes
  configuration.write '.externals'
  reload_configuration

  project.co

  update_ignore args, orig_options
end

#install_project_type(name) ⇒ Object



766
767
768
# File 'lib/externals/ext.rb', line 766

def install_project_type name
  self.path_calculator = Externals.module_eval("#{name.classify}ProjectType::DefaultPathCalculator", __FILE__, __LINE__)
end

#main_projectObject



289
290
291
# File 'lib/externals/ext.rb', line 289

def main_project
  projects.detect {|p| p.main_project?}
end


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

#projectsObject



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/externals/ext.rb', line 256

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_configurationObject



305
306
307
308
309
# File 'lib/externals/ext.rb', line 305

def reload_configuration
  @configuration = nil
  @projects = nil
  configuration
end

#status(args, options) ⇒ Object



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
571
572
573
574
575
576
# File 'lib/externals/ext.rb', line 541

def status args, options
  options ||= {}
  scm = options[: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



269
270
271
272
273
274
275
276
277
278
# File 'lib/externals/ext.rb', line 269

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

#subprojectsObject



281
282
283
284
285
286
287
# File 'lib/externals/ext.rb', line 281

def subprojects
  s = []
  projects.each do |project|
    s << project unless project.main_project?
  end
  s
end

#switch(args, options) ⇒ Object



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
635
636
637
638
639
640
# File 'lib/externals/ext.rb', line 578

def switch args, options
  branch = args[0]

  options ||= {}
  scm = options[: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, options
    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



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
# File 'lib/externals/ext.rb', line 510

def touch_emptydirs args, options
  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



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/externals/ext.rb', line 404

def unfreeze args, options
  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



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/externals/ext.rb', line 467

def uninstall args, options
  unless File.exist?('.externals')
    raise "Hmm... there's no .externals file in this directory."
  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 options[:force_removal]
    Dir.chdir File.dirname(project.path) do
      rm_rf File.basename(project.path)
    end
  end
end

#update(args, options) ⇒ Object



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/externals/ext.rb', line 642

def update args, options
  options ||= {}
  scm = options[: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



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/externals/ext.rb', line 490

def update_ignore args, options
  scm = configuration['.']
  scm = scm['scm'] if scm

  scm ||= options[: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

#version(args, options) ⇒ Object



758
759
760
# File 'lib/externals/ext.rb', line 758

def version(args, options)
  puts Externals::VERSION
end