| 
									
										
										
										
											2021-12-24 01:58:22 +00:00
										 |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2021-12-24 02:34:27 +00:00
										 |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2021-12-25 19:35:09 +00:00
										 |  |  | import CloudRunnerLogger from '../../cloud-runner/services/cloud-runner-logger'; | 
					
						
							|  |  |  | import { CloudRunnerState } from '../../cloud-runner/state/cloud-runner-state'; | 
					
						
							|  |  |  | import { RemoteClientSystem } from './remote-client-system'; | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  | export class SetupRemoteRepository { | 
					
						
							| 
									
										
										
										
											2021-12-19 01:45:55 +00:00
										 |  |  |   public static async run() { | 
					
						
							| 
									
										
										
										
											2021-12-26 00:47:33 +00:00
										 |  |  |     try { | 
					
						
							|  |  |  |       fs.mkdirSync(CloudRunnerState.buildPathFull); | 
					
						
							|  |  |  |       fs.mkdirSync(CloudRunnerState.repoPathFull); | 
					
						
							|  |  |  |       await SetupRemoteRepository.cloneRepoWithoutLFSFiles(); | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 00:47:33 +00:00
										 |  |  |       await SetupRemoteRepository.createLFSHashFiles(); | 
					
						
							|  |  |  |       const LFS_ASSETS_HASH = fs.readFileSync( | 
					
						
							|  |  |  |         `${path.join(CloudRunnerState.repoPathFull, `.lfs-assets-guid`)}`, | 
					
						
							|  |  |  |         'utf8', | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       await SetupRemoteRepository.printLFSHashState(); | 
					
						
							|  |  |  |       const lfsCacheFolder = path.join(CloudRunnerState.cacheFolderFull, `lfs`); | 
					
						
							|  |  |  |       const libraryCacheFolder = path.join(CloudRunnerState.cacheFolderFull, `lib`); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`tree ${CloudRunnerState.builderPathFull}`); | 
					
						
							|  |  |  |       await SetupRemoteRepository.libraryCaching(lfsCacheFolder, libraryCacheFolder); | 
					
						
							|  |  |  |       await SetupRemoteRepository.lfsCaching(lfsCacheFolder, LFS_ASSETS_HASH); | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 00:47:33 +00:00
										 |  |  |       await SetupRemoteRepository.printCacheState(lfsCacheFolder, libraryCacheFolder); | 
					
						
							|  |  |  |       await SetupRemoteRepository.pullLatestLFS(); | 
					
						
							|  |  |  |       await SetupRemoteRepository.cacheLatestLFSFiles(LFS_ASSETS_HASH, lfsCacheFolder); | 
					
						
							|  |  |  |       SetupRemoteRepository.handleCachePurging(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       throw error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async printLFSHashState() { | 
					
						
							| 
									
										
										
										
											2021-12-25 20:34:03 +00:00
										 |  |  |     await RemoteClientSystem.Run( | 
					
						
							|  |  |  |       `echo ' '
 | 
					
						
							| 
									
										
										
										
											2021-12-19 22:04:02 +00:00
										 |  |  |       echo 'Contents of .lfs-assets-guid file:' | 
					
						
							|  |  |  |       cat .lfs-assets-guid | 
					
						
							|  |  |  |       echo ' ' | 
					
						
							|  |  |  |       echo 'Contents of .lfs-assets-guid-sum file:' | 
					
						
							|  |  |  |       cat .lfs-assets-guid-sum | 
					
						
							|  |  |  |       echo ' ' | 
					
						
							| 
									
										
										
										
											2021-12-21 06:47:29 +00:00
										 |  |  |       echo 'Source repository initialized' | 
					
						
							|  |  |  |       ls ${CloudRunnerState.projectPathFull} | 
					
						
							| 
									
										
										
										
											2021-12-25 20:34:03 +00:00
										 |  |  |       echo ' '`,
 | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async printCacheState(lfsCacheFolder: string, libraryCacheFolder: string) { | 
					
						
							| 
									
										
										
										
											2021-12-25 20:34:03 +00:00
										 |  |  |     await RemoteClientSystem.Run( | 
					
						
							|  |  |  |       `echo ' '
 | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |       echo "LFS cache for $branch" | 
					
						
							|  |  |  |       du -sch "${lfsCacheFolder}/" | 
					
						
							|  |  |  |       echo '**' | 
					
						
							|  |  |  |       echo "Library cache for $branch" | 
					
						
							|  |  |  |       du -sch "${libraryCacheFolder}/" | 
					
						
							|  |  |  |       echo '**' | 
					
						
							|  |  |  |       echo "Branch: $branch" | 
					
						
							|  |  |  |       du -sch "${CloudRunnerState.cacheFolderFull}/" | 
					
						
							|  |  |  |       echo '**' | 
					
						
							|  |  |  |       echo 'Full cache' | 
					
						
							|  |  |  |       du -sch "${CloudRunnerState.cacheFolderFull}/" | 
					
						
							| 
									
										
										
										
											2021-12-25 20:34:03 +00:00
										 |  |  |       echo ' '`,
 | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static handleCachePurging() { | 
					
						
							|  |  |  |     if (process.env.purgeRemoteCaching !== undefined) { | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`purging ${CloudRunnerState.purgeRemoteCaching}`); | 
					
						
							|  |  |  |       fs.rmdirSync(CloudRunnerState.cacheFolder, { recursive: true }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async cacheLatestLFSFiles(LFS_ASSETS_HASH: string, lfsCacheFolder: string) { | 
					
						
							|  |  |  |     process.chdir(`${CloudRunnerState.lfsDirectory}/..`); | 
					
						
							| 
									
										
										
										
											2021-12-26 12:16:28 +00:00
										 |  |  |     CloudRunnerLogger.logRemoteCli(fs.existsSync(`${LFS_ASSETS_HASH}.zip`).toString()); | 
					
						
							| 
									
										
										
										
											2021-12-26 02:49:07 +00:00
										 |  |  |     await RemoteClientSystem.Run(`zip -r "${LFS_ASSETS_HASH}.zip" "lfs"`); | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |     fs.copyFileSync(`${LFS_ASSETS_HASH}.zip`, lfsCacheFolder); | 
					
						
							|  |  |  |     CloudRunnerLogger.logRemoteCli(`copied ${LFS_ASSETS_HASH} to ${lfsCacheFolder}`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async pullLatestLFS() { | 
					
						
							|  |  |  |     process.chdir(CloudRunnerState.repoPathFull); | 
					
						
							|  |  |  |     await RemoteClientSystem.Run(`git lfs pull`); | 
					
						
							|  |  |  |     CloudRunnerLogger.logRemoteCli(`pulled latest LFS files`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async lfsCaching(lfsCacheFolder: string, LFS_ASSETS_HASH: string) { | 
					
						
							|  |  |  |     CloudRunnerLogger.logRemoteCli(` `); | 
					
						
							|  |  |  |     CloudRunnerLogger.logRemoteCli(`LFS Caching`); | 
					
						
							|  |  |  |     process.chdir(lfsCacheFolder); | 
					
						
							|  |  |  |     let latestLFSCacheFile; | 
					
						
							|  |  |  |     if (fs.existsSync(`${LFS_ASSETS_HASH}.zip`)) { | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`Match found: using large file hash match ${LFS_ASSETS_HASH}.zip`); | 
					
						
							|  |  |  |       latestLFSCacheFile = `${LFS_ASSETS_HASH}.zip`; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       latestLFSCacheFile = await RemoteClientSystem.Run(`ls -t "${lfsCacheFolder}" | grep .zip$ | head -1`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (fs.existsSync(latestLFSCacheFile)) { | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`LFS cache exists`); | 
					
						
							|  |  |  |       fs.rmdirSync(CloudRunnerState.lfsDirectory, { recursive: true }); | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`LFS cache exists from build $latestLFSCacheFile from $branch`); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run( | 
					
						
							|  |  |  |         `unzip -q "${lfsCacheFolder}/${latestLFSCacheFile}" -d "${path.join(CloudRunnerState.repoPathFull, `.git`)}"`, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`ls -lh "${CloudRunnerState.lfsDirectory}"`); | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`git LFS folder, (should not contain $latestLFSCacheFile)`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async libraryCaching(lfsCacheFolder: string, libraryCacheFolder: string) { | 
					
						
							| 
									
										
										
										
											2021-12-24 04:05:42 +00:00
										 |  |  |     CloudRunnerLogger.logRemoteCli(`Starting checks of cache for the Unity project Library and git LFS files`); | 
					
						
							| 
									
										
										
										
											2021-12-24 02:34:27 +00:00
										 |  |  |     if (!fs.existsSync(lfsCacheFolder)) { | 
					
						
							|  |  |  |       fs.mkdirSync(lfsCacheFolder); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!fs.existsSync(libraryCacheFolder)) { | 
					
						
							|  |  |  |       fs.mkdirSync(libraryCacheFolder); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-24 04:05:42 +00:00
										 |  |  |     CloudRunnerLogger.logRemoteCli(`Library Caching`); | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:22 +00:00
										 |  |  |     //if the unity git project has included the library delete it and echo a warning
 | 
					
						
							|  |  |  |     if (fs.existsSync(CloudRunnerState.libraryFolderFull)) { | 
					
						
							|  |  |  |       fs.rmdirSync(CloudRunnerState.libraryFolderFull, { recursive: true }); | 
					
						
							| 
									
										
										
										
											2021-12-24 04:05:42 +00:00
										 |  |  |       CloudRunnerLogger.logRemoteCli( | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:22 +00:00
										 |  |  |         `!Warning!: The Unity library was included in the git repository (this isn't usually a good practice)`, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     //Restore library cache
 | 
					
						
							| 
									
										
										
										
											2021-12-25 19:35:09 +00:00
										 |  |  |     const latestLibraryCacheFile = await RemoteClientSystem.Run(`ls -t "${libraryCacheFolder}" | grep .zip$ | head -1`); | 
					
						
							|  |  |  |     await RemoteClientSystem.Run(`ls -lh "${libraryCacheFolder}"`); | 
					
						
							| 
									
										
										
										
											2021-12-24 04:05:42 +00:00
										 |  |  |     CloudRunnerLogger.logRemoteCli(`Checking if Library cache ${libraryCacheFolder}/${latestLibraryCacheFile} exists`); | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:22 +00:00
										 |  |  |     if (fs.existsSync(latestLibraryCacheFile)) { | 
					
						
							| 
									
										
										
										
											2021-12-24 04:05:42 +00:00
										 |  |  |       CloudRunnerLogger.logRemoteCli(`Library cache exists`); | 
					
						
							| 
									
										
										
										
											2021-12-25 20:34:03 +00:00
										 |  |  |       const latestCacheFilePath = path.join(libraryCacheFolder, latestLibraryCacheFile); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`unzip -q "${latestCacheFilePath}" -d "$projectPathFull"`); | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:22 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-12-24 02:34:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |   private static async createLFSHashFiles() { | 
					
						
							| 
									
										
										
										
											2021-12-25 20:34:03 +00:00
										 |  |  |     await RemoteClientSystem.Run(`git lfs ls-files -l | cut -d ' ' -f1 | sort > .lfs-assets-guid`); | 
					
						
							|  |  |  |     await RemoteClientSystem.Run(`md5sum .lfs-assets-guid > .lfs-assets-guid-sum`); | 
					
						
							| 
									
										
										
										
											2021-12-25 19:47:22 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static async cloneRepoWithoutLFSFiles() { | 
					
						
							| 
									
										
										
										
											2021-12-26 00:47:33 +00:00
										 |  |  |     try { | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`Initializing source repository for cloning with caching of LFS files`); | 
					
						
							|  |  |  |       process.chdir(CloudRunnerState.repoPathFull); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`git config --global advice.detachedHead false`); | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(`Cloning the repository being built:`); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`git lfs install --skip-smudge`); | 
					
						
							|  |  |  |       CloudRunnerLogger.logRemoteCli(CloudRunnerState.targetBuildRepoUrl); | 
					
						
							| 
									
										
										
										
											2021-12-26 02:06:15 +00:00
										 |  |  |       await RemoteClientSystem.Run(`git clone ${CloudRunnerState.targetBuildRepoUrl} ${CloudRunnerState.repoPathFull}`); | 
					
						
							| 
									
										
										
										
											2021-12-26 00:47:33 +00:00
										 |  |  |       await RemoteClientSystem.Run(`ls -lh`); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`tree`); | 
					
						
							| 
									
										
										
										
											2021-12-26 00:53:07 +00:00
										 |  |  |       CloudRunnerLogger.logRemoteCli(`${CloudRunnerState.buildParams.branch}`); | 
					
						
							|  |  |  |       await RemoteClientSystem.Run(`git checkout ${CloudRunnerState.buildParams.branch}`); | 
					
						
							| 
									
										
										
										
											2021-12-26 00:47:33 +00:00
										 |  |  |       CloudRunnerLogger.logRemoteCli(`Checked out ${process.env.GITHUB_SHA}`); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       throw error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-19 01:45:55 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } |