Class: Nucleon::Plugin::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/core/plugin/project.rb

Constant Summary collapse

@@projects =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_info(namespace, plugin_type, data) ⇒ Object


Utilities



877
878
879
880
# File 'lib/core/plugin/project.rb', line 877

def self.build_info(namespace, plugin_type, data)  
  data = data.split(/\s*,\s*/) if data.is_a?(String)
  super(namespace, plugin_type, data)
end

.collectionObject




10
11
12
# File 'lib/core/plugin/project.rb', line 10

def self.collection
  @@projects
end

.open(directory, provider, options = {}) ⇒ Object


Constructor / Destructor



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/core/plugin/project.rb', line 23

def self.open(directory, provider, options = {})
  config    = Config.ensure(options)    
  directory = File.expand_path(Util::Disk.filename(directory))
  
  if ! @@projects.has_key?(directory) || config.get(:reset, false)
    logger.info("Creating new project at #{directory} with #{provider}")
    
    return Nucleon.project(config.import({
      :name      => directory,
      :directory => directory
    }), provider)
    
  else
    logger.info("Opening existing project at #{directory}")
  end
  
  @@projects[directory]
end

.register_idsObject




16
17
18
# File 'lib/core/plugin/project.rb', line 16

def self.register_ids
  [ :name, :directory ]
end

.translate(data) ⇒ Object




884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/core/plugin/project.rb', line 884

def self.translate(data)
  options = super(data)
  
  case data        
  when String
    options = { :url => data }
  when Hash
    options = data
  end
  
  if options.has_key?(:url)
    if matches = translate_reference(options[:url])
      options[:provider] = matches[:provider]
      options[:url]      = matches[:url]
      options[:revision] = matches[:revision] unless options.has_key?(:revision)
      
      logger.debug("Translating project options: #{options.inspect}")  
    end
  end
  options
end

.translate_reference(reference, editable = false) ⇒ Object




908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'lib/core/plugin/project.rb', line 908

def self.translate_reference(reference, editable = false)
  # ex: github:::username/project[branch/revision]
  if reference && reference.match(/^\s*([a-zA-Z0-9_-]+):::([^\]\s]+)\s*(?:\[\s*([^\]\s]+)\s*\])?\s*$/)
    provider = $1
    url      = $2
    revision = $3
    
    logger.debug("Translating project reference: #{provider}  #{url}  #{revision}")
    
    if provider && Nucleon.loaded_plugins(:nucleon, :project).keys.include?(provider.to_sym)
      klass        = Nucleon.class_const([ :nucleon, :project, provider ])          
      expanded_url = klass.send(:expand_url, url, editable) if klass.respond_to?(:expand_url)
    end
    expanded_url = url unless expanded_url
    
    info = {
      :provider  => provider,
      :reference => url,
      :url       => expanded_url,
      :revision  => revision
    }
    
    logger.debug("Project reference info: #{info.inspect}")
    return info
  end
  nil
end

Instance Method Details

#add_remote_url(name, url, options = {}) ⇒ Object




705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/core/plugin/project.rb', line 705

def add_remote_url(name, url, options = {})
  if can_persist?
    localize do
      config = Config.ensure(options)
    
      if url = extension_set(:add_remote_url, url, { :name => name, :config => config })
        url = translate_edit_url(url) if name == :edit && config.get(:translate, true)
        
        logger.info("Adding project remote url #{url} to #{name}")    
        yield(config, url) if block_given?
      end
    end
  else
    logger.warn("Project #{self.name} does not meet the criteria for persistence and can not have remotes") 
  end
end

#add_subproject(path, url, revision, options = {}) ⇒ Object




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
# File 'lib/core/plugin/project.rb', line 539

def add_subproject(path, url, revision, options = {})    
  success = true
  
  if can_persist?
    localize do
      config = Config.ensure(options).import({ :path => path, :url => url, :revision => revision })
    
      if extension_check(:add_project, { :config => config })
        logger.info("Adding a sub project to #{config[:path]} from #{config[:url]} at #{config[:revision]}")
    
        success = yield(config) if block_given?
      
        if success
          extension(:add_project_success, { :config => config })
        
          config.init(:files, '.')
          config.init(:message, "Adding project #{config[:url]} to #{config[:path]}")
        
          commit(config[:files], { :message => config[:message] })          
          update_subprojects
        end
      else
        success = false
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not have sub projects")   
  end
  success
