diff --git a/deadlock-plugins/deadlock-extension/src/core/gitMission.ts b/deadlock-plugins/deadlock-extension/src/core/gitMission.ts index 99b2c63e14a600c1b49a091cdf7e4b934dc3abda..050b1403f1ed0cf33d3d05722fc859a6be515d4a 100644 --- a/deadlock-plugins/deadlock-extension/src/core/gitMission.ts +++ b/deadlock-plugins/deadlock-extension/src/core/gitMission.ts @@ -34,6 +34,11 @@ export default class GitMission { } + get author() { + + return this.userConfig.getUsername(); + } + async init() { try { diff --git a/deadlock-plugins/deadlock-extension/src/recorder/index.ts b/deadlock-plugins/deadlock-extension/src/recorder/index.ts index 0c3b2f4fb93db23a51145554b72e066aee6c5a9b..9a4b4f0b698f84b48d8fab62249474d77747e4bd 100644 --- a/deadlock-plugins/deadlock-extension/src/recorder/index.ts +++ b/deadlock-plugins/deadlock-extension/src/recorder/index.ts @@ -2,7 +2,7 @@ import CommandRecorder from './command-recorder'; import GitMission from '../core/gitMission'; import UserConfigNode from './userConfigNode'; import { PROJECT_SRC_PATH, PROJECT_THEIA_PATH } from '../core/config'; -import { copyProjectSources, clearFilesExceptGit, log, error } from './utils'; +import { copyProjectSources, clearFilesExceptGit, log, error, renameTempToUserGitFiles } from './utils'; import UserConfig from '../core/userConfig'; @@ -13,7 +13,10 @@ export default class Recorder { if (!userConfig.isProfessor()) { await copyProjectSources(PROJECT_SRC_PATH, PROJECT_THEIA_PATH, ['.git/']); + if (gitMission) { + renameTempToUserGitFiles(PROJECT_THEIA_PATH, gitMission.author); + log('Starting CommandRecorder..'); new CommandRecorder(gitMission).run(); } else { diff --git a/deadlock-plugins/deadlock-extension/src/recorder/utils.ts b/deadlock-plugins/deadlock-extension/src/recorder/utils.ts index b5eff5aef50a53ed2bd718cc75347bc166e0b183..74331fbc5a781394c296e116e28a58efa7ad56a1 100644 --- a/deadlock-plugins/deadlock-extension/src/recorder/utils.ts +++ b/deadlock-plugins/deadlock-extension/src/recorder/utils.ts @@ -2,6 +2,7 @@ import GitMission from '../core/gitMission'; import { PROJECT_SRC_PATH, PROJECT_THEIA_PATH } from '../core/config'; import { format } from 'date-fns'; import { execSync } from "child_process"; +import { existsSync, renameSync, copyFileSync, PathLike } from 'fs'; const util = require('util'); const unlink = util.promisify(require('fs').unlink); @@ -54,6 +55,74 @@ export async function clearFilesExceptGit(path) { }); } +function renameIfExistsSync(srcPath: PathLike, destPath: PathLike) { + + if (existsSync(srcPath)) { + renameSync(srcPath, destPath); + } else { + log(`Renaming : No ${srcPath} found`); + } +} + +function copyFileIfExistsSync(srcPath: PathLike, destPath: PathLike) { + + if (existsSync(srcPath)) { + copyFileSync(srcPath, destPath); + } else { + log(`Copying file: No ${srcPath} found`); + } +} + +function copyFolderIfExistsSync(srcPath: PathLike, destPath: PathLike) { + + if (existsSync(srcPath)) { + execSync(`rsync -r ${srcPath}/* ${destPath}`); + } else { + log(`Copying folder: No ${srcPath} found`); + } +} + + +/** + * + * Rename temporary git files back to actual git files. + * + * @param path Path of the folder containing user's temporary git project files. + * @param userId Identifier used to create the temporary files name + */ +export function renameTempToUserGitFiles(path: string, userId: string) { + + renameIfExistsSync( + Path.join(path,`user-git-${userId}`), + Path.join(path,'.git') + ); + renameIfExistsSync( + Path.join(path, `user-gitignore-${userId}`), + Path.join(path, '.gitignore') + ); +} + +/** + * + * Copy user's git project files and paste them with renamed filenames so that + * they can be saved by the recorder. + * + * @param srcPath Path of the folder containing user's git project files to be saved. + * @param destPath Path where the user's git project files should be pasted. + * @param userId Identifier used to create the temporary files name + */ +function copyGitUserFiles(srcPath: string, destPath: string, userId: string) { + + copyFolderIfExistsSync( + Path.join(srcPath,'.git'), + Path.join(destPath,`user-git-${userId}`) + ); + copyFileIfExistsSync( + Path.join(srcPath, '.gitignore'), + Path.join(destPath, `user-gitignore-${userId}`) + ); +} + export enum CommitFrom { Run = 'Run', Auto = 'Auto' @@ -64,6 +133,7 @@ export async function commitAndPushCode(gitMission: GitMission, from: CommitFrom log('Commit & push'); await clearFilesExceptGit(PROJECT_SRC_PATH); + copyGitUserFiles(PROJECT_THEIA_PATH, PROJECT_SRC_PATH, gitMission.author); execSync(`rsync -r --exclude .git --exclude npm --exclude target ${PROJECT_THEIA_PATH}/* ${PROJECT_SRC_PATH} && cp ${Path.join(__dirname, '.gitignore')} ${PROJECT_SRC_PATH} && chown -R root:root /project`); const currentDate = format(new Date(), "HH'H'mm'_'dd/LL/y");