|
| 1 | +import { ToolchainName, IToolchian, ToolchainManager } from "./ToolchainManager"; |
| 2 | +import { GlobalEvent } from "./GlobalEvents"; |
| 3 | +import { newMessage } from "./Message"; |
| 4 | +import { view_str$prompt$requestAndActivateLicence_warn_setupPath, getLocalLanguageType, LanguageIndexs } from "./StringTable"; |
| 5 | +import { SimpleUIConfig, SimpleUIConfigData_tag, SimpleUIConfigData_divider, SimpleUIConfigData_input, SimpleUIConfigData_button } from './SimpleUIDef'; |
| 6 | +import { WebPanelManager } from "./WebPanelManager"; |
| 7 | +import { runShellCommand, openUrl } from "./utility"; |
| 8 | +import { File } from "../lib/node-utility/File"; |
| 9 | + |
| 10 | +import * as path from "path"; |
| 11 | +import * as os from "os"; |
| 12 | +import * as fs from "fs"; |
| 13 | +import * as vscode from "vscode"; |
| 14 | + |
| 15 | +export function requestAndActivateLicence(toolchainId: ToolchainName) { |
| 16 | + |
| 17 | + const toolchain_ = ToolchainManager.getInstance().getToolchainByName(toolchainId); |
| 18 | + if (!toolchain_) { |
| 19 | + GlobalEvent.emit('msg', newMessage('Error', `Not found this toolchain: '${toolchainId}' !`)); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + if (!ToolchainManager.getInstance().isToolchainPathReady(toolchainId)) { |
| 24 | + GlobalEvent.emit('msg', newMessage('Warning', view_str$prompt$requestAndActivateLicence_warn_setupPath)); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + switch (toolchainId) { |
| 29 | + case 'COSMIC_STM8': |
| 30 | + requestAndActivate_COSMIC_STM8(toolchain_); |
| 31 | + break; |
| 32 | + default: |
| 33 | + GlobalEvent.emit('msg', newMessage('Warning', `We not support this toolchain: '${toolchainId}' !`)); |
| 34 | + break; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function requestAndActivate_COSMIC_STM8(toolchain: IToolchian) { |
| 39 | + |
| 40 | + const toolchainRootDir = toolchain.getToolchainDir(); |
| 41 | + const isChinese = getLocalLanguageType() == LanguageIndexs.Chinese; |
| 42 | + |
| 43 | + const ui_1: SimpleUIConfig = isChinese |
| 44 | + |
| 45 | + // chinese |
| 46 | + ? { |
| 47 | + title: '申请 COSMIC STM8 编译器许可证', |
| 48 | + viewColumn: vscode.ViewColumn.One, |
| 49 | + notTakeFocus: false, |
| 50 | + btns: { |
| 51 | + 'submit': { |
| 52 | + title: '发送申请', |
| 53 | + hidden: false |
| 54 | + }, |
| 55 | + 'reset': { |
| 56 | + title: '', |
| 57 | + hidden: true |
| 58 | + } |
| 59 | + }, |
| 60 | + items: { |
| 61 | + 'userName': { |
| 62 | + type: 'input', |
| 63 | + name: '输入用户名(系统的当前账户名)', |
| 64 | + attrs: { 'singleLine': true }, |
| 65 | + data: <SimpleUIConfigData_input>{ |
| 66 | + value: path.basename(os.homedir()) |
| 67 | + } |
| 68 | + }, |
| 69 | + 'hostId': { |
| 70 | + type: 'input', |
| 71 | + name: '输入主机ID', |
| 72 | + attrs: { 'singleLine': true }, |
| 73 | + data: <SimpleUIConfigData_input>{ |
| 74 | + value: '' |
| 75 | + } |
| 76 | + }, |
| 77 | + 'hostId-1': { |
| 78 | + type: 'button', |
| 79 | + name: '查看当前的主机ID', |
| 80 | + attrs: {}, |
| 81 | + data: <SimpleUIConfigData_button>{ |
| 82 | + clickEvent: 'btn.show-host-id' |
| 83 | + } |
| 84 | + }, |
| 85 | + 'email': { |
| 86 | + type: 'input', |
| 87 | + name: '输入你的邮箱(用于接收许可证)', |
| 88 | + attrs: { 'singleLine': true }, |
| 89 | + data: <SimpleUIConfigData_input>{ |
| 90 | + placeHolder: 'xxx@xxx.xx', |
| 91 | + value: '' |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + // en-us |
| 97 | + : { |
| 98 | + |
| 99 | + title: 'Request Licence For COSMIC STM8 Compiler', |
| 100 | + viewColumn: vscode.ViewColumn.One, |
| 101 | + notTakeFocus: false, |
| 102 | + btns: { |
| 103 | + 'submit': { |
| 104 | + title: 'Send Request', |
| 105 | + hidden: false |
| 106 | + }, |
| 107 | + 'reset': { |
| 108 | + title: '', |
| 109 | + hidden: true |
| 110 | + } |
| 111 | + }, |
| 112 | + items: { |
| 113 | + 'userName': { |
| 114 | + type: 'input', |
| 115 | + name: 'User Name For This PC', |
| 116 | + attrs: { 'singleLine': true }, |
| 117 | + data: <SimpleUIConfigData_input>{ |
| 118 | + value: path.basename(os.homedir()) |
| 119 | + } |
| 120 | + }, |
| 121 | + 'hostId': { |
| 122 | + type: 'input', |
| 123 | + name: 'Enter Your Host ID', |
| 124 | + attrs: { 'singleLine': true }, |
| 125 | + data: <SimpleUIConfigData_input>{ |
| 126 | + value: '' |
| 127 | + } |
| 128 | + }, |
| 129 | + 'hostId-1': { |
| 130 | + type: 'button', |
| 131 | + name: 'Show My Host ID', |
| 132 | + attrs: {}, |
| 133 | + data: <SimpleUIConfigData_button>{ |
| 134 | + clickEvent: 'btn.show-host-id' |
| 135 | + } |
| 136 | + }, |
| 137 | + 'email': { |
| 138 | + type: 'input', |
| 139 | + name: 'Enter Your Email (to receive licence file)', |
| 140 | + attrs: { 'singleLine': true }, |
| 141 | + data: <SimpleUIConfigData_input>{ |
| 142 | + placeHolder: 'xxx@xxx.xx', |
| 143 | + value: '' |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + WebPanelManager.instance().showSimpleConfigUI(ui_1, |
| 150 | + |
| 151 | + // on submited |
| 152 | + (data) => { |
| 153 | + |
| 154 | + // http://vps362893.ovh.net/registerAuto.php?name={1}&hostid={2}&mail={3}&product=LXSTM8FSE_2023 |
| 155 | + // 其中: |
| 156 | + // - {1} 填 用户名 |
| 157 | + // - {2} 填 主机ID |
| 158 | + // - {3} 填 邮箱 |
| 159 | + |
| 160 | + const usrNam = data.items['userName'].data.value.trim(); |
| 161 | + const hostId = data.items['hostId'].data.value.replace(/"/g, '').trim(); |
| 162 | + const email_ = data.items['email'].data.value.trim(); |
| 163 | + |
| 164 | + const url = `http://vps362893.ovh.net/registerAuto.php?name=${usrNam}&hostid=${hostId}&mail=${email_}&product=LXSTM8FSE_2023`; |
| 165 | + |
| 166 | + openUrl(url); |
| 167 | + }, |
| 168 | + |
| 169 | + // on msg |
| 170 | + (msg) => { |
| 171 | + |
| 172 | + if (msg == 'btn.show-host-id') { |
| 173 | + |
| 174 | + runShellCommand('(COSMIC) show host id', |
| 175 | + 'LmregFSE', undefined, true, toolchainRootDir.path); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + setTimeout(() => { |
| 180 | + |
| 181 | + const ui_2: SimpleUIConfig = isChinese |
| 182 | + |
| 183 | + // chinese |
| 184 | + ? { |
| 185 | + title: '激活 COSMIC STM8 编译器许可证', |
| 186 | + viewColumn: vscode.ViewColumn.Two, |
| 187 | + notTakeFocus: true, |
| 188 | + btns: { |
| 189 | + 'submit': { |
| 190 | + title: '激活', |
| 191 | + hidden: false |
| 192 | + }, |
| 193 | + 'reset': { |
| 194 | + title: '', |
| 195 | + hidden: true |
| 196 | + } |
| 197 | + }, |
| 198 | + items: { |
| 199 | + 'licence': { |
| 200 | + type: 'input', |
| 201 | + name: `输入你收到的许可证(邮件附件 'licence.lic' 的完整内容)`, |
| 202 | + attrs: { 'singleLine': false, 'rows': 15 }, |
| 203 | + data: <SimpleUIConfigData_input>{ |
| 204 | + placeHolder: [ |
| 205 | + `#`, |
| 206 | + `# Please don't change or add any lines in this file`, |
| 207 | + `# Generated automatically by Cosmic website on 2023-04-09 07:34:46`, |
| 208 | + `#`, |
| 209 | + `FEATURE LXSTM8FSE_202X cosmic 31.1 09-apr-2024 uncounted \\`, |
| 210 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 211 | + `FEATURE LXSTM8FSE_2020 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 212 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 213 | + `FEATURE LXSTM8FSE_2019 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 214 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 215 | + `FEATURE LXSTM8FSE_2018 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 216 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 217 | + `FEATURE LXSTM8FSE_2017 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 218 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 219 | + ].join(os.EOL), |
| 220 | + value: '' |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + // en-us |
| 226 | + : { |
| 227 | + title: 'Activate Licence For COSMIC STM8 Compiler', |
| 228 | + viewColumn: vscode.ViewColumn.Two, |
| 229 | + notTakeFocus: true, |
| 230 | + btns: { |
| 231 | + 'submit': { |
| 232 | + title: 'Activate', |
| 233 | + hidden: false |
| 234 | + }, |
| 235 | + 'reset': { |
| 236 | + title: '', |
| 237 | + hidden: true |
| 238 | + } |
| 239 | + }, |
| 240 | + items: { |
| 241 | + 'licence': { |
| 242 | + type: 'input', |
| 243 | + name: `Enter Your Licence (Content of 'licence.lic' file in your email)`, |
| 244 | + attrs: { 'singleLine': false, 'rows': 15 }, |
| 245 | + data: <SimpleUIConfigData_input>{ |
| 246 | + placeHolder: [ |
| 247 | + `#`, |
| 248 | + `# Please don't change or add any lines in this file`, |
| 249 | + `# Generated automatically by Cosmic website on 2023-04-09 07:34:46`, |
| 250 | + `#`, |
| 251 | + `FEATURE LXSTM8FSE_202X cosmic 31.1 09-apr-2024 uncounted \\`, |
| 252 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 253 | + `FEATURE LXSTM8FSE_2020 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 254 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 255 | + `FEATURE LXSTM8FSE_2019 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 256 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 257 | + `FEATURE LXSTM8FSE_2018 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 258 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 259 | + `FEATURE LXSTM8FSE_2017 cosmic 31.1 09-apr-2024 uncounted \\`, |
| 260 | + ` HOSTID="xxxxxxxx xxxxxxxx" SIGN=xxxxxxxx`, |
| 261 | + ].join(os.EOL), |
| 262 | + value: '' |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + }; |
| 267 | + |
| 268 | + WebPanelManager.instance().showSimpleConfigUI(ui_2, |
| 269 | + |
| 270 | + // on submited |
| 271 | + (data) => { |
| 272 | + |
| 273 | + const licenceCont: string = data.items['licence'].data.value; |
| 274 | + |
| 275 | + if (!licenceCont.trim()) { |
| 276 | + throw new Error('licence can not be empty !'); |
| 277 | + } |
| 278 | + |
| 279 | + let outDir: File; |
| 280 | + |
| 281 | + const dirs = toolchainRootDir.GetList(File.EXCLUDE_ALL_FILTER, [/License/i]); |
| 282 | + if (dirs.length == 0) { |
| 283 | + outDir = File.fromArray([toolchainRootDir.path, 'License']); |
| 284 | + outDir.CreateDir(false); |
| 285 | + } else { |
| 286 | + outDir = dirs[0]; |
| 287 | + } |
| 288 | + |
| 289 | + fs.writeFileSync(outDir.path + File.sep + 'license.lic', licenceCont); |
| 290 | + }); |
| 291 | + }, 600); |
| 292 | +} |
0 commit comments