Created
May 19, 2024 12:50
-
-
Save BlayTheNinth/b6093c5ccede648f03f45947199a1ab1 to your computer and use it in GitHub Desktop.
port.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const { execSync } = require('child_process'); | |
function copyFolderSync(source, destination, excludeDirs) { | |
if (!fs.existsSync(destination)) { | |
fs.mkdirSync(destination, { recursive: true }); | |
} | |
const items = fs.readdirSync(source, { withFileTypes: true }); | |
items.forEach((item) => { | |
const srcPath = path.join(source, item.name); | |
const destPath = path.join(destination, item.name); | |
if (item.isDirectory()) { | |
if (!excludeDirs.includes(item.name)) { | |
copyFolderSync(srcPath, destPath, excludeDirs); | |
} | |
} else { | |
fs.copyFileSync(srcPath, destPath); | |
} | |
}); | |
} | |
function createGitBranch(directory, branchName) { | |
try { | |
execSync(`git -C ${directory} pull`, { stdio: 'inherit' }); | |
execSync(`git -C ${directory} checkout -b ${branchName}`, { stdio: 'inherit' }); | |
console.log(`Git branch '${branchName}' created in ${directory}`); | |
} catch (error) { | |
console.error(`Failed to create git branch: ${error.message}`); | |
} | |
} | |
function updateGradleProperties(template, destination) { | |
const gradlePropertiesPath = path.join(destination, 'gradle.properties'); | |
if (!fs.existsSync(gradlePropertiesPath)) { | |
console.log('gradle.properties file not found.'); | |
return; | |
} | |
const gradlePropertiesContent = fs.readFileSync(gradlePropertiesPath, 'utf-8'); | |
const updatedPropertiesContent = mergeProperties(gradlePropertiesContent, template.properties); | |
fs.writeFileSync(gradlePropertiesPath, updatedPropertiesContent, 'utf-8'); | |
console.log('gradle.properties updated successfully.'); | |
} | |
function mergeProperties(gradleContent, templateProperties) { | |
const lines = gradleContent.split('\n'); | |
const unusedProperties = {...templateProperties}; | |
const mergedLines = lines.map((line) => { | |
const trimmedLine = line.trim(); | |
if (trimmedLine && !trimmedLine.startsWith('#') && trimmedLine.includes('=')) { | |
const [key] = trimmedLine.split('=').map((part) => part.trim()); | |
if (templateProperties[key] !== undefined) { | |
delete unusedProperties[key]; | |
return `${key} = ${templateProperties[key]}`; | |
} | |
} | |
return line; | |
}); | |
Object.keys(unusedProperties).forEach((key) => { | |
mergedLines.push(`${key} = ${unusedProperties[key]}`); | |
}) | |
return mergedLines.join('\n'); | |
} | |
function bumpModVersion(destination) { | |
const gradlePropertiesPath = path.join(destination, 'gradle.properties'); | |
if (fs.existsSync(gradlePropertiesPath)) { | |
let gradlePropertiesContent = fs.readFileSync(gradlePropertiesPath, 'utf-8'); | |
const versionRegex = /\bversion\s*=\s*(\d+)\.(\d+)\.(\d+)/; | |
const match = gradlePropertiesContent.match(versionRegex); | |
if (match) { | |
const major = parseInt(match[1]); | |
const newVersion = `${major + 1}.0.0`; | |
gradlePropertiesContent = gradlePropertiesContent.replace(versionRegex, `version = ${newVersion}`); | |
fs.writeFileSync(gradlePropertiesPath, gradlePropertiesContent, 'utf-8'); | |
console.log(`gradle.properties version bumped to ${newVersion}`); | |
} else { | |
console.log('Unable to find version property in gradle.properties'); | |
} | |
} else { | |
console.log('gradle.properties file not found.'); | |
} | |
} | |
function writeChangelog(destination, newVersion) { | |
const changelogPath = path.join(destination, 'CHANGELOG.md'); | |
const changelogEntry = `Updated to Minecraft ${newVersion}`; | |
if (fs.existsSync(changelogPath)) { | |
const changelogContent = fs.readFileSync(changelogPath, 'utf-8'); | |
const updatedChangelog = `${changelogEntry}\n\n${changelogContent}`; | |
fs.writeFileSync(changelogPath, updatedChangelog, 'utf-8'); | |
console.log('CHANGELOG.md updated successfully.'); | |
} else { | |
console.log('CHANGELOG.md file not found.'); | |
} | |
} | |
function renameFiles(template, destination) { | |
const filesToRename = template.rename; | |
if (filesToRename) { | |
Object.keys(filesToRename).forEach((oldFile) => { | |
const newFile = filesToRename[oldFile]; | |
const oldPath = path.join(destination, oldFile); | |
const newPath = path.join(destination, newFile); | |
if (fs.existsSync(oldPath)) { | |
fs.renameSync(oldPath, newPath); | |
console.log(`Renamed ${oldFile} to ${newFile}`); | |
} else { | |
console.log(`File ${oldFile} not found.`); | |
} | |
}); | |
} | |
} | |
function copyTemplateFiles(templateDir, destination) { | |
if (!fs.existsSync(templateDir)) { | |
return; | |
} | |
const items = fs.readdirSync(templateDir, { withFileTypes: true }); | |
items.forEach((item) => { | |
const srcPath = path.join(templateDir, item.name); | |
const destPath = path.join(destination, item.name); | |
if (item.isDirectory()) { | |
if (!fs.existsSync(destPath)) { | |
fs.mkdirSync(destPath, { recursive: true }); | |
} | |
copyTemplateFiles(srcPath, destPath); | |
} else { | |
if (!fs.existsSync(path.dirname(destPath))) { | |
fs.mkdirSync(path.dirname(destPath), { recursive: true }); | |
} | |
fs.copyFileSync(srcPath, destPath); | |
} | |
}); | |
} | |
function main() { | |
if (process.argv.length < 5) { | |
console.log('Usage: node port.js <folder> <oldVersion> <newVersion>'); | |
process.exit(1); | |
} | |
const folder = process.argv[2].replace(/[<>:"\/\\|?*]/g, '').replace(/\.$/, ''); | |
const oldVersion = process.argv[3].replace(/[<>:"\/\\|?*]/g, '').replace(/\.$/, ''); | |
const newVersion = process.argv[4].replace(/[<>:"\/\\|?*]/g, '').replace(/\.$/, ''); | |
const excludeDirs = ['.idea', '.gradle']; | |
const source = path.join(oldVersion, folder); | |
const destination = path.join(newVersion, folder); | |
if (!fs.existsSync(source)) { | |
console.log(`Source folder ${source} does not exist.`); | |
process.exit(1); | |
} | |
const templateJsonPath = path.join(newVersion, 'template.json'); | |
if (!fs.existsSync(templateJsonPath)) { | |
console.log('template.json file not found.'); | |
return; | |
} | |
const template = JSON.parse(fs.readFileSync(templateJsonPath, 'utf-8')); | |
// Create a git branch in the old version folder | |
createGitBranch(source, oldVersion); | |
// Copy the folder | |
copyFolderSync(source, destination, excludeDirs); | |
renameFiles(template, destination); | |
copyTemplateFiles(path.join(newVersion, 'template'), destination); | |
// Update gradle.properties | |
updateGradleProperties(template, destination); | |
bumpModVersion(destination); | |
writeChangelog(destination, newVersion); | |
console.log(`Folder copied successfully from ${source} to ${destination}.`); | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"properties": { | |
"minecraft_version": "1.20.6", | |
"minecraft_versions": "1.20.6", | |
"minecraft_version_range": "[1.20.6,1.21)", | |
"pack_format_number": "18", | |
"java_version": "21", | |
"forge_version": "50.0.22", | |
"forge_version_range": "[50,)", | |
"forge_loader_version_range": "[50,)", | |
"neoforge_version": "20.6.70-beta", | |
"neoforge_version_range": "[20.6,)", | |
"neoforge_loader_version_range": "[1,)", | |
"fabric_version": "0.98.0+1.20.6", | |
"fabric_loader_version": "0.15.11", | |
"balm_version": "10.1.0-SNAPSHOT", | |
"balm_version_range": "[10.0.0,)", | |
"mod_author": "BlayTheNinth", | |
"credits": "BlayTheNinth" | |
}, | |
"rename": { | |
"shared": "common", | |
"neoforge/src/main/resources/META-INF/mods.toml": "neoforge/src/main/resources/META-INF/neoforge.mods.toml" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment