taskProvider.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. import * as vscode from 'vscode';
  6. import * as os from 'os';
  7. import { TargetConfigPanel } from './view/TargetConfigPanel';
  8. export interface OwnShellOption {
  9. cmd: string;
  10. options: vscode.ShellExecutionOptions;
  11. }
  12. export class WasmTaskProvider implements vscode.TaskProvider {
  13. constructor(
  14. public _type: Map<string, string>,
  15. public _script: Map<string, string>,
  16. public _wamrVersion: string
  17. ) {}
  18. buildShellOption: OwnShellOption | undefined;
  19. runShellOption: OwnShellOption | undefined;
  20. debugShellOption: OwnShellOption | undefined;
  21. destroyShellOption: OwnShellOption | undefined;
  22. private wasmPromise: Thenable<vscode.Task[]> | undefined = undefined;
  23. public provideTasks(): Thenable<vscode.Task[]> | undefined {
  24. if (!this.wasmPromise) {
  25. /* target name is used for generated aot target */
  26. const targetName =
  27. TargetConfigPanel.buildArgs.outputFileName.split('.')[0];
  28. const heapSize = TargetConfigPanel.buildArgs.hostManagedHeapSize;
  29. if (
  30. os.platform() === 'linux' ||
  31. os.platform() === 'darwin' ||
  32. os.platform() === 'win32'
  33. ) {
  34. /* build */
  35. this.buildShellOption = {
  36. cmd:
  37. os.platform() === 'linux' || os.platform() === 'darwin'
  38. ? 'bash'
  39. : (this._script.get('buildScript') as string),
  40. options: {
  41. executable: this._script.get('buildScript'),
  42. shellArgs: [targetName, this._wamrVersion],
  43. },
  44. };
  45. /* debug */
  46. this.debugShellOption = {
  47. cmd:
  48. os.platform() === 'linux' || os.platform() === 'darwin'
  49. ? 'bash'
  50. : (this._script.get('debugScript') as string),
  51. options: {
  52. executable: this._script.get('debugScript'),
  53. shellArgs: [targetName, this._wamrVersion, heapSize],
  54. },
  55. };
  56. /* run */
  57. this.runShellOption = {
  58. cmd:
  59. os.platform() === 'linux' || os.platform() === 'darwin'
  60. ? 'bash'
  61. : (this._script.get('runScript') as string),
  62. options: {
  63. executable: this._script.get('runScript'),
  64. shellArgs: [targetName, this._wamrVersion, heapSize],
  65. },
  66. };
  67. /* destroy */
  68. /* run */
  69. this.destroyShellOption = {
  70. cmd:
  71. os.platform() === 'linux' || os.platform() === 'darwin'
  72. ? 'bash'
  73. : (this._script.get('destroyScript') as string),
  74. options: {
  75. executable: this._script.get('destroyScript'),
  76. shellArgs: [targetName],
  77. },
  78. };
  79. } else {
  80. this.buildShellOption = {
  81. cmd: "echo 'os platform is not supported yet'",
  82. options: {},
  83. };
  84. this.debugShellOption = {
  85. cmd: "echo 'os platform is not supported yet'",
  86. options: {},
  87. };
  88. this.runShellOption = {
  89. cmd: "echo 'os platform is not supported yet'",
  90. options: {},
  91. };
  92. this.destroyShellOption = {
  93. cmd: "echo 'os platform is not supported yet'",
  94. options: {},
  95. };
  96. }
  97. this.wasmPromise = Promise.resolve([
  98. new vscode.Task(
  99. { type: 'wasm' },
  100. vscode.TaskScope.Workspace,
  101. 'Wasm',
  102. this._type.get('Build') as string,
  103. new vscode.ShellExecution(
  104. this.buildShellOption.cmd,
  105. this.buildShellOption.options
  106. )
  107. ),
  108. new vscode.Task(
  109. { type: 'wasm' },
  110. vscode.TaskScope.Workspace,
  111. 'Wasm',
  112. this._type.get('Run') as string,
  113. new vscode.ShellExecution(
  114. this.runShellOption.cmd,
  115. this.runShellOption.options
  116. )
  117. ),
  118. new vscode.Task(
  119. { type: 'wasm' },
  120. vscode.TaskScope.Workspace,
  121. 'Wasm',
  122. this._type.get('Debug') as string,
  123. new vscode.ShellExecution(
  124. this.debugShellOption.cmd,
  125. this.debugShellOption.options
  126. )
  127. ),
  128. new vscode.Task(
  129. { type: 'wasm' },
  130. vscode.TaskScope.Workspace,
  131. 'Wasm-Container-Before-Build',
  132. this._type.get('Destroy') as string,
  133. new vscode.ShellExecution(
  134. this.destroyShellOption.cmd,
  135. this.destroyShellOption.options
  136. )
  137. ),
  138. new vscode.Task(
  139. { type: 'wasm' },
  140. vscode.TaskScope.Workspace,
  141. 'Wasm-Container-Before-Debug',
  142. this._type.get('Destroy') as string,
  143. new vscode.ShellExecution(
  144. this.destroyShellOption.cmd,
  145. this.destroyShellOption.options
  146. )
  147. ),
  148. new vscode.Task(
  149. { type: 'wasm' },
  150. vscode.TaskScope.Workspace,
  151. 'Wasm-Container-Before-Run',
  152. this._type.get('Destroy') as string,
  153. new vscode.ShellExecution(
  154. this.destroyShellOption.cmd,
  155. this.destroyShellOption.options
  156. )
  157. ),
  158. new vscode.Task(
  159. { type: 'wasm' },
  160. vscode.TaskScope.Workspace,
  161. 'Wasm-Container-After-Build',
  162. this._type.get('Destroy') as string,
  163. new vscode.ShellExecution(
  164. this.destroyShellOption.cmd,
  165. this.destroyShellOption.options
  166. )
  167. ),
  168. new vscode.Task(
  169. { type: 'wasm' },
  170. vscode.TaskScope.Workspace,
  171. 'Wasm-Container-After-Debug',
  172. this._type.get('Destroy') as string,
  173. new vscode.ShellExecution(
  174. this.destroyShellOption.cmd,
  175. this.destroyShellOption.options
  176. )
  177. ),
  178. new vscode.Task(
  179. { type: 'wasm' },
  180. vscode.TaskScope.Workspace,
  181. 'Wasm-Container-After-Run',
  182. this._type.get('Destroy') as string,
  183. new vscode.ShellExecution(
  184. this.destroyShellOption.cmd,
  185. this.destroyShellOption.options
  186. )
  187. ),
  188. ]);
  189. }
  190. return this.wasmPromise;
  191. }
  192. /**
  193. * if the task or task in tasks.json does not set command, `
  194. * resolveTask` will be invoked,
  195. * otherwise, `provideTasks` will be invoked
  196. * @param _task
  197. * @returns
  198. */
  199. public resolveTask(task: vscode.Task): vscode.Task | undefined {
  200. if (task) {
  201. return task;
  202. }
  203. return undefined;
  204. }
  205. }