Class: HooplaSalesforce::Rake::DeployTask

Inherits:
BaseTask
  • Object
show all
Includes:
Utils
Defined in:
lib/hoopla_salesforce/rake/deploy_task.rb

Overview

Deploys your project to salesforce.

Peforms some nice processing as well including:

- Replaces any occurance of __NAMESPACE__ in your code with the appropriate namespace
  declaration if you set a namespace on the task
- Zips up any folders found in src/resources as src/staticresources/#{folder}.resource.
  This allows you to only keep the raw assets in your project

Instance Attribute Summary collapse

Attributes inherited from BaseTask

#enterprise_wsdl, #metadata_wsdl, #name, #password, #token, #username

Instance Method Summary collapse

Methods included from Utils

#extract_class_name, #extract_trigger_name

Constructor Details

#initialize(name = :production) ⇒ DeployTask

Returns a new instance of DeployTask.



44
45
46
47
48
49
50
51
52
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 44

def initialize(name=:production)
  @deploy_file       = "deploy.zip"
  @src               = 'src'
  @package_namespace = nil
  @template_helper   = 'lib/template_helper.rb'
  super
  @package_namespace  += "__" if @package_namespace
  @processed_src     ||= "#{src}-processed" 
end

Instance Attribute Details

#deploy_fileObject

The location of the zip file generated for deployment. Defaults to ‘deploy.zip’



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

def deploy_file
  @deploy_file
end

#package_namespaceObject

The namespace you’re deploying the package to. This will convert all occurrences of __NAMESPACE__ in your source code to your_namespace__. It’s helpful if you’re deploying to both your production org (that has the namespaced package) and a dev org (that does not have one).

default - nil



31
32
33
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 31

def package_namespace
  @package_namespace
end

#processed_srcObject

The directory in which to store processed source files. Defaults to ##src-processed



35
36
37
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 35

def processed_src
  @processed_src
end

#srcObject

Your project root. Defaults to ‘src’



20
21
22
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 20

def src
  @src
end

#template_helperObject

Which file to load before processing templates. Default: lib/template_helper.rb To have this file inject methods into the HooplaSalesforce::TemplateProcessor, be sure to include your module in HooplaSalesforce::TemplateProcessor::VisualForce or HooplaSalesforce::TemplateProcessor::TestPage at the bottom of your template_helper file.



42
43
44
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 42

def template_helper
  @template_helper
end

Instance Method Details

#api_versionObject



123
124
125
126
127
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 123

def api_version
  return @api_version if @api_version
  File.read("#{src}/package.xml") =~ /<version>(.*)<\/version>/i
  @api_version = $1
end

#clean_namespaceObject



54
55
56
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 54

def clean_namespace
  package_namespace.sub(/__$/, '') if package_namespace
end

#defineObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 58

def define
  if name.is_a? Hash
    task_name = name.keys.first
    dependencies = name[task_name]
  else
    task_name = name
    dependencies = []
  end

  namespace :hsf do
    namespace :undeploy do
      desc "Undeploy all components in #{src} from salesforce"
      task task_name => dependencies do
        process_source
        require 'hoopla_salesforce/package_generator'
        PackageGenerator.new(processed_src, api_version).generate_destructive_changes
        make_zipfile
        deploy_zipfile
      end
    end

    namespace :deploy do
      desc "Deploy to salesforce"
      task task_name => dependencies do
        process_source
        make_meta_xmls
        make_zipfile
        deploy_zipfile
      end
    end

    namespace :testpages do
      desc "Renders any page templates as test pages in #{processed_src}/pages-test"
      task task_name => dependencies do
        mkdir_p "#{src}/pages-test"
        make_pages do |template|
          HooplaSalesforce::TemplateProcessor::TestPage.new(src, template)
        end
      end
    end
  end
end

#deploy_zipfileObject



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 251

def deploy_zipfile
  require 'hoopla_salesforce/deployer'
  deployer = HooplaSalesforce::Deployer.new(username, password, token, enterprise_wsdl, )
  if ENV['WEB_TESTS']
    require 'hoopla_salesforce/web_agent'
    deployer.deploy(deploy_file)
    WebAgent.new(username, password).run_tests(test_names, clean_namespace)
  else
    deployer.deploy(deploy_file, test_options)
  end
end

#make_meta(glob) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 145

def make_meta(glob)
  Dir["#{processed_src}/#{glob}"].each do |file|
    meta = "#{file}-meta.xml"
    next if file =~ /-meta\.xml$/ || File.exist?(meta)
    File.open(meta, 'w') do |f|
      f.print yield(file)
    end
  end
end

#make_meta_xmlsObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
215
216
217
218
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 155

