Skip to content

Commit

Permalink
Add support for opening place files using Studio command
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed May 7, 2024
1 parent bec31a5 commit 74645aa
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Added

- It is now possible to open `.rbxl` pr `.rbxlx` file using `Studio` command

## [2.0.3] - 2024-05-05

### Changed
Expand Down
21 changes: 13 additions & 8 deletions src/argon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,8 @@ export function init(project: string, template: string, options: string[]) {
spawn(['init', project, '--template', template, ...options])
}

export function stop(id: number | number[]) {
if (typeof id === 'number') {
spawn(['stop', id.toString()])
} else {
spawn(['stop', ...id.map((id) => id.toString())])
}
export function stop(ids: number[]) {
spawn(['stop', ...ids.map((id) => id.toString())])
}

export function debug(mode: string) {
Expand All @@ -118,8 +114,17 @@ export function exec(code: string, focus?: boolean) {
spawn(['exec', code, ...args])
}

export function studio(check?: boolean) {
const args = check ? ['--check'] : []
export function studio(check?: boolean, place?: string) {
const args = []

if (place) {
args.push(place)
}

if (check) {
args.push('--check')
}

spawn(['studio', ...args])
}

Expand Down
2 changes: 1 addition & 1 deletion src/menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function onDidAccept(action: string, state: State) {
exec.handler()
break
case 'studio':
studio.handler()
await studio.handler()
break
case 'plugin':
await plugin.handler()
Expand Down
5 changes: 1 addition & 4 deletions src/menu/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ export async function handler(state: State): Promise<void> {

if (ids.length !== 0) {
argon.stop(ids)

ids.forEach((id) => {
state.removeSession(id)
})
state.removeSessions(ids)
}

resolve()
Expand Down
31 changes: 29 additions & 2 deletions src/menu/studio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as vscode from 'vscode'
import * as argon from '../argon'
import { findPlaces } from '../util'
import { Item } from '.'

export const item: Item = {
Expand All @@ -7,6 +9,31 @@ export const item: Item = {
action: 'studio',
}

export function handler() {
argon.studio()
function getPlace(): Promise<string | undefined> {
return new Promise((resolve, reject) => {
const places = findPlaces()
places.unshift('$(window) Launch empty')

vscode.window
.showQuickPick(places, {
title: 'Select a place',
})
.then(async (place) => {
if (!place) {
return reject()
}

place.endsWith('.rbxl') || place.endsWith('.rbxlx')
? resolve(place)
: resolve(undefined)
})
})
}

export async function handler() {
const place = await getPlace()

console.log(place)

argon.studio(false, place)
}
12 changes: 6 additions & 6 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export class State {
this.context.workspaceState.update('lastProject', session.project)
}

public removeSession(id: number) {
public removeSessions(ids: number[]) {
const lastProject = this.context.workspaceState.get('lastProject')

this.sessions = this.sessions.filter((session) => {
const matches = session.id === id
const matches = ids.includes(session.id)

if (matches && session.project === lastProject) {
this.context.workspaceState.update('lastProject', undefined)
Expand All @@ -61,10 +61,10 @@ export class State {
}

public cleanup() {
this.sessions.forEach((session) => {
console.log(`Stopping session ${session.id}...`)
argon.stop(session.id)
})
const ids = this.sessions.map((session) => session.id)

console.log(`Stopping ${ids.length} sessions...`)
argon.stop(ids)
}

private updateItem() {
Expand Down
18 changes: 18 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ export function findProjects(placesOnly?: boolean): string[] {
return projects
}

export function findPlaces(): string[] {
const dir = getCurrentDir()

if (!dir) {
throw new Error('Cannot find places without a workspace folder open!')
}

let places = fs
.readdirSync(dir)
.filter(
(entry) =>
(entry.endsWith('.rbxl') || entry.endsWith('.rbxlx')) &&
fs.statSync(path.join(dir, entry)).isFile(),
)

return places
}

export function getProjectName(project: string): string {
if (!path.isAbsolute(project)) {
const dir = getCurrentDir()
Expand Down

0 comments on commit 74645aa

Please sign in to comment.