end

#cacheObject


Property accessor / modifiers



141
142
143
# File 'lib/core/plugin/project.rb', line 141

def cache
  @cache
end

#can_persist?Boolean


Checks

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/core/plugin/project.rb', line 101

def can_persist?
  return top?(directory) if directory
  false
end

#checkout(revision) ⇒ Object




416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/core/plugin/project.rb', line 416

def checkout(revision)
  success = false
  
  if can_persist?
    localize do      
      if extension_check(:checkout, { :revision => revision })
        logger.info("Checking out project #{name} revision: #{revision}")
    
        success = true
        success = yield(success) if block_given?
      
        if success
          set(:revision, revision)
        
          extension(:checkout_success, { :revision => revision })
          load_subprojects
        end
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not checkout a revision")
  end
  success
end

#commit(files = '.', options = {}) ⇒ Object




443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/core/plugin/project.rb', line 443

def commit(files = '.', options = {})
  success = false
  
  if can_persist?
    localize do
      config = Config.ensure(options)
    
      if extension_check(:commit, { :files => files, :config => config })
        logger.info("Committing changes to project #{name}: #{files.inspect}")
    
        time     = Time.new.strftime("%Y-%m-%d %H:%M:%S")
        user     = config.delete(:user, ENV['USER'] + '@' + fact(:fqdn))
    
        message  = config.get(:message, '')
        message  = 'Saving state: ' + ( files.is_a?(Array) ? "\n\n" + files.join("\n") : files.to_s ) if message.empty?
    
        user = 'UNKNOWN' unless user && ! user.empty?
    
        logger.debug("Commit by #{user} at #{time} with #{message}")          
        success = yield(config, time, user, message) if block_given?
      
        if success
          load_revision
        
          extension(:commit_success, { :files => files })
    
          if ! parent.nil? && config.get(:propogate, true)
            logger.info("Commit to parent as parent exists and propogate option given")
      
            parent.load_revision
            parent.commit(directory, config.import({
              :message => "Updating #{path}: #{message}"
            }))
          end
        end
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not be committed to")                
  end
  success     
end

#config(name, options = {}) ⇒ Object




271
272
273
274
275
276
# File 'lib/core/plugin/project.rb', line 271

def config(name, options = {})
  localize do
    config = Config.ensure(options)
    can_persist? && block_given? ? yield(config) : nil
  end
end

#delete_config(name, options = {}) ⇒ Object




294
295
296
297
298
299
300
301
302
303
304
# File 'lib/core/plugin/project.rb', line 294

def delete_config(name, options = {})
  localize do
    config = Config.ensure(options)
  
    if can_persist? && extension_check(:delete_config, { :name => name, :config => config })
      logger.info("Removing project #{self.name} configuration: #{name}")
  
      yield(config) if block_given?
    end
  end
end

#delete_remote(name) ⇒ Object




753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/core/plugin/project.rb', line 753

def delete_remote(name)
  if can_persist?
    localize do
      if extension_check(:delete_remote, { :name => name })
        logger.info("Deleting project remote #{name}")  
        yield if block_given?
      end
    end
  else
    logger.warn("Project #{self.name} does not meet the criteria for persistence and can not have remotes") 
  end
end

#delete_subproject(path) ⇒ 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
# File 'lib/core/plugin/project.rb', line 572

def delete_subproject(path)    
  success = true
  
  if can_persist?
    localize do
      config = Config.new({ :path => path })
    
      if extension_check(:delete_project, { :config => config })
        logger.info("Deleting a sub project at #{config[:path]}")
    
        success = yield(config) if block_given?
      
        if success
          extension(:delete_project_success, { :config => config })
        
          config.init(:files, '.')
          config.init(:message, "Removing project at #{config[:path]}")
        
          commit(config[:files], { :message => config[:message] })      
          update_subprojects
        end
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not have sub projects") 
  end
  success
end

#directory(default = nil) ⇒ Object




208
209
210
# File 'lib/core/plugin/project.rb', line 208

def directory(default = nil)
  get(:directory, default)
end

#eachObject




628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/core/plugin/project.rb', line 628

def each
  if can_persist?
    localize do
      logger.info("Iterating through all sub projects of project #{name}")
    
      subprojects.each do |path, project|
        extension(:process_project, { :project => project })
      
        logger.debug("Running process on sub project #{path}")
        yield(path, project)  
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not have sub projects") 
  end
end

#edit_url(default = nil) ⇒ Object




190
191
192
# File 'lib/core/plugin/project.rb', line 190

def edit_url(default = nil)
  get(:edit, default)
end

#full_path(local_path) ⇒ Object




991
992
993
# File 'lib/core/plugin/project.rb', line 991

def full_path(local_path)
  File.join(directory, local_path)
end

#ignore(files) ⇒ Object




488
489
490
491
492
493
494
# File 'lib/core/plugin/project.rb', line 488

def ignore(files)
  return unless directory && manage_ignore?
  
  files = nil
  files = yield if block_given?
  commit(files, { :message => "Adding project ignores." }) if files
end

#init_projectObject




78
79
80
81
82
83
# File 'lib/core/plugin/project.rb', line 78

def init_project
  init_auth
  init_parent
  init_remotes
  load_revision
end

#local_path(file_path) ⇒ Object




985
986
987
# File 'lib/core/plugin/project.rb', line 985

def local_path(file_path)
  file_path.gsub(directory + File::SEPARATOR, '')  
end

#localize(path = nil) ⇒ Object




969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/core/plugin/project.rb', line 969

def localize(path = nil)
  prev_directory = Dir.pwd
  path           = directory if path.nil?
  
  Dir.chdir(path)
  
  result = safe_exec(true) do
    yield  
  end
  
  Dir.chdir(prev_directory)
  result
end

#manage_ignore=(ignore) ⇒ Object




130
131
132
# File 'lib/core/plugin/project.rb', line 130

def manage_ignore=ignore
  set(:manage_ignore, ignore)
end

#manage_ignore?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/core/plugin/project.rb', line 134

def manage_ignore?
  get(:manage_ignore, false)
end

#normalize(reload) ⇒ Object


Project plugin interface



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/core/plugin/project.rb', line 45

def normalize(reload)
  super
  
  set_directory(Util::Disk.filename(get(:directory, Dir.pwd)))
  register
  
  set_url(get(:url)) if get(:url, false)
  
  myself.plugin_name = path if ! plugin_name || plugin_name.to_sym == plugin_provider
  
  ui.resource = plugin_name
  logger      = plugin_name
  
  if keys = delete(:keys, nil)
    set(:private_key, keys[:private_key])
    set(:public_key, keys[:public_key])
  end
  
  extension(:normalize)
  
  init_project
  extension(:init)
  
  pull if get(:pull, false)
  
  unless reload
    @cache = Util::Cache.new(directory, Nucleon.sha1(plugin_name), '.project_cache')
    init_cache
  end
end

#parent(default = nil) ⇒ Object




253
254
255
# File 'lib/core/plugin/project.rb', line 253

def parent(default = nil)
  get(:parent, default)
end

#pathObject




214
215
216
217
218
219
# File 'lib/core/plugin/project.rb', line 214

def path
  if parent.nil?
    return directory  
  end
  directory.gsub(parent.directory + File::SEPARATOR, '')
end

#private_keyObject




153
154
155
# File 'lib/core/plugin/project.rb', line 153

def private_key
  get(:private_key, nil)
end

#private_key_strObject



157
158
159
160
# File 'lib/core/plugin/project.rb', line 157

def private_key_str
  return Util::Disk.read(private_key) if private_key
  nil
end

#public_keyObject



162
163
164
# File 'lib/core/plugin/project.rb', line 162

def public_key
  get(:public_key, nil)
end

#public_key_strObject



166
167
168
169
# File 'lib/core/plugin/project.rb', line 166

def public_key_str
  return Util::Disk.read(public_key) if public_key
  nil
end

#pull(remote = :origin, options = {}) ⇒ Object


Remote operations



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File 'lib/core/plugin/project.rb', line 769

