@@ -171,16 +171,60 @@ const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryNa
171171 ])
172172}
173173
174+ /**
175+ * Initialize the build
176+ * It consists mostly of setting up the flatpak remote if one other than the default is set
177+ * along with restoring the cache from the latest build
178+ *
179+ * @param {string} repositoryName the repository name to install the runtime from
180+ * @param {string} repositoryUrl the repository url
181+ * @param {PathLike} manifestPath the manifest path
182+ * @param {Boolean} cacheBuildDir whether to cache the build dir or not
183+ * @param {string | undefined} cacheKey the default cache key if there are any
184+ * @returns {Promise<String>} the new cacheKey if none was set before
185+ */
186+ const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey = undefined) => {
187+ /// If the user has set a different runtime source
188+ if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
189+ await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
190+ }
191+
192+ // Restore the cache in case caching is enabled
193+ if (cacheBuildDir) {
194+ if (cacheKey === undefined) {
195+ const manifestHash = (await computeHash(manifestPath)).substring(0, 20)
196+ cacheKey = `flatpak-builder-${manifestHash}`
197+ }
198+
199+ const cacheHitKey = await cache.restoreCache(
200+ CACHE_PATH,
201+ cacheKey,
202+ [
203+ 'flatpak-builder-',
204+ 'flatpak-'
205+ ]
206+ )
207+ if (cacheHitKey !== undefined) {
208+ core.info(`Restored cache with key: ${cacheHitKey}`)
209+ } else {
210+ core.info('No cache was found')
211+ }
212+ }
213+ return cacheKey
214+ }
215+
174216/**
175217 * Run a complete build
176218 *
177219 * @param {object} manifestPath The flatpak manifest path
178220 * @param {boolean} runTests Whether to run tests or not
179221 * @param {string} bundle The bundle's name
180222 * @param {string} repositoryUrl The repository used to install the runtime from
223+ * @param {string} repositoryName the repository name to install the runtime from
181224 * @param {string} buildDir Where to build the application
182225 * @param {string} localRepoName The flatpak repository name
183226 * @param {boolean} cacheBuildDir Whether to enable caching the build directory
227+ * @param {string | undefined} cacheKey the default cache key if there are any
184228 */
185229const run = async (
186230 manifestPath,
@@ -193,32 +237,12 @@ const run = async (
193237 cacheBuildDir,
194238 cacheKey = undefined
195239) => {
196- if (cacheKey === undefined) {
197- const manifestHash = (await computeHash(manifestPath)).substring(0, 20)
198- cacheKey = `flatpak-builder-${manifestHash}`
199- }
200-
201- /// If the user has set a different runtime source
202- if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
203- await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
240+ try {
241+ cacheKey = await prepareBuild(repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey)
242+ } catch (err) {
243+ core.setFailed(`Failed to prepare the build ${err}`)
204244 }
205245
206- // Restore the cache in case caching is enabled
207- if (cacheBuildDir) {
208- const cacheHitKey = await cache.restoreCache(
209- CACHE_PATH,
210- cacheKey,
211- [
212- 'flatpak-builder-',
213- 'flatpak-'
214- ]
215- )
216- if (cacheHitKey !== undefined) {
217- core.info(`Restored cache with key: ${cacheHitKey}`)
218- } else {
219- core.info('No cache was found')
220- }
221- }
222246 parseManifest(manifestPath)
223247 .then((manifest) => {
224248 const modifiedManifest = modifyManifest(manifest, runTests)
0 commit comments