This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverible.ts
75 lines (66 loc) · 2.67 KB
/
verible.ts
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
// Copyright 2022
// Carlos Alberto Ruiz Naranjo [[email protected]]
// Ismael Perez Rojo [[email protected] ]
//
// This file is part of colibri2
//
// Colibri is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Colibri is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with colibri2. If not, see <https://www.gnu.org/licenses/>.
const fs = require("fs");
const path_lib = require("path");
import { Base_formatter } from "./base_formatter";
import * as file_utils from "../utils/file_utils";
import * as utils from "../process/utils";
import { Process } from "../process/process";
import * as common from "./common";
import * as cfg from "../config/config_declaration";
export class Verible extends Base_formatter {
private binary = 'verible-verilog-format';
constructor() {
super();
}
async format_from_code(code: string, opt: cfg.e_formatter_verible): Promise<common.f_result> {
const temp_file = await utils.create_temp_file(code);
const formatted_code = await this.format(temp_file, opt);
file_utils.remove_file(temp_file);
return formatted_code;
}
private async get_path(f_path: string): Promise<string> {
const path_checker = [f_path, path_lib.join(f_path, this.binary),
path_lib.join(f_path, this.binary + '.exe'),
this.binary, this.binary + '.exe'];
const P = new Process();
for (const path_inst of path_checker) {
const cmd = `${path_inst} --version`;
const exec_result = await P.exec_wait(cmd);
if (exec_result.successful === true) {
return path_inst;
}
}
return '';
}
public async format(file: string, opt: cfg.e_formatter_verible) {
const path_bin = await this.get_path(opt.path);
const command = `${path_bin} ${opt.format_args} `;
const P = new Process();
const exec_result = await P.exec_wait(command);
const code_formatted = fs.readFileSync(file, "utf8");
const result: common.f_result = {
code_formatted: code_formatted,
command: command,
successful: exec_result.successful,
message: exec_result.stderr,
};
return result;
}
}