Skip to content

Commit

Permalink
script runner
Browse files Browse the repository at this point in the history
  • Loading branch information
bunsenstraat committed Nov 18, 2023
1 parent 9006f90 commit ddd58cc
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 76 deletions.
6 changes: 4 additions & 2 deletions apps/remix-ide/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { electronConfig } from './app/plugins/electron/electronConfigPlugin'
import { electronTemplates } from './app/plugins/electron/templatesPlugin'
import { xtermPlugin } from './app/plugins/electron/xtermPlugin'
import { ripgrepPlugin } from './app/plugins/electron/ripgrepPlugin'
import { scriptRunnerPlugin } from './app/plugins/electron/scriptRunnerPlugin'
import { compilerLoaderPlugin, compilerLoaderPluginDesktop } from './app/plugins/electron/compilerLoaderPlugin'
import {OpenAIGpt} from './app/plugins/openaigpt'

Expand Down Expand Up @@ -366,7 +367,8 @@ class AppComponent {
this.engine.register([xterm])
const ripgrep = new ripgrepPlugin()
this.engine.register([ripgrep])

const scriptRunner = new scriptRunnerPlugin()
this.engine.register([scriptRunner])
}

const compilerloader = isElectron()? new compilerLoaderPluginDesktop(): new compilerLoaderPlugin()
Expand Down Expand Up @@ -490,7 +492,7 @@ class AppComponent {
await this.appManager.activatePlugin(['solidity-script', 'remix-templates'])

if(isElectron()){
await this.appManager.activatePlugin(['isogit', 'electronconfig', 'electronTemplates', 'xterm', 'ripgrep'])
await this.appManager.activatePlugin(['isogit', 'electronconfig', 'electronTemplates', 'xterm', 'ripgrep', 'scriptRunner'])
}

this.appManager.on(
Expand Down
11 changes: 11 additions & 0 deletions apps/remix-ide/src/app/plugins/electron/scriptRunnerPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ElectronPlugin } from '@remixproject/engine-electron';

export class scriptRunnerPlugin extends ElectronPlugin {
constructor(){
super({
displayName: 'scriptRunner',
name: 'scriptRunner',
description: 'scriptRunner'
})
}
}
1 change: 1 addition & 0 deletions apps/remix-ide/src/remixAppManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export class RemixAppManager extends PluginManager {
const res = await fetch(this.pluginsDirectory)
plugins = await res.json()
plugins = plugins.filter((plugin) => {
if(plugin.name === 'scriptRunner') return false
if (plugin.targets && Array.isArray(plugin.targets) && plugin.targets.length > 0) {
return plugin.targets.includes('remix')
}
Expand Down
8 changes: 8 additions & 0 deletions apps/remixdesktop/ethers
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Minified by jsDelivr using Terser v5.19.2.
* Original file: /npm/[email protected]/lib.commonjs/index.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ethers=void 0;const tslib_1=require("tslib"),ethers=tslib_1.__importStar(require("./ethers.js"));exports.ethers=ethers,tslib_1.__exportStar(require("./ethers.js"),exports);
//# sourceMappingURL=/sm/a1c32dfeb6df16b35d6019f0c3e6ba14be89dc94667adee6e919d228ff2f0493.map
3 changes: 2 additions & 1 deletion apps/remixdesktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"typescript": "^5.1.3"
},
"dependencies": {
"@remix-project/remix-url-resolver": "^0.0.65",
"@remixproject/engine": "0.3.37",
"@remixproject/engine-electron": "0.3.37",
"@remixproject/plugin": "0.3.37",
Expand All @@ -49,7 +50,7 @@
"express": "^4.18.2",
"isomorphic-git": "^1.24.2",
"node-pty": "^0.10.1",
"yarn": "^1.22.19"
"yarn": "^1.22.21"
},
"optionalDependencies": {
"@remix-project/remix-ws-templates": "^1.0.27"
Expand Down
3 changes: 3 additions & 0 deletions apps/remixdesktop/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ConfigPlugin } from './plugins/configPlugin';
import { TemplatesPlugin } from './plugins/templates';
import { RipgrepPlugin } from './plugins/ripgrepPlugin';
import { CompilerLoaderPlugin } from './plugins/compilerLoader';
import { ScriptRunnerPlugin } from './plugins/scriptRunner';

const engine = new Engine()
const appManager = new PluginManager()
Expand All @@ -19,6 +20,7 @@ const configPlugin = new ConfigPlugin()
const templatesPlugin = new TemplatesPlugin()
const ripgrepPlugin = new RipgrepPlugin()
const compilerLoaderPlugin = new CompilerLoaderPlugin()
const scriptRunnerPlugin = new ScriptRunnerPlugin()
engine.register(appManager)
engine.register(fsPlugin)
engine.register(xtermPlugin)
Expand All @@ -27,6 +29,7 @@ engine.register(configPlugin)
engine.register(templatesPlugin)
engine.register(ripgrepPlugin)
engine.register(compilerLoaderPlugin)
engine.register(scriptRunnerPlugin)

appManager.activatePlugin('electronconfig')
appManager.activatePlugin('fs')
Expand Down
18 changes: 10 additions & 8 deletions apps/remixdesktop/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow, dialog, Menu, MenuItem } from 'electron';
import { app, BrowserWindow, dialog, Menu, MenuItem, utilityProcess } from 'electron';
import path from 'path';


Expand Down Expand Up @@ -27,7 +27,7 @@ export const createWindow = async (dir?: string): Promise<void> => {
},
});
if (dir && dir.endsWith('/')) dir = dir.slice(0, -1)
let params = dir ? `?opendir=${encodeURIComponent(dir)}` : '';
const params = dir ? `?opendir=${encodeURIComponent(dir)}` : '';
// and load the index.html of the app.
mainWindow.loadURL(
process.env.NODE_ENV === 'production' || isPackaged ? `file://${__dirname}/remix-ide/index.html` + params :
Expand Down Expand Up @@ -104,13 +104,15 @@ const commandKeys: Record<string, string> = {
};

const menu = [...(process.platform === 'darwin' ? [darwinMenu(commandKeys, execCommand, showAbout)] : []),
FileMenu(commandKeys, execCommand),
GitMenu(commandKeys, execCommand),
EditMenu(commandKeys, execCommand),
ViewMenu(commandKeys, execCommand),
TerminalMenu(commandKeys, execCommand),
WindowMenu(commandKeys, execCommand, []),
FileMenu(commandKeys, execCommand),
GitMenu(commandKeys, execCommand),
EditMenu(commandKeys, execCommand),
ViewMenu(commandKeys, execCommand),
TerminalMenu(commandKeys, execCommand),
WindowMenu(commandKeys, execCommand, []),
]

Menu.setApplicationMenu(Menu.buildFromTemplate(menu))



6 changes: 3 additions & 3 deletions apps/remixdesktop/src/plugins/compilerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CompilerLoaderPluginClient extends ElectronBasePluginClient {
}

async getLists(){

return await getLists()
}
}

