diff --git a/.github/workflows/native-bindings.yml b/.github/workflows/native-bindings.yml index cadfefd69d..8c03ce742e 100644 --- a/.github/workflows/native-bindings.yml +++ b/.github/workflows/native-bindings.yml @@ -3,6 +3,9 @@ name: Glue on: pull_request_target: types: [closed] + paths: + - 'templates/**' + - 'native/**' jobs: generate-jsb: diff --git a/.github/workflows/native-clang-format.yml b/.github/workflows/native-clang-format.yml index a5342fb904..be7590727d 100644 --- a/.github/workflows/native-clang-format.yml +++ b/.github/workflows/native-clang-format.yml @@ -4,6 +4,9 @@ name: Apply clang format on: pull_request_target: types: [closed] + paths: + - 'templates/**' + - 'native/**' jobs: Clang-Format: diff --git a/.github/workflows/native-compile-linux.yml b/.github/workflows/native-compile-linux.yml index 56c1c41d46..ec3536cfa1 100644 --- a/.github/workflows/native-compile-linux.yml +++ b/.github/workflows/native-compile-linux.yml @@ -1,6 +1,10 @@ name: Linux -on: [pull_request] +on: + pull_request: + paths: + - 'templates/**' + - 'native/**' jobs: compile_linux: diff --git a/.github/workflows/native-compile-platforms.yml b/.github/workflows/native-compile-platforms.yml index 1217b387b4..1831aedad9 100644 --- a/.github/workflows/native-compile-platforms.yml +++ b/.github/workflows/native-compile-platforms.yml @@ -1,6 +1,10 @@ name: Compile -on: [pull_request] +on: + pull_request: + paths: + - 'templates/**' + - 'native/**' jobs: compile_windows: diff --git a/.github/workflows/native-linter-android.yml b/.github/workflows/native-linter-android.yml index bdf881f1ba..4b7fd83ca3 100644 --- a/.github/workflows/native-linter-android.yml +++ b/.github/workflows/native-linter-android.yml @@ -1,6 +1,10 @@ name: Linter -on: pull_request +on: + pull_request: + paths: + - 'templates/**' + - 'native/**' jobs: # Set the job key. The key is displayed as the job name diff --git a/.github/workflows/native-sync-template-version.yml b/.github/workflows/native-sync-template-version.yml new file mode 100644 index 0000000000..df2e35c1ef --- /dev/null +++ b/.github/workflows/native-sync-template-version.yml @@ -0,0 +1,38 @@ +name: Template Validation + +#on: [pull_request] +on: + pull_request: + paths: + - 'templates/common/**' + - 'templates/android/**' + - 'templates/windows/**' + - 'templates/ios/**' + - 'templates/mac/**' + - 'templates/qnx/**' + - 'templates/linux/**' + - 'templates/ohos/**' + +jobs: + compile_linux: + name: "Linux-Ubuntu" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - id: files + continue-on-error: true + name: compatibility-info.json need update + uses: jitterbit/get-changed-files@v1 + - run: | + subfile="templates/compatibility-info.json" + for changed_file in ${{ steps.files.outputs.all }}; do + echo "Do something with this ${changed_file}." + if [[ "$changed_file" =~ .*"$subfile" ]]; then + echo "compatibility-info.json updated!" + exit 0 + else + echo "Test file $changed_file" + fi + done + echo "native templates is modified, pls consider if 'templates/compatibility-info.json' needs to be updated." + exit 1 \ No newline at end of file diff --git a/.github/workflows/native-unit-test.yml b/.github/workflows/native-unit-test.yml index 727b06daf6..17a09bfd2a 100644 --- a/.github/workflows/native-unit-test.yml +++ b/.github/workflows/native-unit-test.yml @@ -1,6 +1,10 @@ name: Run Tests -on: [pull_request] +on: + pull_request: + paths: + - 'templates/**' + - 'native/**' jobs: unit-test-with-cmake: diff --git a/EngineErrorMap.md b/EngineErrorMap.md index eaafe27e7b..cc43012bd5 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -3263,3 +3263,8 @@ Diffuse map resource is missing, please change environment lighting type to rege ### 15003 The shadow visible distance is so small that CSM stratification is not effective, Please change the value of shadowDistance so that it is 10 times greater than 0.1 + + +### 15004 + +The native folder may be generated from older versions, please refer https://docs.cocos.com/creator/manual/en/release-notes/ to upgrade. \ No newline at end of file diff --git a/native/cmake/scripts/plugins_parser.js b/native/cmake/scripts/plugins_parser.js index 287fd482fa..f39884ca58 100644 --- a/native/cmake/scripts/plugins_parser.js +++ b/native/cmake/scripts/plugins_parser.js @@ -47,7 +47,7 @@ function search_cc_config_json_levels(dir, depth) { for (let searchPath of PROJ_SEARCH_PATHS) { if (!fs.existsSync(searchPath)) { - console.warn(`[searching plugins] directory ${searchPath} does not exist`); + console.log(`[searching plugins] directory ${searchPath} does not exist`); continue; } search_cc_config_json_levels(searchPath, 1); @@ -55,7 +55,7 @@ for (let searchPath of PROJ_SEARCH_PATHS) { if (cc_config_json_list.length === 0) { - console.warn("[searching plugins] no plugins found!"); + console.log("[searching plugins] no plugins found!"); process.exit(0) } @@ -207,13 +207,13 @@ for (let plugin_dir of cc_config_json_list) { continue; } if (!test_enable_by_configurations(cc_plugin_json)) { - console.warn(` ${maybe_plugin_name} disabled by configuration`); + console.log(` ${maybe_plugin_name} disabled by configuration`); continue; } const plugin_name = cc_plugin_json.name; const module_type = get_property_variants(cc_plugin_json, "module_type") if (module_type !== undefined && module_type !== 'release') { - console.warn(` plugin ${plugin_name} is not a release, should be include or add_subdirectory in dev env.`); + console.log(` plugin ${plugin_name} is not a release, should be include or add_subdirectory in dev env.`); continue; } const packages = parse_package_dependency(cc_plugin_json); @@ -247,7 +247,7 @@ for (let plugin_dir of cc_config_json_list) { if (packages.length > 0) { console.log(` record plugin ${plugin_name}`); } else { - console.warn(` no sub module found`); + console.log(` no sub module found`); } } catch (e) { console.error(`Parsing plugin directory: ${plugin_dir}`) diff --git a/scripts/native-pack-tool/source/base/default.ts b/scripts/native-pack-tool/source/base/default.ts index 26b88e0248..e7f299b7c4 100644 --- a/scripts/native-pack-tool/source/base/default.ts +++ b/scripts/native-pack-tool/source/base/default.ts @@ -56,6 +56,8 @@ export interface ICMakeConfig { export type InternaleNativePlatform = 'mac' | 'android' | 'windows' | 'ios' | 'ohos'; +const ErrorCodeIncompatible = 15004; + export interface INativePlatformOptions { extends?: InternaleNativePlatform, //传入继承的平台,将会继承已有平台注册的一些代码 overwrite?: InternaleNativePlatform, //传入继承但如果有同名的方法等会复写平台,将会继承已有平台注册的一些代码 @@ -92,15 +94,226 @@ export abstract class NativePackTool { } protected async copyCommonTemplate() { - // 拷贝引擎原生模板到项目的 native 目录下 TODO 为何每次都重新拷贝? - await fs.copy(this.paths.commonDirInCocos, this.paths.commonDirInPrj); + if (!fs.existsSync(ps.join(this.paths.commonDirInPrj, 'CMakeLists.txt'))) { + await fs.copy(this.paths.commonDirInCocos, this.paths.commonDirInPrj, { overwrite: false }); + } + } + + private get projEngineVersionPath() { + return ps.join(this.paths.commonDirInPrj, 'cocos-version.json'); + } + + private _debugInfo: any = null; + private get DebugInfos(): { [key: number]: string } { + if (!this._debugInfo) { + this._debugInfo = require(ps.join(Paths.enginePath, 'DebugInfos.json')); + if (!this._debugInfo) { + console.error(`Failed to load DebugInfos.json`); + } + } + return this._debugInfo; + } + + private _versionParser: any = null; + private get versionParser() { + if (!this._versionParser) { + const scriptPath = ps.join(Paths.enginePath, 'native/cmake/scripts/plugin_support/plugin_cfg.js'); + this._versionParser = require(scriptPath); + } + return this._versionParser; + } + + /** + * Read version number from cocos-version.json + */ + protected tryReadProjectTemplateVersion(): { version: string, skipCheck: boolean | undefined } | null { + const versionJsonPath = this.projEngineVersionPath; + if (!fs.existsSync(versionJsonPath)) { + console.warn(`${versionJsonPath} not exists`); + return null; + } + try { + const content = fs.readJsonSync(versionJsonPath); + if (content.version === undefined) { + console.error(`Field 'version' missing in ${versionJsonPath}`); + return null; + } + return content; + } catch (e) { + console.error(`Failed to read json file ${versionJsonPath}`); + console.error(e); + } + return null; + } + + /** + * Read package.json file in the root folder and return the version field. + */ + protected tryGetEngineVersion(): string | null { + const pkgJSON = ps.join(Paths.enginePath, 'package.json'); + if (!fs.existsSync(pkgJSON)) { + console.error(`Failed to read file ${pkgJSON}`); + return null; + } + return fs.readJsonSync(pkgJSON).version || "3.6.0"; + } + + /** + * Version condition from compatibility-info.json for current platform. + */ + protected tryGetCompatibilityInfo(): string | null { + const compInfo = ps.join(Paths.enginePath, 'templates/compatibility-info.json'); + if (!fs.existsSync(compInfo)) { + console.error(`${compInfo} does not exist`); + return null; + } + const json = fs.readJsonSync(compInfo); + if (!json.native) { + console.error(`${compInfo} does not contain "native" field`); + return null; + } + const native = json.native; + const defaultCfg = native.default; + if (!defaultCfg) { + console.error(`${compInfo} does not contain "native.default" field`); + return null; + } + const plt = this.params.platform; + if (!native[plt]) { + return defaultCfg; + } + return native[plt]; + } + + private skipVersionCheck = false; + /** + * The engine version used to generate the 'native/' folder should match the + * condition written in the 'compatibility-info.json' file. + */ + private validateTemplateVersion(): boolean { + console.log(`Checking template version...`); + const engineVersion = this.tryGetEngineVersion(); + const projEngineVersionObj = this.tryReadProjectTemplateVersion(); + if (projEngineVersionObj === null) { + console.error(`Error code ${ErrorCodeIncompatible}, ${this.DebugInfos[ErrorCodeIncompatible]}`) + return false; + } + let versionRange = this.tryGetCompatibilityInfo(); + const projEngineVersion = projEngineVersionObj?.version; + if (!versionRange) { + console.error(`Ignore version range check`); + return true; + } + if (projEngineVersionObj.skipCheck === true) { + console.log(`Skip version range check by project`); + this.skipVersionCheck = true; + return true; + } + let cond = this.versionParser.parse(versionRange); + if (!cond) { + return true; + } + if (cond.match(projEngineVersion)) { + + const newerThanEngineVersion = this.versionParser.parse(`>${engineVersion}`); + if (newerThanEngineVersion.match(projEngineVersion)) { + console.warn(`warning: ${projEngineVersion} is newer than engine version ${engineVersion}`); + } + return true; + } + console.error(`'native/' folder was generated by ${projEngineVersion} which is incompatible with ${engineVersion}, condition: '${versionRange}'`); + console.error(`${this.DebugInfos[ErrorCodeIncompatible]}`) + return false; + } + + /** + * Utility function to check if a file exists dst as in src. + */ + protected validateDirectory(src: string, dst: string, missingDirs: string[]) { + if (!fs.existsSync(dst)) { + missingDirs.push(dst); + return; + } + const st = fs.statSync(src); + if (!st.isDirectory()) { + return; + } + let list = fs.readdirSync(src); + for (let f of list) { + if(f.startsWith('.')) continue; + this.validateDirectory(ps.join(src, f), ps.join(dst, f), missingDirs); + } + } + + /** + * Check files under `native/engine/platform` folder + */ + protected validatePlatformDirectory(missing: string[]): void { + console.log(`Validating platform source code directories...`); + const srcDir = ps.join(this.paths.nativeTemplateDirInCocos, this.params.platform); + const dstDir = this.paths.platformTemplateDirInPrj; + this.validateDirectory(srcDir, dstDir, missing) + } + + /** + * Check if any file removed from the 'native/' folder + */ + private validateTemplateConsistency() { + console.log(`Validating template consistency...`); + let commonSrc = this.paths.commonDirInCocos; + let commonDst = this.paths.commonDirInPrj; + let missingDirs: string[] = []; + // validate common directory + this.validateDirectory(commonSrc, commonDst, missingDirs); + this.validatePlatformDirectory(missingDirs); + if (missingDirs.length > 0) { + console.error(`Following files are missing`); + for (let f of missingDirs) { + console.error(` ${f}`); + } + console.error(`Consider fix the problem or remove the directory`); + return false; + } + return true; + } + + /** + * - Ensure the engine version used to generete 'native/' folder is compatible + * with the current engine version. + * - Check if any file under the 'native/' folder is removed. + */ + protected validateNativeDir() { + try { + if (this.validateTemplateVersion()) { + if (!this.skipVersionCheck && !this.validateTemplateConsistency()) { + console.error(`Failed to validate "native" directory`); + } + } + } catch (e) { + console.error(`Failed to validate native directory`); + console.error(e); + } + } + + /** + * Write cocos-version.json into native/common/cocos-version.json + */ + protected writeEngineVersion() { + if (!fs.existsSync(this.projEngineVersionPath)) { + fs.writeJSON(this.projEngineVersionPath, { + version: this.tryGetEngineVersion(), + skipCheck: false, + }); + } } protected async copyPlatformTemplate() { - // TODO version check if (!fs.existsSync(this.paths.platformTemplateDirInPrj)) { - // 拷贝 lite 仓库的 templates/平台/ 文件到 native 目录 + // 拷贝 templates/平台/ 文件到 "native" 目录 await fs.copy(ps.join(this.paths.nativeTemplateDirInCocos, this.params.platform), this.paths.platformTemplateDirInPrj, { overwrite: false }); + this.writeEngineVersion(); + } else { + this.validateNativeDir(); } } @@ -223,7 +436,7 @@ export abstract class NativePackTool { config.encrypted = true; fs.writeJSONSync(configPath, config); - + fs.copySync(scriptDest, ps.join(backupPath, ps.relative(this.paths.buildAssetsDir, scriptDest))); fs.removeSync(scriptDest); } @@ -241,9 +454,9 @@ export abstract class NativePackTool { } } - create?(): Promise; - make?(): Promise; - run?(): Promise; + create?(): Promise; + make?(): Promise; + run?(): Promise; } // cocos.compile.json @@ -283,7 +496,7 @@ export class CocosParams { * @zh 是否压缩脚本 * @en is compress script */ - compressZip?: boolean; + compressZip?: boolean; /** * @zh 加密密钥 * @en encrypt Key @@ -301,7 +514,7 @@ export class CocosParams { CC_USE_GLES2: true, USE_SERVER_MODE: 'set(USE_SERVER_MODE OFF)', NET_MODE: 'set(NET_MODE 0)', - XXTEAKEY : '', + XXTEAKEY: '', CC_ENABLE_SWAPPY: false, } diff --git a/scripts/native-pack-tool/source/platforms/android.ts b/scripts/native-pack-tool/source/platforms/android.ts index 74316ea26e..08b8db0fb4 100644 --- a/scripts/native-pack-tool/source/platforms/android.ts +++ b/scripts/native-pack-tool/source/platforms/android.ts @@ -40,11 +40,20 @@ export class AndroidPackTool extends NativePackTool { } // 原生工程不重复拷贝 TODO 复用前需要做版本检测 if (!fs.existsSync(this.paths.platformTemplateDirInPrj)) { - // 拷贝 lite 仓库的 templates/android/build 文件到构建输出目录 + // 拷贝 lite 仓库的 templates/android/template 文件到构建输出目录 await fs.copy(ps.join(this.paths.nativeTemplateDirInCocos, this.params.platform, 'template'), this.paths.platformTemplateDirInPrj, { overwrite: false }); + this.writeEngineVersion(); + } else { + this.validateNativeDir(); } } + protected validatePlatformDirectory(missing: string[]): void { + const srcDir = ps.join(this.paths.nativeTemplateDirInCocos, this.params.platform, 'template'); + const dstDir = this.paths.platformTemplateDirInPrj; + this.validateDirectory(srcDir, dstDir, missing); + } + async create() { await this.copyCommonTemplate(); await this.copyPlatformTemplate(); @@ -141,7 +150,7 @@ export class AndroidPackTool extends NativePackTool { console.log(`update settings.properties`); await cchelper.replaceInFile([ { reg: '^rootProject\\.name.*', text: `rootProject.name = "${this.params.projectName}"` }, - { reg: ':CocosGame', text: `:${this.params.projectName}`} + { reg: ':CocosGame', text: `:${this.params.projectName}` } ], ps.join(this.paths.nativePrjDir, 'settings.gradle')); console.log(`update gradle.properties`); @@ -215,7 +224,7 @@ export class AndroidPackTool extends NativePackTool { console.log('android instant not configured'); return; } - const url =this.params.platformParams.remoteUrl; + const url = this.params.platformParams.remoteUrl; if (!url) { return; } @@ -278,7 +287,7 @@ export class AndroidPackTool extends NativePackTool { return true; } -// ---------------------------- run ------------------------- // + // ---------------------------- run ------------------------- // async run() { if (await this.install()) { diff --git a/templates/README.md b/templates/README.md new file mode 100644 index 0000000000..28223e9d0a --- /dev/null +++ b/templates/README.md @@ -0,0 +1,45 @@ + +### `compatibility-info.json` + +This file is used to declare the compatibility of template files with previous versions. + + +Example: + +```json +{ + // include all supported native platforms, such as windows, ios, android, mac etc. + "native": // required + { + "default": ">=3.6.0", // required, applied if any specific platform value is not provided + "windows": VERSION_RANGE // optional, supported version for Windows + "mac": VERSION_RANGE // optional, supported version for Mac + "ios": VERSION_RANGE // optional, supported version for iOS + "android": VERSION_RANGE // optional, supported version for Android + } +} +``` + +#### The `VERSION_RANGE` syntax examples + +##### Simple conditions + +1. `>=3.6.0` +2. `>3.5.1` +3. `<3.5.1` +4. `<=3.5.1` +5. `3.3.2` , specify version +6. `!3.5.0` , excluded version + +##### Composite conditions + +The space is the 'AND' operation, and the '||' is the 'OR' operation + +1. `>=3.6.0 <3.7.0` +2. `3.4.2 || >= 3.6.0` +3. `>=3.4.0 !3.4.2 <3.5.0 || 3.6.0` + +##### Wildcard conditions + +1. `3.x` is equivalent to `>=3.0.0 <4.0.0` +2. `3.4.x` is equivalent to `>= 3.4.0 <3.5.0 diff --git a/templates/compatibility-info.json b/templates/compatibility-info.json new file mode 100644 index 0000000000..f6ce2ecba2 --- /dev/null +++ b/templates/compatibility-info.json @@ -0,0 +1,5 @@ +{ + "native": { + "default": ">=3.6.0" + } +} \ No newline at end of file