-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_manager.py
97 lines (88 loc) · 3.39 KB
/
files_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import shutil
import re
from termcolor import cprint
class Manager(object):
def __init__(self, output_directory, force, compiler_version, contract_name):
super(Manager, self).__init__()
self.output_directory = output_directory
self.force = force
self.compiler_version = compiler_version
self.contract_name = contract_name
self.is_library = False
try:
#os.makedirs(self.output_directory, exist_ok= self.force)
cprint("Creating project %s at %s" % (self.contract_name, self.output_directory),"blue",attrs=["bold"])
shutil.copytree('FoundryTemplate/', '%s/' % (self.output_directory), dirs_exist_ok=self.force)
except OSError as error:
cprint("[ERROR] The output directory is already existing, use the -f or --force argument to overwwrite it","red",attrs=["bold"])
exit(1)
def create(self, filename, content):
if("/contracts-upgradeable/" in filename):
split_segment = "contracts-upgradeable/"
else:
split_segment = "contracts/"
if(filename.startswith('@')):
self.is_library = True
filename = filename.split(split_segment)[1]
else:
try:
filename = filename.split(split_segment)[1]
except:
if not ".sol" in filename:
filename += ".sol"
path = '%s/src/%s' % (self.output_directory, filename)
basedir = os.path.dirname(path)
if not os.path.exists(basedir):
os.makedirs(basedir)
# edit libraries from remote to local by removing the @ in the import
#content_parsed = ""
#for line in content.split('\n'):
# print(line)
# if('import "@' in str(line)):
# line = 'import "./%s' % line.split(split_segment)[1]
# elif('from "@' in str(line)):
# line = line.replace('from "@', 'from "./')
# content_parsed += "%s\n" % line
# dirty tweak to remove any non UTF-8 char
content_parsed = content.encode('ascii', 'ignore').decode('utf-8', 'ignore')
with open(path, 'w') as f:
f.write(content_parsed)
if not self.is_library:
script_path = "%s/script/%s" % (self.output_directory, filename.replace('.sol', '.s.sol'))
test_path = "%s/test/%s" % (self.output_directory, filename.replace('.sol', '.t.sol'))
basedir = os.path.dirname(script_path)
if not os.path.exists(basedir):
os.makedirs(basedir)
with open(script_path, 'w') as f:
f.write(self.generate_script_files(filename))
basedir = os.path.dirname(test_path)
if not os.path.exists(basedir):
os.makedirs(basedir)
with open(test_path, 'w') as f:
f.write(self.generate_test_files(filename))
def generate_script_files(self, filename):
solidity_version = re.findall(r'\d{0,2}\.\d{0,2}\.\d{0,2}',self.compiler_version)[0]
return("""// SPDX-License-Identifier: UNLICENSED
pragma solidity ^%s;
import "forge-std/Script.sol";
contract %sScript is Script {
function setUp() public {}
function run() public {
vm.broadcast();
}
}
""" % (solidity_version, self.contract_name))
def generate_test_files(self, filename):
solidity_version = re.findall(r'\d{0,2}\.\d{0,2}\.\d{0,2}',self.compiler_version)[0]
return("""// SPDX-License-Identifier: UNLICENSED
pragma solidity ^%s;
import "forge-std/Test.sol";
import "../src/%s.sol";
contract %sTest is Test {
%s public my_contract;
function setUp() public {
my_contract = new %s();
}
}
""" % (solidity_version, self.contract_name, self.contract_name, self.contract_name, self.contract_name))