Skip to content

Commit

Permalink
Create Samples directory on application start (#12299)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
4e6 authored Mar 4, 2025
1 parent adadc9b commit ce97822
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/ide-desktop/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class App {
this.setChromeOptions(chromeOptions)
security.enableAll()

this.onStart().catch((err) => {
logger.error(err)
})

electron.app.on('before-quit', () => {
this.isQuitting = true
})
Expand Down Expand Up @@ -151,6 +155,33 @@ class App {
}
}

/** Background tasks scheduled on the application startup. */
async onStart() {
const userData = electron.app.getPath('userData')
const versionInfoPath = pathModule.join(userData, 'version_info.json')
const versionInfoPathExists = await fs
.access(versionInfoPath, fs.constants.F_OK)
.then(() => true)
.catch(() => false)

if (versionInfoPathExists) {
const versionInfoText = await fs.readFile(versionInfoPath, 'utf8')
const versionInfoJson = JSON.parse(versionInfoText)

if (debug.VERSION_INFO.version === versionInfoJson.version && !contentConfig.VERSION.isDev())
return
}

const writeVersionInfoPromise = fs.writeFile(
versionInfoPath,
JSON.stringify(debug.VERSION_INFO),
'utf8',
)
const downloadSamplesPromise = projectManagement.downloadSamples()

return Promise.allSettled([writeVersionInfoPromise, downloadSamplesPromise])
}

/** Process the command line arguments. */
processArguments(args = fileAssociations.CLIENT_ARGUMENTS) {
// We parse only "client arguments", so we don't have to worry about the Electron-Dev vs
Expand Down
41 changes: 41 additions & 0 deletions app/ide-desktop/client/src/projectManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
import * as crypto from 'node:crypto'
import * as fs from 'node:fs'
import * as https from 'node:https'
import * as os from 'node:os'
import * as pathModule from 'node:path'
import type * as stream from 'node:stream'
Expand All @@ -33,6 +34,9 @@ export const PROJECT_METADATA_RELATIVE_PATH = '.enso/project.json'
/** The filename suffix for the project bundle, including the leading period character. */
const BUNDLED_PROJECT_SUFFIX = '.enso-project'

const SAMPLES_URL = 'https://github.com/enso-org/project-templates/archive/refs/heads/main.tar.gz'
const SAMPLES_DIRECTORY_NAME = 'Samples'

// ===================
// === ProjectInfo ===
// ===================
Expand Down Expand Up @@ -460,3 +464,40 @@ export function bumpMetadata(
})).id
return { id, name, parentDirectory }
}

/** Download project templates GitHub repo into the Samples directory if one not exists. */
export async function downloadSamples(): Promise<void> {
logger.log('Downloading samples.')

const samplesDirectory = pathModule.join(getProjectsDirectory(), SAMPLES_DIRECTORY_NAME)

return new Promise((resolve, reject) => {
fs.access(samplesDirectory, fs.constants.F_OK, (err) => {
if (err == null) {
return resolve()
}
fs.mkdir(samplesDirectory, { recursive: true }, (err) => {
if (err != null) {
logger.error(err)
return reject(err)
}
https.get(SAMPLES_URL, (redirectResponse) => {
const location = redirectResponse.headers.location
if (location) {
https.get(location, (response) => {
response
.pipe(
tar.x({
C: samplesDirectory,
strip: 1,
}),
)
.on('end', () => resolve())
.on('error', reject)
})
}
})
})
})
})
}

0 comments on commit ce97822

Please sign in to comment.