def make_meta_xmls
  make_meta "classes/*.cls" do |klass|
    <<-EOS.margin
      <?xml version="1.0" encoding="UTF-8"?>
      <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
        <apiVersion>#{api_version}</apiVersion>
      </ApexClass>
    EOS
  end

  make_meta "pages/*.page" do |page|
    <<-EOS.margin
      <?xml version="1.0" encoding="UTF-8"?>
      <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
        <apiVersion>#{api_version}</apiVersion>
        <label>#{File.basename(page, '.page')}</label>
      </ApexClass>
    EOS
  end

  make_meta "documents/**/*" do |doc|
    if File.directory?(doc)
      m = <<-EOS.margin
        <?xml version="1.0" encoding="UTF-8"?>
        <DocumentFolder xmlns="http://soap.sforce.com/2006/04/metadata">
          <name>#{File.basename(doc)}</name>
          <accessType>Public</accessType>
          <publicFolderAccess>ReadOnly</publicFolderAccess>
        </DocumentFolder>
      EOS
    else
      <<-EOS.margin
        <?xml version="1.0" encoding="UTF-8"?>
        <Document xmlns="http://soap.sforce.com/2006/04/metadata">
          <internalUseOnly>false</internalUseOnly>
          <name>#{File.basename(doc)}</name>
          <public>true</public>
          <description>A document</description>
        </Document>
      EOS
    end
  end

  make_meta "staticresources/*.resource" do |resource|
    <<-EOS.margin
      <?xml version="1.0" encoding="UTF-8"?>
      <StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
        <cacheControl>Private</cacheControl>
        <contentType>application/zip</contentType>
        <description>A static resource</description>
      </StaticResource>
    EOS
  end

  make_meta "triggers/*.trigger" do |trigger|
    <<-EOS.margin
      <?xml version="1.0" encoding="UTF-8"?>
      <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
          <apiVersion>#{api_version}</apiVersion>
          <status>Active</status>
      </ApexTrigger>
    EOS
  end
end

#make_pagesObject



129
130
131
132
133
134
135
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 129

def make_pages
  require template_helper if File.exist?(template_helper)

  Dir["#{src}/pages/*.page.erb"].each do |template|
    yield template
  end
end

#make_resourcesObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 220

def make_resources
  require 'zip/zip'

  staticresources = "#{processed_src}/staticresources"
  mkdir staticresources
  
  Dir["#{processed_src}/resources/*"].each do |f|
    next unless File.directory?(f)
    resourcename = File.basename(f) + ".resource"
    resourcefile = "#{staticresources}/#{resourcename}"
    Zip::ZipFile.open(resourcefile, Zip::ZipFile::CREATE) do |zip|
      Dir["#{f}/**/*"].each do |file|
        zip.add(file.sub("#{f}/", ''), file)
      end
    end

    source_meta = "#{f}.resource-meta.xml"
    cp source_meta, "#{resourcefile}-meta.xml" if File.exist?(source_meta)
  end
end

#make_zipfileObject



241
242
243
244
245
246
247
248
249
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 241

def make_zipfile
  require 'zip/zip'
  rm_f deploy_file
  Zip::ZipFile.open(deploy_file, Zip::ZipFile::CREATE) do |zip|
    Dir["#{processed_src}/**/*"].each do |file|
      zip.add(file.sub(/^#{processed_src}/, src), file)
    end 
  end
end

#preprocess_filesObject



137
138
139
140
141
142
143
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 137

def preprocess_files
  require template_helper if File.exist?(template_helper)

  Dir["#{processed_src}/**/*.erb"].each do |template|
    yield template
  end
end

#process_sourceObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 274

def process_source
  rm_rf processed_src
  cp_r src, processed_src
  make_resources
  make_pages do |template|
    HooplaSalesforce::TemplateProcessor::VisualForce.new(processed_src, template)
    rm template.sub(src, processed_src)
  end

  preprocess_files do |template|
    HooplaSalesforce::TemplateProcessor::Generic.new(processed_src, template)
    rm template
  end

  substitute_namespaces
end

#substitute_namespacesObject



263
264
265
266
267
268
269
270
271
272
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 263

def substitute_namespaces
  Dir["#{processed_src}/**/*"].each do |f|
    next if File.directory?(f)
    system %Q|ruby -i -n -e 'print $_.gsub("__NAMESPACE__", "#{package_namespace}")' "#{f}"|
  end

  if package_namespace
    system %Q|ruby -i -n -e 'print $_.gsub("#{package_namespace}", "#{clean_namespace}")' "#{processed_src}/package.xml"|
  end
end

#test_namesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 101

def test_names
  if ENV['TEST_NAMES']
    ENV['TEST_NAMES'].split(',')
  else
    Dir["#{processed_src}/classes/*.cls"].inject([]) do |names, f|
      body = File.read(f)
      if body =~ /(testMethod|@isTest)/ && name = extract_class_name(body)
        names << name
      else
        names
      end
    end
  end
end

#test_optionsObject



116
117
118
119
120
121
# File 'lib/hoopla_salesforce/rake/deploy_task.rb', line 116

def test_options
  unless test_names.empty?
    test_names.map! { |n| "#{clean_namespace}.#{n}" } if package_namespace
    { "wsdl:runTests" => test_names }
  end
end