Skip to content

Commit

Permalink
[CI] Fix Flathub Workflow (#3661)
Browse files Browse the repository at this point in the history
* [CI] Fix Flathub Workflow

* Update versionNames in package.json

---------

Co-authored-by: Flavio F Lima <[email protected]>
  • Loading branch information
flavioislima and flavioislima authored Mar 31, 2024
1 parent 55e9399 commit f026465
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 43 deletions.
89 changes: 57 additions & 32 deletions flathub/update-flathub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import * as crypto from 'crypto'
import * as os from 'os'
import * as child_process from 'child_process'
import axios from 'axios'
import * as convert from 'xml-js'
import { Element } from 'xml-js'
import { XMLParser, XMLBuilder } from 'fast-xml-parser'

async function main() {
console.log('tag name: ', process.env.RELEASE_VERSION)
Expand All @@ -15,12 +14,12 @@ async function main() {
console.log('updating url in com.heroicgameslauncher.hgl.yml')
const ymlFilePath =
'./com.heroicgameslauncher.hgl/com.heroicgameslauncher.hgl.yml'
let hpYml = fs.readFileSync(ymlFilePath).toString()
let heroicYml = fs.readFileSync(ymlFilePath).toString()

const releaseString = `https://github.com/${repoName}/releases/download/${
process.env.RELEASE_VERSION
}/Heroic-${process.env.RELEASE_VERSION?.substring(1)}.AppImage`
hpYml = hpYml.replace(
heroicYml = heroicYml.replace(
/https:\/\/github.com\/Heroic-Games-Launcher\/HeroicGamesLauncher\/releases\/download\/v.*..*..*\/Heroic-.*..*..*.AppImage/,
releaseString
)
Expand All @@ -47,26 +46,26 @@ async function main() {
const sha512 = hashSum.digest('hex')
fs.rmSync(outputFile)

hpYml = hpYml.replace(/sha512: [0-9, a-f]{128}/, `sha512: ${sha512}`)
heroicYml = heroicYml.replace(/sha512: [0-9, a-f]{128}/, `sha512: ${sha512}`)

fs.writeFileSync(ymlFilePath, hpYml)
fs.writeFileSync(ymlFilePath, heroicYml)

// update release version and date on xml tag in com.heroicgameslauncher.hgl.metainfo.xml
console.log(
'updating release version and date on xml tag in com.heroicgameslauncher.hgl.metainfo.xml'
)
const xmlFilePath =
'./com.heroicgameslauncher.hgl/com.heroicgameslauncher.hgl.metainfo.xml'
let hpXml = fs.readFileSync(xmlFilePath).toString()
let heroicXml = fs.readFileSync(xmlFilePath).toString()
const date = new Date()
const isoDate = date.toISOString().slice(0, 10)

hpXml = hpXml.replace(
heroicXml = heroicXml.replace(
/release version="v.*..*..*" date="[0-9]{4}-[0-9]{2}-[0-9]{2}"/,
`release version="${process.env.RELEASE_VERSION}" date="${isoDate}"`
)

fs.writeFileSync(xmlFilePath, hpXml)
fs.writeFileSync(xmlFilePath, heroicXml)
console.log(
'Finished updating flathub release! Be sure to update release notes manually before merging.'
)
Expand Down Expand Up @@ -101,43 +100,69 @@ async function main() {
)

const releaseNotesJson = JSON.parse(releaseNotesStdOut)
const releaseNotesComponents = releaseNotesJson.body.split('\n')
const releaseNotesComponents: string[] = releaseNotesJson.body.split('\n')

const hpXmlJson = convert.xml2js(hpXml, { compact: false }) as Element
const releaseNotesElements: Element[] = []
// XML Modification with fast-xml-parser
heroicXml = fs.readFileSync(xmlFilePath).toString()

const parserOptions = {
ignoreAttributes: false,
preserveOrder: true,
format: true, // Enable formatting
indentBy: ' ' // Use two spaces for indentation
}

const parser = new XMLParser(parserOptions)

const heroicXmlJson = parser.parse(heroicXml)

const builder = new XMLBuilder(parserOptions)

const releaseNotesElements: Record<string, Array<Record<string, string>>>[] =
[] // An array to hold generated <li> elements

for (const [i, releaseComponent_i] of releaseNotesComponents.entries()) {
//update metainfo hpXml
if (i === 0) continue
if (!releaseComponent_i.startsWith('*')) continue
if (releaseComponent_i.includes('http')) continue
const releaseNoteElement: Element = {
type: 'element',
name: 'li',
elements: [
{
type: 'text',
text: releaseComponent_i.slice(1) //remove the asterisk
}

const li = releaseComponent_i
.replace(/\n/g, '')
.replace(/\r/g, '')
.replace(/\t/g, '')
.slice(1)
.trim()

// Creating the <li> element (compatible with fast-xml-parser)
const releaseNoteElement = {
li: [
{ '#text': li } // Directly set the text within the <li> element
]
}

releaseNotesElements.push(releaseNoteElement)
}

const componentsTag = hpXmlJson.elements?.[0]
const releasesTag = componentsTag?.elements?.find(
(val) => val.name === 'releases'
) //.releases.release.description.ul =
const releaseListTag =
releasesTag?.elements?.[0].elements?.[0].elements?.find(
(val) => val.name === 'ul'
)
const componentsTag = heroicXmlJson[1].component
// Locate the 'releases' element within the 'componentsTag'
const releasesTag = componentsTag.find((val) => val.releases !== undefined)

// Proceed to find the <ul> element as before
const releaseListTag = releasesTag?.releases[0].release[0].description[1]

if (releaseListTag === undefined) {
throw 'releaseListTag ul undefined'
}
releaseListTag.elements = releaseNotesElements
hpXml = convert.js2xml(hpXmlJson)
fs.writeFileSync(xmlFilePath, hpXml)

releaseListTag.ul = [...releaseNotesElements] // Set the <ul> element to the generated <li> elements

console.log('new releaseListTag = ', JSON.stringify(releaseListTag, null, 2))

const updatedHeroicXml = builder.build(heroicXmlJson)

console.log('updatedheroicXml = ', updatedHeroicXml)

fs.writeFileSync(xmlFilePath, updatedHeroicXml)

console.log(
'Finished updating flathub release! Please review and merge the flathub repo PR manually.'
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "heroic",
"version": "2.13.0",
"version": "2.14.0",
"versionNames": {
"stable": "Dorry & Broggy",
"stable": "Flame Emperor Sabo",
"beta": "Caesar Clown"
},
"private": false,
Expand Down Expand Up @@ -131,6 +131,7 @@
"eslint-config-prettier": "8.7.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-react": "7.31.11",
"fast-xml-parser": "^4.3.6",
"husky": "8.0.3",
"i18next-parser": "7.7.0",
"jest": "29.5.0",
Expand All @@ -147,8 +148,7 @@
"unimported": "1.26.0",
"vite": "3.2.8",
"vite-plugin-electron": "0.10.2",
"vite-plugin-svgr": "2.4.0",
"xml-js": "1.6.11"
"vite-plugin-svgr": "2.4.0"
},
"resolutions": {
"ts-morph": "17.0.1"
Expand Down
19 changes: 12 additions & 7 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f026465

Please sign in to comment.