Skip to content

Commit ce97822

Browse files
authored
Create Samples directory on application start (#12299)
close #12267 Changelog: - update: create the `version_info.json` in the `userData` directory with the version of the launched application - update: download the project templates into the `Samples` directory if the version changed and if the directory does not exist # Important Notes One caveat is that until #12290 is fixed, the Samples directory will always be recreated if the user removes it.
1 parent adadc9b commit ce97822

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

app/ide-desktop/client/src/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class App {
9999
this.setChromeOptions(chromeOptions)
100100
security.enableAll()
101101

102+
this.onStart().catch((err) => {
103+
logger.error(err)
104+
})
105+
102106
electron.app.on('before-quit', () => {
103107
this.isQuitting = true
104108
})
@@ -151,6 +155,33 @@ class App {
151155
}
152156
}
153157

158+
/** Background tasks scheduled on the application startup. */
159+
async onStart() {
160+
const userData = electron.app.getPath('userData')
161+
const versionInfoPath = pathModule.join(userData, 'version_info.json')
162+
const versionInfoPathExists = await fs
163+
.access(versionInfoPath, fs.constants.F_OK)
164+
.then(() => true)
165+
.catch(() => false)
166+
167+
if (versionInfoPathExists) {
168+
const versionInfoText = await fs.readFile(versionInfoPath, 'utf8')
169+
const versionInfoJson = JSON.parse(versionInfoText)
170+
171+
if (debug.VERSION_INFO.version === versionInfoJson.version && !contentConfig.VERSION.isDev())
172+
return
173+
}
174+
175+
const writeVersionInfoPromise = fs.writeFile(
176+
versionInfoPath,
177+
JSON.stringify(debug.VERSION_INFO),
178+
'utf8',
179+
)
180+
const downloadSamplesPromise = projectManagement.downloadSamples()
181+
182+
return Promise.allSettled([writeVersionInfoPromise, downloadSamplesPromise])
183+
}
184+
154185
/** Process the command line arguments. */
155186
processArguments(args = fileAssociations.CLIENT_ARGUMENTS) {
156187
// We parse only "client arguments", so we don't have to worry about the Electron-Dev vs

app/ide-desktop/client/src/projectManagement.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
import * as crypto from 'node:crypto'
1313
import * as fs from 'node:fs'
14+
import * as https from 'node:https'
1415
import * as os from 'node:os'
1516
import * as pathModule from 'node:path'
1617
import type * as stream from 'node:stream'
@@ -33,6 +34,9 @@ export const PROJECT_METADATA_RELATIVE_PATH = '.enso/project.json'
3334
/** The filename suffix for the project bundle, including the leading period character. */
3435
const BUNDLED_PROJECT_SUFFIX = '.enso-project'
3536

37+
const SAMPLES_URL = 'https://github.com/enso-org/project-templates/archive/refs/heads/main.tar.gz'
38+
const SAMPLES_DIRECTORY_NAME = 'Samples'
39+
3640
// ===================
3741
// === ProjectInfo ===
3842
// ===================
@@ -460,3 +464,40 @@ export function bumpMetadata(
460464
})).id
461465
return { id, name, parentDirectory }
462466
}
467+
468+
/** Download project templates GitHub repo into the Samples directory if one not exists. */
469+
export async function downloadSamples(): Promise<void> {
470+
logger.log('Downloading samples.')
471+
472+
const samplesDirectory = pathModule.join(getProjectsDirectory(), SAMPLES_DIRECTORY_NAME)
473+
474+
return new Promise((resolve, reject) => {
475+
fs.access(samplesDirectory, fs.constants.F_OK, (err) => {
476+
if (err == null) {
477+
return resolve()
478+
}
479+
fs.mkdir(samplesDirectory, { recursive: true }, (err) => {
480+
if (err != null) {
481+
logger.error(err)
482+
return reject(err)
483+
}
484+
https.get(SAMPLES_URL, (redirectResponse) => {
485+
const location = redirectResponse.headers.location
486+
if (location) {
487+
https.get(location, (response) => {
488+
response
489+
.pipe(
490+
tar.x({
491+
C: samplesDirectory,
492+
strip: 1,
493+
}),
494+
)
495+
.on('end', () => resolve())
496+
.on('error', reject)
497+
})
498+
}
499+
})
500+
})
501+
})
502+
})
503+
}

0 commit comments

Comments
 (0)