[win] Open sketch.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2019-07-23 20:58:14 +02:00
parent 75f7d3ca7c
commit c0e279f3e8
3 changed files with 23 additions and 30 deletions

View File

@ -357,11 +357,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
async openSketchFiles(uri: string) { async openSketchFiles(uri: string) {
const fileStat = await this.fileSystem.getFileStat(uri); const fileStat = await this.fileSystem.getFileStat(uri);
if (fileStat) { if (fileStat) {
const sketchFiles = await this.sketches.getSketchFiles(fileStat); const uris = await this.sketches.getSketchFiles(fileStat);
sketchFiles.forEach(sketchFile => { for (const uri of uris) {
const uri = new URI(sketchFile.uri); this.editorManager.open(new URI(uri));
this.editorManager.open(uri); }
});
} }
} }

View File

@ -4,7 +4,7 @@ export const SketchesServicePath = '/services/sketches-service';
export const SketchesService = Symbol('SketchesService'); export const SketchesService = Symbol('SketchesService');
export interface SketchesService { export interface SketchesService {
getSketches(fileStat?: FileStat): Promise<Sketch[]> getSketches(fileStat?: FileStat): Promise<Sketch[]>
getSketchFiles(fileStat: FileStat): Promise<FileStat[]> getSketchFiles(fileStat: FileStat): Promise<string[]>
} }
export interface Sketch { export interface Sketch {

View File

@ -4,6 +4,7 @@ import URI from "@theia/core/lib/common/uri";
import { FileStat, FileSystem } from "@theia/filesystem/lib/common"; import { FileStat, FileSystem } from "@theia/filesystem/lib/common";
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import { FileUri } from "@theia/core/lib/node";
export const ALLOWED_FILE_EXTENSIONS = [".c", ".cpp", ".h", ".hh", ".hpp", ".s", ".pde", ".ino"]; export const ALLOWED_FILE_EXTENSIONS = [".c", ".cpp", ".h", ".hh", ".hpp", ".s", ".pde", ".ino"];
@ -19,16 +20,16 @@ export class SketchesServiceImpl implements SketchesService {
const uri = new URI(fileStat.uri); const uri = new URI(fileStat.uri);
const sketchFolderPath = await this.filesystem.getFsPath(uri.toString()); const sketchFolderPath = await this.filesystem.getFsPath(uri.toString());
if (sketchFolderPath) { if (sketchFolderPath) {
const files = fs.readdirSync(sketchFolderPath); const fileNames = fs.readdirSync(sketchFolderPath);
files.forEach(file => { for (const fileName of fileNames) {
const filePath = path.join(sketchFolderPath, file); const filePath = path.join(sketchFolderPath, fileName);
if (this.isSketchFolder(filePath, file)) { if (this.isSketchFolder(filePath, fileName)) {
sketches.push({ sketches.push({
name: file, name: fileName,
uri: filePath uri: FileUri.create(filePath).toString()
}); });
} }
}); }
} }
} }
return sketches; return sketches;
@ -38,25 +39,19 @@ export class SketchesServiceImpl implements SketchesService {
* Return all allowed files. * Return all allowed files.
* File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino" * File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino"
*/ */
async getSketchFiles(sketchFileStat: FileStat): Promise<FileStat[]> { async getSketchFiles(sketchFileStat: FileStat): Promise<string[]> {
const files: FileStat[] = []; const uris: string[] = [];
const sketchUri = new URI(sketchFileStat.uri); const sketchUri = new URI(sketchFileStat.uri);
const sketchPath = await this.filesystem.getFsPath(sketchUri.toString()); const sketchPath = await this.filesystem.getFsPath(sketchUri.toString());
if (sketchPath) { if (sketchPath) {
if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) { if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) {
const sketchDirContents = fs.readdirSync(sketchPath); const fileNames = fs.readdirSync(sketchPath);
for (const i in sketchDirContents) { for (const fileName of fileNames) {
const fileName = sketchDirContents[i];
const filePath = path.join(sketchPath, fileName); const filePath = path.join(sketchPath, fileName);
if (fs.existsSync(filePath) && if (ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1
fs.lstatSync(filePath).isFile() && && fs.existsSync(filePath)
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) { && fs.lstatSync(filePath).isFile()) {
const fileStat = await this.filesystem.getFileStat(filePath); uris.push(FileUri.create(filePath).toString())
if (fileStat) {
console.log("111111111111", fileStat);
files.push(fileStat);
console.log("222222222222222", files);
}
} }
} }
} else { } else {
@ -65,13 +60,12 @@ export class SketchesServiceImpl implements SketchesService {
const sketchFolderStat = await this.filesystem.getFileStat(sketchUri.path.dir.toString()); const sketchFolderStat = await this.filesystem.getFileStat(sketchUri.path.dir.toString());
if (sketchFolderStat) { if (sketchFolderStat) {
const sketchDirContents = await this.getSketchFiles(sketchFolderStat); const sketchDirContents = await this.getSketchFiles(sketchFolderStat);
files.push(...sketchDirContents); uris.push(...sketchDirContents);
} }
} }
} }
} }
console.log("###FILEPATH###", files); return uris;
return files;
} }
protected isSketchFolder(path: string, name: string): boolean { protected isSketchFolder(path: string, name: string): boolean {