Expand All @@ -123,14 +123,14 @@ const getLists = async()=>{
let wasmData

try{
let binRes = await axios.get(baseURLBin + '/list.json')
const binRes = await axios.get(baseURLBin + '/list.json')
await fs.writeFile(cacheDir + '/binlist.json', JSON.stringify(binRes.data, null, 2))
binData = binRes.data
}catch(e) {
}

try{
let wasmRes = await axios.get(baseURLWasm + '/list.json')
const wasmRes = await axios.get(baseURLWasm + '/list.json')
await fs.writeFile(cacheDir + '/wasmlist.json', JSON.stringify(wasmRes.data, null, 2))
wasmData = wasmRes.data
}catch(e) {
Expand Down
68 changes: 41 additions & 27 deletions apps/remixdesktop/src/plugins/configPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,63 @@ import { ElectronBasePlugin, ElectronBasePluginClient } from "@remixproject/plug

import { Profile } from "@remixproject/plugin-utils";
import { readConfig, writeConfig } from "../utils/config";
import { app, utilityProcess } from "electron";
import path from "path";

const profile: Profile = {
displayName: 'electronconfig',
name: 'electronconfig',
description: 'Electron Config'
displayName: 'electronconfig',
name: 'electronconfig',
description: 'Electron Config'
}

export class ConfigPlugin extends ElectronBasePlugin {
clients: ConfigPluginClient[] = []
constructor() {
super(profile, clientProfile, ConfigPluginClient)
this.methods = [...super.methods, 'writeConfig', 'readConfig']
}
clients: ConfigPluginClient[] = []
constructor() {
super(profile, clientProfile, ConfigPluginClient)
this.methods = [...super.methods, 'writeConfig', 'readConfig']
}

async writeConfig(data: any): Promise<void> {
writeConfig(data)
}
async writeConfig(data: any): Promise<void> {
writeConfig(data)
}

async readConfig(webContentsId: any): Promise<any> {
return readConfig()
}
async readConfig(webContentsId: any): Promise<any> {
return readConfig()
}

}

const clientProfile: Profile = {
name: 'electronconfig',
displayName: 'electronconfig',
description: 'Electron Config',
methods: ['writeConfig', 'readConfig']
name: 'electronconfig',
displayName: 'electronconfig',
description: 'Electron Config',
methods: ['writeConfig', 'readConfig']
}

class ConfigPluginClient extends ElectronBasePluginClient {

constructor(webContentsId: number, profile: Profile) {
super(webContentsId, profile)
}
constructor(webContentsId: number, profile: Profile) {
super(webContentsId, profile)

async writeConfig(data: any): Promise<void> {
writeConfig(data)
}
}

async readConfig(): Promise<any> {
return readConfig()
}
async onActivation(): Promise<void> {
//utilityProcess.fork('/Users/filipmertens/Documents/GitHub/remix-project/apps/remixdesktop/node_modules/yarn/bin/yarn.js')
/*const child = utilityProcess.fork(path.join(__dirname, 'utility.js'), [app.getPath('userData')])
this.call('terminal' as any, 'log', JSON.stringify(process.env))
child.on('message', (data) => {
console.log('message from child', data)
this.call('terminal', 'log', data)
})
*/
}

async writeConfig(data: any): Promise<void> {
writeConfig(data)
}

async readConfig(): Promise<any> {
return readConfig()
}

}
98 changes: 98 additions & 0 deletions apps/remixdesktop/src/plugins/scriptRunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { ElectronBasePlugin, ElectronBasePluginClient } from "@remixproject/plugin-electron"
import { utilityProcess } from "electron"
import path from "path"
import * as esbuild from 'esbuild'
import { RemixURLResolver } from '@remix-project/remix-url-resolver'
import fs from 'fs/promises'
import os, { arch } from 'os'

const resolver = new RemixURLResolver()
export const cacheDir = path.join(os.homedir(), '.cache_remix_ide')

const profile = {
"name": "scriptRunner",
"displayName": "Script Runner",
"description": "Execute script and emit logs",
}

const convertPathToPosix = (pathName: string): string => {
return pathName.split(path.sep).join(path.posix.sep)
}

export class ScriptRunnerPlugin extends ElectronBasePlugin {
constructor() {
super(profile, clientProfile, ScriptRunnerClient)
this.methods = [...super.methods, 'execute']
}
}

const clientProfile = {
"name": "scriptRunner",
"displayName": "Script Runner",
"description": "Execute script and emit logs",
"methods": ["execute"]
}

class ScriptRunnerClient extends ElectronBasePluginClient {
workingDir: string = ''
constructor(webContentsId: number, profile: any) {
super(webContentsId, profile)
this.onload(() => {
this.on('fs' as any, 'workingDirChanged', async (path: string) => {
this.workingDir = path
})
})
}

async execute(content: string, path: string): Promise<void> {
path = convertPathToPosix(this.fixPath(path))
console.log('execute', path)
const out = convertPathToPosix(this.fixPath('dist'))
const build = await esbuild.build({
entryPoints: [path],
bundle: true,
outdir: out,
plugins: [onResolvePlugin],
})
console.log(build)
if(build.errors.length > 0) {
console.log('ERRORS', build.errors)
return
}

}

fixPath(path: string): string {
if (this.workingDir === '') throw new Error('workingDir is not set')
if (path) {
if (path.startsWith('/')) {
path = path.slice(1)
}
}
path = this.workingDir + (!this.workingDir.endsWith('/') ? '/' : '') + path
return path
}
}


const onResolvePlugin = {
name: 'onResolve',
setup(build: esbuild.PluginBuild) {

build.onLoad({
filter: /.*/,
}, async args => {
console.log('onLoad', args)
/*if(args.namespace && args.namespace !== 'file'){
const imported = await resolver.resolve(args.path)
console.log('imported', imported)
return {
contents: imported.content,
loader: 'js',
}
}*/
return undefined

})
}
}
20 changes: 20 additions & 0 deletions apps/remixdesktop/src/plugins/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
console.log('Starting utilities')
process.parentPort.postMessage('start utilities')
import { spawn } from 'node:child_process';
const ls = spawn('ls', ['-la'])
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
process.parentPort.postMessage(data.toString())
});

process.parentPort.postMessage(JSON.stringify(process.env))

const ls2 = spawn('yarn', ['-v'])
ls2.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
process.parentPort.postMessage(data.toString())
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
process.parentPort.postMessage(data.toString())
})
2 changes: 1 addition & 1 deletion apps/remixdesktop/src/plugins/xtermPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class XtermPluginClient extends ElectronBasePluginClient {
parsedEnv = parseEnv(stdout)
}

let env = parsedEnv || process.env
const env = parsedEnv || process.env

const ptyProcess = pty.spawn(shell || defaultShell, [], {
name: 'xterm-color',
Expand Down
2 changes: 1 addition & 1 deletion apps/remixdesktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ console.log('preload.ts', new Date().toLocaleTimeString())

/* preload script needs statically defined API for each plugin */

const exposedPLugins = ['fs', 'git', 'xterm', 'isogit', 'electronconfig', 'electronTemplates', 'ripgrep', 'compilerloader']
const exposedPLugins = ['fs', 'git', 'xterm', 'isogit', 'electronconfig', 'electronTemplates', 'ripgrep', 'compilerloader', 'scriptRunner']

let webContentsId: number | undefined

Expand Down
Loading

0 comments on commit ddd58cc

Please sign in to comment.