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



929
930
931
932
# File 'lib/core/plugin/project.rb', line 929

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




910
911
912
# File 'lib/core/plugin/project.rb', line 910

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




916
917
918
919
920
921
922
923
924
# File 'lib/core/plugin/project.rb', line 916

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
41
# 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,
      :corl_file => config.get(:corl_file, true)
    }), 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



890
891
892
# File 'lib/core/plugin/project.rb', line 890

def self.state_file
  '.corl'
end

.store_provider(directory, provider) ⇒ Object



898
899
900
901
902
903
904
905
906
# File 'lib/core/plugin/project.rb', line 898

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




936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/core/plugin/project.rb', line 936

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




961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/core/plugin/project.rb', line 961

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




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

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




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

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



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

def cache
  @cache
end

#can_persist?Boolean


Checks

Returns:



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

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

#checkout(revision) ⇒ 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
# File 'lib/core/plugin/project.rb', line 423

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




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

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




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

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

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




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

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




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

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




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

def delete_subproject(path)
  success = true

  if can_persist?
    localize do
      config = Config.new({ :path => path }, {}, true, false)

      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




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

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

#eachObject




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

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




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

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

#full_path(local_path) ⇒ Object




1044
1045
1046
# File 'lib/core/plugin/project.rb', line 1044

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

#ignore(files) ⇒ Object




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

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




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

def init_project
  init_auth
  init_parent
  init_remotes
  load_revision
end

#local_path(file_path) ⇒ Object




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

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

#localize(path = nil) ⇒ Object




1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/core/plugin/project.rb', line 1022

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




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

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

#manage_ignore?Boolean

Returns:



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

def manage_ignore?
  get(:manage_ignore, false)
end

#normalize(reload) ⇒ Object


Project plugin interface



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

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

    if get(:corl_file, true) && ! self.class.load_provider(directory)
      self.class.store_provider(directory, plugin_provider)
    end
  end
end

#parent(default = nil) ⇒ Object




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

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

#pathObject




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

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

#private_keyObject




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

def private_key
  get(:private_key, nil)
end

#private_key_strObject



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

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

#public_keyObject



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

def public_key
  get(:public_key, nil)
end

#public_key_strObject



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

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

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


Remote operations



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
810
811
812
813
# File 'lib/core/plugin/project.rb', line 776

def pull(remote = :origin, options = {})
  config = Config.ensure(options)

  config[:remote] = remote(:edit) && remote == :origin ? :edit : 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




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
880
881
882
883
884
885
# File 'lib/core/plugin/project.rb', line 817

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?
    unless remote(remote)
      logger.warn("Project #{plugin_name} does not have the remote '#{remote}' defined")
      return true
    end
    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
        begin
          success = push_project.call(remote)
          raise unless success

        rescue
          tries -= 1
          retry if tries > 0
        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 #{plugin_name} does not meet the criteria for persistence and can not push to remotes")
  end
  success
end

#referenceObject




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

def reference
  get(:reference, nil)
end

#registerObject


Plugin operations



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

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




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

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




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

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

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




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

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




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

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




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/core/plugin/project.rb', line 731

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




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

def set_location(directory)
  set_directory(directory)

  yield if block_given?

  init_project
end

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




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

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




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

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:



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

def subproject?(path)
  false
end

#subprojects(default = nil) ⇒ Object




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

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

#top?(path) ⇒ Boolean


Returns:



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

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

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




1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/core/plugin/project.rb', line 1010

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




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

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

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




997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/core/plugin/project.rb', line 997

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




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

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