Skip to content

Commit

Permalink
chore: refactor config manager
Browse files Browse the repository at this point in the history
  • Loading branch information
asaezper committed Dec 3, 2023
1 parent 462b130 commit 169211c
Show file tree
Hide file tree
Showing 38 changed files with 297 additions and 468 deletions.
3 changes: 3 additions & 0 deletions packages/colibri/src/config/config_declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export type e_config = {
"questa" : e_tools_questa,
"raptor" : e_tools_raptor,
}
[key: string]: {
[key: string]: any;
};
};
export type e_general_general = {
pypath : string,
Expand Down
278 changes: 63 additions & 215 deletions packages/colibri/src/config/config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,261 +18,109 @@
// along with TerosHDL. If not, see <https://www.gnu.org/licenses/>.

import {
get_default_config, e_config, e_tools_general_select_tool, e_linter_general_linter_vhdl,
e_linter_general_linter_verilog, e_linter_general_lstyle_vhdl, e_linter_general_lstyle_verilog,
e_formatter_general_formatter_vhdl, e_formatter_general_formatter_verilog, get_config_from_json
get_default_config, e_config, get_config_from_json
} from './config_declaration';
import * as cfg_aux from "./auxiliar_config";
import { WEB_CONFIG } from "./config_web";
import { read_file_sync, save_file_sync } from "../utils/file_utils";

export class Config_manager {
private sync_file_path = "";
export class ConfigManager {
private config: e_config;

constructor(sync_file_path = "") {
this.sync_file_path = sync_file_path;
this.config = this.read_config_from_filesystem(sync_file_path);
}

public set_config(new_config: e_config) {
this.config = new_config;
this.save_config_to_filesystem();
constructor(config: e_config) {
this.config = config;
}

public set_config_from_json(new_config: any) {
try {
const config = get_config_from_json(new_config);
this.config = config;
this.save_config_to_filesystem();
// eslint-disable-next-line no-empty
} catch (error) { }
public set_config(config_dict: Record<string, any>) {
this.config = get_config_from_json(config_dict);
}

public get_config(): e_config {
return this.config;
}

public get_html(): string {
return WEB_CONFIG;
public toString(): string {
return JSON.stringify(this.config, null, 4);
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Output manager
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public read_config_from_filesystem(file_path: string) {
if (file_path === "") {
return get_default_config();
}
try {
const file_content = read_file_sync(file_path);
const config_saved = JSON.parse(file_content);
const config = get_config_from_json(config_saved);
return config;
} catch (error) {
return get_default_config();
}
export class GlobalConfigManager extends ConfigManager {
private static instance: GlobalConfigManager;

public static newInstance(sync_file_path: string) {
GlobalConfigManager.instance = new GlobalConfigManager(sync_file_path, get_default_config());
return GlobalConfigManager.getInstance();
}

public save_config_to_filesystem() {
if (this.sync_file_path === "") {
return;
}
try {
const config_string = JSON.stringify(this.config, null, 4);
save_file_sync(this.sync_file_path, config_string);
public static getInstance(): GlobalConfigManager {
if (!GlobalConfigManager.instance) {
throw Error("You need to create the GlobalConfigManager instance first using newInstance method.");
}
// eslint-disable-next-line no-empty
catch (error) { }
return GlobalConfigManager.instance;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Linter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public get_vhdl_linter_name() {
return this.config.linter.general.linter_vhdl;
public static get_html(): string {
return WEB_CONFIG;
}

public get_verilog_linter_name() {
return this.config.linter.general.linter_verilog;
}
private sync_file_path = "";

public get_vhdl_style_linter_name() {
return this.config.linter.general.lstyle_vhdl;
private constructor(sync_file_path: string, config: e_config) {
super(config);
this.sync_file_path = sync_file_path;
}

public get_verilog_style_linter_name() {
return this.config.linter.general.lstyle_verilog;
public load() {
const file_content = read_file_sync(this.sync_file_path);
const config_saved = JSON.parse(file_content);
this.set_config(config_saved);
}

public get_linter_config_vhdl(): string {
const linter_name = this.config.linter.general.linter_vhdl;
if (linter_name === e_linter_general_linter_vhdl.ghdl) {
return this.config.linter.ghdl.arguments;
}
else if (linter_name === e_linter_general_linter_vhdl.modelsim) {
return this.config.linter.modelsim.vhdl_arguments;
}
else if (linter_name === e_linter_general_linter_vhdl.vivado) {
return this.config.linter.vivado.vhdl_arguments;
}
else {
return "";
}
public save() {
const config_string = JSON.stringify(this.get_config(), null, 4);
save_file_sync(this.sync_file_path, config_string);
}

public get_linter_config_verilog(): string {
const linter_name = this.config.linter.general.linter_verilog;
if (linter_name === e_linter_general_linter_verilog.icarus) {
return this.config.linter.icarus.arguments;
}
else if (linter_name === e_linter_general_linter_verilog.modelsim) {
return this.config.linter.modelsim.verilog_arguments;
}
else if (linter_name === e_linter_general_linter_verilog.verilator) {
return this.config.linter.verilator.arguments;
}
else if (linter_name === e_linter_general_linter_verilog.vivado) {
return this.config.linter.vivado.verilog_arguments;
}
else {
return "";
}
}
}

public get_style_linter_config_vhdl(): string {
const linter_name = this.config.linter.general.lstyle_vhdl;
if (linter_name === e_linter_general_lstyle_vhdl.vsg) {
return this.config.linter.vsg.arguments;
}
else {
return "";
}
}
export function create_copy_with_undefined_values<T extends Record<string, any>>(obj: T): T {
const result: Record<string, any> = {};

public get_style_linter_config_verilog(): string {
const linter_name = this.config.linter.general.lstyle_verilog;
if (linter_name === e_linter_general_lstyle_verilog.verible) {
return this.config.linter.verible.arguments;
}
else {
return "";
for (const key in obj) {
const value = obj[key];
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
result[key] = create_copy_with_undefined_values(value);
} else {
result[key] = undefined as any;
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Formatter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public get_formatter_name_vhdl() {
return this.config.formatter.general.formatter_vhdl;
}
return result as T;
}

public get_formatter_name_verilog() {
return this.config.formatter.general.formatter_verilog;
}
export function merge_configs<T extends Record<string, any>>(main_config: T, default_config: T): T {

public get_formatter_config_vhdl() {
const formatter_name = this.get_formatter_name_vhdl();
if (formatter_name === e_formatter_general_formatter_vhdl.standalone) {
return this.config.formatter.standalone;
}
else if (formatter_name === e_formatter_general_formatter_vhdl.vsg) {
return this.config.formatter.svg;
}
else {
return this.config.formatter.standalone;
}
}
const result: Record<string, any> = {};

public get_formatter_config_verilog() {
const formatter_name = this.get_formatter_name_verilog();
if (formatter_name === e_formatter_general_formatter_verilog.istyle) {
return this.config.formatter.istyle;
}
else if (formatter_name === e_formatter_general_formatter_verilog.s3sv) {
return this.config.formatter.s3sv;
}
else if (formatter_name === e_formatter_general_formatter_verilog.verible) {
const config = {
format_args : this.config.formatter.verible.format_args,
path: this.config.tools.verible.installation_path
};
return config;
}
else {
return this.config.formatter.istyle;
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Exec
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public get_exec_config(): cfg_aux.t_exec_config {
const exec_config: cfg_aux.t_exec_config = {
execution_mode: this.config.tools.general.execution_mode,
python_path: this.config.general.general.pypath,
developer_mode: this.config.general.general.developer_mode,
waveform_viewer: this.config.tools.general.waveform_viewer
};
return exec_config;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Template
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public get_template_config(): cfg_aux.t_template_options {
const options: cfg_aux.t_template_options = {
header_file_path: this.config.templates.general.header_file_path,
indent_char: this.config.templates.general.indent,
clock_generation_style: this.config.templates.general.clock_generation_style,
instance_style: this.config.templates.general.instance_style
};
return options;
}
const keys = Array.from(new Set([...Object.keys(main_config), ...Object.keys(default_config)]));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Documenter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public get_documenter_config(): cfg_aux.t_documenter_options {
const options: cfg_aux.t_documenter_options = {
generic_visibility: this.config.documentation.general.generics,
port_visibility: this.config.documentation.general.ports,
signal_visibility: this.config.documentation.general.signals,
constant_visibility: this.config.documentation.general.constants,
type_visibility: this.config.documentation.general.types,
function_visibility: this.config.documentation.general.functions,
instantiation_visibility: this.config.documentation.general.instantiations,
process_visibility: this.config.documentation.general.process,
language: this.config.documentation.general.language,
vhdl_symbol: this.config.documentation.general.symbol_vhdl,
verilog_symbol: this.config.documentation.general.symbol_verilog,
enable_fsm: this.config.documentation.general.fsm
};
return options;
}
keys.forEach((key) => {
const value1 = main_config[key];
const value2 = default_config[key];

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tools
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public get_tool_options(): cfg_aux.t_tool_options {
const tool_name = this.config.tools.general.select_tool;
const tool_config = (<any>this.config.tools)[tool_name];
const tool_options: cfg_aux.t_tool_options = {
name: this.config.tools.general.select_tool,
installation_path: tool_config['installation_path'],
config: tool_config
};
return tool_options;
}
if (value1 !== undefined && value2 !== undefined
&& typeof value1 === typeof value2
&& typeof value1 === 'object' && !Array.isArray(value1)) {
// Both are object, so merge them
result[key] = merge_configs(value1, value2);
} else if (value1 !== undefined) {
// Keep main one
result[key] = value1;
} else {
// Use default one
result[key] = value2;
}
});

public get_tool_name(): e_tools_general_select_tool {
return this.config.tools.general.select_tool;
}
}
return result as T;

export function merge_configs(general_config: e_config | undefined, secondary_config: e_config) {
if (general_config === undefined) {
return secondary_config;
}
return general_config;
}
3 changes: 3 additions & 0 deletions packages/colibri/src/config/helpers/config_declaration.nj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export type e_config = {
{%- endfor %}
}
{%- endfor %}
[key: string]: {
[key: string]: any;
};
};

{%- for tp0 in type_declaration -%}
Expand Down
Loading

0 comments on commit 169211c

Please sign in to comment.