|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | + |
| 4 | +class BuildRunner { |
| 5 | + constructor(commandExecutor, loggingUtils) { |
| 6 | + this.commandExecutor = commandExecutor; |
| 7 | + this.loggingUtils = loggingUtils; |
| 8 | + } |
| 9 | + |
| 10 | + async build(args = [], options = {}) { |
| 11 | + await this.commandExecutor.runner.initialize(); |
| 12 | + |
| 13 | + const projectInfo = this.commandExecutor.runner.getRustProjectInfo(); |
| 14 | + let buildArgs = args; |
| 15 | + |
| 16 | + if (options.release) { |
| 17 | + buildArgs = ['--release', ...buildArgs]; |
| 18 | + } |
| 19 | + |
| 20 | + this.loggingUtils.info('🔨 Building Rust project...'); |
| 21 | + |
| 22 | + try { |
| 23 | + const result = await this.commandExecutor.executeCargoCommand('build', buildArgs, options); |
| 24 | + |
| 25 | + this._showBuildInfo(projectInfo, options.release); |
| 26 | + |
| 27 | + return result; |
| 28 | + } catch (error) { |
| 29 | + this._suggestBuildFix(error.message); |
| 30 | + throw error; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + async check(args = [], options = {}) { |
| 35 | + await this.commandExecutor.runner.initialize(); |
| 36 | + |
| 37 | + this.loggingUtils.info('🔍 Checking Rust code...'); |
| 38 | + |
| 39 | + try { |
| 40 | + const result = await this.commandExecutor.executeCargoCommand('check', args, options); |
| 41 | + |
| 42 | + this.loggingUtils.info('✅ Code check completed successfully'); |
| 43 | + |
| 44 | + return result; |
| 45 | + } catch (error) { |
| 46 | + this._suggestCheckFix(error.message); |
| 47 | + throw error; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + async run(args = [], options = {}) { |
| 52 | + await this.commandExecutor.runner.initialize(); |
| 53 | + |
| 54 | + let runArgs = args; |
| 55 | + |
| 56 | + if (options.release) { |
| 57 | + runArgs = ['--release', ...runArgs]; |
| 58 | + } |
| 59 | + |
| 60 | + this.loggingUtils.info('🚀 Running Rust project...'); |
| 61 | + |
| 62 | + try { |
| 63 | + const result = await this.commandExecutor.executeCargoCommand('run', runArgs, options); |
| 64 | + |
| 65 | + this.loggingUtils.info('✅ Project execution completed'); |
| 66 | + |
| 67 | + return result; |
| 68 | + } catch (error) { |
| 69 | + this._suggestRunFix(error.message); |
| 70 | + throw error; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + _showBuildInfo(projectInfo, isRelease) { |
| 75 | + this.loggingUtils.info('\n📊 Build Information:'); |
| 76 | + this.loggingUtils.info('='.repeat(40)); |
| 77 | + this.loggingUtils.info(`Build type: ${isRelease ? 'Release' : 'Debug'}`); |
| 78 | + this.loggingUtils.info(`Project type: ${this._getProjectTypeDescription(projectInfo)}`); |
| 79 | + this.loggingUtils.info(`Rust edition: ${projectInfo.edition || '2018'}`); |
| 80 | + this.loggingUtils.info(`Dependencies: ${projectInfo.dependencies}`); |
| 81 | + this.loggingUtils.info(`Dev dependencies: ${projectInfo.devDependencies}`); |
| 82 | + this.loggingUtils.info(`Build dependencies: ${projectInfo.buildDependencies}`); |
| 83 | + |
| 84 | + if (projectInfo.frameworks.length > 0) { |
| 85 | + this.loggingUtils.info(`Frameworks: ${projectInfo.frameworks.join(', ')}`); |
| 86 | + } |
| 87 | + |
| 88 | + this.loggingUtils.info('='.repeat(40)); |
| 89 | + } |
| 90 | + |
| 91 | + _getProjectTypeDescription(projectInfo) { |
| 92 | + const types = []; |
| 93 | + if (projectInfo.isBinary) types.push('Binary'); |
| 94 | + if (projectInfo.isLibrary) types.push('Library'); |
| 95 | + if (projectInfo.isWorkspace) types.push('Workspace'); |
| 96 | + return types.length > 0 ? types.join(' + ') : 'Unknown'; |
| 97 | + } |
| 98 | + |
| 99 | + _suggestBuildFix(errorMessage) { |
| 100 | + this.loggingUtils.info('\n💡 Build Error Suggestions:'); |
| 101 | + |
| 102 | + if (errorMessage.includes('could not compile')) { |
| 103 | + this.loggingUtils.info(' • Check compiler error messages'); |
| 104 | + this.loggingUtils.info(' • Run cargo check for detailed errors'); |
| 105 | + this.loggingUtils.info(' • Check Cargo.toml syntax'); |
| 106 | + } |
| 107 | + |
| 108 | + if (errorMessage.includes('linker')) { |
| 109 | + this.loggingUtils.info(' • Check linker configuration'); |
| 110 | + this.loggingUtils.info(' • Install required system libraries'); |
| 111 | + this.loggingUtils.info(' • Check target triple configuration'); |
| 112 | + } |
| 113 | + |
| 114 | + if (errorMessage.includes('feature')) { |
| 115 | + this.loggingUtils.info(' • Check feature flags in Cargo.toml'); |
| 116 | + this.loggingUtils.info(' • Enable required features'); |
| 117 | + this.loggingUtils.info(' • Check dependency feature requirements'); |
| 118 | + } |
| 119 | + |
| 120 | + if (errorMessage.includes('target')) { |
| 121 | + this.loggingUtils.info(' • Check target configuration'); |
| 122 | + this.loggingUtils.info(' • Install target: rustup target add <target>'); |
| 123 | + this.loggingUtils.info(' • Set target in .cargo/config.toml'); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + _suggestCheckFix(errorMessage) { |
| 128 | + this.loggingUtils.info('\n💡 Check Error Suggestions:'); |
| 129 | + |
| 130 | + if (errorMessage.includes('type mismatch')) { |
| 131 | + this.loggingUtils.info(' • Check function signatures'); |
| 132 | + this.loggingUtils.info(' • Verify type annotations'); |
| 133 | + this.loggingUtils.info(' • Use type inference where possible'); |
| 134 | + } |
| 135 | + |
| 136 | + if (errorMessage.includes('unused')) { |
| 137 | + this.loggingUtils.info(' • Remove unused imports/variables'); |
| 138 | + this.loggingUtils.info(' • Add #[allow(unused)] attribute if needed'); |
| 139 | + this.loggingUtils.info(' • Check for dead code'); |
| 140 | + } |
| 141 | + |
| 142 | + if (errorMessage.includes('cannot move')) { |
| 143 | + this.loggingUtils.info(' • Check ownership and borrowing'); |
| 144 | + this.loggingUtils.info(' • Use references (&) instead of moving'); |
| 145 | + this.loggingUtils.info(' • Consider using Clone or Copy traits'); |
| 146 | + } |
| 147 | + |
| 148 | + if (errorMessage.includes('trait bound')) { |
| 149 | + this.loggingUtils.info(' • Check trait implementations'); |
| 150 | + this.loggingUtils.info(' • Add required trait bounds'); |
| 151 | + this.loggingUtils.info(' • Use where clauses for complex bounds'); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + _suggestRunFix(errorMessage) { |
| 156 | + this.loggingUtils.info('\n💡 Run Error Suggestions:'); |
| 157 | + |
| 158 | + if (errorMessage.includes('not found')) { |
| 159 | + this.loggingUtils.info(' • Build project first: cargo build'); |
| 160 | + this.loggingUtils.info(' • Check binary name in Cargo.toml'); |
| 161 | + this.loggingUtils.info(' • Run with --bin flag for workspace projects'); |
| 162 | + } |
| 163 | + |
| 164 | + if (errorMessage.includes('permission')) { |
| 165 | + this.loggingUtils.info(' • Check executable permissions'); |
| 166 | + this.loggingUtils.info(' • Run chmod +x on binary'); |
| 167 | + this.loggingUtils.info(' • Check antivirus/firewall settings'); |
| 168 | + } |
| 169 | + |
| 170 | + if (errorMessage.includes('argument')) { |
| 171 | + this.loggingUtils.info(' • Check command line arguments'); |
| 172 | + this.loggingUtils.info(' • Use -- to separate cargo args from program args'); |
| 173 | + this.loggingUtils.info(' • Check argument parsing in main function'); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +module.exports = BuildRunner; |
0 commit comments