在开发中,经常会遇到本地工程错乱或者误操作导致本地本地项目被删除,此时利用SFDX: Retrieve Source from Org只会拉取新建并且名称相同的组件,若通过创建一个个文件,然后再拉取的方式,不免显得过于笨拙。
本人便踩过这个坑,这里分享一下个人的解决方案。
在我看来,从Org中一次拉取多个组件文件或者全部组件文件,有三种解决方案,下面一一叙述:
1. 利用manifest新建工程
1)打开Vs Code,进入命令行模式(shift+cmmand+p/F1), SFDX: Create Project with Manifest
直接创建标准模式即可,当然可根据自身实际选择。这里直接创建工程:sample
在sample/manifest目录下,可发现名为package.xml的文件,标准内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
[/span>Package xmlns=""
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>version
从packge.xml文件中可以看出,主要分为一下标准类型:
ApexClass: Apex类文件
ApexComponent: Apex组件
ApexPage: Visualforce组件
ApexTestSuite: Apex测试类
ApexTrigger: Apex的触发问价
AuraDefinitionBundle: Apex的自定义Aura组件
LightningCompentBundle: Apex的自定义LWC组件
StaticResource: 项目中的静态文件
2)认证Org并设置为默认Org
# 认证Org
SFDX: Authorize an Org
# 设置默认Org
SFDX: Set a Default Org
查看Org列表
【kaiwu3@localhost:~/sample】$sfdx force:org:list
=== Orgs
ALIAS USERNAME ORG ID CONNECTED STATUS
─── ──────────── ──────────────────────── ────────────────── ────────────────
(U) k_Reset kaiwu3@*.com 00D2v000001Xk5DEAS Connected
No active scratch orgs found. Specify --all to see all scratch orgs
3) 利用pacakge.xml拉取文件
右击sample/manifest/pacakge.xml,选择SFDX: Retrieve Source in Manifest from Org
19:55:16.740 sfdx force:source:retrieve --manifest /Users/kaiwu3/Works/Code/sfdcTmp/sample/manifest/package.xml
=== Retrieved Source
FULL NAME TYPE PROJECT PATH
──────────────────────────────────────────────────────────────────────── ──────────────────────── ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
heatmapmock StaticResource force-app/main/default/staticresources/heatmapmock.resource-meta.xml
heatmapmock StaticResource force-app/main/default/staticresources/heatmapmock.js
heatmap StaticResource force-app/main/default/staticresources/heatmap.resource-meta.xml
heatmap StaticResource force-app/main/default/staticresources/heatmap.js
......
此时,你会发现package.xml中定义的标准组件,都已经成功拉到本地工程。
2. 利用Package Manage方式自由拉取文件
1) Org中创建Package包
上述manifest方式拉取的文件,仅限于创建工程是自动生成的package.xml中定义的几种类型,有时并不能满足我们的需求(比如Org迁移或者备份,此时不仅需要上述的组件文件,还需要保留在Org中创建的Object等信息)。此时,你会发现Package Manager可以满足你的一切需求。
在Org中,【Setup】-->【Package Manager】-->【new】
点击Add按钮,添加所需组件。
返回Package Maneger界面可见Package列表存在sample的包。
2)本地工程下载smaple包
【kaiwu3@localhost:~/Works/Code/sfdcTmp/sample】$mkdir pacakge
# 下载包命令:sfdx force:mdapi:retrieve -s -r { {pathToCreatePackageFile}} -u { {sandboxUsername}} -p { {packageName}}
【kaiwu3@localhost:~/Works/Code/sfdcTmp/sample】$sfdx force:mdapi:retrieve -s -r package/ -u kaiwu3@*.com -p sample
Retrieving source...
=== Status
Status: InProgress
jobid: 09S2v000006nL64EAE
=== Result
Status: Succeeded
jobid: 09S2v000006nL64EAE
# 产生的zip包路径
Wrote retrieve zip to /Users/kaiwu3/Works/Code/sfdcTmp/sample/package/unpackaged.zip.
3)转换package为工程文件
# 解压下载包(下载包一般命名为:unpacakge.zip)
【kaiwu3@localhost:~/sfdcTmp/sample/package】$unzip unpackaged.zip
Archive: unpackaged.zip
inflating: staticresources/leaflet1.resource
inflating: staticresources/leaflet1.resource-meta.xml
inflating: aura/SmartLocks/SmartLocks.css
inflating: aura/SmartLocks/SmartLocksHelper.js
inflating: aura/SmartLocks/SmartLocksController.js
inflating: aura/SmartHome/SmartHome.css
inflating: aura/SmartHome/SmartHomeController.js
......
inflating: package.xml
# 将解压文件转换匹配到对应工程中
# 转换命令:sfdx force:mdapi:convert -r {pacakge.xml所在目录}
【kaiwu3@localhost:~/Works/Code/sfdcTmp/sample】$sfdx force:mdapi:convert -r package/
=== Converted Source
STATE FULL NAME TYPE PROJECT PATH
───── ─────────────────────────────────────────────────────── ───────────────── ────────────────────────────────────────────────────────────────────────────────────────────────────────────
Add CCTG CustomApplication force-app/main/default/applications/CCTG.app-meta.xml
Add Companyc-Company Layout Layout force-app/main/default/layouts/Companyc-Company Layout.layout-meta.xml
Add Employeec-Employee Layout Layout force-app/main/default/layouts/Employeec-Employee Layout.layout-meta.xml
......
细心的同学应该发现了,package manager方式中也存在package.xml文件,区别在哪呢?
<?xml version="1.0" encoding="UTF-8"?>
[/span>Package xmlns=""
[/span>fullName
[/span>types
[/span>members
...
[/span>name
[/span>types
[/span>members
[/span>members
...
[/span>name
[/span>types
[/span>members
[/span>members
...
[/span>name
[/span>types
[/span>members
...
[/span>name
[/span>types
[/span>members
...
[/span>name
[/span>types
[/span>members
...
[/span>name
[/span>types
[/span>members
...
[/span>name
[/span>types
[/span>members
...
[/span>name
...
[/span>types
[/span>members
...
[/span>name
[/span>types
[/span>members
[/span>name
[/span>types
[/span>members
[/span>name
[/span>version
<span style="color: rgba(0, 0, 255, 1)