Class: Nucleon::Plugin::Project

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

Constant Summary collapse

@@projects =
{}
@@project_data =

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_info(namespace, plugin_type, data) ⇒ Object


Utilities



923
924
925
926
# File 'lib/core/plugin/project.rb', line 923

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

.clear_provider(directory) ⇒ Object




904
905
906
# File 'lib/core/plugin/project.rb', line 904

def self.clear_provider(directory)
  @@project_data.delete(directory)
end

.collectionObject




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

def self.collection
  @@projects
end

.load_provider(directory, override = nil) ⇒ Object




910
911
912
913
914
915
916
917
918
# File 'lib/core/plugin/project.rb', line 910

def self.load_provider(directory, override = nil)
  @@project_data[directory] = {} unless @@project_data.has_key?(directory)
  
  if override.nil? && @@project_data[directory].empty?
    json_data                 = Util::Disk.read(File.join(directory, state_file))
    @@project_data[directory] = hash(Util::Data.parse_json(json_data)) if json_data
  end
  override.nil? ? symbol_map(@@project_data[directory])[:provider] : override  
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

.state_fileObject


State configurations



884
885
886
# File 'lib/core/plugin/project.rb', line 884

def self.state_file
  '.corl'
end

.store_provider(directory, provider) ⇒ Object



892
893
894
895
896
897
898
899
900
# File 'lib/core/plugin/project.rb', line 892

def self.store_provider(directory, provider)
  if File.directory?(directory)
    @@project_data[directory] = {
      :provider => provider
    }
    json_data = Util::Data.to_json(@@project_data[directory], true)    
    Util::Disk.write(File.join(directory, state_file), json_data)
  end
end

.translate(data) ⇒ Object




930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
# File 'lib/core/plugin/project.rb', line 930

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[:reference] = matches[:reference]
      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




955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/core/plugin/project.rb', line 955

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




711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/core/plugin/project.rb', line 711

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




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

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



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

def cache
  @cache
end

#can_persist?Boolean


Checks

Returns:

  • (Boolean)


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

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

#checkout(revision) ⇒ Object




422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/core/plugin/project.rb', line 422

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




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
485
486
487
488
489
490
# File 'lib/core/plugin/project.rb', line 449

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




277
278
279
280
281
282
# File 'lib/core/plugin/project.rb', line 277

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

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




300
301
302
303
304
305
306
307
308
309
310
# File 'lib/core/plugin/project.rb', line 300

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




759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/core/plugin/project.rb', line 759

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




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

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




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

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

#eachObject




634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/core/plugin/project.rb', line 634

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




196
197
198
# File 'lib/core/plugin/project.rb', line 196

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

#full_path(local_path) ⇒ Object




1038
1039
1040
# File 'lib/core/plugin/project.rb', line 1038

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

#ignore(files) ⇒ Object




494
495
496
497
498
499
500
# File 'lib/core/plugin/project.rb', line 494

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




84
85
86
87
88
89
# File 'lib/core/plugin/project.rb', line 84

def init_project
  init_auth
  init_parent
  init_remotes
  load_revision
end

#local_path(file_path) ⇒ Object




1032
1033
1034
# File 'lib/core/plugin/project.rb', line 1032

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

#localize(path = nil) ⇒ Object




1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/core/plugin/project.rb', line 1016

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




136
137
138
# File 'lib/core/plugin/project.rb', line 136

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

#manage_ignore?Boolean

Returns:

  • (Boolean)


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

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
75
76
77
78
79
80
# File 'lib/core/plugin/project.rb', line 45

def normalize(reload)
  super
  
  directory = Util::Disk.filename(get(:directory, Dir.pwd))
  
  set_directory(directory)
  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
    
    unless self.class.load_provider(directory)
      self.class.store_provider(directory, plugin_provider)
    end
  end
end

#parent(default = nil) ⇒ Object




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

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

#pathObject




220
221
222
223
224
225
# File 'lib/core/plugin/project.rb', line 220

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

#private_keyObject




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

def private_key
  get(:private_key, nil)
end

#private_key_strObject



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

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

#public_keyObject



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

def public_key
  get(:public_key, nil)
end

#public_key_strObject



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

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

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


Remote operations



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
804
805
806
807
808
809
# File 'lib/core/plugin/project.rb', line 775

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




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
873
874
875
876
877
878
879
# File 'lib/core/plugin/project.rb', line 813

def push(remote = :edit, options = {})
  config  = Config.ensure(options).import({ :remote => remote })
  no_pull = config.delete(:no_pull, false)
  success = false
  
  push_project = lambda do |push_remote|
    logger.info("Pushing to #{push_remote} from #{directory}") 
    success = yield(config, push_remote) if block_given? && ( no_pull || 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




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

def reference
  get(:reference, nil)
end

#registerObject


Plugin operations



94
95
96
97
98
99
100
101
102
# File 'lib/core/plugin/project.rb', line 94

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




675
676
677
678
679
680
681
682
683
684
# File 'lib/core/plugin/project.rb', line 675

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




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

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

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




286
287
288
289
290
291
292
293
294
295
296
# File 'lib/core/plugin/project.rb', line 286

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




202
203
204
205
206
207
208
209
210
# File 'lib/core/plugin/project.rb', line 202

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




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

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




249
250
251
252
253
254
255
# File 'lib/core/plugin/project.rb', line 249

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

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




688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/core/plugin/project.rb', line 688

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




185
186
187
188
189
190
191
192
# File 'lib/core/plugin/project.rb', line 185

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)


121
122
123
# File 'lib/core/plugin/project.rb', line 121

def subproject?(path)
  false
end

#subprojects(default = nil) ⇒ Object




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

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

#top?(path) ⇒ Boolean


Returns:

  • (Boolean)


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

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

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




1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/core/plugin/project.rb', line 1004

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




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

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

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




991
992
993
994
995
996
997
998
999
1000
# File 'lib/core/plugin/project.rb', line 991

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




179
180
181
# File 'lib/core/plugin/project.rb', line 179

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