Skip to content

Commit

Permalink
Merge pull request #28 from alan-morey/develop
Browse files Browse the repository at this point in the history
v1.31
  • Loading branch information
alan-morey committed Aug 6, 2014
2 parents d9bead2 + 9b29bb8 commit c88f04a
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 31 deletions.
2 changes: 1 addition & 1 deletion ant-includes/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ sf.maxPoll = 200
sf.batchSize = 20

# The version of the antlib jar in lib
sf.antlib.version = 30.0
sf.antlib.version = 31.0
41 changes: 31 additions & 10 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@

<taskdef uri="antlib:com.salesforce" resource="com/salesforce/antlib.xml" classpath="lib/ant-salesforce-${sf.antlib.version}.jar" onerror="failall" />

<taskdef name="xmlmerge" classname="ch.elca.el4j.services.xmlmerge.anttask.XmlMergeTask" classpath="lib/module-xml_merge-common-3.1.jar;lib/jdom-1.1.3.jar;lib/jaxen-1.1.1.jar;lib/slf4j-api-1.7.7.jar" />

<import file="ant-includes/list-targets.xml" />

<property environment="env"/>

<macrodef name="forceMetaBackup">
<attribute name="args" default="" />

<sequential>
<property name="force.meta.backup.script" value="force-meta-backup.groovy" />

<exec osfamily="unix" executable="${force.meta.backup.script}" resolveexecutable="true" failonerror="true">
<arg line="@{args}" />
</exec>

<exec osfamily="windows" executable="cmd" failonerror="true">
<arg line="/c ${env.GROOVY_HOME}/bin/groovy.bat ${force.meta.backup.script} @{args}" />
</exec>
</sequential>
</macrodef>

<target name="clean">
<delete dir="${build.dir}" />
</target>

<target name="generatePackageXmlAndBuildXml" depends="-setUp">
<property name="script.name" value="force-meta-backup.groovy" />

<exec osfamily="unix" executable="${script.name}" resolveexecutable="true" />

<exec osfamily="windows" executable="cmd">
<arg value="/c"/>
<arg value="${env.GROOVY_HOME}/bin/groovy.bat"/>
<arg file="${script.name}" />
</exec>
<forceMetaBackup />
</target>

<target name="bulkRetrievable" depends="generatePackageXmlAndBuildXml, -setUpMetadataDir">
Expand All @@ -32,13 +44,22 @@

<target name="retrieveMiscMetadata" depends="generatePackageXmlAndBuildXml,-setUpMetadataDir">
<antcall target="-sfRetrieve"><param name="unpackaged" value="${build.dir}/misc-package.xml" /></antcall>
<antcall target="-sfRetrieve"><param name="unpackaged" value="${build.dir}/profile-package.xml" /></antcall>

<ant antfile="${build.dir}/profile-packages-target.xml" />

<antcall target="xmlMergeProfilesAndPermissions" />
</target>

<target name="backupMetadata" depends="clean,bulkRetrievable,bulkRetrieveFolders,retrieveMiscMetadata">
</target>

<target name="xmlMergeProfilesAndPermissions">
<forceMetaBackup args="--build-xml-merge-target" />
<ant antfile="${build.dir}/profile-packages-merge-target.xml" />
</target>

<target name="-sfRetrieve">
<echo>Retrieving ${unpackaged}</echo>
<sf:retrieve
unpackaged="${unpackaged}"
retrieveTarget="${build.metadata.dir}"
Expand Down
165 changes: 145 additions & 20 deletions force-meta-backup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import com.sforce.soap.metadata.ListMetadataQuery
import com.sforce.ws.SoapFaultException
import java.net.URLEncoder

import groovy.io.FileType
import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil


class ForceService {
def forceServiceConnector
def metadata
Expand Down Expand Up @@ -226,9 +228,9 @@ class BulkMetadataManifestBuilder {
'AccountSharingRules',
'AccountTerritorySharingRules',
'AnalyticSnapshot',
'AppMenu',
'ApexComponent',
'ApexTrigger',
'AppMenu',
'ApprovalProcess',
'ArticleType',
'AssignmentRules',
Expand All @@ -238,6 +240,7 @@ class BulkMetadataManifestBuilder {
'CampaignSharingRules',
'CaseSharingRules',
'Community',
'ConnectedApp',
'ContactSharingRules',
'CustomApplicationComponent',
'CustomLabels',
Expand Down Expand Up @@ -270,9 +273,9 @@ class BulkMetadataManifestBuilder {
'Scontrol',
'Settings',
'SharingSet',
'SiteDotCom',
'Skill',
'StaticResource',
'SiteDotCom',
'Territory',
'Workflow'
]
Expand Down Expand Up @@ -471,7 +474,6 @@ class MiscMetadataManifestBuilder {
static final PACKAGE_XML = 'misc-package.xml'

static final TYPES = [
'Letterhead',
'AccountCriteriaBasedSharingRule',
'AccountOwnerSharingRule',
'AccountSharingRules',
Expand All @@ -490,6 +492,7 @@ class MiscMetadataManifestBuilder {
'LeadCriteriaBasedSharingRule',
'LeadOwnerSharingRule',
'LeadSharingRules',
'Letterhead',
'OpportunityCriteriaBasedSharingRule',
'OpportunityOwnerSharingRule',
'OpportunitySharingRules',
Expand Down Expand Up @@ -559,16 +562,14 @@ class MiscMetadataManifestBuilder {
class ProfilesMetadataManifestBuilder {
def forceService
def config
def packageXmlPath

static final PACKAGE_XML = 'profile-package.xml'

static final TYPES = [
'ApexClass',
'ApexPage',
'CustomApplication',
'CustomObject',
'CustomObjectTranslation',
'CustomPermission',
'CustomTab',
'ExternalDataSource',
'Layout'
Expand All @@ -582,7 +583,6 @@ class ProfilesMetadataManifestBuilder {
ProfilesMetadataManifestBuilder(ForceService forceService, config) {
this.forceService = forceService
this.config = config
packageXmlPath = "${config['build.dir']}/${PACKAGE_XML}"
}

private getGroupedFileProperties() {
Expand All @@ -607,41 +607,158 @@ class ProfilesMetadataManifestBuilder {
grouped[type] << fileProperties
}

grouped
grouped.each { k, v ->
v.sort { a, b ->
a.namespacePrefix <=> b.namespacePrefix ?: a.fullName <=> b.fullName
}
}
}

def writePackageXml() {
def writePackageXmlForType(type, fileProperties) {
def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'

def xml = builder.bind {
mkp.xmlDeclaration()
Package(xmlns: 'http://soap.sforce.com/2006/04/metadata') {

groupedFileProperties.each { type, fileProperties ->
types {
fileProperties.each { fp ->
members { mkp.yield fp.fullName }
}

name() { mkp.yield type}
types {
fileProperties.each { fp ->
members { mkp.yield fp.fullName }
}

name() { mkp.yield type}
}

PERMISSON_TYPES.each { type ->
PERMISSON_TYPES.each { metadataType ->
types {
members '*'
name type
name metadataType
}
}

version { mkp.yield forceService.apiVersion }
}
}

def writer = FileWriterFactory.create(packageXmlPath)
def writer = FileWriterFactory.create(profilePackageXmlPath(type))
XmlUtil.serialize(xml, writer)
}

def writePackageXml() {
groupedFileProperties.each { type, fileProperties ->
writePackageXmlForType type, fileProperties
}

writeBuildXml()
}

private profilePackageXmlPath(type) {
"${config['build.dir']}/profile-packages/${type}.xml"
}

private writeBuildXml() {
def writer = FileWriterFactory.create("${config['build.dir']}/profile-packages-target.xml")
def builder = new MarkupBuilder(writer)

def targetName = 'profilesPackageRetrieve'

builder.project('xmlns:sf': 'antlib:com.salesforce', 'default': targetName) {
'import'(file: '../ant-includes/setup-target.xml')

target(name: targetName, depends: '-setUpMetadataDir') {
TYPES.each { type ->
def retrieveTarget = "${config['build.dir']}/profile-packages-metadata/$type"

forceService.withValidMetadataType(type) {
mkdir(dir: retrieveTarget)

'sf:retrieve'(
unpackaged: profilePackageXmlPath(type),
retrieveTarget: retrieveTarget,
username: '${sf.username}',
password: '${sf.password}',
serverurl: '${sf.serverurl}',
pollWaitMillis: '${sf.pollWaitMillis}',
maxPoll: '${sf.maxPoll}'
)
}
}
}
}
}
}

class XmlMergeTargetBuilder {
def config
def srcDir

XmlMergeTargetBuilder(config) {
this.config = config
srcDir = "${config['build.dir']}/profile-packages-metadata"
}

private getData() {
def data = [
profiles: new TreeSet(),
permissionsets: new TreeSet()
]

def dir = new File(srcDir)

dir.eachFileRecurse (FileType.FILES) { file ->
if (file.name ==~ /.+\.profile$/) {
data.profiles << file.name
} else if (file.name ==~ /.+\.permissionset/) {
data.permissionsets << file.name
}
}

data
}

private writeBuildXml() {
def writer = FileWriterFactory.create("${config['build.dir']}/profile-packages-merge-target.xml")
def builder = new MarkupBuilder(writer)

def targetName = 'profilesPackageXmlMerge'
def metadataDir = "${config['build.dir']}/metadata"

builder.project('default': targetName) {
'import'(file: '../ant-includes/setup-target.xml')

target(name: targetName) {
data.each { type, filenames ->
def destDir = "$metadataDir/$type"
mkdir(dir: destDir)

filenames.each { filename ->
echo "Xml Merging: $filename"
xmlmerge(dest: "$destDir/$filename", conf: 'xmlmerge.properties'
) {
fileset(dir: srcDir) {
include(name: "**/$filename")
}
}
}
}

copy(todir: metadataDir) {
fileset(dir: srcDir) {
include(name: '**/classes/*')
include(name: '**/pages/*')
include(name: '**/applications/*')
include(name: '**/objects/*')
include(name: '**/objectTranslations/*')
include(name: '**/tabs/*')
include(name: '**/layouts/*')
include(name: '**/dataSources/*')
}

cutdirsmapper(dirs: 1)
}
}
}
}
}


Expand All @@ -654,6 +771,7 @@ static void main(args) {
cli.with {
b longOpt: 'build-dir', args: 1, 'build directory'
h longOpt: 'help', 'usage information'
_ longOpt: 'build-xml-merge-target', 'Builds XML Merge target for Profile and PermissionSets XML files'
}

def options = cli.parse(args)
Expand All @@ -672,6 +790,13 @@ static void main(args) {

def forceService = ForceServiceFactory.create('build.properties')

if (options.'build-xml-merge-target') {
def xmlMerge = new XmlMergeTargetBuilder(config)
xmlMerge.writeBuildXml()
return
}

// Default Action

def bulk = new BulkMetadataManifestBuilder(forceService, config)
bulk.writeBuildXml()
Expand Down
Binary file not shown.
Binary file added lib/jaxen-1.1.1.jar
Binary file not shown.
Binary file added lib/jdom-1.1.3.jar
Binary file not shown.
Binary file added lib/module-xml_merge-common-3.1.jar
Binary file not shown.
Binary file added lib/slf4j-api-1.7.7.jar
Binary file not shown.
1 change: 1 addition & 0 deletions xmlmerge.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
action.default=COMPLETE

0 comments on commit c88f04a

Please sign in to comment.