github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/builders/flatpak/builder.go (about)

     1  package flatpak
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"io"
     7  	"path/filepath"
     8  
     9  	"github.com/docker/docker/client"
    10  	"github.com/pojntfx/hydrapp/hydrapp/pkg/builders"
    11  	"github.com/pojntfx/hydrapp/hydrapp/pkg/executors"
    12  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers"
    13  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers/flatpak"
    14  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers/xdg"
    15  	"github.com/pojntfx/hydrapp/hydrapp/pkg/utils"
    16  )
    17  
    18  const (
    19  	Image = "ghcr.io/pojntfx/hydrapp-build-flatpak"
    20  )
    21  
    22  func NewBuilder(
    23  	ctx context.Context,
    24  	cli *client.Client,
    25  
    26  	image string, // OCI image to use
    27  	pull bool, // Whether to pull the image or not
    28  	src, // Input directory
    29  	dst string, // Output directory
    30  	onID func(id string), // Callback to handle container ID
    31  	stdout io.Writer, // Writer to handle container output
    32  	iconFilePath, // Path to icon to use
    33  	appID string, // Android app ID to use
    34  	pgpKey []byte, // PGP key contents
    35  	pgpKeyPassword, // Password for the PGP key
    36  	pgpKeyID, // ID of the PGP key to use
    37  	baseURL, // Base URL where the repo is to be hosted
    38  	architecture, // Architecture to build for
    39  	appName, // App name
    40  	appDescription, // App description
    41  	appSummary, // App summary
    42  	appSPDX, // App SPDX license identifier
    43  	appURL string, // App URL
    44  	releases []renderers.Release, // App releases
    45  	overwrite bool, // Overwrite files even if they exist
    46  	branchID, // Branch ID
    47  	branchName, // Branch name
    48  	goMain, // Directory with the main package to build
    49  	goFlags, // Flags to pass to the Go command
    50  	goGenerate string, // Command to execute go generate with
    51  ) *Builder {
    52  	return &Builder{
    53  		ctx,
    54  		cli,
    55  
    56  		image,
    57  		pull,
    58  		src,
    59  		dst,
    60  		onID,
    61  		stdout,
    62  		iconFilePath,
    63  		appID,
    64  		base64.StdEncoding.EncodeToString(pgpKey),
    65  		pgpKeyPassword,
    66  		pgpKeyID,
    67  		baseURL,
    68  		architecture,
    69  		appName,
    70  		appDescription,
    71  		appSummary,
    72  		appSPDX,
    73  		appURL,
    74  		releases,
    75  		overwrite,
    76  		branchID,
    77  		branchName,
    78  		goMain,
    79  		goFlags,
    80  		goGenerate,
    81  	}
    82  }
    83  
    84  type Builder struct {
    85  	ctx context.Context
    86  	cli *client.Client
    87  
    88  	image string
    89  	pull  bool
    90  	src,
    91  	dst string
    92  	onID   func(id string)
    93  	stdout io.Writer
    94  	iconFilePath,
    95  	appID,
    96  	pgpKey,
    97  	pgpKeyPassword,
    98  	pgpKeyID,
    99  	baseURL,
   100  	architecture,
   101  	appName,
   102  	appDescription,
   103  	appSummary,
   104  	appSPDX,
   105  	appURL string
   106  	releases  []renderers.Release
   107  	overwrite bool
   108  	branchID,
   109  	branchName,
   110  	goMain,
   111  	goFlags,
   112  	goGenerate string
   113  }
   114  
   115  func (b *Builder) Render(workdir string, ejecting bool) error {
   116  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   117  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   118  
   119  	return utils.WriteRenders(
   120  		filepath.Join(workdir, b.goMain),
   121  		[]renderers.Renderer{
   122  			xdg.NewIconRenderer(
   123  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   124  				"icon-16x16.png",
   125  				utils.ImageTypePNG,
   126  				16,
   127  				16,
   128  			),
   129  			xdg.NewIconRenderer(
   130  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   131  				"icon-22x22.png",
   132  				utils.ImageTypePNG,
   133  				22,
   134  				22,
   135  			),
   136  			xdg.NewIconRenderer(
   137  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   138  				"icon-24x24.png",
   139  				utils.ImageTypePNG,
   140  				24,
   141  				24,
   142  			),
   143  			xdg.NewIconRenderer(
   144  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   145  				"icon-32x32.png",
   146  				utils.ImageTypePNG,
   147  				32,
   148  				32,
   149  			),
   150  			xdg.NewIconRenderer(
   151  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   152  				"icon-36x36.png",
   153  				utils.ImageTypePNG,
   154  				36,
   155  				36,
   156  			),
   157  			xdg.NewIconRenderer(
   158  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   159  				"icon-48x48.png",
   160  				utils.ImageTypePNG,
   161  				48,
   162  				48,
   163  			),
   164  			xdg.NewIconRenderer(
   165  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   166  				"icon-64x64.png",
   167  				utils.ImageTypePNG,
   168  				64,
   169  				64,
   170  			),
   171  			xdg.NewIconRenderer(
   172  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   173  				"icon-72x72.png",
   174  				utils.ImageTypePNG,
   175  				72,
   176  				72,
   177  			),
   178  			xdg.NewIconRenderer(
   179  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   180  				"icon-96x96.png",
   181  				utils.ImageTypePNG,
   182  				96,
   183  				96,
   184  			),
   185  			xdg.NewIconRenderer(
   186  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   187  				"icon-128x128.png",
   188  				utils.ImageTypePNG,
   189  				128,
   190  				128,
   191  			),
   192  			xdg.NewIconRenderer(
   193  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   194  				"icon-192x192.png",
   195  				utils.ImageTypePNG,
   196  				192,
   197  				192,
   198  			),
   199  			xdg.NewIconRenderer(
   200  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   201  				"icon-256x256.png",
   202  				utils.ImageTypePNG,
   203  				256,
   204  				256,
   205  			),
   206  			xdg.NewIconRenderer(
   207  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   208  				"icon-512x512.png",
   209  				utils.ImageTypePNG,
   210  				512,
   211  				512,
   212  			),
   213  			xdg.NewDesktopRenderer(
   214  				appID,
   215  				appName,
   216  				b.appDescription,
   217  			),
   218  			xdg.NewMetainfoRenderer(
   219  				appID,
   220  				appName,
   221  				b.appDescription,
   222  				b.appSummary,
   223  				b.appSPDX,
   224  				b.appURL,
   225  				b.releases,
   226  			),
   227  			flatpak.NewManifestRenderer(
   228  				appID,
   229  				b.goMain,
   230  				b.goFlags,
   231  				b.goGenerate,
   232  			),
   233  		},
   234  		b.overwrite,
   235  		ejecting,
   236  	)
   237  }
   238  
   239  func (b *Builder) Build() error {
   240  	dst := builders.GetFilepathForBranch(b.dst, b.branchID)
   241  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   242  	baseURL := builders.GetPathForBranch(b.baseURL, b.branchID, "")
   243  
   244  	return executors.DockerRunImage(
   245  		b.ctx,
   246  		b.cli,
   247  		b.image,
   248  		b.pull,
   249  		true,
   250  		b.src,
   251  		dst,
   252  		b.onID,
   253  		b.stdout,
   254  		map[string]string{
   255  			"APP_ID":           appID,
   256  			"PGP_KEY":          b.pgpKey,
   257  			"PGP_KEY_PASSWORD": b.pgpKeyPassword,
   258  			"PGP_KEY_ID":       b.pgpKeyID,
   259  			"BASE_URL":         baseURL,
   260  			"ARCHITECTURE":     b.architecture,
   261  			"GOMAIN":           b.goMain,
   262  		},
   263  		b.Render,
   264  		[]string{},
   265  	)
   266  }