|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.22; |
| 3 | + |
| 4 | +import {console} from "forge-std/console.sol"; |
| 5 | +import {Vm} from "forge-std/Vm.sol"; |
| 6 | + |
| 7 | +library EnvUtils { |
| 8 | + Vm constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code"))))); |
| 9 | + |
| 10 | + function updateEnvVariable(string memory variableName, address value) internal { |
| 11 | + string memory envPath = ".env"; |
| 12 | + string memory addressString = vm.toString(value); |
| 13 | + string memory targetVar = string.concat(variableName, "="); |
| 14 | + |
| 15 | + // Check if file exists |
| 16 | + if (!vm.exists(envPath)) { |
| 17 | + console.log(".env file not found"); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + // Read the entire file |
| 22 | + string memory content = vm.readFile(envPath); |
| 23 | + string[] memory lines = splitLines(content); |
| 24 | + |
| 25 | + // Process lines and make updates |
| 26 | + string memory newContent = ""; |
| 27 | + bool found = false; |
| 28 | + |
| 29 | + for (uint i = 0; i < lines.length; i++) { |
| 30 | + string memory currentLine = lines[i]; |
| 31 | + |
| 32 | + // Check if this is the line we want to replace |
| 33 | + if (startsWith(currentLine, targetVar)) { |
| 34 | + currentLine = string.concat(targetVar, addressString); |
| 35 | + found = true; |
| 36 | + } |
| 37 | + |
| 38 | + // Add line to new content |
| 39 | + if (i > 0) { |
| 40 | + newContent = string.concat(newContent, "\n"); |
| 41 | + } |
| 42 | + newContent = string.concat(newContent, currentLine); |
| 43 | + } |
| 44 | + |
| 45 | + // If not found, append it |
| 46 | + if (!found) { |
| 47 | + newContent = string.concat(newContent, "\n", targetVar, addressString); |
| 48 | + } |
| 49 | + |
| 50 | + // Write back to file |
| 51 | + vm.writeFile(envPath, newContent); |
| 52 | + console.log("Updated .env file with new", variableName, ":", addressString); |
| 53 | + } |
| 54 | + |
| 55 | + // Helper function to split a string by newlines |
| 56 | + function splitLines(string memory _content) internal pure returns (string[] memory) { |
| 57 | + bytes memory content = bytes(_content); |
| 58 | + |
| 59 | + // Count the number of lines |
| 60 | + uint lineCount = 1; |
| 61 | + for (uint i = 0; i < content.length; i++) { |
| 62 | + if (content[i] == bytes1("\n")) { |
| 63 | + lineCount++; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Split the content |
| 68 | + string[] memory lines = new string[](lineCount); |
| 69 | + uint lineStart = 0; |
| 70 | + uint currentLine = 0; |
| 71 | + |
| 72 | + for (uint i = 0; i < content.length; i++) { |
| 73 | + if (content[i] == bytes1("\n") || i == content.length - 1) { |
| 74 | + uint lineEnd = i; |
| 75 | + if (i == content.length - 1 && content[i] != bytes1("\n")) { |
| 76 | + lineEnd = i + 1; |
| 77 | + } |
| 78 | + |
| 79 | + // Extract line |
| 80 | + bytes memory line = new bytes(lineEnd - lineStart); |
| 81 | + for (uint j = lineStart; j < lineEnd; j++) { |
| 82 | + line[j - lineStart] = content[j]; |
| 83 | + } |
| 84 | + |
| 85 | + lines[currentLine] = string(line); |
| 86 | + currentLine++; |
| 87 | + lineStart = i + 1; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return lines; |
| 92 | + } |
| 93 | + |
| 94 | + // Helper function to check if a string starts with a prefix |
| 95 | + function startsWith(string memory _str, string memory _prefix) internal pure returns (bool) { |
| 96 | + bytes memory str = bytes(_str); |
| 97 | + bytes memory prefix = bytes(_prefix); |
| 98 | + |
| 99 | + if (str.length < prefix.length) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + for (uint i = 0; i < prefix.length; i++) { |
| 104 | + if (str[i] != prefix[i]) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | +} |
0 commit comments