-
-
Notifications
You must be signed in to change notification settings - Fork 447
Added an optional user modifiable default sketch file when creating a new project. #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d28398e
Added a modifiable default sketch for new project
nbourre f41c1cd
Removed unused file
nbourre 6444edc
WiP : Now nothing's working... :(
nbourre 9169e20
yarn i18n:generate for the settings
nbourre 2190f81
Updated the desription for markdown description.
nbourre 12ab5d9
Lintered the code
nbourre 016eb2a
Remove undesirable whitespaces
nbourre f38ee10
Applied kittaakos suggestions
nbourre 558a71e
Removed extra whitespaces
nbourre 4f2010e
Fixed default `.ino` for the missings empty lines.
nbourre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import { | |
maybeNormalizeDrive, | ||
TempSketchPrefix, | ||
} from './is-temp-sketch'; | ||
import { join } from 'path'; | ||
|
||
const RecentSketches = 'recent-sketches.json'; | ||
|
||
|
@@ -47,6 +48,7 @@ export class SketchesServiceImpl | |
autoStart: true, | ||
concurrency: 1, | ||
}); | ||
private bluePrintContent: string; | ||
|
||
@inject(ILogger) | ||
@named('sketches-service') | ||
|
@@ -447,20 +449,9 @@ export class SketchesServiceImpl | |
const sketchDir = path.join(parentPath, sketchName); | ||
const sketchFile = path.join(sketchDir, `${sketchName}.ino`); | ||
await fs.mkdir(sketchDir, { recursive: true }); | ||
await fs.writeFile( | ||
sketchFile, | ||
`void setup() { | ||
// put your setup code here, to run once: | ||
|
||
} | ||
this.bluePrintContent = this.bluePrintContent || (await this.loadDefault()); | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
|
||
} | ||
`, | ||
{ encoding: 'utf8' } | ||
); | ||
await fs.writeFile(sketchFile, this.bluePrintContent, { encoding: 'utf8' }); | ||
return this.loadSketch(FileUri.create(sketchDir).toString()); | ||
} | ||
|
||
|
@@ -637,6 +628,62 @@ void loop() { | |
return false; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private tryParse(raw: string): any | undefined { | ||
try { | ||
return JSON.parse(raw); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
// Returns the default.ino from the settings or from default folder. | ||
private async loadDefault(): Promise<string> { | ||
const root = await this.root(await this.sketchbookUri()); | ||
const configDirUri = await this.envVariableServer.getConfigDirUri(); | ||
const configDirPath = FileUri.fsPath(configDirUri); | ||
|
||
let result: string; | ||
result = `void setup() { | ||
// put your setup code here, to run once: | ||
|
||
} | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
|
||
}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are undesired whitespaces in the template. |
||
|
||
try { | ||
const raw = await fs.readFile(join(configDirPath, 'settings.json'), { | ||
encoding: 'utf8', | ||
}); | ||
const json = this.tryParse(raw); | ||
if (json) { | ||
const value = json['arduino.sketch.inoBlueprint']; | ||
|
||
const filename = | ||
typeof value === 'string' && !!value | ||
? value | ||
: `${root}/default/default.ino`; | ||
kittaakos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let raw = ''; | ||
|
||
raw = await fs.readFile(filename, { encoding: 'utf8' }); | ||
result = raw; | ||
|
||
this.logger.info(`-- Default found at ${filename}`); | ||
} | ||
} catch (error) { | ||
if ('code' in error && error.code === 'ENOENT') { | ||
return result; | ||
} | ||
throw error; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
interface SketchWithDetails extends Sketch { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.