def pull(remote = :origin, options = {})
  config  = Config.ensure(options).import({ :remote => remote })
  success = false
  
  if can_persist?
    localize do      
      if extension_check(:pull, { :directory => directory, :config => config })
        remote = config.delete(:remote)
            
        if remote(remote)
          logger.info("Pulling from #{remote} into #{directory}") 
          success = yield(config, remote) if block_given?
        end
        
        if success
          update_subprojects
           
          extension(:pull_success, { :directory => directory, :remote => remote, :config => config })          
           
          if ! parent.nil? && config.get(:propogate, true)
            logger.debug("Commit to parent as parent exists and propogate option was given")
      
            parent.commit(directory, config.import({
              :message     => "Pulling updates for subproject #{path}",
              :allow_empty => true
            }))
          end
        end
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not pull from remotes")       
  end
  success
end

#push(remote = :edit, options = {}) ⇒ Object




807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/core/plugin/project.rb', line 807

def push(remote = :edit, options = {})
  config  = Config.ensure(options).import({ :remote => remote })
  success = false
  
  push_project = lambda do |push_remote|
    logger.info("Pushing to #{push_remote} from #{directory}") 
    success = yield(config, push_remote) if block_given? && pull(push_remote, config)  
  end
  
  if can_persist?
    localize do     
      if extension_check(:push, { :directory => directory, :config => config })
        remote = config.delete(:remote)
        tries  = config.delete(:tries, 5)
      
        # TODO: Figure out a better way through specialized exception handling
        if remote(remote)
          begin
            success = push_project.call(remote)
            raise unless success
            
          rescue
            tries -= 1
            retry if tries > 0
          end
        end
        
        if success
          config.delete(:revision)
        
          extension(:push_success, { :directory => directory, :remote => remote, :config => config })  
    
          if config.get(:propogate, true)
            unless parent.nil?
              propogate_up = config.get(:propogate_up, nil)
              
              if propogate_up.nil? || propogate_up
                logger.debug("Commit to parent as parent exists and propogate option was given")
                parent.push(remote, Config.new(config.export.dup).import({ 
                  :propogate_up   => true, 
                  :propogate_down => false 
                }))
              end
            end
            
            logger.debug("Pushing sub projects")
            
            propogate_down = config.get(:propogate_down, nil)
      
            if propogate_down.nil? || propogate_down
              each do |path, project|
                project.push(remote, Config.new(config.export.dup).import({ 
                  :propogate_up   => false, 
                  :propogate_down => true 
                }))
              end
            end
          end
        end
      end
    end
  else
    logger.warn("Project #{name} does not meet the criteria for persistence and can not push to remotes") 
  end
  success
end

#referenceObject




147
148
149
# File 'lib/core/plugin/project.rb', line 147

def reference
  get(:reference, nil)
end

#registerObject


Plugin operations



88
89
90
91
92
93
94
95
96
# File 'lib/core/plugin/project.rb', line 88

def register
  super
  if directory
    lib_path = File.join(directory, 'lib')
    if File.directory?(lib_path)
      Nucleon.register(lib_path)
    end
  end
end

#remote(name) ⇒ Object




669
670
671
672
673
674
675
676
677
678
# File 'lib/core/plugin/project.rb', line 669

def remote(name)
  url = nil
  if can_persist?
    localize do
      logger.info("Fetching remote url for #{name}")
      url = yield if block_given?
    end
  end
  url
end

#revision(default = nil) ⇒ Object




265
266
267
# File 'lib/core/plugin/project.rb', line 265

def revision(default = nil)
  get(:revision, default).to_s
end

#set_config(name, value, options = {}) ⇒ Object




280
281
282
283
284
285
286
287
288
289
290
# File 'lib/core/plugin/project.rb', line 280

def set_config(name, value, options = {})
  localize do
    config = Config.ensure(options) 
  
    if can_persist? && value = extension_set(:set_config, value, { :name => name, :config => config })
      logger.info("Setting project #{self.name} configuration: #{name} = #{value.inspect}")
  
      yield(config, value) if block_given?
    end
  end
end

#set_edit_url(url) ⇒ Object




196
197
198
199
200
201
202
203
204
# File 'lib/core/plugin/project.rb', line 196

