mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-11-10 18:59:28 +00:00
fixed save-as. added sketchload
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import * as fs from 'fs';
|
||||
import { promisify } from 'util';
|
||||
|
||||
export const constants = fs.constants;
|
||||
|
||||
export const existsSync = fs.existsSync;
|
||||
export const lstatSync = fs.lstatSync;
|
||||
export const readdirSync = fs.readdirSync;
|
||||
export const statSync = fs.statSync;
|
||||
export const writeFileSync = fs.writeFileSync;
|
||||
export const readFileSync = fs.readFileSync;
|
||||
export const accessSync = fs.accessSync;
|
||||
export const renameSync = fs.renameSync;
|
||||
|
||||
export const exists = promisify(fs.exists);
|
||||
export const lstat = promisify(fs.lstat);
|
||||
@@ -14,6 +18,8 @@ export const readdir = promisify(fs.readdir);
|
||||
export const stat = promisify(fs.stat);
|
||||
export const writeFile = promisify(fs.writeFile);
|
||||
export const readFile = promisify(fs.readFile);
|
||||
export const access = promisify(fs.access);
|
||||
export const rename = promisify(fs.rename);
|
||||
|
||||
export const watchFile = fs.watchFile;
|
||||
export const unwatchFile = fs.unwatchFile;
|
||||
|
||||
@@ -2,14 +2,24 @@ import { injectable, inject } from 'inversify';
|
||||
import * as os from 'os';
|
||||
import * as temp from 'temp';
|
||||
import * as path from 'path';
|
||||
import * as fs from './fs-extra';
|
||||
import { ncp } from 'ncp';
|
||||
import { Stats } from 'fs';
|
||||
import * as fs from './fs-extra';
|
||||
import URI from '@theia/core/lib/common/uri';
|
||||
import { FileUri, BackendApplicationContribution } from '@theia/core/lib/node';
|
||||
import { ConfigService } from '../common/protocol/config-service';
|
||||
import { SketchesService, Sketch } from '../common/protocol/sketches-service';
|
||||
import URI from '@theia/core/lib/common/uri';
|
||||
|
||||
export const ALLOWED_FILE_EXTENSIONS = ['.c', '.cpp', '.h', '.hh', '.hpp', '.s', '.pde', '.ino'];
|
||||
|
||||
// As currently implemented on Linux,
|
||||
// the maximum number of symbolic links that will be followed while resolving a pathname is 40
|
||||
const MAX_FILESYSTEM_DEPTH = 40;
|
||||
|
||||
export namespace Extensions {
|
||||
export const MAIN = ['.ino', '.pde'];
|
||||
export const SOURCE = ['.c', '.cpp', '.s'];
|
||||
export const ADDITIONAL = ['.h', '.c', '.hpp', '.hh', '.cpp', '.s'];
|
||||
}
|
||||
|
||||
// TODO: `fs`: use async API
|
||||
@injectable()
|
||||
@@ -43,46 +53,208 @@ export class SketchesServiceImpl implements SketchesService, BackendApplicationC
|
||||
for (const fileName of fileNames) {
|
||||
const filePath = path.join(fsPath, fileName);
|
||||
if (await this.isSketchFolder(FileUri.create(filePath).toString())) {
|
||||
const stat = await fs.stat(filePath);
|
||||
sketches.push({
|
||||
mtimeMs: stat.mtimeMs,
|
||||
name: fileName,
|
||||
uri: FileUri.create(filePath).toString()
|
||||
});
|
||||
try {
|
||||
const stat = await fs.stat(filePath);
|
||||
const sketch = await this.loadSketch(FileUri.create(filePath).toString());
|
||||
sketches.push({
|
||||
...sketch,
|
||||
mtimeMs: stat.mtimeMs
|
||||
});
|
||||
} catch {
|
||||
console.warn(`Could not load sketch from ${filePath}.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sketches.sort((left, right) => right.mtimeMs - left.mtimeMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all allowed files.
|
||||
* File extensions: 'c', 'cpp', 'h', 'hh', 'hpp', 's', 'pde', 'ino'
|
||||
* This is the TS implementation of `SketchLoad` from the CLI.
|
||||
* See: https://github.com/arduino/arduino-cli/issues/837
|
||||
* Based on: https://github.com/arduino/arduino-cli/blob/eef3705c4afcba4317ec38b803d9ffce5dd59a28/arduino/builder/sketch.go#L100-L215
|
||||
*/
|
||||
async getSketchFiles(uri: string): Promise<string[]> {
|
||||
const uris: string[] = [];
|
||||
const fsPath = FileUri.fsPath(uri);
|
||||
if (fs.lstatSync(fsPath).isDirectory()) {
|
||||
if (await this.isSketchFolder(uri)) {
|
||||
const basename = path.basename(fsPath)
|
||||
const fileNames = await fs.readdir(fsPath);
|
||||
for (const fileName of fileNames) {
|
||||
const filePath = path.join(fsPath, fileName);
|
||||
if (ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1
|
||||
&& fs.existsSync(filePath)
|
||||
&& fs.lstatSync(filePath).isFile()) {
|
||||
const uri = FileUri.create(filePath).toString();
|
||||
if (fileName === basename + '.ino') {
|
||||
uris.unshift(uri); // The sketch file is the first.
|
||||
} else {
|
||||
uris.push(uri);
|
||||
}
|
||||
async loadSketch(uri: string): Promise<Sketch> {
|
||||
const sketchPath = FileUri.fsPath(uri);
|
||||
const exists = await fs.exists(sketchPath);
|
||||
if (!exists) {
|
||||
throw new Error(`${uri} does not exist.`);
|
||||
}
|
||||
const stat = await fs.lstat(sketchPath);
|
||||
let sketchFolder: string | undefined;
|
||||
let mainSketchFile: string | undefined;
|
||||
|
||||
// If a sketch folder was passed, save the parent and point sketchPath to the main sketch file
|
||||
if (stat.isDirectory()) {
|
||||
sketchFolder = sketchPath;
|
||||
// Allowed extensions are .ino and .pde (but not both)
|
||||
for (const extension of Extensions.MAIN) {
|
||||
const candidateSketchFile = path.join(sketchPath, `${path.basename(sketchPath)}${extension}`);
|
||||
const candidateExists = await fs.exists(candidateSketchFile);
|
||||
if (candidateExists) {
|
||||
if (!mainSketchFile) {
|
||||
mainSketchFile = candidateSketchFile;
|
||||
} else {
|
||||
throw new Error(`Multiple main sketch files found (${path.basename(mainSketchFile)}, ${path.basename(candidateSketchFile)})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return uris;
|
||||
|
||||
// Check main file was found.
|
||||
if (!mainSketchFile) {
|
||||
throw new Error(`Unable to find a sketch file in directory ${sketchFolder}`);
|
||||
}
|
||||
|
||||
// Check main file is readable.
|
||||
try {
|
||||
await fs.access(mainSketchFile, fs.constants.R_OK);
|
||||
} catch {
|
||||
throw new Error('Unable to open the main sketch file.');
|
||||
}
|
||||
|
||||
const mainSketchFileStat = await fs.lstat(mainSketchFile);
|
||||
if (mainSketchFileStat.isDirectory()) {
|
||||
throw new Error(`Sketch must not be a directory.`);
|
||||
}
|
||||
} else {
|
||||
sketchFolder = path.dirname(sketchPath);
|
||||
mainSketchFile = sketchPath;
|
||||
}
|
||||
|
||||
const files: string[] = [];
|
||||
let rootVisited = false;
|
||||
const err = await this.simpleLocalWalk(sketchFolder, MAX_FILESYSTEM_DEPTH, async (fsPath: string, info: Stats, error: Error | undefined) => {
|
||||
if (error) {
|
||||
console.log(`Error during sketch processing: ${error}`);
|
||||
return error;
|
||||
}
|
||||
const name = path.basename(fsPath);
|
||||
if (info.isDirectory()) {
|
||||
if (rootVisited) {
|
||||
if (name.startsWith('.') || name === 'CVS' || name === 'RCS') {
|
||||
return new SkipDir();
|
||||
}
|
||||
} else {
|
||||
rootVisited = true
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (name.startsWith('.')) {
|
||||
return undefined;
|
||||
}
|
||||
const ext = path.extname(fsPath);
|
||||
const isMain = Extensions.MAIN.indexOf(ext) !== -1;
|
||||
const isAdditional = Extensions.ADDITIONAL.indexOf(ext) !== -1;
|
||||
if (!isMain && !isAdditional) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.access(fsPath, fs.constants.R_OK);
|
||||
files.push(fsPath);
|
||||
} catch { }
|
||||
|
||||
return undefined;
|
||||
});
|
||||
|
||||
if (err) {
|
||||
console.error(`There was an error while collecting the sketch files: ${sketchPath}`)
|
||||
throw err;
|
||||
}
|
||||
|
||||
return this.newSketch(sketchFolder, mainSketchFile, files);
|
||||
|
||||
}
|
||||
|
||||
private newSketch(sketchFolderPath: string, mainFilePath: string, allFilesPaths: string[]): Sketch {
|
||||
let mainFile: string | undefined;
|
||||
const paths = new Set<string>();
|
||||
for (const p of allFilesPaths) {
|
||||
if (p === mainFilePath) {
|
||||
mainFile = p;
|
||||
} else {
|
||||
paths.add(p);
|
||||
}
|
||||
}
|
||||
if (!mainFile) {
|
||||
throw new Error('Could not locate main sketch file.');
|
||||
}
|
||||
const additionalFiles: string[] = [];
|
||||
const otherSketchFiles: string[] = [];
|
||||
for (const p of Array.from(paths)) {
|
||||
const ext = path.extname(p);
|
||||
if (Extensions.MAIN.indexOf(ext) !== -1) {
|
||||
if (path.dirname(p) === sketchFolderPath) {
|
||||
otherSketchFiles.push(p);
|
||||
}
|
||||
} else if (Extensions.ADDITIONAL.indexOf(ext) !== -1) {
|
||||
// XXX: this is a caveat with the CLI, we do not know the `buildPath`.
|
||||
// https://github.com/arduino/arduino-cli/blob/0483882b4f370c288d5318913657bbaa0325f534/arduino/sketch/sketch.go#L108-L110
|
||||
additionalFiles.push(p);
|
||||
} else {
|
||||
throw new Error(`Unknown sketch file extension '${ext}'.`);
|
||||
}
|
||||
}
|
||||
additionalFiles.sort();
|
||||
otherSketchFiles.sort();
|
||||
|
||||
return {
|
||||
uri: FileUri.create(sketchFolderPath).toString(),
|
||||
mainFileUri: FileUri.create(mainFile).toString(),
|
||||
name: path.basename(sketchFolderPath),
|
||||
additionalFileUris: additionalFiles.map(p => FileUri.create(p).toString()),
|
||||
otherSketchFileUris: otherSketchFiles.map(p => FileUri.create(p).toString())
|
||||
}
|
||||
}
|
||||
|
||||
protected async simpleLocalWalk(
|
||||
root: string,
|
||||
maxDepth: number,
|
||||
walk: (fsPath: string, info: Stats | undefined, err: Error | undefined) => Promise<Error | undefined>): Promise<Error | undefined> {
|
||||
|
||||
let { info, err } = await this.lstat(root);
|
||||
if (err) {
|
||||
return walk(root, undefined, err);
|
||||
}
|
||||
if (!info) {
|
||||
return new Error(`Could not stat file: ${root}.`);
|
||||
}
|
||||
err = await walk(root, info, err);
|
||||
if (err instanceof SkipDir) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (info.isDirectory()) {
|
||||
if (maxDepth <= 0) {
|
||||
return walk(root, info, new Error(`Filesystem bottom is too deep (directory recursion or filesystem really deep): ${root}`));
|
||||
}
|
||||
maxDepth--;
|
||||
const files: string[] = [];
|
||||
try {
|
||||
files.push(...await fs.readdir(root));
|
||||
} catch { }
|
||||
for (const file of files) {
|
||||
err = await this.simpleLocalWalk(path.join(root, file), maxDepth, walk);
|
||||
if (err instanceof SkipDir) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private async lstat(fsPath: string): Promise<{ info: Stats, err: undefined } | { info: undefined, err: Error }> {
|
||||
const exists = await fs.exists(fsPath);
|
||||
if (!exists) {
|
||||
return { info: undefined, err: new Error(`${fsPath} does not exist`) };
|
||||
}
|
||||
try {
|
||||
const info = await fs.lstat(fsPath);
|
||||
return { info, err: undefined };
|
||||
} catch (err) {
|
||||
return { info: undefined, err };
|
||||
}
|
||||
const sketchDir = path.dirname(fsPath);
|
||||
return this.getSketchFiles(FileUri.create(sketchDir).toString());
|
||||
}
|
||||
|
||||
async createNewSketch(): Promise<Sketch> {
|
||||
@@ -129,10 +301,7 @@ void loop() {
|
||||
|
||||
}
|
||||
`, { encoding: 'utf8' });
|
||||
return {
|
||||
name: sketchName,
|
||||
uri: FileUri.create(sketchDir).toString()
|
||||
}
|
||||
return this.loadSketch(FileUri.create(sketchDir).toString());
|
||||
}
|
||||
|
||||
async getSketchFolder(uri: string): Promise<Sketch | undefined> {
|
||||
@@ -142,10 +311,7 @@ void loop() {
|
||||
let currentUri = new URI(uri);
|
||||
while (currentUri && !currentUri.path.isRoot) {
|
||||
if (await this.isSketchFolder(currentUri.toString())) {
|
||||
return {
|
||||
name: currentUri.path.base,
|
||||
uri: currentUri.toString()
|
||||
};
|
||||
return this.loadSketch(currentUri.toString());
|
||||
}
|
||||
currentUri = currentUri.parent;
|
||||
}
|
||||
@@ -173,20 +339,35 @@ void loop() {
|
||||
|
||||
async copy(sketch: Sketch, { destinationUri }: { destinationUri: string }): Promise<string> {
|
||||
const source = FileUri.fsPath(sketch.uri);
|
||||
if (await !fs.exists(source)) {
|
||||
const exists = await fs.exists(source);
|
||||
if (!exists) {
|
||||
throw new Error(`Sketch does not exist: ${sketch}`);
|
||||
}
|
||||
const destination = FileUri.fsPath(destinationUri);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
ncp.ncp(source, destination, error => {
|
||||
ncp.ncp(source, destination, async error => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
const newName = path.basename(destination);
|
||||
try {
|
||||
await fs.rename(path.join(destination, new URI(sketch.mainFileUri).path.base), path.join(destination, `${newName}.ino`));
|
||||
await this.loadSketch(destinationUri); // Sanity check.
|
||||
resolve();
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
return FileUri.create(destination).toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SkipDir extends Error {
|
||||
constructor() {
|
||||
super('skip this directory');
|
||||
Object.setPrototypeOf(this, SkipDir.prototype);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user