Organize the codebase

Add support for dotnet projects

Signed-off-by: Shakar <5h4k4r.b4kr@gmail.com>
This commit is contained in:
Shakar 2023-09-19 10:58:54 +03:00
parent 1e31d49f27
commit 8d8142bf0e
No known key found for this signature in database
GPG Key ID: DA55A26823AE3C28

View File

@ -11,17 +11,15 @@ async function run() {
const filePathInput = core.getInput('filePath'); const filePathInput = core.getInput('filePath');
const labelInput = core.getInput('label'); const labelInput = core.getInput('label');
const filePath = getProjectInfoFile(filePathInput); const file = getProjectInfoFile(filePathInput);
console.log(`Label: ${labelInput}`) console.log(`Label: ${labelInput}`)
console.log(`File path: ${filePath}`) console.log(`File path: ${file}`)
core.setOutput("label", labelInput); core.setOutput("label", labelInput);
const packageJson = require(filePath); const version = getProjectVersion(file);
const version = packageJson.version;
// the version is in semantic format, so we can split it by dot // the version is in semantic format, so we can split it by dot
const versionParts = version.split('.'); const versionParts = version.split('.');
@ -45,14 +43,13 @@ async function run() {
// join the parts back together // join the parts back together
const newVersion = versionParts.join('.'); const newVersion = versionParts.join('.');
updateProjectVersion(file, newVersion);
console.log(`Old version: ${version}. New version: ${newVersion}`) console.log(`Old version: ${version}. New version: ${newVersion}`)
packageJson.version = newVersion; fs.writeFileSync(file, JSON.stringify(projectInfoFile, null, 2));
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2)); await commitChanges(core.getInput('filePath'), file);
await commitChanges(core.getInput('filePath'));
const payload = JSON.stringify(github.context.payload, undefined, 2) const payload = JSON.stringify(github.context.payload, undefined, 2)
@ -64,9 +61,9 @@ async function run() {
// Region functions // Region functions
async function commitChanges(filePath) { async function commitChanges(filePath, file) {
const commitMessage = 'Commit message here'; const commitMessage = 'Commit message here';
const newContent = 'New content to be added'; const newContent = file;
const githubToken = core.getInput('githubToken'); const githubToken = core.getInput('githubToken');
@ -83,7 +80,6 @@ async function commitChanges(filePath) {
`https://api.github.com/repos/${owner}/${repo}/branches/${branch}` `https://api.github.com/repos/${owner}/${repo}/branches/${branch}`
); );
console.log('branchResponse: ' + branchResponse.data)
const baseTreeSha = branchResponse.data.commit.sha; const baseTreeSha = branchResponse.data.commit.sha;
// Create a new blob with the updated content // Create a new blob with the updated content
@ -100,7 +96,6 @@ async function commitChanges(filePath) {
}, },
} }
); );
console.log('blobResponse success')
const newBlobSha = blobResponse.data.sha; const newBlobSha = blobResponse.data.sha;
// Create a new tree with the updated blob // Create a new tree with the updated blob
@ -164,14 +159,32 @@ async function commitChanges(filePath) {
} }
} }
function getProjectInfoFile(filePathInput) { function getProjectInfoFile(filePath) {
if (filePathInput == null || filePathInput == undefined || filePathInput == '') { if (filePath == null || filePath == undefined || filePath == '') {
// List files inside the root directory of the repository // List files inside the root directory of the repository
const files = fs.readdirSync(process.cwd()); const files = fs.readdirSync(process.cwd());
// Return the first file that matches .csproj or package.json // Return the first file that matches .csproj or package.json
const projectInfoFile = files.find(file => file.match(/\.csproj|package\.json/)); const projectInfoFile = files.find(file => file.match(/\.csproj|package\.json/));
return `${process.cwd()}/${projectInfoFile}`; return require(`${process.cwd()}/${projectInfoFile}`);
} }
else else
return `${process.cwd()}/${filePathInput}`; return require(`${process.cwd()}/${filePath}`);
}
function getProjectVersion(file) {
// Update the version if the file is .csproj
if (file.match(/\.csproj/))
return file.Project.PropertyGroup[0].Version;
else if (file.match(/package\.json/))
return file.version;
}
function updateProjectVersion(file, newVersion) {
// Update the version if the file is .csproj
if (file.match(/\.csproj/))
file.Project.PropertyGroup[0].Version = newVersion;
else if (file.match(/package\.json/))
file.version = newVersion;
} }