Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
X
xy_camera
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
template admin
xy_camera
Commits
3d36cbfc
Commit
3d36cbfc
authored
Mar 24, 2022
by
范雪寒
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 构建时替换所有的uuid
parent
0843d964
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
115 additions
and
86 deletions
+115
-86
buildCocos.js
bin/buildCocos.js
+115
-86
No files found.
bin/buildCocos.js
View file @
3d36cbfc
...
@@ -128,10 +128,10 @@ async function buildWebBundle() {
...
@@ -128,10 +128,10 @@ async function buildWebBundle() {
await
buildCocos
(
args
);
await
buildCocos
(
args
);
}
}
function
createConfigFile
(
projectName
,
type
)
{
function
createConfigFile
(
projectName
,
type
)
{
let
iosVersion
=
""
;
let
iosVersion
=
""
;
let
androidVersion
=
""
;
let
androidVersion
=
""
;
if
(
!
type
)
{
if
(
!
type
)
{
const
androidPaths
=
fs
.
readdirSync
(
`dist/android/
${
projectName
}
`
);
const
androidPaths
=
fs
.
readdirSync
(
`dist/android/
${
projectName
}
`
);
const
androidConfigFileName
=
androidPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
const
androidConfigFileName
=
androidPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
androidVersion
=
androidConfigFileName
.
split
(
'
.
'
)[
1
];
androidVersion
=
androidConfigFileName
.
split
(
'
.
'
)[
1
];
...
@@ -139,11 +139,11 @@ function createConfigFile (projectName, type) {
...
@@ -139,11 +139,11 @@ function createConfigFile (projectName, type) {
const
iosConfigFileName
=
iosPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
const
iosConfigFileName
=
iosPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
iosVersion
=
iosConfigFileName
.
split
(
'
.
'
)[
1
];
iosVersion
=
iosConfigFileName
.
split
(
'
.
'
)[
1
];
}
else
{
}
else
{
if
(
type
==
"
android
"
)
{
if
(
type
==
"
android
"
)
{
const
androidPaths
=
fs
.
readdirSync
(
`dist/android/
${
projectName
}
`
);
const
androidPaths
=
fs
.
readdirSync
(
`dist/android/
${
projectName
}
`
);
const
androidConfigFileName
=
androidPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
const
androidConfigFileName
=
androidPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
androidVersion
=
androidConfigFileName
.
split
(
'
.
'
)[
1
];
androidVersion
=
androidConfigFileName
.
split
(
'
.
'
)[
1
];
}
else
{
}
else
{
const
iosPaths
=
fs
.
readdirSync
(
`dist/ios/
${
projectName
}
`
);
const
iosPaths
=
fs
.
readdirSync
(
`dist/ios/
${
projectName
}
`
);
const
iosConfigFileName
=
iosPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
const
iosConfigFileName
=
iosPaths
.
find
(
path
=>
path
.
indexOf
(
'
config
'
)
==
0
);
iosVersion
=
iosConfigFileName
.
split
(
'
.
'
)[
1
];
iosVersion
=
iosConfigFileName
.
split
(
'
.
'
)[
1
];
...
@@ -164,7 +164,7 @@ function createConfigFile (projectName, type) {
...
@@ -164,7 +164,7 @@ function createConfigFile (projectName, type) {
fs
.
writeFileSync
(
'
dist/config.json
'
,
JSON
.
stringify
(
config
));
fs
.
writeFileSync
(
'
dist/config.json
'
,
JSON
.
stringify
(
config
));
}
}
function
compressAll
(
projectName
)
{
function
compressAll
(
projectName
)
{
const
tarStream
=
new
compressing
.
zip
.
Stream
();
const
tarStream
=
new
compressing
.
zip
.
Stream
();
tarStream
.
addEntry
(
'
dist/play
'
);
tarStream
.
addEntry
(
'
dist/play
'
);
tarStream
.
addEntry
(
'
dist/form
'
);
tarStream
.
addEntry
(
'
dist/form
'
);
...
@@ -176,7 +176,7 @@ function compressAll (projectName) {
...
@@ -176,7 +176,7 @@ function compressAll (projectName) {
tarStream
.
pipe
(
destStream
);
tarStream
.
pipe
(
destStream
);
}
}
function
build_check
()
{
function
build_check
()
{
const
dirNames
=
process
.
cwd
().
split
(
/
\/
|
\\
/
);
const
dirNames
=
process
.
cwd
().
split
(
/
\/
|
\\
/
);
const
projectName
=
dirNames
[
dirNames
.
length
-
1
];
const
projectName
=
dirNames
[
dirNames
.
length
-
1
];
const
path
=
'
assets
'
const
path
=
'
assets
'
...
@@ -211,58 +211,22 @@ function build_check () {
...
@@ -211,58 +211,22 @@ function build_check () {
return
projectName
;
return
projectName
;
}
}
function
changeSettingToWebDesktop
()
{
function
changeSettingToWebDesktop
()
{
const
path
=
'
assets
'
const
path
=
'
assets
'
const
folderName
=
getFolderName
(
path
);
const
folderName
=
getFolderName
(
path
);
editFolderMeta
(
path
,
folderName
,
false
);
editFolderMeta
(
path
,
folderName
,
false
);
}
}
function
changeSettingsToBundle
()
{
function
changeSettingsToBundle
()
{
const
path
=
'
assets
'
const
path
=
'
assets
'
const
folderName
=
getFolderName
(
path
);
const
folderName
=
getFolderName
(
path
);
editFolderMeta
(
path
,
folderName
,
true
);
editFolderMeta
(
path
,
folderName
,
true
);
}
}
function
replaceUuids
()
{
async
function
replaceUuids
()
{
console
.
log
(
'
build_step_0 开始~!
'
);
console
.
log
(
'
build_step_0 开始~!
'
);
const
path
=
'
assets
'
function
getFolderName
(
path
)
{
let
folderName
=
''
;
fs
.
readdirSync
(
path
).
find
(
fileName
=>
{
const
st
=
fs
.
statSync
(
`
${
path
}
/
${
fileName
}
`
);
if
(
st
.
isDirectory
())
{
folderName
=
fileName
;
}
});
return
folderName
;
}
const
folderName
=
getFolderName
(
path
);
let
oldFireUuid
=
''
;
let
oldJsUuid
=
''
;
let
oldJsShortUuid
=
''
;
let
oldJsId
=
''
;
const
fireMetaStr
=
fs
.
readFileSync
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.fire.meta`
);
if
(
fireMetaStr
.
indexOf
(
'
57ea7c61-9b8b-498a-b024-c98ee9124beb
'
)
>
0
)
{
// 老Cocos脚手架
oldFireUuid
=
'
57ea7c61-9b8b-498a-b024-c98ee9124beb
'
;
oldJsUuid
=
'
f4ede462-f8d7-4069-ba80-915611c058ca
'
;
oldJsShortUuid
=
'
f4edeRi+NdAabqAkVYRwFjK
'
;
oldJsId
=
'
e687yyoRBIzZAOVRL8Sseh
'
;
}
if
(
fireMetaStr
.
indexOf
(
'
0737ce42-24f0-45c6-8e1a-8bdab4f74ba3
'
)
>
0
)
{
// 新Cocos脚手架
oldFireUuid
=
'
0737ce42-24f0-45c6-8e1a-8bdab4f74ba3
'
;
oldJsUuid
=
'
408a67f8-65fa-4cf1-8cf2-83e20e1a0fd5
'
;
oldJsShortUuid
=
'
408a6f4ZfpM8Yzyg+IOGg/V
'
;
oldJsId
=
'
eaTVUpqahPfZeO9+sUI7RP
'
;
}
if
(
oldFireUuid
===
''
)
{
return
;
}
function
editFolderMeta
(
path
,
folderName
)
{
function
editFolderMeta
(
path
,
folderName
)
{
const
metaPath
=
`
${
path
}
/
${
folderName
}
.meta`
;
const
metaPath
=
`
${
path
}
/
${
folderName
}
.meta`
;
const
metaDataStr
=
fs
.
readFileSync
(
metaPath
);
const
metaDataStr
=
fs
.
readFileSync
(
metaPath
);
...
@@ -274,7 +238,6 @@ function replaceUuids () {
...
@@ -274,7 +238,6 @@ function replaceUuids () {
if
(
!
fs
.
existsSync
(
path
))
{
if
(
!
fs
.
existsSync
(
path
))
{
return
;
return
;
}
}
const
fileStr
=
fs
.
readFileSync
(
path
);
const
fileStr
=
fs
.
readFileSync
(
path
);
const
newFileStr
=
fileStr
.
toString
().
replace
(
new
RegExp
(
replaceStr
.
replace
(
/
[
.*+?^${}()|[
\]\\]
/g
,
"
\\
$&
"
),
'
g
'
),
newStr
);
const
newFileStr
=
fileStr
.
toString
().
replace
(
new
RegExp
(
replaceStr
.
replace
(
/
[
.*+?^${}()|[
\]\\]
/g
,
"
\\
$&
"
),
'
g
'
),
newStr
);
fs
.
writeFileSync
(
path
,
newFileStr
);
fs
.
writeFileSync
(
path
,
newFileStr
);
...
@@ -284,26 +247,92 @@ function replaceUuids () {
...
@@ -284,26 +247,92 @@ function replaceUuids () {
return
uuid
.
substring
(
0
,
5
)
+
Base64
.
fromUint8Array
(
bytes
).
substring
(
2
);
return
uuid
.
substring
(
0
,
5
)
+
Base64
.
fromUint8Array
(
bytes
).
substring
(
2
);
}
}
editFolderMeta
(
path
,
folderName
);
const
path
=
'
assets
'
function
getFolderName
(
path
)
{
let
folderName
=
''
;
fs
.
readdirSync
(
path
).
find
(
fileName
=>
{
const
st
=
fs
.
statSync
(
`
${
path
}
/
${
fileName
}
`
);
if
(
st
.
isDirectory
())
{
folderName
=
fileName
;
}
});
return
folderName
;
}
async
function
fileForEach
(
src
,
func
)
{
//读取目录
const
st
=
fs
.
statSync
(
src
);
if
(
st
.
isFile
())
{
await
func
(
src
);
return
;
}
const
paths
=
fs
.
readdirSync
(
src
);
for
(
let
i
=
0
;
i
<
paths
.
length
;
i
++
)
{
let
path
=
paths
[
i
];
const
newSrc
=
`
${
src
}
/
${
path
}
`
;
const
st
=
fs
.
statSync
(
newSrc
);
if
(
st
.
isFile
())
{
await
func
(
newSrc
);
}
else
if
(
st
.
isDirectory
())
{
await
fileForEach
(
newSrc
,
func
);
}
}
}
const
newFireUuid
=
v4
();
function
createUuidData
(
uuid
)
{
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.fire.meta`
,
oldFireUuid
,
newFireUuid
);
const
shortUuid
=
getShortUuid
(
uuid
);
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.fire`
,
oldFireUuid
,
newFireUuid
);
const
newUuid
=
v4
();
fileReplace
(
'
settings/builder.json
'
,
oldFireUuid
,
newFireUuid
);
const
newShortUuid
=
getShortUuid
(
newUuid
);
return
{
oldUuid
:
uuid
,
oldShortUuid
:
shortUuid
,
newUuid
:
newUuid
,
newShortUuid
:
newShortUuid
,
};
}
const
newJsUuid
=
v4
();
const
uuidList
=
[];
const
newJsShortUuid
=
getShortUuid
(
newJsUuid
);
await
fileForEach
(
path
,
(
path
)
=>
{
const
newJsId
=
v4
().
replace
(
/-/g
,
''
).
substring
(
0
,
oldJsId
.
length
);
const
nameList
=
path
.
split
(
'
.
'
);
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.ts.meta`
,
oldJsUuid
,
newJsUuid
);
if
(
nameList
[
nameList
.
length
-
1
]
==
'
meta
'
)
{
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.js.meta`
,
oldJsUuid
,
newJsUuid
);
const
data
=
JSON
.
parse
(
fs
.
readFileSync
(
path
));
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.fire`
,
oldFireUuid
,
newFireUuid
);
uuidList
.
push
(
createUuidData
(
data
.
uuid
));
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.fire`
,
oldJsShortUuid
,
newJsShortUuid
);
if
(
data
.
subMetas
)
{
fileReplace
(
`assets/
${
folderName
}
/scene/
${
folderName
}
.fire`
,
oldJsId
,
newJsId
);
for
(
const
key
in
data
.
subMetas
)
{
const
subMet
=
data
.
subMetas
[
key
];
if
(
subMet
.
uuid
)
{
uuidList
.
push
(
createUuidData
(
subMet
.
uuid
));
}
}
}
}
});
const
pathList
=
[
path
,
'
settings
'
,
'
project.json
'
];
for
(
let
i
=
0
;
i
<
pathList
.
length
;
i
++
)
{
const
path
=
pathList
[
i
];
await
fileForEach
(
path
,
(
path
)
=>
{
const
nameList
=
path
.
split
(
'
.
'
);
const
expectNameList
=
[
'
png
'
,
'
jpg
'
,
'
mp3
'
,
'
wav
'
];
if
(
expectNameList
.
includes
(
nameList
[
nameList
.
length
-
1
]))
{
return
;
}
const
data
=
fs
.
readFileSync
(
path
);
uuidList
.
forEach
(
uuiddata
=>
{
if
(
data
.
includes
(
uuiddata
.
oldUuid
))
{
fileReplace
(
path
,
uuiddata
.
oldUuid
,
uuiddata
.
newUuid
);
}
if
(
data
.
includes
(
uuiddata
.
oldShortUuid
))
{
fileReplace
(
path
,
uuiddata
.
oldShortUuid
,
uuiddata
.
newShortUuid
);
}
});
});
}
console
.
log
(
'
build_step_0 完成~!
'
);
console
.
log
(
'
build_step_0 完成~!
'
);
}
}
function
replaceIndexHtml
()
{
function
replaceIndexHtml
()
{
const
data
=
fs
.
readFileSync
(
'
index.html
'
);
const
data
=
fs
.
readFileSync
(
'
index.html
'
);
fs
.
writeFileSync
(
'
dist/play/index.html
'
,
data
);
fs
.
writeFileSync
(
'
dist/play/index.html
'
,
data
);
}
}
...
@@ -324,7 +353,7 @@ module.exports = {
...
@@ -324,7 +353,7 @@ module.exports = {
await
buildForm
();
await
buildForm
();
// 替换uuid
// 替换uuid
replaceUuids
();
await
replaceUuids
();
// 改设置为非bundle
// 改设置为非bundle
changeSettingToWebDesktop
();
changeSettingToWebDesktop
();
...
@@ -377,7 +406,7 @@ module.exports = {
...
@@ -377,7 +406,7 @@ module.exports = {
// 构建前检查
// 构建前检查
const
projectName
=
build_check
();
const
projectName
=
build_check
();
// 替换uuid
// 替换uuid
replaceUuids
();
await
replaceUuids
();
// 改设置为bundle
// 改设置为bundle
changeSettingsToBundle
();
changeSettingsToBundle
();
...
@@ -397,7 +426,7 @@ module.exports = {
...
@@ -397,7 +426,7 @@ module.exports = {
// 构建前检查
// 构建前检查
const
projectName
=
build_check
();
const
projectName
=
build_check
();
// 替换uuid
// 替换uuid
replaceUuids
();
await
replaceUuids
();
// 改设置为bundle
// 改设置为bundle
changeSettingsToBundle
();
changeSettingsToBundle
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment