From 0e59de22364a0e7e5ec5f56b2ea18c173cf984a9 Mon Sep 17 00:00:00 2001 From: dhzdhd Date: Fri, 15 Apr 2022 19:38:48 +0530 Subject: [PATCH] Add support for execution of other langs --- lib/main.dart | 3 ++ lib/src/home/services/api.dart | 61 +++++++++++++++++++---------- lib/src/home/services/language.dart | 5 +-- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index bcdd26a..c70fb5f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,5 @@ +import 'package:excode/src/home/services/api.dart'; + import 'src/factory.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -9,6 +11,7 @@ import 'src/settings/services/settings_service.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); initialise(); + await ApiHandler.initRuntimeVersionData(); final settingsController = SettingsController(SettingsService()); await settingsController.loadSettings(); diff --git a/lib/src/home/services/api.dart b/lib/src/home/services/api.dart index 40e60bf..c9747cd 100644 --- a/lib/src/home/services/api.dart +++ b/lib/src/home/services/api.dart @@ -11,9 +11,7 @@ enum Languages { crystal, dart, dash, - denoTS, nodeTS, - denoJS, nodeJS, basic, fSharp, @@ -78,37 +76,60 @@ enum Languages { } class ApiHandler { - static const _url = 'https://emkc.org/api/v2/piston/execute'; + static const _executeUrl = 'https://emkc.org/api/v2/piston/execute'; + static const _runtimeUrl = 'https://emkc.org/api/v2/piston/runtimes'; + static final Map _data = {}; + + static Future initRuntimeVersionData() async { + final res = await dio.get(_runtimeUrl); + for (var i in res.data) { + _data[i['language']] = i['version']; + } + } static Map getDataFromLang(Languages lang) { switch (lang) { - case Languages.python: - return {'version': '3.10.0'}; + case Languages.coffeeScript: + return {'language': 'coffeescript', 'version': _data['coffeescript']}; + case Languages.nodeTS: + return {'language': 'typescript', 'version': _data['typescript']}; + case Languages.nodeJS: + return {'language': 'javascript', 'version': _data['javascript']}; + case Languages.basic: + return {'language': 'basic.net', 'version': _data['basic.net']}; + case Languages.cSharp: + return {'language': 'csharp.net', 'version': _data['csharp.net']}; case Languages.fSharp: - return {'language': 'fsharp.net', 'version': '5.0.201'}; - case Languages.rust: - return {'version': '1.50.0'}; + return {'language': 'fsharp.net', 'version': _data['fsharp.net']}; + case Languages.cpp: + return {'language': 'c++', 'version': _data['c++']}; + case Languages.golfScript: + return {'language': 'golfscript', 'version': _data['golfscript']}; + case Languages.oCaml: + return {'language': 'ocaml', 'version': _data['ocaml']}; default: - return {'language': 'python', 'version': '3.10.0'}; + return {'language': 'python', 'version': _data['python']}; } } static Future> executeCode( Languages lang, String content) async { late final Response res; + final data = { + 'language': lang.name, + 'version': _data.containsKey(lang.name) ? _data[lang.name] : '', + 'files': [ + {'content': content} + ], + 'stdin': '', + 'args': ['1', '2', '3'], + 'compile_timeout': 10000, + 'run_timeout': 3000, + }; + if (!_data.containsKey(lang.name)) data.addAll(getDataFromLang(lang)); try { - res = await dio.post(_url, data: { - 'language': lang.name, - ...getDataFromLang(lang), - 'files': [ - {'content': content} - ], - 'stdin': '', - 'args': ['1', '2', '3'], - 'compile_timeout': 10000, - 'run_timeout': 3000, - }); + res = await dio.post(_executeUrl, data: data); } on DioError catch (err) { print(err.message); return {'error': err.message}; diff --git a/lib/src/home/services/language.dart b/lib/src/home/services/language.dart index 408d5c1..b26333f 100644 --- a/lib/src/home/services/language.dart +++ b/lib/src/home/services/language.dart @@ -165,10 +165,8 @@ Map getThemeFromEnum(Themes theme) { String getThemeLangFromEnum(Languages lang) { switch (lang) { case Languages.cobol: // ? - case Languages.denoTS: case Languages.nodeTS: return 'typescript'; - case Languages.denoJS: case Languages.nodeJS: case Languages.golfScript: return 'javascript'; @@ -227,6 +225,7 @@ String getThemeLangFromEnum(Languages lang) { case Languages.vlang: // ? case Languages.zig: // ? return 'markdown'; + default: + return Languages.values[lang.index].name.toLowerCase(); } - return Languages.values[lang.index].name.toLowerCase(); }