-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd.coffee
executable file
·150 lines (135 loc) · 5.46 KB
/
cmd.coffee
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env iced
### !pragma coverage-skip-block ###
require "fy"
fs = require "fs"
path = require "path"
import_resolver = require "./src/import_resolver"
ast_gen = require "./src/ast_gen"
ast_transform = require("./src/ast_transform")
type_inference = require("./src/type_inference").gen
translate = require("./src/translate_ligo").gen
translate_ds = require("./src/translate_ligo_default_state").gen
{execSync} = require "child_process"
shellEscape = require "shell-escape"
# ###################################################################################################
argv = require("minimist") process.argv.slice(2),
boolean: ["router", "silent", "solc-force", "ds", "test", "disable_enums_to_nat", "print_solidity_ast"]
string: ["solc", "outfile", "dir", "outdir", "contract"]
alias:
"o": "outfile"
"d": "dir"
"D": "outdir"
"q": "quiet"
"a": "print_solidity_ast"
argv.router ?= true
argv.quiet ?= false
argv.contract ?= false
argv.solc ?= "0.4.26"
argv["solc-force"] ?= false
argv.ds ?= false
argv.test ?= false
argv.disable_enums_to_nat ?= false
argv.print_solidity_ast ?= false
argv.outfile ?= null
argv.dir ?= null
argv.outdir ?= "."
# ###################################################################################################
walkSync = (dir, filelist = []) ->
files = fs.readdirSync(dir)
filelist = filelist || []
files.forEach (file) ->
thepath = path.join(dir, file)
if fs.statSync(thepath).isDirectory()
filelist = walkSync thepath, filelist
else
filelist.push(thepath)
filelist
process_file = (file)->
code = import_resolver file
ast = ast_gen code,
auto_version : !argv["solc-force"]
suggest_solc_version : argv.solc
quiet : argv.quiet
allow_download : true
if argv.print_solidity_ast
puts ast
solidity_to_ast4gen = require("./src/solidity_to_ast4gen").gen
new_ast = solidity_to_ast4gen ast
outfile = path.parse(argv.outfile) if argv.outfile
opt = {
router : argv.router,
contract : argv.contract
replace_enums_by_nats: not argv.disable_enums_to_nat
keep_dir_structure: argv.dir != null
}
new_ast = ast_transform.pre_ti new_ast, opt
new_ast = type_inference new_ast, opt
new_ast = ast_transform.post_ti new_ast, opt
code = translate new_ast, opt
code += """\n(* this code is generated from #{file} by sol2ligo transpiler *)\n"""
if argv.outfile
name = outfile.name
if outfile.ext
name += outfile.ext
else
name += ".ligo"
name = path.join outfile.dir, name
if outfile.dir
execSync shellEscape ["mkdir", "-p", outfile.dir]
fs.writeFileSync name, code
else
puts code
if argv.ds or argv.outfile
ds_code = translate_ds new_ast
if argv.outfile
filepath = path.join outfile.dir, outfile.name + ".storage"
fs.writeFileSync filepath, ds_code
else
puts """
----- BEGIN DEFAULT STATE -----
#{ds_code}
----- END DEFAULT STATE -----
"""
if argv.test
code = code.replace /\(\* EmitStatement \*\);/g, ""
fs.writeFileSync "test.ligo", code
if fs.existsSync "ligo_tmp.log"
fs.unlinkSync "ligo_tmp.log"
try
execSync "ligo compile-contract test.ligo main > ./ligo_tmp.log", {stdio: "inherit"}
catch err
puts "ERROR"
puts fs.readFileSync "./ligo_tmp.log", "utf-8"
if new_ast.need_prevent_deploy
perr "WARNING. Generated code is not correct. DO NOT deploy it without prior thorough checking!"
process.exitCode = 1
return
if !(file = argv._[0])? and !(file = argv.file) and !(argv.dir)
puts """
usage ./cmd.coffee <file.sol>
--router generate router default: 1
-q, --quiet suppress errors default: false
--solc suggested solc version if pragma is not specified default: 0.4.26
--solc-force override solc version in pragma default: false
--ds print default state. You need it for deploy default: false
--test test compile with ligo (must be installed) default: false
--disable_enums_to_nat Do not transform enums to number constants default: false
-a,--print_solidity_ast Print parsed Solidity AST before transpiling default: false
--contract <name> Name of contract to generate router for default: <last contract>
-o, --outfile <name> Name for output file. Adds `.ligo` if no extension specified default: <prints to stdout>
-d, --dir <path> Keep original directory structure and yield multiple ligo files default: <single file to stdout>
-D, --outdir <path> Output directory to be used with -d option, otherwise ignored default: <current dir>
see test.ligo, test.pp.ligo and ligo_tmp.log
for more detailed help take a look at https://github.com/madfish-solutions/sol2ligo/wiki/CLI-usage
"""
process.exit()
if argv.dir
files = walkSync argv.dir
dirname = path.basename argv.dir
for file in files
rel = path.relative argv.dir, file
filepath = path.parse rel
argv.outfile = path.join argv.outdir, filepath.dir, filepath.name
process_file file
else
process_file file