def set_edit_url(url)
  url = url.strip
  if url && url = extension_set(:set_edit_url, url)      
    logger.info("Setting project #{name} edit url to #{url}")
    
    set(:edit, url)
    set_remote(:edit, url)
  end
end

#set_host_remote(name, hosts, path, options = {}) ⇒ Object




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
# File 'lib/core/plugin/project.rb', line 724

def set_host_remote(name, hosts, path, options = {})
  if can_persist?
    localize do
      config = Config.ensure(options).import({ :path => path, :translate => false })
      hosts  = array(hosts)
    
      unless hosts.empty?
        if hosts = extension_set(:set_host_remote, hosts, { :name => name, :config => config })
          unless ! hosts || hosts.empty?
            path = config.delete(:path)
        
            logger.info("Setting host remote #{name} for #{hosts.inspect} at #{path}") 
            set_remote(name, translate_url(hosts.shift, path, config.export), config)
      
            hosts.each do |host|
              logger.debug("Adding remote url to #{host}")
              add_remote_url(name, translate_url(host, path, config.export), config)
            end
          end
        end
      end
    end
  else
    logger.warn("Project #{self.name} does not meet the criteria for persistence and can not have remotes") 
  end
end

#set_location(directory) ⇒ Object




243
244
245
246
247
248
249
# File 'lib/core/plugin/project.rb', line 243

def set_location(directory)
  set_directory(directory)
      
  yield if block_given?
  
  init_project
end

#set_remote(name, url, options = {}) ⇒ Object




682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/core/plugin/project.rb', line 682

def set_remote(name, url, options = {})
  config = Config.ensure(options)
  
  if can_persist?
    localize do
      unless url.strip.empty? 
        if url = extension_set(:set_remote, url, { :name => name })
          delete_remote(name)
          
          url = translate_edit_url(url) if name == :edit && config.get(:translate, true)
  
          logger.info("Setting project remote #{name} to #{url}")
          yield(url) if block_given?
        end
      end
    end
  else
    logger.warn("Project #{self.name} does not meet the criteria for persistence and can not have remotes") 
  end
end

#set_url(url) ⇒ Object




179
180
181
182
183
184
185
186
# File 'lib/core/plugin/project.rb', line 179

def set_url(url)
  if url && url = extension_set(:set_url, url.strip)      
    logger.info("Setting project #{name} url to #{url}")
    
    set(:url, url)
    set_remote(:origin, url)
  end
end

#subproject?(path) ⇒ Boolean


Returns:

  • (Boolean)


115
116
117
# File 'lib/core/plugin/project.rb', line 115

def subproject?(path)
  false
end

#subprojects(default = nil) ⇒ Object




259
260
261
# File 'lib/core/plugin/project.rb', line 259

def subprojects(default = nil)
  get(:subprojects, default)
end

#top?(path) ⇒ Boolean


Returns:

  • (Boolean)


108
109
110
111
# File 'lib/core/plugin/project.rb', line 108

def top?(path)
  return true if File.directory?(path)
  false
end

#translate_edit_url(url, options = {}) ⇒ Object




957
958
959
960
961
962
963
964
965
# File 'lib/core/plugin/project.rb', line 957

def translate_edit_url(url, options = {})
  config = Config.ensure(options)
  
  if block_given?
    temp_url = yield(config)
    url      = temp_url if temp_url
  end
  url
end

#translate_reference(reference, editable = false) ⇒ Object




938
939
940
# File 'lib/core/plugin/project.rb', line 938

def translate_reference(reference, editable = false)
  myself.class.translate_reference(reference, editable)
end

#translate_url(host, path, options = {}) ⇒ Object




944
945
946
947
948
949
950
951
952
953
# File 'lib/core/plugin/project.rb', line 944

def translate_url(host, path, options = {})
  config = Config.ensure(options)
  url    = "#{host}/#{path}"
  
  if block_given?
    temp_url = yield(config)
    url      = temp_url if temp_url
  end
  url
end

#url(default = nil) ⇒ Object




173
174
175
# File 'lib/core/plugin/project.rb', line 173

def url(default = nil)
  get(:url, default)
end