Class: HooplaSalesforce::PackageGenerator

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/hoopla_salesforce/package_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#extract_class_name, #extract_trigger_name

Constructor Details

#initialize(processed_src, api_version) ⇒ PackageGenerator

Right now this only works with making a destructiveChanges.xml Need to add support namespace, etc before using to generate package.xml



13
14
15
16
# File 'lib/hoopla_salesforce/package_generator.rb', line 13

def initialize(processed_src, api_version)
  @processed_src = processed_src
  @api_version = api_version
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



9
10
11
# File 'lib/hoopla_salesforce/package_generator.rb', line 9

def api_version
  @api_version
end

#processed_srcObject (readonly)

Returns the value of attribute processed_src.



8
9
10
# File 'lib/hoopla_salesforce/package_generator.rb', line 8

def processed_src
  @processed_src
end

Instance Method Details

#destructive_changes_xmlObject



83
84
85
86
87
88
89
90
91
# File 'lib/hoopla_salesforce/package_generator.rb', line 83

def destructive_changes_xml
  <<-EOS.margin
    <?xml version="1.0" encoding="UTF-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
      #{package_types}
      <version>#{api_version}</version>
    </Package>
  EOS
end

#empty_package_xmlObject



74
75
76
77
78
79
80
81
# File 'lib/hoopla_salesforce/package_generator.rb', line 74

def empty_package_xml
  <<-EOS.margin
    <?xml version="1.0" encoding="UTF-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
      <version>#{api_version}</version>
    </Package>
  EOS
end

#generate_destructive_changesObject



108
109
110
111
112
# File 'lib/hoopla_salesforce/package_generator.rb', line 108

def generate_destructive_changes
  write_xml "package.xml",            empty_package_xml
  write_xml "destructiveChanges.xml", destructive_changes_xml
  remove_processed_files_for_undeploy
end

#map_files(glob, &block) ⇒ Object



23
24
25
# File 'lib/hoopla_salesforce/package_generator.rb', line 23

def map_files(glob, &block)
  Dir["#{processed_src}/#{glob}"].map(&block)
end

#members_for_apex_classObject



27
28
29
30
31
32
# File 'lib/hoopla_salesforce/package_generator.rb', line 27

def members_for_apex_class
  map_files("classes/*.cls") do |klass|
    data = File.read(klass)
    extract_class_name(data)
  end
end

#members_for_apex_pageObject



41
42
43
44
45
# File 'lib/hoopla_salesforce/package_generator.rb', line 41

def members_for_apex_page
  map_files("pages/*.page") do |page|
    File.basename(page, '.page')
  end
end

#members_for_apex_triggerObject



34
35
36
37
38
39
# File 'lib/hoopla_salesforce/package_generator.rb', line 34

def members_for_apex_trigger
  map_files("triggers/*.trigger") do |trigger|
    data = File.read(trigger)
    extract_trigger_name(data)
  end
end

#members_for_custom_applicationObject



53
54
55
56
57
# File 'lib/hoopla_salesforce/package_generator.rb', line 53

def members_for_custom_application
  map_files("applications/*.app") do |app|
    File.read(app).match(/<fullName>([^<]*)<\/fullName>/)[1]
  end
end

#members_for_custom_fieldObject



59
60
61
62
63
64
65
66
# File 'lib/hoopla_salesforce/package_generator.rb', line 59

def members_for_custom_field
  map_files("objects/*.object") do |object|
    obj_name = File.basename(object, ".object")
    data = File.read(object)
    xml_doc = Nokogiri::XML(data)
    xml_doc.search("fullName").map { |f| [obj_name, f.text].join('.') }
  end.flatten
end

#members_for_custom_tabObject



47
48
49
50
51
# File 'lib/hoopla_salesforce/package_generator.rb', line 47

def members_for_custom_tab
  map_files("tabs/*.tab") do |tab|
    File.basename(tab, '.tab')
  end
end

#members_for_static_resourceObject



68
69
70
71
72
# File 'lib/hoopla_salesforce/package_generator.rb', line 68

def members_for_static_resource
  map_files("staticresources/*.resource") do |resource|
    File.basename(resource, ".resource")
  end
end

#package_typesObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hoopla_salesforce/package_generator.rb', line 93

def package_types
  supported_types.map do |type|
    members = send("members_for_#{type}")
    unless members.empty?
      members_xml = members.map { |m| "<members>#{m}</members>" }.join("\n")
      <<-EOS.margin
        <types>
          #{members_xml}
          <name>#{type.camelize}</name>
        </types>
      EOS
    end
  end.join("\n")
end

#protected_files_for_destructionObject



120
121
122
# File 'lib/hoopla_salesforce/package_generator.rb', line 120

def protected_files_for_destruction
  %W(#{processed_src}/package.xml #{processed_src}/destructiveChanges.xml)
end

#remove_processed_files_for_undeployObject



124
125
126
127
128
# File 'lib/hoopla_salesforce/package_generator.rb', line 124

def remove_processed_files_for_undeploy
  Dir["#{processed_src}/*"].each do |file|
    FileUtils.rm_rf(file) unless protected_files_for_destruction.include?(file)
  end 
end

#supported_typesObject



18
19
20
21
# File 'lib/hoopla_salesforce/package_generator.rb', line 18

def supported_types
  %w(apex_class apex_page custom_tab custom_application apex_trigger custom_field
     static_resource)
end

#write_xml(file, data) ⇒ Object



114
115
116
117
118
# File 'lib/hoopla_salesforce/package_generator.rb', line 114

def write_xml(file, data)
  File.open("#{processed_src}/#{file}", 'w') do |pkg|
    pkg.print data
  end
end