11import { defineTarget , manualSetup } from '@profullstack/sh1pt-core' ;
2+ import { mkdir , writeFile } from 'node:fs/promises' ;
3+ import { join } from 'node:path' ;
24
35interface Config {
46 appId : string ; // Reverse-DNS app ID, e.g. "com.example.MyApp"
57 branch ?: 'stable' | 'beta' ;
68 runtime ?: string ; // e.g. "org.freedesktop.Platform"
79 runtimeVersion ?: string ; // e.g. "23.08"
10+ sdk ?: string ; // e.g. "org.freedesktop.Sdk"
811 sdkExtensions ?: string [ ] ; // e.g. ["org.freedesktop.Sdk.Extension.node20"]
912 flathubRepo ?: string ; // defaults to "https://github.com/flathub/flathub"
13+ command ?: string ;
14+ moduleName ?: string ;
15+ buildsystem ?: 'simple' | 'meson' | 'cmake' | 'autotools' | string ;
16+ buildCommands ?: string [ ] ;
17+ sourceUrl ?: string ;
18+ sourceSha256 ?: string ;
19+ finishArgs ?: string [ ] ;
20+ }
21+
22+ function yamlString ( value : string ) : string {
23+ return JSON . stringify ( value ) ;
24+ }
25+
26+ function renderList ( values : string [ ] , indent : string ) : string [ ] {
27+ return values . map ( ( value ) => `${ indent } - ${ yamlString ( value ) } ` ) ;
28+ }
29+
30+ function renderFlatpakManifest ( ctx : { projectDir : string ; version : string ; channel : string } , config : Config ) : string {
31+ const branch = config . branch ?? ( ctx . channel === 'stable' ? 'stable' : 'beta' ) ;
32+ const runtime = config . runtime ?? 'org.freedesktop.Platform' ;
33+ const runtimeVersion = config . runtimeVersion ?? '23.08' ;
34+ const sdk = config . sdk ?? 'org.freedesktop.Sdk' ;
35+ const command = config . command ?? config . appId . split ( '.' ) . at ( - 1 ) ?? config . appId ;
36+ const moduleName = config . moduleName ?? command ;
37+ const buildsystem = config . buildsystem ?? 'simple' ;
38+ const buildCommands = config . buildCommands ?? [ 'install -D app "$FLATPAK_DEST/bin/app"' ] ;
39+ const finishArgs = config . finishArgs ?? [ '--share=network' ] ;
40+ const sourceUrl = config . sourceUrl ?? ctx . projectDir ;
41+ const lines = [
42+ `app-id: ${ yamlString ( config . appId ) } ` ,
43+ `runtime: ${ yamlString ( runtime ) } ` ,
44+ `runtime-version: ${ yamlString ( runtimeVersion ) } ` ,
45+ `sdk: ${ yamlString ( sdk ) } ` ,
46+ `command: ${ yamlString ( command ) } ` ,
47+ `branch: ${ yamlString ( branch ) } ` ,
48+ ] ;
49+
50+ if ( config . sdkExtensions ?. length ) {
51+ lines . push ( 'sdk-extensions:' ) ;
52+ lines . push ( ...renderList ( config . sdkExtensions , ' ' ) ) ;
53+ }
54+
55+ if ( finishArgs . length ) {
56+ lines . push ( 'finish-args:' ) ;
57+ lines . push ( ...renderList ( finishArgs , ' ' ) ) ;
58+ }
59+
60+ lines . push ( 'modules:' ) ;
61+ lines . push ( ` - name: ${ yamlString ( moduleName ) } ` ) ;
62+ lines . push ( ` buildsystem: ${ yamlString ( buildsystem ) } ` ) ;
63+ lines . push ( ' build-commands:' ) ;
64+ lines . push ( ...renderList ( buildCommands , ' ' ) ) ;
65+ lines . push ( ' sources:' ) ;
66+
67+ if ( config . sourceUrl ) {
68+ lines . push ( ' - type: archive' ) ;
69+ lines . push ( ` url: ${ yamlString ( sourceUrl ) } ` ) ;
70+ if ( config . sourceSha256 ) {
71+ lines . push ( ` sha256: ${ yamlString ( config . sourceSha256 ) } ` ) ;
72+ }
73+ } else {
74+ lines . push ( ' - type: dir' ) ;
75+ lines . push ( ` path: ${ yamlString ( sourceUrl ) } ` ) ;
76+ }
77+
78+ lines . push ( '' ) ;
79+ return lines . join ( '\n' ) ;
1080}
1181
1282export default defineTarget < Config > ( {
@@ -17,14 +87,13 @@ export default defineTarget<Config>({
1787 const branch = config . branch ?? ( ctx . channel === 'stable' ? 'stable' : 'beta' ) ;
1888 const runtime = config . runtime ?? 'org.freedesktop.Platform' ;
1989 const runtimeVersion = config . runtimeVersion ?? '23.08' ;
90+ const manifestPath = join ( ctx . outDir , `${ config . appId } .yml` ) ;
2091 ctx . log ( `render ${ config . appId } .yml manifest for v${ ctx . version } (branch: ${ branch } )` ) ;
2192 ctx . log ( `runtime: ${ runtime } //${ runtimeVersion } ` ) ;
22- // TODO: render Flatpak manifest YAML from template:
23- // app-id: ${appId} runtime: ${runtime} runtime-version: ${runtimeVersion}
24- // sdk-extensions: ${sdkExtensions}
25- // modules: [{ name: <appName>, sources: [{ type: archive, url: ..., sha256: ... }] }]
93+ await mkdir ( ctx . outDir , { recursive : true } ) ;
94+ await writeFile ( manifestPath , renderFlatpakManifest ( ctx , config ) , 'utf-8' ) ;
2695 // TODO: run `flatpak-builder --repo=repo --force-clean builddir ${appId}.yml`
27- return { artifact : ` ${ ctx . outDir } / ${ config . appId } .flatpak` } ;
96+ return { artifact : manifestPath } ;
2897 } ,
2998 async ship ( ctx , config ) {
3099 const branch = config . branch ?? ( ctx . channel === 'stable' ? 'stable' : 'beta' ) ;
0 commit comments