mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-11-06 17:08:32 +00:00
Implemented possibility to open sketches via file navigator.
Signed-off-by: jbicker <jan.bicker@typefox.io>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { injectable } from "inversify";
|
||||
import { injectable, inject } from "inversify";
|
||||
import { SketchesService, Sketch } from "../common/protocol/sketches-service";
|
||||
import URI from "@theia/core/lib/common/uri";
|
||||
import { FileStat } from "@theia/filesystem/lib/common";
|
||||
import { FileStat, FileSystem } from "@theia/filesystem/lib/common";
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
@@ -10,10 +10,14 @@ export const ALLOWED_FILE_EXTENSIONS = [".c", ".cpp", ".h", ".hh", ".hpp", ".s",
|
||||
@injectable()
|
||||
export class SketchesServiceImpl implements SketchesService {
|
||||
|
||||
@inject(FileSystem)
|
||||
protected readonly filesystem: FileSystem;
|
||||
|
||||
async getSketches(fileStat?: FileStat): Promise<Sketch[]> {
|
||||
const sketches: Sketch[] = [];
|
||||
if (fileStat && fileStat.isDirectory) {
|
||||
const sketchFolderPath = this.getPath(fileStat);
|
||||
const uri = new URI(fileStat.uri);
|
||||
const sketchFolderPath = uri.path.toString()
|
||||
const files = fs.readdirSync(sketchFolderPath);
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(sketchFolderPath, file);
|
||||
@@ -32,18 +36,30 @@ export class SketchesServiceImpl implements SketchesService {
|
||||
* Return all allowed files.
|
||||
* File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino"
|
||||
*/
|
||||
async getSketchFiles(sketchDir: FileStat): Promise<string[]> {
|
||||
async getSketchFiles(sketchFileStat: FileStat): Promise<string[]> {
|
||||
const files: string[] = [];
|
||||
const sketchDirPath = this.getPath(sketchDir);
|
||||
const sketchDirContents = fs.readdirSync(sketchDirPath);
|
||||
sketchDirContents.forEach(fileName => {
|
||||
const filePath = path.join(sketchDirPath, fileName);
|
||||
if (fs.existsSync(filePath) &&
|
||||
fs.lstatSync(filePath).isFile() &&
|
||||
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
|
||||
const sketchUri = new URI(sketchFileStat.uri);
|
||||
const sketchPath = sketchUri.path.toString();
|
||||
if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) {
|
||||
const sketchDirContents = fs.readdirSync(sketchPath);
|
||||
sketchDirContents.forEach(fileName => {
|
||||
const filePath = path.join(sketchPath, fileName);
|
||||
if (fs.existsSync(filePath) &&
|
||||
fs.lstatSync(filePath).isFile() &&
|
||||
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
|
||||
files.push(filePath);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const sketchDir = sketchUri.path.dir;
|
||||
if (this.isSketchFolder(sketchDir.toString(), sketchDir.name)) {
|
||||
const sketchFolderStat = await this.filesystem.getFileStat(sketchDir.toString());
|
||||
if (sketchFolderStat) {
|
||||
const sketchDirContents = await this.getSketchFiles(sketchFolderStat);
|
||||
files.push(...sketchDirContents);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
@@ -58,10 +74,4 @@ export class SketchesServiceImpl implements SketchesService {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getPath(fileStat: FileStat) {
|
||||
const fileStatUri = fileStat.uri;
|
||||
const uri = new URI(fileStatUri);
|
||||
return uri.path.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user